# $t$ test for paired samples in R

I have two vector samples $$x,y$$:


x = c(107.32362,  89.15552,  90.75148, 109.51189,  98.43394,  97.25716 ,115.39168,
95.68554,  93.60000, 103.00000 ,112.70000, 138.40000,  93.30000 , 87.70000,
83.30000, 105.00000 ,103.40000 ,105.70000, 107.80000,  96.10000, 102.00000,
105.40000, 123.90000 ,141.10000)
y =  c(100.1,  82.0,  39.0,  18.0,  83.8 ,109.1 ,114.0 , 95.4,  93.1, 116.8  ,56.9 , 56.6,  86.3
,62.0,  38.9,  85.2 ,124.4, 124.3, 139.2, 105.6, 117.4 ,125.2, 111.5, 131.7)


I want to calculate the t.test for equality of means (or the differences).I am assuming the values are paired.

Because the samples are paired (matched) we must use the differences between the paired values as : $$D = X_{1i} - X_{2i}$$ The point estimate for the paired difference population mean $$\mu_{D}=\mu_{x} - \mu_{y}=0$$ is denoted as $$\bar{D} = \frac{\sum_{i=1}^{n}D_{i}}{n}$$ and the standard deviation of the differences $$S_{D} = \sqrt{ \frac{\sum_{i=1}^{n}(D_{i}-\bar{D})^2 }{n-1}}$$ The test statistic for the equality of means is :

$$t =\frac{\bar{D}-\mu_{D}}{ \frac{S_D}{\sqrt{n}}} = 1.835492$$

d = x-y
t = (mean(d)-0)/ (sd(d)/sqrt(length(d)));t
[1] 1.835492


For a two-tail test with a given level of significance $$\alpha$$, we reject the null hypothesis if the calculated test statistic is greater than the upper-tail critical value $$t_{n-1}$$ from the $$t$$ distribution, or if the calculated test statistic is less than the lower-tail critical value $$-t_{n-1}$$ from the $$t$$ distribution. That is, the decision rule is: Reject $$H_{0}$$ if $$t > t_{n-1}$$ or if $$t< -t_{n-1}$$ otherwise, do not reject $$H_{0}$$. The degrees of freedom in our example and $$24-1=23$$ and for $$\alpha =0.10$$ the inverse cumulative distribution for $$1-\alpha =0.90$$ with 23 degrees of freedom of $$t$$ distribution is $$1.31946$$

Therefore $$t>t_{n-1}$$ or $$1.83>1.31$$ and I reject the null hypothesis.

t>qt(0.9,23)
[1] TRUE


But when I run the t.test() function in R it reports me a p.value greater than 0.05 (0.0794) and I must not reject the null hypothesis.

> t.test(x,y,
paired = TRUE,
alternative = "two.sided",
conf.level = 0.90)

Paired t-test

data:  x and y
t = 1.8355, df = 23, p-value = 0.0794
alternative hypothesis: true difference in means is not equal to 0
90 percent confidence interval:
0.7990197 23.3185494
sample estimates:
mean of the differences
12.05878


What is wrong here ?

• You're using an $\alpha$ of $0.10$, so you'd have to reject if $p\leq 0.1$, not $0.05$ (that would correspond to an $\alpha$ of $0.05$). The conf.level option only influences the confidence interval, which does not include $0$ and is thus consistent with your decision based on the critical $t$ value. Commented Dec 23, 2022 at 17:55
• @COOLSerdash oh my god!!!What a silly mistake.It was obvious.Thank you very much Commented Dec 23, 2022 at 17:57
• If you put 10% in each tail (as you seem to be doing) you would have a 20% test, not a 10% test. If you wanted a 10% test you'd put 5% in each tail. (If you want a 5% test you'd put 2.5% in each tail.) Commented Dec 24, 2022 at 3:37

For a two-sided test the relevant critical point is $$t_{n-1,\alpha/2}$$ so you would find this as follows:

#Find critical point
n     <- length(d)
alpha <- 0.1
CRIT  <- qt(1-alpha/2, df = n-1)

#Show critical point
CRIT
[1] 1.713872


Since $$|t| = 1.835492 > 1.713872 = t_{n-1,\alpha/2}$$ you reject the null hypothesis at this significance level. This matches the decision from the t.test output --- since $$p = 0.0794 < 0.1 = \alpha$$ you likewise reject the null.