Skip to content

Extracting weights from an lme object

2 messages · robert bagchi, Ben Bolker

#
Dear list,

My apologies if this is an elementary problem, but I can't find a solution
and it's driving me crazy!

As part of a function that bootstraps from an lme model, I need to extract
the weights argument that was used during model fitting.

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?

Any help would be much appreciated.
Robi

--
3 days later
#
robert bagchi <bagchi.r at ...> writes:
[snip]
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