Weighted Regression

Here is a quick example of how to recreate weighted regression without needing the weights=... part of lm().  It also shows how to set up your own intercept in R. 


set.seed(50)
x0=rep(1,100)         #specify the intercept because they multiply by the weights
x=round(runif(100),2)*100
w=(runif(100,1,10))^2
y=round(3*x+rnorm(100,0,10),2)
plot(x,y)

X=cbind(x0,x)

summary(lm(y_x))  
summary(lm(y~0+X))   # this is to show a different way of setting up the intercept in R

summary(lm(y~0+X, weights=w))   #with weights=....
y2=sqrt(w)*y
X2=sqrt(w)*X
summary(lm(y2~0+X2))                 #without weights=...

 

Tags: