使用下面的代码,我总结了一个线性模型:
x = c(1,2,3)
y = c(1,2,3)
m = lm(y ~ x)
summary(m)
打印:
Call:
lm(formula = y ~ x)
Residuals:
1 2 3
0 0 0
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0 0 NA NA
x 1 0 Inf <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0 on 1 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: Inf on 1 and 1 DF, p-value: < 2.2e-16
Warning message:
In summary.lm(m) : essentially perfect fit: summary may be unreliable
如何创建新的汇总函数,它只返回&#39;系数&#39;财产:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.874016 0.160143 -11.70 <2e-16 ***
waiting 0.075628 0.002219 34.09 <2e-16 ***
这是我的代码:
tosum2 <- summary(m)
summary.myclass <- function(x)
{
return(x$Coefficients)
}
class(tosum2) <- c('myclass', 'summary')
summary(tosum2)
但返回NULL。
更新:
如何从汇总函数中检查可用的方法(coef是摘要中可用方法的示例)? methods(class="summary")
返回null