[R] seeking a help on if function

Berend Hasselman bhh at xs4all.nl
Mon Dec 17 09:49:24 CET 2012


On 17-12-2012, at 08:45, Agnes Ayang wrote:

> Hello r helpers! Below is the whole coding for my programme. Before proceed more further, let me explain for you. First of all, I need to compute trimmed mean. Till that step is ok. Then I need to compute ssdw which is sum of square deviation. If I do equal trimming at both tail of distribution that I chose, I will use the first ssd formulae which is "a". But if I am doing unequal trimming, then I will be using "b". My problem is I dont know how to come out with a value of ssdw. For example, if the value of gw1=gw2 (which mean equal trimming), the result will come out as "a" not the value of ssdw when equal trimming is done. I f anyone could with this, I really appreciate it. I need the value of ssdw so that I can proceed with computing the F test. Thanks.
> 
> Regards,
> Hyo Min
> UPM Malaysia
> 
> 
> n1<-15
> miu<-0
> sd1<-1
> 
> data1<-rnorm(n1,miu,sd1)
> 
> 
> ## data transformation as G=0.5 and h=0.5 (data is normal)
> G<-0.5
> h<-0.5
> w<-((exp(G*data1)-1)/G)*(exp((h*data1^2)/2))
> 
> group1<-sort(w)
> 
> 
> g<-0.15    ## proportion of trimming
> 
> ## group 1
> uw<-floor(0.2*n1)
> low1<-mean(group1[1:uw])
> uppw1<-mean(group1[(n1-uw+1):n1])
> UWx<-uppw1-low1
> 
> vw<-floor(0.5*n1)
> low2<-mean(group1[1:vw])
> uppw2<-mean(group1[(n1-vw+1):n1])
> LWx<-uppw2-low2
> 
> gam.low<-g*(UWx/(UWx+LWx))
> gam.upp<-g-gam.low
> 
> gw1<-floor(n1*gam.low)
> gw2<-floor(n1*gam.upp)
> hw<-n1-gw1-gw2
> trim.mean1<-1/hw*(sum(group1[(gw1+1):(n1-gw2)]))
> 
> a<-((gw1+1)*((group1[gw1+1]-trim.mean1)^2))+sum(group1[(gw1+2):(n1-gw1-1)]-trim.mean1)^2+((gw1+1)*(group1[n1-gw1]-trim.mean1)^2)
> b<-(((gw1+1)*((group1[gw1+1]-trim.mean1)^2))+sum(group1[(gw1+2):(n1-gw2-1)]-trim.mean1)^2+((gw2+1)*(group1[n1-gw2]-trim.mean1)^2)-(gw1*(group1[gw1+1]-trim.mean1)+gw2*(group1[n1-gw2]-trim.mean1)^2))/n1
> ssdw<-ifelse(gw1==gw2, "a", "b")

Don't use the quotes: you want the variable or object a not the character string "a".

ssdw<-ifelse(gw1==gw2, a, b)

Since gw1 and gw2 are scalars you can dispense with the ifelse.
This will do as well

ssdw <- if(gw1==gw2) a else b

or

if(gw1==gw2) ssdw <- a else ssdw <- b

Berend

> ssdw
> 
> ## calculate Ft test statistic
> H<-hw
> C<-1
> A<-hw*trim.mean1
> xbar<-A/H
> 
> X<-hw*(trim.mean1-xbar)^2
> Y<-ssdw
> df1<-1
> df2<-H-C
> 
> upper<-X/df1
> lower<-Y/df2
> Ft<-upper/lower
> Ft
> pv<-1-pf(Ft,df1,df2)
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list