0

I am going to use a structural weight like varIndent(from = ~1 |Sex) and a custom made vector of weights called w in lme function in R. So I am wondering if anybody knows how to combine the weights in the function. This is an example (just made for this question)

n=25
data=data.frame(
  y   = rnorm(n),
  Sex = as.factor(sample(c('Male','Female','Female'),size = n,replace = TRUE)),
  Age = as.factor(sample(c('1-1-2013','10-3-2013','1-5-2013'),size = n,replace = TRUE)),
  Area= as.factor(sample(c('a','b','a'),size = n,replace = TRUE))
)
w = runif(n)
MY_WEIGHT_VECTOR = w/sum(w)



l = lme(
  fixed = y ~ Sex + Age,
  data = data,
  random = ~ 1 |    Area,
  weights = varIdent(form =  ~1 | Sex)+???MY_WEIGHT_VECTOR
)
MyQ
  • 459
  • 3
  • 13
  • I *think* you can use `varComb` to combine `varIdent` and `varFixed`, but I've never had reason to try and you are not providing data for testing. – Roland Aug 18 '17 at 10:32
  • Thanks @Roland. I am making a custom-made weight vector that is a vector w with sum(w)=1. I checked the help page for varComb() and I am not clear if I understand it. Please can you provide an example – MyQ Aug 18 '17 at 10:38
  • No, unless you make it easy by providing a small reproducible example. – Roland Aug 18 '17 at 10:40
  • @Roland please see the update. Actually, I am looking for the way to feed weights into the weight parameter of the function. – MyQ Aug 18 '17 at 10:59

1 Answers1

0

I believe you want something like this:

data$v <- 1/MY_WEIGHT_VECTOR^2

l = lme(
  fixed = y ~ Sex + Age,
  data = data,
  random = ~ 1 |    Area,
  weights = varComb(varIdent(form =  ~1 | Sex), varFixed(~ v))
)

varFixed takes the v vector and uses it as a fixed residual variance. Demonstrating that this indeed results in the desired weights:

varfun <- varFixed(~ v)
varfun <- Initialize(varfun, data)
all.equal(varWeights(varfun), MY_WEIGHT_VECTOR)
#[1] TRUE
Roland
  • 127,288
  • 10
  • 191
  • 288
  • I check your code for some examples. Why you use square weights? It seems the actual weights must be used! – TPArrow Aug 23 '17 at 09:36