简体   繁体   中英

How do you use variables in a regression formula in R?

How do you use a variable in a regression formula?

For example, using the 'Animals' dataset (in MASS), the following works fine:

data(Animals)
model <- lm(body ~ brain, data = Animals)

But what I want to do is:

data(Animals)
x <- "body"
y <- "brain"
model <- lm(x ~ y, data = Animals)

This obviously doesn't work, but I can't figure out what I need to do. Ultimately, I'm trying to put the formula inside a loop and have it run something different each time.

Sorry if the answer is very obvious - I've searched but I can't solve it.

Many thanks

使用get功能

lm(get(x) ~ get(y), data = Animals)

You need to make a proper formula from your character values. The easiest way in this case is the reformulate() function

reformulate(y,x)
# body ~ brain

then you can use this in your lm() call

lm(reformulate(y,x), data = Animals)
# 
# Call:
# lm(formula = reformulate(y, x), data = Animals)
# 
# Coefficients:
# (Intercept)        brain  
#  4316.32258     -0.06594  

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