Software tutorial/Transformation of data in a linear model

 ← Testing a linear model in R (previous step) Tutorial index Next step: Investigating outliers, discrepancies and other influential points →

This is shown by example for a few different types of transformations:

Description Desired model Formula function in R
Fit only an intercept $$y = b_0$$ lm(y ~ 1) the character is a "one"
Standard, univariate model $$y = b_0 + b_1 x$$ lm(y ~ x)
Force intercept to zero (check the degrees of freedom!) $$y = b_1 x$$ lm(y ~ x + 0)
Transformation of an $$x$$ $$y = b_0 + b_1\sqrt{x}$$ lm(y ~ sqrt(x))
Transformation of $$y$$ $$\log(y) = b_0 + b_1 x$$ lm(log(y) ~ x)
Transformation of $$y$$ $$100/y= b_0 + b_1 x$$ lm(100/y ~ x)
Transformation of $$x$$: +, -, /, and ^ do not work on the right hand side! $$y= b_0 + \dfrac{b_1}{x}$$ lm(y ~ 1/x) will work, but is not doing what you expect. It is fitting a model $$y = b_0$$!! Be careful.
Most transformations of $$x$$ must be wrapped in an AsIs I() operation: $$y= b_0 + \dfrac{b_1}{x}$$ lm(y ~ I(1/x)) will work as expected
Another use of the AsIs I() operation $$y= b_0 + b_1 x^2$$ lm(y ~ I(x^2))
Another use of the AsIs I() operation $$y= b_0 + b_1 (x - \bar{x})$$ lm(y ~ I(x - mean(x)))