# How to use weights in function lm in R?

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!

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
summary(lm(y~x,data=df,weights=(df\$wght1)^(3/4)))

# Fancy weights as formula on column of the data set
summary(lm(y~x,data=df,weights=I(wght1^(3/4))))

# Mundane weights as the column of the data set
summary(lm(y~x,data=df,weights=wght1))


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

• but do the weights have to sum to one? I get different results in my lm summary if they are scaled vs not... – Palace Chan Jun 21 '16 at 22:32
• No, the weights need not to sum up to one. What is different in the lm summary? The coefficients or the standard errors? – mpiktas Jun 22 '16 at 6:06
• Residuals and their standard error differ but coefficients and their errors don't. – Palace Chan Jun 22 '16 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.

• 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 – lokheart Apr 22 '12 at 14:26