假设我有fit=lm(y~x)。向我展示fit组件的R命令是什么?我记得,如果我要运行mystery_Rcommand(fit),那么控制台将返回一个组件列表,如"fitted.values“、”残差“、”系数“等。了解这些组件将允许我执行fit$fitted.values并查看拟合值和其他不匹配的值。但我不记得那个R命令是什么。有人能帮忙吗?
发布于 2014-07-02 18:19:35
fitted(),resid(),coef(),.将提取这些成分。methods(class = "lm")会给你看更多。
str()将向您展示模型的结构和列表的组件。?lm将告诉您lm()返回的对象中应该包含哪些组件。
一个例子,来自?lm
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
str(lm.D9)
coef(lm.D9)
fitted(lm.D9)制作(编辑):
> str(lm.D9)
List of 13
$ coefficients : Named num [1:2] 5.032 -0.371
..- attr(*, "names")= chr [1:2] "(Intercept)" "groupTrt"
$ residuals : Named num [1:20] -0.862 0.548 0.148 1.078 -0.532 ...
..- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
$ effects : Named num [1:20] -21.674 -0.83 0.197 1.127 -0.483 ...
..- attr(*, "names")= chr [1:20] "(Intercept)" "groupTrt" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:20] 5.03 5.03 5.03 5.03 5.03 ...
..- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
....
> coef(lm.D9)
(Intercept) groupTrt
5.032 -0.371
> fitted(lm.D9)
1 2 3 4 5 6 7 8 9 10 11 12 13
5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 5.032 4.661 4.661 4.661
14 15 16 17 18 19 20
4.661 4.661 4.661 4.661 4.661 4.661 4.661https://stackoverflow.com/questions/24538234
复制相似问题