# Difference between calculating a t-statistic manually and with R

I am following a MOOC course on statistical analysis. The course goes through theoretical and practical exercises in R. One of these exercises consists in the following:

I have a database called "nc", presenting a list of 1000 births in North Carolina. Within this database there are two variables: weight (numerical) and habit (categorical, with 2 levels = (1) nonsmokers, (2) smokers).

We want to understand if children's weights may be correlated with their mothers' smoking habits. To do this, we calculate a t-statistic between the weight means of the two groups (nonsmokers, smokers). Following the summary for the data:

nc %>% group_by(habit) %>% summarize(mean(weight), sd(weight), n())
# A tibble: 3 × 4
habit     mean(weight) sd(weight) n()
<fct>              <dbl>        <dbl> <int>
1 nonsmoker           7.14         1.52   873
2 smoker              6.83         1.39   126
3 NA                  3.63        NA        1


to calculate the t-statistic, the teacher used the method inference(), as the following:

inference(weight, habit, nc, "ht", "mean", method = "theoretical", alternative = "twosided")


which provided a t-statistic of 2.359.

Response variable: numerical
Explanatory variable: categorical (2 levels)
n_nonsmoker = 873, y_bar_nonsmoker = 7.1443, s_nonsmoker = 1.5187
n_smoker = 126, y_bar_smoker = 6.8287, s_smoker = 1.3862
H0: mu_nonsmoker =  mu_smoker
HA: mu_nonsmoker != mu_smoker
t = 2.359, df = 125
p_value = 0.0199


I tried to manually calculate the t-statistic through the following calculation:

t-statistic = (mean_nonsmoker - mean-smoker)/sqrt(sd_nonsmoker**2/n_nonsmoker + sd_smoker**2/n_smoker)
t-statistic = (7.14 - 6.83)/sqrt(1.52**2/873 + 1.39**2/126)


but I obtained a t-statistic of 2.312. Why are the two t-statistic different? Does the software use a different calculation?

• What is the meaning of MOOC? Commented Jun 17, 2022 at 17:23
• @sextusempiricus "massive online open course". eg a course from Coursera or similar Commented Jun 17, 2022 at 17:41
• @TakeMeToTheMoon I suggest that you use the full description in the text. Why use the abbreviation? Commented Jun 17, 2022 at 18:09

(7.1443 - 6.8287)/sqrt((1.5187**2)/873 + (1.3862**2)/126)

returns 2.359407 as the answer.
• Rather than getting summarise() to print more significant figures, you would be better off programmatically extracting values. In the tidyverse this frequently done via the filter() command. You can also use \$ and indexing [,] in base R to extract values. I'll stop here though as programming is not the focus of this site. Commented Jun 17, 2022 at 11:51
• @takemetothemoon Why at all do you use the output of summarize()? You can directly write the formulas with mean and sd, or, if you find that more legible, assign the result of mean and sd to variables and use these subsequently. Commented Jun 17, 2022 at 12:44