From bbo|ker @end|ng |rom gm@||@com  Mon Sep 16 16:57:29 2024
From: bbo|ker @end|ng |rom gm@||@com (Ben Bolker)
Date: Mon, 16 Sep 2024 10:57:29 -0400
Subject: [Rd] numerical issue with t.test
In-Reply-To: <2e646d2d-5ad3-052f-3b24-1de9d3eb8596@dewey.myzen.co.uk>
References: <CALK03d1EgjSDw69yKHyqO2y=-7-NVW58LikosoxrNqJhUmZ8OQ@mail.gmail.com>
 <2e646d2d-5ad3-052f-3b24-1de9d3eb8596@dewey.myzen.co.uk>
Message-ID: <0af3164f-c5f5-4dfb-b1b8-c950fef3b925@gmail.com>

   To be more specific, this replicates the computations that t.test is 
doing (stripped of all the different cases that stats:::t.test.default 
handles)

z <- err1-err2
se <- sqrt(var(z)/length(z))
mz <- mean(z)
tstat <- mz/se
2*pt(tstat, df= length(z)-1, lower.tail = FALSE)

On 2024-09-16 10:54 a.m., Michael Dewey wrote:
> Dear Toby
> 
> I see no problem there. If you compute the mean and variance of err1 - 
> err2 which is what the paired test is working on then that might help to 
> see what is going on.
> 
> Michael
> 
> On 16/09/2024 15:47, Toby Hocking wrote:
>> Hi! I expected that t.test should report a very large p-value (close
>> to 1), even when using paired=TRUE, for the data below (which are very
>> similar). However, I observe p-value = 0.02503 which indicates a
>> significant difference, even though there is none. Can this be fixed
>> please? This is with R-4.4.1. For reference below I use paired=FALSE
>> with the same data, and I get p-value = 1 as expected.
>>
>>> err1 = c(-1.6076199373862132, -1.658521185520103, 
>>> -1.6549424312339873, -1.5887767975086149, -1.634129577540383, 
>>> -1.7442711937982249)
>>> err2 = c(-1.6076199373862132, -1.6585211855201032, 
>>> -1.6549424312339875, -1.5887767975086149, -1.6341295775403832, 
>>> -1.7442711937982252)
>>> t.test(err1,err2,paired=TRUE)
>>
>> ???? Paired t-test
>>
>> data:? err1 and err2
>> t = 3.1623, df = 5, p-value = 0.02503
>> alternative hypothesis: true mean difference is not equal to 0
>> 95 percent confidence interval:
>> ? 2.769794e-17 2.683615e-16
>> sample estimates:
>> mean difference
>> ??? 1.480297e-16
>>
>>> t.test(err1,err2,paired=FALSE)
>>
>> ???? Welch Two Sample t-test
>>
>> data:? err1 and err2
>> t = 0, df = 10, p-value = 1
>> alternative hypothesis: true difference in means is not equal to 0
>> 95 percent confidence interval:
>> ? -0.06988771? 0.06988771
>> sample estimates:
>> mean of x mean of y
>> -1.648044 -1.648044
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
> 

-- 
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
 > E-mail is sent at my convenience; I don't expect replies outside of 
working hours.



[Rd] numerical issue with t.test
Ben Bolker