R 如何简洁地从一个数据框中编写一个包含多个变量的公式?

R 如何简洁地从一个数据框中编写一个包含多个变量的公式?,r,dataframe,glm,lm,R,Dataframe,Glm,Lm,假设我有一个响应变量和一个包含三个协变量的数据(作为一个玩具示例): 我想对数据进行线性回归: fit = lm(y ~ d$x1 + d$x2 + d$y2) 有没有办法写出公式,这样我就不必写出每个单独的协变量?比如说 fit = lm(y ~ d) (我希望数据框中的每个变量都是协变量。)我这样问是因为我的数据框中实际上有50个变量,所以我想避免写出x1+x2+x3+etc是的,当然,只需将响应y添加为数据框中的第一列,并调用lm(): d2<-data.frame(y,d) &

假设我有一个响应变量和一个包含三个协变量的数据(作为一个玩具示例):

我想对数据进行线性回归:

fit = lm(y ~ d$x1 + d$x2 + d$y2)
有没有办法写出公式,这样我就不必写出每个单独的协变量?比如说

fit = lm(y ~ d)

(我希望数据框中的每个变量都是协变量。)我这样问是因为我的数据框中实际上有50个变量,所以我想避免写出
x1+x2+x3+etc

是的,当然,只需将响应
y
添加为数据框中的第一列,并调用
lm()

d2<-data.frame(y,d)
> d2
  y x1 x2 x3
1 1  4  3  4
2 4 -1  9 -4
3 6  3  8 -2
> lm(d2)

Call:
lm(formula = d2)

Coefficients:
(Intercept)           x1           x2           x3  
    -5.6316       0.7895       1.1579           NA  
d2
y x1 x2 x3
1 1  4  3  4
2 4 -1  9 -4
3 6  3  8 -2
>lm(d2)
电话:
lm(公式=d2)
系数:
(截距)x1x2x3
-5.6316 0.7895 1.1579 NA

另外,我关于R的信息指出,赋值为
有一个特殊的标识符,可以在公式中用来表示所有变量,它是
标识符

y <- c(1,4,6)
d <- data.frame(y = y, x1 = c(4,-1,3), x2 = c(3,9,8), x3 = c(4,-4,-2))
mod <- lm(y ~ ., data = d)

其中,
将只引用
x3
,因为
x1
x2
已经在公式中。

一种稍微不同的方法是从字符串创建公式。在
公式
帮助页面中,您将找到以下示例:

## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:25, sep="")
fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))

朱巴方法的一个扩展是使用
重新格式化,这是一个专门为此类任务设计的函数

## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:25, sep="")

reformulate(xnam, "y")
y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + 
    x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + 
    x22 + x23 + x24 + x25
##为具有大量变量的模型创建公式:

xnam您可以检查包
leaps
,尤其是函数
regsubsets()
用于模型选择的函数。如文件所述:

通过穷举搜索、向前或向后逐步或顺序替换进行模型选择

我构建此解决方案时,
重新格式化
不考虑变量名是否有空格

add_backticks = function(x) {
    paste0("`", x, "`")
}

x_lm_formula = function(x) {
    paste(add_backticks(x), collapse = " + ")
}

build_lm_formula = function(x, y){
    if (length(y)>1){
        stop("y needs to be just one variable")
    }
    as.formula(        
        paste0("`",y,"`", " ~ ", x_lm_formula(x))
    )
}

# Example
df <- data.frame(
    y = c(1,4,6), 
    x1 = c(4,-1,3), 
    x2 = c(3,9,8), 
    x3 = c(4,-4,-2)
    )

# Model Specification
columns = colnames(df)
y_cols = columns[1]
x_cols = columns[2:length(columns)]
formula = build_lm_formula(x_cols, y_cols)
formula
# output
# "`y` ~ `x1` + `x2` + `x3`"

# Run Model
lm(formula = formula, data = df)
# output
Call:
    lm(formula = formula, data = df)

Coefficients:
    (Intercept)           x1           x2           x3  
        -5.6316       0.7895       1.1579           NA  
add_backticks=函数(x){
粘贴0(“`”,x,“`”)
}
x_lm_公式=函数(x){
粘贴(添加反勾号(x),折叠=“+”)
}
build_lm_公式=函数(x,y){
如果(长度(y)>1){
stop(“y只需是一个变量”)
}
as.公式(
paste0(“`”,y,“`,“~”,x_lmu_公式(x))
)
}
#范例

df可能的副本也请参阅。谢谢!是的,我知道每个人都说要使用@gratur一个原因是像
foo(bar)这样的东西为什么
x3
NA
?数据框“d”有4列(y、x1、x2和x3)。那么如果公式是“y~”,那么右边的意思是“所有列”吗除了左边列出的那些?@stackoverflowuser2010是,
技术上意味着
数据
中的所有变量都不在公式中。@森林生态学家如果你是说
数据
是一个从该列表中查找公式中变量的列表,那么是的。数据框、列表或环境是可以接受的
data
参数的选项。如果你不是这个意思,你需要再扩展一点。@Gavin。这就是我的意思。谢谢。我如何使用data[[x]]作为列出的变量而不是实际的变量名(例如,'x3')?例如,我将如何进行以下工作?:
lm(d[[1]~d[[3]]+,data=d)
它适用于列表中的
名称;假设您有
ll这对于从文件中读取这些值非常有效。谢谢!注意as.formula部分是必须的
## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:25, sep="")
fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))
R> fmla
y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + 
    x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + 
    x22 + x23 + x24 + x25
## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:25, sep="")

reformulate(xnam, "y")
y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + 
    x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + 
    x22 + x23 + x24 + x25
# add y variable to data.frame d
d <- cbind(y, d)
reformulate(names(d)[-1], names(d[1]))
y ~ x1 + x2 + x3
mod <- lm(reformulate(names(d)[-1], names(d[1])), data=d)
add_backticks = function(x) {
    paste0("`", x, "`")
}

x_lm_formula = function(x) {
    paste(add_backticks(x), collapse = " + ")
}

build_lm_formula = function(x, y){
    if (length(y)>1){
        stop("y needs to be just one variable")
    }
    as.formula(        
        paste0("`",y,"`", " ~ ", x_lm_formula(x))
    )
}

# Example
df <- data.frame(
    y = c(1,4,6), 
    x1 = c(4,-1,3), 
    x2 = c(3,9,8), 
    x3 = c(4,-4,-2)
    )

# Model Specification
columns = colnames(df)
y_cols = columns[1]
x_cols = columns[2:length(columns)]
formula = build_lm_formula(x_cols, y_cols)
formula
# output
# "`y` ~ `x1` + `x2` + `x3`"

# Run Model
lm(formula = formula, data = df)
# output
Call:
    lm(formula = formula, data = df)

Coefficients:
    (Intercept)           x1           x2           x3  
        -5.6316       0.7895       1.1579           NA