Does R have a function for weighted least squares? Specifically, I am looking for something that computes intercept and slope.
Data sets
The dependent variable is dataset 3 and dataset 1 and 2 are the independent variables.
Yes, of course, there is a weights=
option to lm()
, the basic linear model fitting function. Quick example:
R> df <- data.frame(x=1:10)
R> lm(x ~ 1, data=df) ## i.e. the same as mean(df$x)
Call:
lm(formula = x ~ 1, data = df)
Coefficients:
(Intercept)
5.5
R> lm(x ~ 1, data=df, weights=seq(0.1, 1.0, by=0.1))
Call:
lm(formula = x ~ 1, data = df, weights = seq(0.1, 1, by = 0.1))
Coefficients:
(Intercept)
7
R>
so by weighing later observations more heavily the mean of the sequence 1 to 10 moves from 5.5 to 7.