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