I am getting a difference in results when running some analysis using by and
tapply compare to using a for loop. I've tried searching the web but had no
luck with the keywords I used.
I've attached a simple example below to illustrates my problem. I get a
difference in the mean of yvar, diff and the p-value using tapply & by
compared to a for loop. I cannot see what I am doing wrong. Can anyone help?
> # Simulate some data - I'll do 2 simulations...
>
> xvar = rnorm(40, 20, 5)
> yvar = rnorm(40, 22, 2)
> num = factor(rep(1:2, each=20))
> sdat = data.frame(cbind(num, xvar, yvar))
>
> # Define a function to do a simple t test and return some values...
>
> kindtest = function(varx, vary){
+ res = t.test(varx, vary)
+ x.mn = res$estimate[1]
+ y.mn = res$estimate[2]
+ diff = y.mn-x.mn
+ pval = res$p.value
+ cat("Mean xvar =", x.mn, " Mean yvar =", y.mn)
+ cat(" diff =", diff, " p-value=", pval, "\n\n")
+ list(x.mn=x.mn, y.mn=y.mn, diff=diff, pval=pval)
+ }
## Results from by and tapply
> attach(sdat)
> bres = by(xvar, num, kindtest, yvar)
Mean xvar = 19.8904 Mean yvar = 21.97729 diff = 2.086891 p-value=
0.06222805
Mean xvar = 19.88329 Mean yvar = 21.97729 diff = 2.093996 p-value=
0.05245329
> tres = tapply(xvar, num, kindtest, yvar)
Mean xvar = 19.8904 Mean yvar = 21.97729 diff = 2.086891 p-value=
0.06222805
Mean xvar = 19.88329 Mean yvar = 21.97729 diff = 2.093996 p-value=
0.05245329
> detach(sdat,1)
## Results from for
> for(i in 1:2) {
+ subdat= subset(sdat, num==i)
+ kindtest(subdat$xvar, subdat$yvar)
+ }
Mean xvar = 19.8904 Mean yvar = 21.98615 diff = 2.095746 p-value=
0.07319223
Mean xvar = 19.88329 Mean yvar = 21.96843 diff = 2.085141 p-value=
0.05850057
OKAY - I'm going to brave and show you that I am still on version 1.9.0! I
asked the IT/IS department for an upgrade when version 2 was first released!
Last I heard my request was in the black hole of documented and undocumented
processes to approve software upgrades... So this error may not occur in the
latest version... If so, just let me know which of the above is correct (if
any) and I'll just live with it (or run it at home on version 2.1.1).
Thanks.
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 1
minor 9.0
year 2004
month 04
day 12
language R
Thanks,
Saghir
---------------------------------------------------------
Legal Notice: This electronic mail and its attachments are i...{{dropped}}
______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html