[R-sig-ME] Extracting weights from an lme object
robert bagchi
bagchi.r at gmail.com
Tue Oct 14 14:51:29 CEST 2014
Dear Dr Bolker,
Great - that worked perfectly. Thanks!
In case anyone needs to do this in the future, I am reliably getting the
weights in the correct order by doing
head(1/getCovariate(mod$modelStruct$varStruct)[order(order(getGroups(mod)))])
This seems to work fine for nested models too.
All the best,
Robi
>
> ---------- Forwarded message ----------
> From: Ben Bolker <bbolker at gmail.com>
> To: r-sig-mixed-models at r-project.org
> Cc:
> Date: Tue, 14 Oct 2014 02:30:45 +0000 (UTC)
> Subject: Re: [R-sig-ME] Extracting weights from an lme object
> robert bagchi <bagchi.r at ...> writes:
>
> >
> > Dear list,
> >
>
> [snip]
>
> > For example, if we make up some (simple) data
> >
> > ## covariates
> > n <-20
> > dat <- data.frame(x=runif(n),grp=sample(letters[1:10], n, replace=T),
> > wts=runif(n))
> > dat$wts <- dat$wts/mean(dat$wts)
> >
> > ## model matrices
> > xmat <- model.matrix(~x, data=dat)
> > zmat <- model.matrix(~0+grp, data=dat)
> > ## effects
> > beta <- c(0, 0)
> > b <- rnorm(10)
> > ## response
> > dat$y <- rnorm(n=n, mean=xmat %*% beta + zmat %*% b, sd=sqrt(1/dat$wts))
> >
> > ## we can then fit the model with
> > require(nlme)
> >
> > mod <- lme(y~x, random=~1|grp, weights=varFixed(value=~I(1/wts)),
> > data=dat)
> > ## however, while we can extract the weights used during the model
> fitting
> > with
> > (getCovariate(mod$modelStruct$varStruct))
> >
> > ## They are in a *different order* from those in the original data
> > (1/dat$wts)
> >
> > Does anyone know a fairly fool-proof way of getting the weights out in
> the
> > same format as went into the model?
>
> Don't know, but my guess is that lme is internally sorting the
> weights by group (a way to test this would be to set up an example
> with the data in group order and see if the order of the weights
> remained unchanged). If so, you could try to replicate the process
> of ordering by group and figure out the inverse permutation required
> to undo that ordering ...
>
> Ben Bolker
>
>
>
[[alternative HTML version deleted]]
More information about the R-sig-mixed-models
mailing list