How does the R compute the confidence/predictions intervals?
I can't get the same values with a t-student in Excel. I think the diference is in the residual variance. I would like to know how the R calculate it?
Here is a comparison of R's calculation of confidence intervals on a t-test of two 10 element samples to my manual calculation. Check particularly whether Excel uses the adjusted degrees of freedom.
n1 <- 10
n2 <- 10
set.seed(3123)
x1 <- rnorm(n1, 0, 1)
x2 <- rnorm(n2, 1, 1)
t.test(x1, x2)
#>
#> Welch Two Sample t-test
#>
#> data: x1 and x2
#> t = -2.87, df = 17.109, p-value = 0.01057
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#> -1.810749 -0.276839
#> sample estimates:
#> mean of x mean of y
#> -0.05637133 0.98742264
#Welch's approx of df
df <- (sd(x1)^2/n1 + sd(x2)^2/n2)^2/((sd(x1)^2/n1)^2/(n1 -1) + (sd(x2)^2/n2)^2/(n2 - 1))
df
#> [1] 17.10865
#critical value of t dist at df
t_crit <- qt(0.975, df)
t_crit
#> [1] 2.108795
#Lower confid value
(mean(x1) - mean(x2)) - t_crit * sqrt(sd(x1)^2/n1 + sd(x2)^2/n2)
#> [1] -1.810749
#Upper confid value
(mean(x1) - mean(x2)) + t_crit * sqrt(sd(x1)^2/n1 + sd(x2)^2/n2)
#> [1] -0.276839