Skip to contents

Datasets and wrapper functions for tidyverse-friendly introductory linear regression, used in "Statistical Inference via Data Science: A ModernDive into R and the tidyverse" available at https://moderndive.com/.

Author

Maintainer: Albert Y. Kim albert.ys.kim@gmail.com (ORCID)

Authors:

Other contributors:

Examples

library(moderndive)

# Fit regression model:
life_exp_model <- lm(
  life_expectancy_2022 ~ gdp_per_capita,
  data = un_member_states_2024
)

# Regression tables:
get_regression_table(life_exp_model)
#> # A tibble: 2 × 7
#>   term           estimate std_error statistic p_value lower_ci upper_ci
#>   <chr>             <dbl>     <dbl>     <dbl>   <dbl>    <dbl>    <dbl>
#> 1 intercept          71.4     0.479    149.         0     70.5     72.4
#> 2 gdp_per_capita      0       0          9.85       0      0        0  

# Information on each point in a regression:
get_regression_points(life_exp_model)
#> # A tibble: 188 × 5
#>       ID life_expectancy_2022 gdp_per_capita life_expectancy_2022_hat residual
#>    <int>                <dbl>          <dbl>                    <dbl>    <dbl>
#>  1     1                 53.6           356.                     71.5   -17.8 
#>  2     2                 79.5          6810.                     72.3     7.17
#>  3     3                 78.0          4343.                     72.0     6.05
#>  4     4                 83.4         41993.                     76.9     6.49
#>  5     5                 62.1          3000.                     71.8    -9.69
#>  6     6                 77.8         19920.                     74.0     3.77
#>  7     7                 78.3         13651.                     73.2     5.11
#>  8     8                 76.1          7018.                     72.3     3.80
#>  9     9                 83.1         65100.                     80.0     3.13
#> 10    10                 82.3         52085.                     78.3     4.02
#> # ℹ 178 more rows

# Regression summaries
get_regression_summaries(life_exp_model)
#> # A tibble: 1 × 9
#>   r_squared adj_r_squared   mse  rmse sigma statistic p_value    df  nobs
#>       <dbl>         <dbl> <dbl> <dbl> <dbl>     <dbl>   <dbl> <dbl> <dbl>
#> 1     0.343         0.339  31.4  5.60  5.63      97.0       0     1   188

# Plotting parallel slopes models
library(ggplot2)
ggplot(evals, aes(x = age, y = score, color = ethnicity)) +
  geom_point() +
  geom_parallel_slopes(se = FALSE)