I’ve been doing a lot of meta-analytic things lately. More on that anon. But one quick thing that came up was variance weighting with mixed models in R, and after a few web searches, I wanted to post this, more as a note-to-self and others than anything. Now, in a simple linear model, weighting by variance or sample size is straightforward.

```
#variance
lm(y ~ x, data = dat, weights = 1/v)
#sample size
lm(y ~ x, data = dat, weights = n)
```

You can use the same sort of weights argument with lmer. But, what about if you’re using nlme? There are reasons to do so. Things change a bit, as nlme uses a wide array of weighting functions for the variance to give it some wonderful flexibility – indeed, it’s a reason to use nlme in the first place! But, for such a simple case, to get the equivalent of the above, here’s the tricky little difference. I’m using gls, generalized least squares, but this should work for lme as well.

```
#variance
gls(y ~ x, data=dat, weights = ~v)
#sample size
gls(y ~ x, data = dat, weights = ~1/n)
```

OK, end note to self. Thanks to John Griffin for prompting this.

I’m trying to use sample weights to provide unbiased estimates that are more representative of the population. Would I substitute my sample weights variable into your variable labeled ‘n’ in the nlme instance? Thanks!

So long as a higher weight means more confidence, then yes. Note that you may want to explore nlme’s varIdent, varFixed, and other functions if you want a more flexible weighting schema.