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

robert bagchi bagchi.r at gmail.com
Fri Oct 10 15:56:49 CEST 2014


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

-- 
-- 
Dr Robert Bagchi
Ecosystem Management
Department of Environmental Systems Science
ETH Zürich
Universitätstrasse 16
8092 Zurich
Switzerland

tel: +41 (0)44 63 26805
email: bagchi.r at gmail.com (preferred)
robert.bagchi at usys.ethz.ch
web: http://www.ecology.ethz.ch/people/obass/rbagchi

*Recent publications:*
Pathogens and insect herbivores drive rainforest plant diversity and
composition,
<http://www.nature.com/nature/journal/vaop/ncurrent/full/nature12911.html>
Nature.
Testing for enemy‐mediated density‐dependence in the mortality of
seedlings: field experiments with five Neotropical tree species
<http://onlinelibrary.wiley.com/doi/10.1111/j.1600-0706.2013.00835.x/full>,
Oikos.
Shifting Baselines on a Tropical Forest Frontier: Extirpations Drive
Declines in Local Ecological Knowledge
<http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0086598#pone-0086598-g003>,
PLoS One

	[[alternative HTML version deleted]]



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