使用R创建一个新的lm对象? [英] Create a new lm object using R?

查看:84
本文介绍了使用R创建一个新的lm对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个X矩阵和y矢量,例如:

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...

进行分解后,我找到系数B和残差E.如何创建一个新的lm对象(即lmQ)来存储B和E的结果,以获得类似的结果:

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...

请帮助谢谢!

推荐答案

对象只是具有class属性的列表.

Objects are just lists with a class attribute.

创建一个列表,分配组件,设置类-直到它看起来像str()给出的那样. 现在,lm()返回一些较大的对象,因此您需要做一些工作:

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"
> 

这篇关于使用R创建一个新的lm对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆