R lm

lm is a built-in R function for fitting linear models.


Example

mod <- lm(y ~ x)


Description

The lm function fits a linear model using OLS. The LHS and RHS of the model are delimited by a tilde (~), as in y ~ x. For a model fit by lm, there should only be one (dependent) variable on the LHS.

The RHS is a linear combination of parameters, like a + b + c. The intercept term is implicit but can be omitted by specifying a literal 0 or -1 on the LHS. For example,  y ~ 0 + x.

An interaction term is specified like factor1:factor2. Further note that factor1 * factor2 is a shorthand for factor1 + factor2 + factor1:factor2.

If the data option is specified, names are looked up as columns in there first.

mod <- lm(foo ~ bar, data=df)

lm returns an object that can be passed to the plot function.


CategoryRicottone

R/BuiltinFunctions/Lm (last edited 2026-09-03 18:41:58 by DominicRicottone)