如何将字符串公式传递给 R 的 lm 并查看摘要中的公式?
How to pass string formula to R's lm and see the formula in the summary?
在下面的 R 会话中,summary(model)
将公式显示为 model_str
。如何让它显示为 mpg ~ cyl + hp
,同时仍然能够通过字符串设置模型公式?
> data(mtcars)
> names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
> model_str <- 'mpg ~ cyl + hp'
> model <- lm(model_str, data=mtcars)
> summary(model)
Call:
lm(formula = model_str, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.4948 -2.4901 -0.1828 1.9777 7.2934
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 36.90833 2.19080 16.847 < 2e-16 ***
cyl -2.26469 0.57589 -3.933 0.00048 ***
hp -0.01912 0.01500 -1.275 0.21253
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.173 on 29 degrees of freedom
Multiple R-squared: 0.7407, Adjusted R-squared: 0.7228
F-statistic: 41.42 on 2 and 29 DF, p-value: 3.162e-09
它有点乱七八糟,所以可能很脆弱,但是修改模型的 call
元素的 formula
元素是可行的:
model$call$formula <- formula(model_str)
summary(model)
## Call:
## lm(formula = mpg ~ cyl + hp, data = mtcars)
您可以直接在一行中构建和评估调用:
eval(as.call(list(quote(lm), formula = model_str, data = quote(mtcars))))
#>
#> Call:
#> lm(formula = "mpg ~ cyl + hp", data = mtcars)
#>
#> Coefficients:
#> (Intercept) cyl hp
#> 36.90833 -2.26469 -0.01912
使用str2lang
,然后do.call
。
fo <- str2lang("mpg ~ hp + am")
do.call("lm", list(fo, quote(mtcars)))
#
# Call:
# lm(formula = mpg ~ hp + am, data = mtcars)
#
# Coefficients:
# (Intercept) hp am
# 26.58491 -0.05889 5.27709
使用 do.call
以便 model_str
在被发送到 lm
之前得到评估但是引用 mtcars
这样它就不会被评估(否则会有一个巨大的输出显示mtcars
).
中的实际值
do.call("lm", list(as.formula(model_str), data = quote(mtcars)))
给予:
Call:
lm(formula = mpg ~ cyl + hp, data = mtcars)
Coefficients:
(Intercept) cyl hp
36.90833 -2.26469 -0.01912
在下面的 R 会话中,summary(model)
将公式显示为 model_str
。如何让它显示为 mpg ~ cyl + hp
,同时仍然能够通过字符串设置模型公式?
> data(mtcars)
> names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
> model_str <- 'mpg ~ cyl + hp'
> model <- lm(model_str, data=mtcars)
> summary(model)
Call:
lm(formula = model_str, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.4948 -2.4901 -0.1828 1.9777 7.2934
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 36.90833 2.19080 16.847 < 2e-16 ***
cyl -2.26469 0.57589 -3.933 0.00048 ***
hp -0.01912 0.01500 -1.275 0.21253
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.173 on 29 degrees of freedom
Multiple R-squared: 0.7407, Adjusted R-squared: 0.7228
F-statistic: 41.42 on 2 and 29 DF, p-value: 3.162e-09
它有点乱七八糟,所以可能很脆弱,但是修改模型的 call
元素的 formula
元素是可行的:
model$call$formula <- formula(model_str)
summary(model)
## Call:
## lm(formula = mpg ~ cyl + hp, data = mtcars)
您可以直接在一行中构建和评估调用:
eval(as.call(list(quote(lm), formula = model_str, data = quote(mtcars))))
#>
#> Call:
#> lm(formula = "mpg ~ cyl + hp", data = mtcars)
#>
#> Coefficients:
#> (Intercept) cyl hp
#> 36.90833 -2.26469 -0.01912
使用str2lang
,然后do.call
。
fo <- str2lang("mpg ~ hp + am")
do.call("lm", list(fo, quote(mtcars)))
#
# Call:
# lm(formula = mpg ~ hp + am, data = mtcars)
#
# Coefficients:
# (Intercept) hp am
# 26.58491 -0.05889 5.27709
使用 do.call
以便 model_str
在被发送到 lm
之前得到评估但是引用 mtcars
这样它就不会被评估(否则会有一个巨大的输出显示mtcars
).
do.call("lm", list(as.formula(model_str), data = quote(mtcars)))
给予:
Call:
lm(formula = mpg ~ cyl + hp, data = mtcars)
Coefficients:
(Intercept) cyl hp
36.90833 -2.26469 -0.01912