【发布时间】:2016-01-04 12:24:01
【问题描述】:
使用我自己的数据集,我的系数太多了。 我只是想打印摘要而不(或部分)打印系数。
示例脚本:
lm.fit <- lm(iris$Sepal.Length ~ iris$Petal.Length)
summary(lm.fit)
输出:
> summary(lm.fit)
Call:
lm(formula = iris$Sepal.Length ~ iris$Petal.Length)
Residuals:
Min 1Q Median 3Q Max
-1.24675 -0.29657 -0.01515 0.27676 1.00269
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.30660 0.07839 54.94 <2e-16 ***
iris$Petal.Length 0.40892 0.01889 21.65 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4071 on 148 degrees of freedom
Multiple R-squared: 0.76, Adjusted R-squared: 0.7583
F-statistic: 468.6 on 1 and 148 DF, p-value: < 2.2e-16
期望的输出:
Call:
lm(formula = iris$Sepal.Length ~ iris$Petal.Length)
Residuals:
Min 1Q Median 3Q Max
-1.24675 -0.29657 -0.01515 0.27676 1.00269
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4071 on 148 degrees of freedom
Multiple R-squared: 0.76, Adjusted R-squared: 0.7583
F-statistic: 468.6 on 1 and 148 DF, p-value: < 2.2e-16
我知道我可以使用从下面提到的names 中选择的summary(lm.fit)$names 进行呼叫。但我需要的是相反的。总结没有系数。
> names(summary(lm.fit))
[1] "call" "terms" "residuals" "coefficients"
[5] "aliased" "sigma" "df" "r.squared"
[9] "adj.r.squared" "fstatistic" "cov.unscaled"
这可能吗?如果有,怎么做?
编辑:我实际上需要这个来自 RcppArmadillo 包的 fastLm。
我只是希望lm() 和fastLm() 的输出相同。 @LyzanderR 提供的代码非常适用于 lm()。
使用@LyzanderR 的示例,我在谷歌上搜索并创建了以下sn-p,它替换了RcppArmadillo 包提供的打印摘要。
print.summary.fastLm <- function(x, ...) {
## Alternate print summary
## Prints without coefficients
cat("\nCall:\n")
print(x$call)
cat("\nResiduals:\n")
print(x$residSum)
cat("\n")
#printCoefmat(x$coefficients, P.values=TRUE, has.Pvalue=TRUE)
digits <- max(3, getOption("digits") - 3)
cat("\nResidual standard error: ", formatC(x$sigma, digits=digits), " on ",
formatC(x$df), " degrees of freedom\n", sep="")
cat("Multiple R-squared: ", formatC(x$r.squared, digits=digits),
",\tAdjusted R-squared: ",formatC(x$adj.r.squared, digits=digits),
"\n", sep="")
invisible(x)
}
【问题讨论】: