How to Use the Tilde Operator (~) in R


You can use the tilde operator (~) in R to separate the left hand side of an equation from the right hand side.

This operator is most commonly used with the lm() function in R, which is used to fit linear regression models.

The basic syntax for the lm() function is:

model <- lm(y ~ x1 + x2, data=df)

The variable name on the left side of the tilde operator (y) represents the response variable.

The variable names on the right side of the tilde operator (x1, x2) represent the predictor variables.

The following examples show how to use this tilde operator in different scenarios.

Example 1: Use Tilde Operator with One Predictor Variable

Suppose we fit the following simple linear regression model in R:

model <- lm(y ~ x, data=df)

This particular regression model has one response variable (y) and one predictor variable (x).

If we wrote out this regression equation in statistical notation it would look like this:

y = β0 + β1x

Example 2: Use Tilde Operator with Multiple Predictor Variables

Suppose we fit the following multiple linear regression model in R:

model <- lm(y ~ x1 + x2 + x3, data=df)

This particular regression model has one response variable (y) and three predictor variables (x1, x2, x3).

If we wrote out this regression equation in statistical notation it would look like this:

y = β0 + β1x1 + β2x2 + β3x3

Example 3: Use Tilde Operator with Unknown Number of Predictor Variables

Suppose we fit the following multiple linear regression model in R:

model <- lm(y ~ ., data=df)

This particular syntax indicates that we would like to use y as the response variable and every other variable in the data frame as predictor variables.

This syntax is useful when we want to fit a regression model with tons of predictor variables but we don’t want to type out the individual name of every single predictor variable.

Additional Resources

The following tutorials explain how to use other common functions in R:

How to Use Dollar Sign ($) Operator in R
How to Use “NOT IN” Operator in R
How to Use %in% Operator in R

Leave a Reply

Your email address will not be published.