简体   繁体   中英

How to use a variable in lm() function in R?

Let us say I have a dataframe (df) with two columns called "height" and "weight".

Let's say I define:

x = "height"

How do I use x within my lm() function? Neither df[x] nor just using x works.

Two ways:

Create a formula with paste

x = "height"
lm(paste0(x, '~', 'weight'), df)

Or use reformulate

lm(reformulate("weight", x), df)

Using reproducible example with mtcars dataset:

x = "Cyl"
lm(paste0(x, '~', 'mpg'), data = mtcars)

#Call:
#lm(formula = paste0(x, "~", "mpg"), data = mtcars)

#Coefficients:
#(Intercept)          mpg  
#    11.2607      -0.2525  

and same with

lm(reformulate("mpg", x), mtcars)

We can use glue to create the formula

x <- "height"
lm(glue::glue('{x} ~ weight'), data = df)

Using a reproducible example with mtcars

x <- 'cyl'
lm(glue::glue('{x} ~ mpg'), data = mtcars)

#Call:
#lm(formula = glue::glue("{x} ~ mpg"), data = mtcars)

#Coefficients:
#(Intercept)          mpg  
#    11.2607      -0.2525  

When you run x = "height" your are assigning a string of characters to the variable x .

Consider this data frame:


df <- data.frame(
  height = c(176, 188, 165),
  weight = c(75, 80, 66)
)

If you want a regression using height and weight you can either do this:

lm(height ~ weight, data = df)

# Call:
#   lm(formula = height ~ weight, data = df)
# 
# Coefficients:
#   (Intercept)       weight  
#        59.003        1.593 

or this:

lm(df$height ~ df$weight)

# Call:
#   lm(formula = df$height ~ df$weight)
# 
# Coefficients:
#   (Intercept)    df$weight  
#        59.003        1.593  

If you really want to use x instead of height , you must have a variable called x (in your df or in your environment). You can do that by creating a new variable:

x <-  df$height
y <- df$weight

lm(x ~ y)  

# Call:
#   lm(formula = x ~ y)
# 
# Coefficients:
#   (Intercept)            y  
#        59.003        1.593  


Or by changing the names of existing variables:

names(df) <- c("x", "y")
lm(x ~ y, data = df)

# Call:
#   lm(formula = x ~ y, data = df)
# 
# Coefficients:
#   (Intercept)            y  
#        59.003        1.593

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM