Regression, ANOVA, t-test are related…

In a new episode of things I forgot.

minipost
stats
dont-forget
Author
Published

June 8, 2021

Modified

March 26, 2024

Source: https://stats.stackexchange.com/questions/59047/how-are-regression-the-t-test-and-the-anova-all-versions-of-the-general-linear

I Always fail in remember the code to show how this models are related, so I will put here for my future me. An important thing to do is check the p-values.

The data, according help(sleep):

Data which show the effect of two soporific drugs (increase in hours of sleep compared to control) on 10 patients -Scheffé, Henry (1959) The Analysis of Variance. New York, NY: Wiley.

Now, load packages and data.

Code
library(tibble)
library(broom)

data("sleep")

sleep <- as_tibble(sleep)

glimpse(sleep)
Rows: 20
Columns: 3
$ extra <dbl> 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0, 1.9, 0.8, …
$ group <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
$ ID    <fct> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Regression

Code
linear_model <- lm(extra ~ group, data = sleep)

summary(linear_model)

Call:
lm(formula = extra ~ group, data = sleep)

Residuals:
   Min     1Q Median     3Q    Max 
-2.430 -1.305 -0.580  1.455  3.170 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   0.7500     0.6004   1.249   0.2276  
group2        1.5800     0.8491   1.861   0.0792 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.899 on 18 degrees of freedom
Multiple R-squared:  0.1613,    Adjusted R-squared:  0.1147 
F-statistic: 3.463 on 1 and 18 DF,  p-value: 0.07919
Code
tidy(linear_model)
# A tibble: 2 × 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)     0.75     0.600      1.25  0.228 
2 group2          1.58     0.849      1.86  0.0792

ANOVA

Code
anova <- aov(extra ~ group, data = sleep)

summary(anova)
            Df Sum Sq Mean Sq F value Pr(>F)  
group        1  12.48  12.482   3.463 0.0792 .
Residuals   18  64.89   3.605                 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Code
tidy(anova)
# A tibble: 2 × 6
  term         df sumsq meansq statistic p.value
  <chr>     <dbl> <dbl>  <dbl>     <dbl>   <dbl>
1 group         1  12.5  12.5       3.46  0.0792
2 Residuals    18  64.9   3.60     NA    NA     

\(t\)-test

Code
t_test <- t.test(extra ~ group, var.equal = TRUE, data = sleep) 

t_test

    Two Sample t-test

data:  extra by group
t = -1.8608, df = 18, p-value = 0.07919
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
 -3.363874  0.203874
sample estimates:
mean in group 1 mean in group 2 
           0.75            2.33 
Code
tidy(t_test)
# A tibble: 1 × 10
  estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high
     <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl>
1    -1.58      0.75      2.33     -1.86  0.0792        18    -3.36     0.204
# ℹ 2 more variables: method <chr>, alternative <chr>

Reuse

Citation

BibTeX citation:
@online{kunst_fuentes2021,
  author = {Kunst Fuentes, Joshua},
  title = {Regression, {ANOVA,} t-Test Are Related...},
  date = {2021-06-08},
  url = {https://jkunst.com/blog/posts/2021-06-08-regression-anova-t-test/},
  langid = {en}
}
For attribution, please cite this work as:
Kunst Fuentes, Joshua. 2021. “Regression, ANOVA, t-Test Are Related...” June 8, 2021. https://jkunst.com/blog/posts/2021-06-08-regression-anova-t-test/.