Why doesn't R output for a paired t-test match the formula for a confidence interval in the t-distribution?

My understanding is that the formula for a confidence interval when using a t-test is as follows:

$$\bar x \pm t_{n-1,\alpha / 2} \frac{S_d}{\sqrt{n}}$$

where $$S_d$$ is the standard deviation of the data.

Let me demonstrate an example of a paired t-test in R.

> a <- c(1,2,3,2,3,1,2,3,1)
> b <- c(2,1,3,1,2,3,2,1,1)
> t.test(a, b, paired = TRUE, alternative = "two.sided")

Paired t-test

data:  a and b
t = 0.5547, df = 8, p-value = 0.5943
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.7016018  1.1460462
sample estimates:
mean of the differences
0.2222222


The calculation of the mean, I understand. Since I am doing a paired t-test, I understand that my data is the difference between a and b, and I'm essentially performing a one-sample t-test compared to zero. I verify the mean as follows:

> mean(a-b)
[1] 0.2222222


As for the confidence interval, I just need the standard deviation, which I calculate as follows:

> sd(a-b)
[1] 1.20185


Using the confidence interval formula for a t-distribution and plugging in the t-statistic found above and sample size of n = 9, I get the following:

> mean(a-b) + c(-1,1)*(0.5547)*(sd(a-b))/sqrt(9)
[1] 7.861113e-08 4.444444e-01


So as you can see, the CI I get, to 3 decimal places, is (0.000, 0.444). That's nowhere close to what the t.test function gave me, which is (-0.702, 1.15).

What did I get wrong here? Why are my results different?

0.5547 is not the $$t$$ you are looking for. The $$t$$ you want to use in the equation is the critical value for the $$t$$-distribution with 8 degrees of freedom (n - 1).

qt(0.975, 8) = 2.262


So

mean(a-b) + c(-1,1)*(qt(0.975, 8))*(sd(a-b))/sqrt(9)

[1] -0.7016018  1.1460462


which matches the output from t.test(). It's nice to check these calculations by hand sometimes to make sure you know where all the numbers are coming from.