Running Basic Linear Models

stats
lm
R
Published

November 13, 2022

In R, basic linear models are run with the lm() function.

lm(y ~ x1 + x2, data = data)

R will choose which type of linear model to do depending on the type of values in x1 and x2.

In all cases, y is continuous.

No count data, no binary (binomial) data, no proprotions.

(Although technically sometimes count data is close enough to continuous and proportions can be transformed)

Quick example

library(palmerpenguins) # To access the penguins data set
m <- lm(body_mass_g ~ bill_depth_mm, data = penguins)
m

Call:
lm(formula = body_mass_g ~ bill_depth_mm, data = penguins)

Coefficients:
  (Intercept)  bill_depth_mm  
       7488.7         -191.6  

But wait! How do I interpret that?

All the details are stored in the model object m. We can use many other R functions to check our assumptions, summarize the results, or create ANOVA tables.

But first, you really should check your Diagnostics!

Side note on NA’s

By default missing values (NA’s) are dropped from the analysis and from the data contained by the model. However, even better is to use na.exclude.

m <- lm(body_mass_g ~ bill_depth_mm, data = penguins, na.action = na.exclude)

This also excludes missing values (NA’s) from the analysis, but not from the data you extract from the model (which makes it much easier to combine back with your raw data set when performing Diagnostics).

Session Info

devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       Ubuntu 22.04.3 LTS
 system   x86_64, linux-gnu
 ui       X11
 language en_CA:en
 collate  en_CA.UTF-8
 ctype    en_CA.UTF-8
 tz       America/Winnipeg
 date     2023-09-04
 pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package        * version date (UTC) lib source
 cachem           1.0.8   2023-05-01 [1] CRAN (R 4.3.0)
 callr            3.7.3   2022-11-02 [1] CRAN (R 4.3.0)
 cli              3.6.1   2023-03-23 [1] CRAN (R 4.3.0)
 crayon           1.5.2   2022-09-29 [1] CRAN (R 4.3.0)
 devtools         2.4.5   2022-10-11 [1] CRAN (R 4.3.0)
 digest           0.6.31  2022-12-11 [1] CRAN (R 4.3.0)
 ellipsis         0.3.2   2021-04-29 [1] CRAN (R 4.3.0)
 evaluate         0.20    2023-01-17 [1] CRAN (R 4.3.0)
 fansi            1.0.4   2023-01-22 [1] CRAN (R 4.3.0)
 fastmap          1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
 fs               1.6.2   2023-04-25 [1] CRAN (R 4.3.0)
 glue             1.6.2   2022-02-24 [1] CRAN (R 4.3.0)
 htmltools        0.5.5   2023-03-23 [1] CRAN (R 4.3.0)
 htmlwidgets      1.6.2   2023-03-17 [1] CRAN (R 4.3.0)
 httpuv           1.6.9   2023-02-14 [1] CRAN (R 4.3.0)
 jsonlite         1.8.4   2022-12-06 [1] CRAN (R 4.3.0)
 knitr            1.42    2023-01-25 [1] CRAN (R 4.3.0)
 later            1.3.0   2021-08-18 [1] CRAN (R 4.3.0)
 lifecycle        1.0.3   2022-10-07 [1] CRAN (R 4.3.0)
 magrittr         2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
 memoise          2.0.1   2021-11-26 [1] CRAN (R 4.3.0)
 mime             0.12    2021-09-28 [1] CRAN (R 4.3.0)
 miniUI           0.1.1.1 2018-05-18 [1] CRAN (R 4.3.0)
 palmerpenguins * 0.1.1   2022-08-15 [1] CRAN (R 4.3.0)
 pillar           1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
 pkgbuild         1.4.0   2022-11-27 [1] CRAN (R 4.3.0)
 pkgconfig        2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
 pkgload          1.3.2   2022-11-16 [1] CRAN (R 4.3.0)
 prettyunits      1.1.1   2020-01-24 [1] CRAN (R 4.3.0)
 processx         3.8.1   2023-04-18 [1] CRAN (R 4.3.0)
 profvis          0.3.7   2020-11-02 [1] CRAN (R 4.3.0)
 promises         1.2.0.1 2021-02-11 [1] CRAN (R 4.3.0)
 ps               1.7.5   2023-04-18 [1] CRAN (R 4.3.0)
 purrr            1.0.1   2023-01-10 [1] CRAN (R 4.3.0)
 R6               2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
 Rcpp             1.0.10  2023-01-22 [1] CRAN (R 4.3.0)
 remotes          2.4.2   2021-11-30 [1] CRAN (R 4.3.0)
 rlang            1.1.1   2023-04-28 [1] CRAN (R 4.3.0)
 rmarkdown        2.21    2023-03-26 [1] CRAN (R 4.3.0)
 rstudioapi       0.14    2022-08-22 [1] CRAN (R 4.3.0)
 sessioninfo      1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
 shiny            1.7.4   2022-12-15 [1] CRAN (R 4.3.0)
 stringi          1.7.12  2023-01-11 [1] CRAN (R 4.3.0)
 stringr          1.5.0   2022-12-02 [1] CRAN (R 4.3.0)
 tibble           3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
 urlchecker       1.0.1   2021-11-30 [1] CRAN (R 4.3.0)
 usethis          2.2.0   2023-06-06 [1] CRAN (R 4.3.0)
 utf8             1.2.3   2023-01-31 [1] CRAN (R 4.3.0)
 vctrs            0.6.2   2023-04-19 [1] CRAN (R 4.3.0)
 xfun             0.39    2023-04-20 [1] CRAN (R 4.3.0)
 xtable           1.8-4   2019-04-21 [1] CRAN (R 4.3.0)
 yaml             2.3.7   2023-01-23 [1] CRAN (R 4.3.0)

 [1] /home/steffi/R/x86_64-pc-linux-gnu-library/4.3
 [2] /usr/local/lib/R/site-library
 [3] /usr/lib/R/site-library
 [4] /usr/lib/R/library

──────────────────────────────────────────────────────────────────────────────