Adaikalavan Ramasamy
2004-Oct-01 12:47 UTC
[Rd] same test statistic for t-test with and without equal variance assumption
Could some kindly tell me if I am supposed to be getting the same test statistic value with var.equal=TRUE and var.equal=FALSE in t.test ? set.seed(1066) x1 <- rnorm(50) x2 <- rnorm(50) t.test(x1, x2, var.equal=FALSE)$statistic # 0.5989774 t.test(x1, x2, var.equal=TRUE)$statistic # 0.5989774 ??? Here are my own calculations that shows that perhaps the result when var.equal=TRUE is wrong. n1 <- length(x1); n2 <- length(x2) m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) v1 <- var(x1) ; v2 <- var(x2) # t-test with UNequal variance denom1 <- sqrt( v1/n1 + v2/n2 ) num / denom1 # gives 0.5989774 # t-test with equal variance sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) denom2 <- sp * sqrt( 1/n1 + 1/n2 ) num / denom2 # gives 0.5913777 I tested this using R-1.9.1 (21/06/2004) on Redhat Fedora Core 2 and Windows 2000 Professional with the same results. Any suggestions would be kindly appreciated. Regards, -- Adaikalavan Ramasamy ramasamy@cancer.org.uk Centre for Statistics in Medicine http://www.ihs.ox.ac.uk/csm/ Cancer Research UK Tel : 01865 226 677 Old Road Campus, Headington, Oxford Fax : 01865 226 962
Peter Dalgaard
2004-Oct-01 13:17 UTC
[Rd] same test statistic for t-test with and without equal variance assumption
Adaikalavan Ramasamy <ramasamy@cancer.org.uk> writes:> Could some kindly tell me if I am supposed to be getting the same test > statistic value with var.equal=TRUE and var.equal=FALSE in t.test ? > > set.seed(1066) > x1 <- rnorm(50) > x2 <- rnorm(50) > > t.test(x1, x2, var.equal=FALSE)$statistic # 0.5989774 > t.test(x1, x2, var.equal=TRUE)$statistic # 0.5989774 ??? > > > Here are my own calculations that shows that perhaps the result when > var.equal=TRUE is wrong. > > n1 <- length(x1); n2 <- length(x2) > m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) > v1 <- var(x1) ; v2 <- var(x2) > > # t-test with UNequal variance > denom1 <- sqrt( v1/n1 + v2/n2 ) > num / denom1 # gives 0.5989774 > > # t-test with equal variance > sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) > denom2 <- sp * sqrt( 1/n1 + 1/n2 ) > num / denom2 # gives 0.5913777 > > > I tested this using R-1.9.1 (21/06/2004) on Redhat Fedora Core 2 and > Windows 2000 Professional with the same results. > > Any suggestions would be kindly appreciated.Your calculation is wrong. Try increasing the variance of x1 and x2...> set.seed(1066) > x1 <- rnorm(50,,100) > x2 <- rnorm(50,,100) > m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) > v1 <- var(x1) ; v2 <- var(x2) > denom1 <- sqrt( v1/n1 + v2/n2 ) > num / denom1 # gives 0.5989774[1] 0.5989774> sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) > denom2 <- sp * sqrt( 1/n1 + 1/n2 ) > num / denom2 # gives 0.5913777[1] 0.005913777 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
Adaikalavan Ramasamy
2004-Oct-01 14:55 UTC
[Rd] same test statistic for t-test with and without equal variance assumption
Peter, thank you! I forgot the to square root in calculating sp. sp <- sqrt( ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) ) For several simulation runs, the test statistics from both tests are remarkably similar (difference is less than 10e-16). I naively assumed that the statistics value should be slightly but visibly different too. It appear that the effects of equal and unequal variance assumptions are only felt through the degrees of freedom calculation. Regards, Adai On Fri, 2004-10-01 at 12:15, Peter Dalgaard wrote:> Adaikalavan Ramasamy <ramasamy@cancer.org.uk> writes: > > > Could some kindly tell me if I am supposed to be getting the same test > > statistic value with var.equal=TRUE and var.equal=FALSE in t.test ? > > > > set.seed(1066) > > x1 <- rnorm(50) > > x2 <- rnorm(50) > > > > t.test(x1, x2, var.equal=FALSE)$statistic # 0.5989774 > > t.test(x1, x2, var.equal=TRUE)$statistic # 0.5989774 ??? > > > > > > Here are my own calculations that shows that perhaps the result when > > var.equal=TRUE is wrong. > > > > n1 <- length(x1); n2 <- length(x2) > > m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) > > v1 <- var(x1) ; v2 <- var(x2) > > > > # t-test with UNequal variance > > denom1 <- sqrt( v1/n1 + v2/n2 ) > > num / denom1 # gives 0.5989774 > > > > # t-test with equal variance > > sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) > > denom2 <- sp * sqrt( 1/n1 + 1/n2 ) > > num / denom2 # gives 0.5913777 > > > > > > I tested this using R-1.9.1 (21/06/2004) on Redhat Fedora Core 2 and > > Windows 2000 Professional with the same results. > > > > Any suggestions would be kindly appreciated. > > Your calculation is wrong. Try increasing the variance of x1 and x2... > > > set.seed(1066) > > x1 <- rnorm(50,,100) > > x2 <- rnorm(50,,100) > > m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) > > v1 <- var(x1) ; v2 <- var(x2) > > denom1 <- sqrt( v1/n1 + v2/n2 ) > > num / denom1 # gives 0.5989774 > [1] 0.5989774 > > sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) > > denom2 <- sp * sqrt( 1/n1 + 1/n2 ) > > num / denom2 # gives 0.5913777 > [1] 0.005913777 >
Adaikalavan Ramasamy
2004-Oct-01 15:16 UTC
[Rd] same test statistic for t-test with and without equal variance assumption
Apologies, I made an incorrect statement. The statistics from t-test with difference variance assumptions are the same if both groups have the same length. Sorry for troubling everyone again. On Fri, 2004-10-01 at 13:56, Adaikalavan Ramasamy wrote:> Peter, thank you! I forgot the to square root in calculating sp. > > sp <- sqrt( ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) ) > > For several simulation runs, the test statistics from both tests are > remarkably similar (difference is less than 10e-16). I naively assumed > that the statistics value should be slightly but visibly different too. > > It appear that the effects of equal and unequal variance assumptions are > only felt through the degrees of freedom calculation. > > Regards, Adai > > > On Fri, 2004-10-01 at 12:15, Peter Dalgaard wrote: > > Adaikalavan Ramasamy <ramasamy@cancer.org.uk> writes: > > > > > Could some kindly tell me if I am supposed to be getting the same test > > > statistic value with var.equal=TRUE and var.equal=FALSE in t.test ? > > > > > > set.seed(1066) > > > x1 <- rnorm(50) > > > x2 <- rnorm(50) > > > > > > t.test(x1, x2, var.equal=FALSE)$statistic # 0.5989774 > > > t.test(x1, x2, var.equal=TRUE)$statistic # 0.5989774 ??? > > > > > > > > > Here are my own calculations that shows that perhaps the result when > > > var.equal=TRUE is wrong. > > > > > > n1 <- length(x1); n2 <- length(x2) > > > m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) > > > v1 <- var(x1) ; v2 <- var(x2) > > > > > > # t-test with UNequal variance > > > denom1 <- sqrt( v1/n1 + v2/n2 ) > > > num / denom1 # gives 0.5989774 > > > > > > # t-test with equal variance > > > sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) > > > denom2 <- sp * sqrt( 1/n1 + 1/n2 ) > > > num / denom2 # gives 0.5913777 > > > > > > > > > I tested this using R-1.9.1 (21/06/2004) on Redhat Fedora Core 2 and > > > Windows 2000 Professional with the same results. > > > > > > Any suggestions would be kindly appreciated. > > > > Your calculation is wrong. Try increasing the variance of x1 and x2... > > > > > set.seed(1066) > > > x1 <- rnorm(50,,100) > > > x2 <- rnorm(50,,100) > > > m1 <- mean(x1) ; m2 <- mean(x2) ; num <- (m1 - m2) > > > v1 <- var(x1) ; v2 <- var(x2) > > > denom1 <- sqrt( v1/n1 + v2/n2 ) > > > num / denom1 # gives 0.5989774 > > [1] 0.5989774 > > > sp <- ( (n1-1)*v1 + (n2-1)*v2 )/(n1 + n2 - 2) > > > denom2 <- sp * sqrt( 1/n1 + 1/n2 ) > > > num / denom2 # gives 0.5913777 > > [1] 0.005913777 > > > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >