是这样的,你可以用 names 或者 str 函数看到一个object内部的情况和typeof函数来看object的type。
这样你就看到残差在residuals里面,而这个summary是一个list,所以你就可以用 summary(mdl)$residuals
来得到残差。
- > names(summary(mdl))
- [1] "call" "terms" "residuals" "coefficients"
- [5] "aliased" "sigma" "df" "r.squared"
- [9] "adj.r.squared" "fstatistic" "cov.unscaled"
- >str(summary(mdl))
- List of 11
- $ call : language lm(formula = y1 ~ x1)
- $ terms :Classes 'terms', 'formula' length 3 y1 ~ x1
- .. ..- attr(*, "variables")= language list(y1, x1)
- .. ..- attr(*, "factors")= int [1:2, 1] 0 1
- .. .. ..- attr(*, "dimnames")=List of 2
- .. .. .. ..$ : chr [1:2] "y1" "x1"
- .. .. .. ..$ : chr "x1"
- .. ..- attr(*, "term.labels")= chr "x1"
- .. ..- attr(*, "order")= int 1
- .. ..- attr(*, "intercept")= int 1
- .. ..- attr(*, "response")= int 1
- .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
- .. ..- attr(*, "predvars")= language list(y1, x1)
- .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
- .. .. ..- attr(*, "names")= chr [1:2] "y1" "x1"
- $ residuals : Named num [1:100] -1.452 -0.327 0.114 2.161 -1.562 ...
- ..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
- $ coefficients : num [1:2, 1:4] -0.00504 4.89803 0.1043 0.10449 -0.04835 ...
- ..- attr(*, "dimnames")=List of 2
- .. ..$ : chr [1:2] "(Intercept)" "x1"
- .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
- $ aliased : Named logi [1:2] FALSE FALSE
- ..- attr(*, "names")= chr [1:2] "(Intercept)" "x1"
- $ sigma : num 1.04
- $ df : int [1:3] 2 98 2
- $ r.squared : num 0.957
- $ adj.r.squared: num 0.957
- $ fstatistic : Named num [1:3] 2197 1 98
- ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
- $ cov.unscaled : num [1:2, 1:2] 0.010081 0.000903 0.000903 0.010118
- ..- attr(*, "dimnames")=List of 2
- .. ..$ : chr [1:2] "(Intercept)" "x1"
- .. ..$ : chr [1:2] "(Intercept)" "x1"
- - attr(*, "class")= chr "summary.lm"
- > typeof(summary(mdl))
- [1] "list"
复制代码