[R-sig-ME] Extracting weights from an lme object

Ben Bolker bbolker at gmail.com
Tue Oct 14 04:30:45 CEST 2014


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



More information about the R-sig-mixed-models mailing list