Could anyone offer some pointers on how to use the weights argument in R's lm function? Say, for instance you were trying to fit a model on traffic data, and you had several hundred rows, each of which represented a city (with a different population). If you wanted the model to adjust the relative influence of each observation based on population size, could you simply specify weights=[the column containing the city's population]? Is that the sort of vector that can go into weights? Or would you need to use a different R function/package/approach entirely?

Curious to hear how people tackle this one - didn't see it covered in any of the linear modeling tutorials I saw out there. Thanks!


2 Answers 2


I think R help page of lm answers your question pretty well. The only requirement for weights is that the vector supplied must be the same length as the data. You can even supply only the name of the variable in the data set, R will take care of the rest, NA management, etc. You can also use formulas in the weight argument. Here is the example:

x <-c(rnorm(10),NA)
df <- data.frame(y=1+2*x+rnorm(11)/2, x=x, wght1=1:11)

## Fancy weights as numeric vector

# Fancy weights as formula on column of the data set

# Mundane weights as the column of the data set

Note that weights must be positive, otherwise R will produce an error.

  • $\begingroup$ but do the weights have to sum to one? I get different results in my lm summary if they are scaled vs not... $\endgroup$ Jun 21, 2016 at 22:32
  • $\begingroup$ No, the weights need not to sum up to one. What is different in the lm summary? The coefficients or the standard errors? $\endgroup$
    – mpiktas
    Jun 22, 2016 at 6:06
  • $\begingroup$ Residuals and their standard error differ but coefficients and their errors don't. $\endgroup$ Jun 22, 2016 at 13:58

What you suggest should work. See if this makes sense:

lm(c(8000, 50000, 116000) ~ c(6, 7, 8))
lm(c(8000, 50000, 116000) ~ c(6, 7, 8), weight = c(123, 123, 246))
lm(c(8000, 50000, 116000, 116000) ~ c(6, 7, 8, 8))

The second line produces the same intercept and slope as the third line (distinct from the first line's result), by giving one observation relatively twice the weight of each of the other two observations, similar to the impact of duplicating the third observation.

  • $\begingroup$ I tried that but found that the summary output are different for 2nd and 3rd line, especially for the p-value of the coefficient, I wonder this will happen if the 2 statements refer to the same dataset. I posted a question about this at stackoverflow.com/questions/10268689/weighted-regression-in-r $\endgroup$
    – lokheart
    Apr 22, 2012 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.