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!
 A: 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.  
A: 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.
