2014-02-22 29 views
0

假設我有一個X矩陣和Y矢量如下面:使用R創建一個新的lm對象?

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.如何創建一個新的流明對象(即LMQ),該將我的結果存儲在B和E中,以便得到類似如下的結果:

> lmQ(X) 
$coefficients 
    X1   X2 
    B1   B2 
$residuals 
[1] R1 R2 R3... 

請幫忙謝謝!

回答

3

對象只是具有class屬性的列表。

創建一個列表,分配組件,設置類 - 直到它看起來像str()給出的。 現在,lm()返回有點大的物體,所以你有一些工作要做:

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