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?

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

1 Answer 1


I'd put my money on this being a small rounding error. Using more precise values from the second table of values

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

returns 2.359407 as the answer.

  • $\begingroup$ Wow, that's great. A short question: how could you change the details of the summarize() function? $\endgroup$ Commented Jun 17, 2022 at 10:40
  • $\begingroup$ 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. $\endgroup$
    – jcken
    Commented Jun 17, 2022 at 11:51
  • 1
    $\begingroup$ @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. $\endgroup$
    – cdalitz
    Commented Jun 17, 2022 at 12:44
  • $\begingroup$ @cdalitz, you are very right. I didn't think about it. Thanks for the advice $\endgroup$ Commented Jun 17, 2022 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.