如何在R中使用lm()函数的变量?

5

假设我有一个包含两列名为“height”和“weight”的数据框(df)。

假设我定义:

x = "height"

我如何在lm()函数中使用x?使用df[x]或仅使用x都不起作用。

3个回答

6

两种方法:

使用paste函数创建公式

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

或者使用reformulate

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

我不知道。由于给定的文本没有提供任何需要翻译的内容,因此无法回答这个问题,请提供更具体的信息或指示。
x = "Cyl"
lm(paste0(x, '~', 'mpg'), data = mtcars)

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

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

并且与之相同

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

2
我们可以使用 glue 来创建公式。
x <- "height"
lm(glue::glue('{x} ~ weight'), data = df)

使用可重复的示例,使用 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  

1
我认为这实际上是最优雅的解决方案。我想要补充的是,如果已经加载了tidyverse,那么str_glue将已经可以从stringr包中使用。我相信它实际上是一个关于glue::glue的小包装器,但这可能会减少对特定项目的依赖。 - Raul Guarini Riva

1
当你运行x = "height"时,你正在将一串字符赋值给变量x
考虑这个数据框架:

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

如果您想使用heightweight进行回归分析,可以采取以下两种方法:
lm(height ~ weight, data = df)

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

或者这个:
lm(df$height ~ df$weight)

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

如果你真的想使用x而不是height,你必须有一个名为x的变量(在你的df或环境中)。你可以通过创建一个新变量来实现:
x <-  df$height
y <- df$weight

lm(x ~ y)  

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


或者通过更改现有变量的名称:

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接