O'Reilly logo

The R Book by Michael J. Crawley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Model Formulae in R

The structure of the model is specified in the model formula like this:

response variable~explanatory variable(s)

where the tilde symbol ~ reads ‘is modelled as a function of’ (see Table 9.3 for examples).

Table 9.3. Examples of R model formulae. In a model formula, the function I case i) stands for ‘as is’ and is used for generating sequences I(1:10) or calculating quadratic terms I(x^2).

images

images

So a simple linear regression of y on x would be written as

y ~ x

and a one-way ANOVA where sex is a two-level factor would be written as

y ~ sex

The right-hand side of the model formula shows:

  • the number of explanatory variables and their identities – their attributes (e.g. continuous or categorical) are usually defined prior to the model fit;
  • the interactions between the explanatory variables (if any);
  • non-linear terms in the explanatory variables.

On the right of the tilde, one also has the option to specify offsets or error terms in some special cases. As with the response variable, the explanatory variables can appear as transformations, or as powers or polynomials.

It is very important to note that symbols are used differently in model formulae than in arithmetic expressions. In particular:

+ indicates inclusion of an explanatory variable in the model (not addition); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required