在线性模型中结合 cbind 和 paste
Combining cbind and paste in linear model
我想知道如何提出 lm
公式语法,使我能够将 paste
与 cbind
一起用于多元多元回归。
示例
在我的模型中,我有一组变量,对应于下面的原始示例:
data(mtcars)
depVars <- paste("mpg", "disp")
indepVars <- paste("qsec", "wt", "drat")
问题
我想用我的 depVars
和 indepVars
创建一个模型。手写的模型看起来像这样:
modExmple <- lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars)
我有兴趣生成相同的公式而不引用变量名称并且仅使用上面定义的 depVars
和 indepVars
向量。
尝试 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)
旁注
- 我找到了一些关于如何将
paste
与公式一起使用的 good examples,但我想知道如何与 cbind
. 结合使用
- 这主要是一道语法题;在我的真实数据中,我有许多变量我想引入到模型中,并且使用先前生成的向量更加简洁,并使代码更易于呈现。实际上,我只对创建一个包含
cbind
的公式对象感兴趣,变量名对应一个向量,其余变量对应另一个向量。
- 一句话,我想得到
modExample
中的公式,而无需 键入变量名。
认为可行。
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"
我想知道如何提出 lm
公式语法,使我能够将 paste
与 cbind
一起用于多元多元回归。
示例
在我的模型中,我有一组变量,对应于下面的原始示例:
data(mtcars)
depVars <- paste("mpg", "disp")
indepVars <- paste("qsec", "wt", "drat")
问题
我想用我的 depVars
和 indepVars
创建一个模型。手写的模型看起来像这样:
modExmple <- lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars)
我有兴趣生成相同的公式而不引用变量名称并且仅使用上面定义的 depVars
和 indepVars
向量。
尝试 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)
旁注
- 我找到了一些关于如何将
paste
与公式一起使用的 good examples,但我想知道如何与cbind
. 结合使用
- 这主要是一道语法题;在我的真实数据中,我有许多变量我想引入到模型中,并且使用先前生成的向量更加简洁,并使代码更易于呈现。实际上,我只对创建一个包含
cbind
的公式对象感兴趣,变量名对应一个向量,其余变量对应另一个向量。 - 一句话,我想得到
modExample
中的公式,而无需 键入变量名。
认为可行。
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"