s-news
[Top] [All Lists]

Re: How to extract elements from a fitted model?

To: WEN Songqiao <sqwen@math.hkbu.edu.hk>
Subject: Re: How to extract elements from a fitted model?
From: Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>
Date: Mon, 10 Apr 2006 13:33:58 -0500
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <00bc01c65c80$3d727b80$b70ab69e@PC20026570>
Organization: PDF Solutions, Inc.
References: <00bc01c65c80$3d727b80$b70ab69e@PC20026570>
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

WEN Songqiao wrote:
Hi, everyone
I want to do simulation with R, how can I extract the estimate "0.9005372 1.1191585 2.0367914" and "0.9957503 0.9231837" from the following fitted model and put them into a null vector a?
thanks,
WEN Songqiao
----------------------------------------------------------------------------------------------------------------------------------------------
Linear mixed-effects model fit by REML
  Data: data1
  Log-restricted-likelihood: -478.909
  Fixed: yij ~ xi1 + xi2
(Intercept)         xi1         xi2
  0.9005372   1.1191585   2.0367914
Random effects:
 Formula: ~+1 | subject
        (Intercept)  Residual
StdDev:   0.9957503 0.9231837
Number of Observations: 300
Number of Groups: 100
----------------------------------------------------------------------------------------------------------------------------------------

R? Do you mean S-PLUS since you've sent your comments to S-News? If you really mean R, then use the posting guide:

http://www.R-project.org/posting-guide.html

and resubmit your question to R-help. I think my answers are the same in either case, but it's best to keep the questions for R on R-help and the questions for S-PLUS on S-News.

Use ?fixef to get the fixed effects and my "extract.stdev" function below to get the random effects:

> # From ?lme
> fm2 <- lme(distance ~ age + Sex, data=Orthodont, random = ~ 1)
> fm2
Linear mixed-effects model fit by REML
  Data: Orthodont
  Log-restricted-likelihood: -218.7563
  Fixed: distance ~ age + Sex
 (Intercept)       age       Sex
    17.70671 0.6601852 -2.321023

Random effects:
 Formula:  ~ 1 | Subject
        (Intercept) Residual
StdDev:    1.807425 1.431592

Number of Observations: 108
Number of Groups: 27
> fixef(fm2)
 (Intercept)       age       Sex
    17.70671 0.6601852 -2.321023
> extract.stdev
function(object)
{
  struct <- object$modelStruct$reStruct
  sigma <- object$sigma
  Var <- rev(sapply(struct, as.matrix))
  labels <- sapply(seq(Var), function(x, names)
  paste(rev(names[1:x]), collapse = " %in% "), names = names(Var))
  Sdev <- c(sqrt(Var) * sigma, sigma)
  names(Sdev) <- c(labels, "Residual")
  Sdev
}
> extract.stdev(fm2)
  Subject Residual
 1.807425 1.431592
>


<Prev in Thread] Current Thread [Next in Thread>