在线性模型中结合 cbind 和 paste

Combining cbind and paste in linear model

我想知道如何提出 lm 公式语法,使我能够将 pastecbind 一起用于多元多元回归。

示例

在我的模型中,我有一组变量,对应于下面的原始示例:

data(mtcars)
depVars <- paste("mpg", "disp")
indepVars <- paste("qsec", "wt", "drat")

问题

我想用我的 depVarsindepVars 创建一个模型。手写的模型看起来像这样:

modExmple <- lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars)

我有兴趣生成相同的公式而不引用变量名称并且仅使用上面定义的 depVarsindepVars 向量。


尝试 1

例如,我的想法对应于:

mod1 <- lm(formula = formula(paste(cbind(paste(depVars, collapse = ",")), " ~ ",
                                   indepVars)), data = mtcars)

尝试 2

我也试过这个:

mod2 <- lm(formula = formula(cbind(depVars), paste(" ~ ",
                                                   paste(indepVars, 
                                                         collapse = " + "))),
           data = mtcars)

旁注

认为可行。

data(mtcars)
depVars <- c("mpg", "disp")
indepVars <- c("qsec", "wt", "drat")

lm(formula(paste('cbind(',
                 paste(depVars, collapse = ','),
                 ') ~ ',
                 paste(indepVars, collapse = '+'))), data = mtcars)

以下所有解决方案都使用这些定义:

depVars <- c("mpg", "disp")
indepVars <- c("qsec", "wt", "drat")

1) 字符串公式 创建一个表示公式的字符串然后运行 lm 使用do.call。请注意,输出中显示的公式正确显示并写出。

fo <- sprintf("cbind(%s) ~ %s", toString(depVars), paste(indepVars, collapse = "+"))
do.call("lm", list(fo, quote(mtcars)))

给予:

Call:
lm(formula = "cbind(mpg, disp) ~ qsec+wt+drat", data = mtcars)

Coefficients:
             mpg       disp    
(Intercept)   11.3945  452.3407
qsec           0.9462  -20.3504
wt            -4.3978   89.9782
drat           1.6561  -41.1148

1a) 这也行得通:

fo <- sprintf("cbind(%s) ~.", toString(depVars))
do.call("lm", list(fo, quote(mtcars[c(depVars, indepVars)])))

给予:

Call:
lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars[c(depVars, 
    indepVars)])

Coefficients:
             mpg       disp    
(Intercept)   11.3945  452.3407
qsec           0.9462  -20.3504
wt            -4.3978   89.9782
drat           1.6561  -41.1148

2) reformulate @ak运行 和@Konrad,在问题下方的评论中建议使用 reformulate。这种方法产生一个 "formula" 对象,而上面的方法产生一个字符串作为公式。 (如果上面的先前解决方案需要这样做,则可以使用 fo <- formula(fo) 。)请注意,重要的是 reformulate 的响应参数是调用对象而不是字符串,否则 reformulate 会将字符串解释为单个变量的名称。

fo <- reformulate(indepVars, parse(text = sprintf("cbind(%s)", toString(depVars)))[[1]])
do.call("lm", list(fo, quote(mtcars)))

给予:

Call:
lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars)

Coefficients:
             mpg       disp    
(Intercept)   11.3945  452.3407
qsec           0.9462  -20.3504
wt            -4.3978   89.9782
drat           1.6561  -41.1148

3) lm.fit 另一种完全不用公式的方法是:

m <- as.matrix(mtcars)
fit <- lm.fit(cbind(1, m[, indepVars]), m[, depVars])

输出是包含以下组件的列表:

> names(fit)
[1] "coefficients"  "residuals"     "effects"       "rank"         
[5] "fitted.values" "assign"        "qr"            "df.residual"