# 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.

-
lm(c(8000, 50000, 116000) ~ c(6, 7, 8))

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