从nlme模型中提取固定效果

  • 本文关键字:提取 nlme 模型 nlme
  • 更新时间 :
  • 英文 :


我试图从nlme模型中提取固定效果截距(1.807425)和残差(1.431592),但结构中似乎没有这些值,尽管它们出现在摘要中。我怎样才能得到它们呢?

library(nlme)
fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
summary(fm2)
> summary(fm2)
Linear mixed-effects model fit by REML
 Data: Orthodont 
       AIC      BIC    logLik
  447.5125 460.7823 -218.7563
Random effects:
 Formula: ~1 | Subject
        (Intercept) Residual
StdDev:    1.807425 1.431592
Fixed effects: distance ~ age + Sex 
                Value Std.Error DF   t-value p-value
(Intercept) 17.706713 0.8339225 80 21.233044  0.0000
age          0.660185 0.0616059 80 10.716263  0.0000
SexFemale   -2.321023 0.7614168 25 -3.048294  0.0054
 Correlation: 
          (Intr) age   
age       -0.813       
SexFemale -0.372  0.000
Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-3.74889609 -0.55034466 -0.02516628  0.45341781  3.65746539 
Number of Observations: 108
Number of Groups: 27 

> str(summary(fm2))
List of 22
 $ modelStruct :List of 1
  ..$ reStruct:List of 1
  .. ..$ Subject:Classes 'pdLogChol', 'pdSymm', 'pdMat'  atomic [1:1] 0.233
  .. .. .. ..- attr(*, "formula")=Class 'formula'  language ~1
  .. .. .. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. .. ..- attr(*, "Dimnames")=List of 2
  .. .. .. .. ..$ : chr "(Intercept)"
  .. .. .. .. ..$ : chr "(Intercept)"
  .. ..- attr(*, "settings")= num [1:4] 1 1 0 4
  .. ..- attr(*, "class")= chr "reStruct"
  .. ..- attr(*, "plen")= Named int 1
  .. .. ..- attr(*, "names")= chr "Subject"
  ..- attr(*, "settings")= num [1:4] 1 0 1 4
  ..- attr(*, "class")= chr [1:3] "lmeStructInt" "lmeStruct" "modelStruct"
  ..- attr(*, "pmap")= logi [1, 1] TRUE
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr "reStruct"
  ..- attr(*, "fixedSigma")= logi FALSE
 $ dims        :List of 5
  ..$ N    : int 108
  ..$ Q    : int 1
  ..$ qvec : Named num [1:3] 1 0 0
  .. ..- attr(*, "names")= chr [1:3] "Subject" "" ""
  ..$ ngrps: Named int [1:3] 27 1 1
  .. ..- attr(*, "names")= chr [1:3] "Subject" "X" "y"
  ..$ ncol : Named num [1:3] 1 3 1
  .. ..- attr(*, "names")= chr [1:3] "Subject" "" ""
 $ contrasts   :List of 1
  ..$ Sex: num [1:2, 1] 0 1
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "Male" "Female"
  .. .. ..$ : chr "Female"
 $ coefficients:List of 2
  ..$ fixed : Named num [1:3] 17.71 0.66 -2.32
  .. ..- attr(*, "names")= chr [1:3] "(Intercept)" "age" "SexFemale"
  ..$ random:List of 1
  .. ..$ Subject: num [1:27, 1] -1.7 -1.7 -1.38 -1.16 -1.05 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:27] "M16" "M05" "M02" "M11" ...
  .. .. .. ..$ : chr "(Intercept)"
 $ varFix      : num [1:3, 1:3] 0.6954 -0.0417 -0.2362 -0.0417 0.0038 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "(Intercept)" "age" "SexFemale"
  .. ..$ : chr [1:3] "(Intercept)" "age" "SexFemale"
 $ sigma       : num 1.43
 $ apVar       : num [1:2, 1:2] 0.026925 -0.000981 -0.000981 0.00625
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "reStruct.Subject" "lSigma"
  .. ..$ : chr [1:2] "reStruct.Subject" "lSigma"
  ..- attr(*, "Pars")= Named num [1:2] 0.592 0.359
  .. ..- attr(*, "names")= chr [1:2] "reStruct.Subject" "lSigma"
  ..- attr(*, "natural")= logi TRUE
 $ logLik      : num -219
 $ numIter     : NULL
 $ groups      :'data.frame':   108 obs. of  1 variable:
  ..$ Subject: Ord.factor w/ 27 levels "M16"<"M05"<"M02"<..: 15 15 15 15 3 3 3 3 7 7 ...
 $ call        : language lme.formula(fixed = distance ~ age + Sex, data = Orthodont, random = ~1)
 $ terms       :Classes 'terms', 'formula'  language distance ~ age + Sex
  .. ..- attr(*, "variables")= language list(distance, age, Sex)
  .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:3] "distance" "age" "Sex"
  .. .. .. ..$ : chr [1:2] "age" "Sex"
  .. ..- attr(*, "term.labels")= chr [1:2] "age" "Sex"
  .. ..- attr(*, "order")= int [1:2] 1 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(distance, age, Sex)
  .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "factor"
  .. .. ..- attr(*, "names")= chr [1:3] "distance" "age" "Sex"
 $ method      : chr "REML"
 $ fitted      : num [1:108, 1:2] 23 24.3 25.6 26.9 23 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:108] "1" "2" "3" "4" ...
  .. ..$ : chr [1:2] "fixed" "Subject"
 $ residuals   : Named num [1:5] -3.7489 -0.5503 -0.0252 0.4534 3.6575
  ..- attr(*, "names")= chr [1:5] "Min" "Q1" "Med" "Q3" ...
 $ fixDF       :List of 2
  ..$ X    : Named num [1:3] 80 80 25
  .. ..- attr(*, "names")= chr [1:3] "(Intercept)" "age" "SexFemale"
  ..$ terms: Named num [1:3] 80 80 25
  .. ..- attr(*, "names")= chr [1:3] "(Intercept)" "age" "Sex"
  ..- attr(*, "assign")=List of 3
  .. ..$ (Intercept): int 1
  .. ..$ age        : int 2
  .. ..$ Sex        : int 3
  ..- attr(*, "varFixFact")= num [1:3, 1:3] 0.3741 -0.6777 0.3102 0 0.0616 ...
 $ na.action   : NULL
 $ data        :Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':  108 obs. of  4 variables:
  ..$ distance: num [1:108] 26 25 29 31 21.5 22.5 23 26.5 23 22.5 ...
  ..$ age     : num [1:108] 8 10 12 14 8 10 12 14 8 10 ...
  ..$ Subject : Ord.factor w/ 27 levels "M16"<"M05"<"M02"<..: 15 15 15 15 3 3 3 3 7 7 ...
  ..$ Sex     : Factor w/ 2 levels "Male","Female": 1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "outer")=Class 'formula'  language ~Sex
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  ..- attr(*, "formula")=Class 'formula'  language distance ~ age | Subject
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  ..- attr(*, "labels")=List of 2
  .. ..$ x: chr "Age"
  .. ..$ y: chr "Distance from pituitary to pterygomaxillary fissure"
  ..- attr(*, "units")=List of 2
  .. ..$ x: chr "(yr)"
  .. ..$ y: chr "(mm)"
  ..- attr(*, "FUN")=function (x)  
  .. ..- attr(*, "source")= chr "function (x) max(x, na.rm = TRUE)"
  ..- attr(*, "order.groups")= logi TRUE
 $ corFixed    : num [1:3, 1:3] 1 -0.813 -0.372 -0.813 1 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "(Intercept)" "age" "SexFemale"
  .. ..$ : chr [1:3] "(Intercept)" "age" "SexFemale"
 $ tTable      : num [1:3, 1:5] 17.7067 0.6602 -2.321 0.8339 0.0616 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "(Intercept)" "age" "SexFemale"
  .. ..$ : chr [1:5] "Value" "Std.Error" "DF" "t-value" ...
 $ BIC         : num 461
 $ AIC         : num 448
 - attr(*, "class")= chr [1:2] "summary.lme" "lme"
 - attr(*, "units")=List of 2
  ..$ x: chr "(yr)"
  ..$ y: chr "(mm)"
 - attr(*, "labels")=List of 2
  ..$ x: chr "Age"
  ..$ y: chr "Distance from pituitary to pterygomaxillary fissure"
 - attr(*, "verbose")= logi FALSE
 - attr(*, "oClass")= chr "lme"

您最好使用提取函数。

分别用fixef(fm2)resid(fm2)得到固定效果和残差。

然而,这些不是你在问题中陈述的数字,这是RE的sd。要获得RE成分的方差和相关性,你可以使用VarCorr(fm2)(注意VarCorr返回一个字符矩阵)

对于剩余方差,您应该尝试fm2$sigma

最新更新