问题
Assuming I have an X matrix and y vector such as the following:
X=
[,1] [,2] [,3]
[1,] 83.0 234.289 235.6 ...
[2,] 88.5 259.426 232.5 ...
[3,] 88.2 258.054 368.2 ...
y=
[1] 60.323 61.122 60.171...
After conducting a decomposition, I find the coefficients B and residuals E. How can I create a new lm object (ie lmQ) that stores my results for B and E in order to get something like this:
> lmQ(X)
$coefficients
X1 X2
B1 B2
$residuals
[1] R1 R2 R3...
Please help thanks!
回答1:
Objects are just lists with a class
attribute.
Create a list, assign the components, set the class -- til it looks like what str()
gives.
Now, lm()
returns somewhat large objects so you have some work to do:
> df <- data.frame(y=runif(10), x=rnorm(10))
> fit <- lm(y ~ x, df)
> str(fit)
List of 12
$ coefficients : Named num [1:2] 0.5368 0.0314
..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
$ residuals : Named num [1:10] 0.2899 -0.3592 -0.1753 0.3187 -0.0235 ...
..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
$ effects : Named num [1:10] -1.734 0.104 -0.325 0.485 -0.158 ...
..- attr(*, "names")= chr [1:10] "(Intercept)" "x" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:10] 0.553 0.526 0.573 0.479 0.569 ...
..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
..$ qr : num [1:10, 1:2] -3.162 0.316 0.316 0.316 0.316 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:10] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$ qraux: num [1:2] 1.32 1.22
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 8
$ xlevels : Named list()
$ call : language lm(formula = y ~ x, data = df)
$ terms :Classes 'terms', 'formula' length 3 y ~ x
.. ..- attr(*, "variables")= language list(y, x)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. ..$ : chr "x"
.. ..- attr(*, "term.labels")= chr "x"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, x)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y" "x"
$ model :'data.frame': 10 obs. of 2 variables:
..$ y: num [1:10] 0.843 0.167 0.398 0.798 0.545 ...
..$ x: num [1:10] 0.504 -0.337 1.151 -1.835 1.011 ...
..- attr(*, "terms")=Classes 'terms', 'formula' length 3 y ~ x
.. .. ..- attr(*, "variables")= language list(y, x)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. .. ..$ : chr "x"
.. .. ..- attr(*, "term.labels")= chr "x"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(y, x)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
- attr(*, "class")= chr "lm"
>
来源:https://stackoverflow.com/questions/21955145/create-a-new-lm-object-using-r