[R] Extract estimates from each dataset: MI package

Joris Meys jorismeys at gmail.com
Fri Jun 18 15:00:11 CEST 2010


That's indeed the information I needed. I found the solution, and I'll
explain you how I reached it as well. Once you find your way around
objects, you can solve these problems yourself rather easily. So bear
with me for a moment. If you don't know what S4 classes are, please do
a search on www.rseek.org and read the introductory material.

With showMethods() you can find which methods apply to the model
object you have :

showMethods(class=class(model))

This gives a list of formal methods, but in your case it doesn't give
any useful function for extracting individual fits. Apparently no
get-methods exist for the different slots in a mi.pooled object.

Next step is to dive into the object itself.
> str(model)
Formal class 'mi.pooled' [package "mi"] with 4 slots
  ..@ .Data    : list()
  ..@ call     : language lmer.mi(formula = y ~ x1 + x2 + (1 | x3),
mi.object = IMP)
  ..@ mi.pooled:List of 2
  .. ..$ coefficients: Named num [1:3] 1.193 0.847 0.947
  .. .. ..- attr(*, "names")= chr [1:3] "(Intercept)" "x1" "x2"
  .. ..$ se          : Named num [1:3] 0.819 0.157 0.263
  .. .. ..- attr(*, "names")= chr [1:3] "(Intercept)" "x1" "x2"
  ..@ mi.fit   :List of 3
  .. ..$ Chain1:Formal class 'mer' [package "lme4"] with 34 slots
... (tons more output)

This tells you that :
 1. the object is a formal class (and hence an S4 class). You can see
that by the @ symbol, which is used for formal objects. If the list
contains only $ symbols, then it is a pure list in the strict sense of
the word.
 2. There are 4 slots in the object. They can be objects again, or lists, or ...
 3. The slot mi.fit is a list of 3 objects, and they appear to be of
the 'mer' class. That's the S4 class which originates from the lme4
package and hence contains the fits. This is the thing you need.

I ran it on the example I found in the help files of mi, so you might
have more items in that list, but the idea stays the same.

The correct way of reaching a slot is by either using the method for
it or when that one is not there, to use the function slot() :

fits <- slot(model,"mi.fit")

This in turn gives a list of 3 fits, each fit for a chain. You can
access them by :
fits$Chain1 etc.
or
fits[[1]] etc.

Alternatively, you can use a dirty hack :

fits <- model at mi.fit # gives the same list
fit1 <- model at mi.fit$Chain1 # gives the fit for the first chain
...

This direct accessing of slots is advised against for a number of very
good reasons, but I found it handy in a number of cases, especially to
extract specific data in very complex objects without having to call a
number of functions.

Hope this helps.
Cheers
Joris

On Fri, Jun 18, 2010 at 12:50 PM, KDT <dkadengye at gmail.com> wrote:
>
> Thanks Joris and pardon me for over assuming. let me add more information.
> My data is very huge and it is nested with repeated measurements. This is a
> sample of the dataset.
>     id sex lang sch      age chapt item length Resp
>      1   1        0     8 27.02095       3    1      4      0
>      1   1        0     8 27.02095       3    2     10      0
>      1   1        0     8 27.02095       1    3     10      0
>      1   1        0     8 27.02095       2    4     68      0
>      1   1        0     8 27.02095       2    5     63     NA
>      2   1        1     4 21.04946       3    1      4     NA
>      2   1        1     4 21.04946       3    2     10      1
>      2   1        1     4 21.04946       1    3     10      0
>      2   1        1     4 21.04946       2    4     68     NA
>      2   1        1     4 21.04946       2    5     63     NA
>      3   1        0     1 29.69218       3    1      4     NA
>      3   1        0     1 29.69218       3    2     10      1
>      3   1        0     1 29.69218       1    3     10      1
>      3   1        0     1 29.69218       2    4     68      1
>      3   1        0     1 29.69218       2    5     63      1
>      4   1        0     3 26.95328       3    1      4      0
>      4   1        0     3 26.95328       3    2     10     NA
>      4   1        0     3 26.95328       1    3     10      1
>      4   1        0     3 26.95328       2    4     68      0
>      4   1        0     3 26.95328       2    5     63     NA
>  he imputation model  and the model I am fitting are is as follows:
> imp <- mi(mydata,n.iter=6,n.imp=3, rand.imp.method="bootstrap",
> preprocess=F,
>         run.past.convergence=F, check.coef.convergence=T,add.noise=F, post.run=F)
>
> model <- lmer.mi(Resp~1+ sex + lang + age + length + (1|id) + (1|item)+
> (1|sch) + (1|chapt),imp, family=binomial(link="logit"))
> print(modelmi)
> display(modelmi)
>
> After fitting a model, I can use display(model) to visualize the pooled
> estimates as well as estimates of each imputed dataset. I can visualize
> these also by typing print(model).
>
> However I would like to know how I can extract estimates of single imputed
> datasets. I have tried several commands for the first imputed dataset like
> mi.pooled$coefficients[[1]] , summary(model$analyses[[1]], etc etc but each
> do not work and i keep getting an error "object of type 'closure' is not
> subsettable"
>
> Hope this makes my question clear.
>
> Thanks
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Extract-estimates-from-each-dataset-MI-package-tp2259864p2260019.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



More information about the R-help mailing list