公式接口解释.
为指定所有其他变量。要删除特定变量,只需subset
:
summary(lm(hp~.,data=subset(mtcars,select=-qsec)))
Call:
lm(formula = hp ~ ., data = subset(mtcars, select = -qsec))
Residuals:
Min 1Q Median 3Q Max
-39.100 -15.833 1.593 17.677 33.582
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 47.4615 127.7481 0.372 0.71380
mpg -2.1924 1.9776 -1.109 0.27957
cyl 8.8756 9.4878 0.935 0.35970
disp 0.4537 0.1335 3.399 0.00258 **
drat -4.3969 15.7096 -0.280 0.78218
wt -30.4149 15.2230 -1.998 0.05823 .
vs 24.1493 18.2235 1.325 0.19871
am 11.1254 19.2025 0.579 0.56823
gear 7.5968 14.2172 0.534 0.59847
carb 19.2907 6.5193 2.959 0.00725 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 25.41 on 22 degrees of freedom
Multiple R-squared: 0.9025, Adjusted R-squared: 0.8627
F-statistic: 22.63 on 9 and 22 DF, p-value: 3.923e-09
如果您正在使用不解释“。”的函数,那么您可以修改您的函数以用于setdiff
删除不需要的变量:
as.formula(paste("hp ~",paste(setdiff(names(mtcars),c("hp","qsec")),collapse="+")))
hp ~ mpg + cyl + disp + drat + wt + vs + am + gear + carb