I am trying to manually calculate beta-coefficients using Weighted Least Squares, which are given by:
X should comprise only one variable and the coefficients should include an intercept. I tried it as follows:
set.seed(1)
x = as.matrix(mtcars$wt)
y = as.matrix(mtcars$mpg)
w = runif(length(x))
(t(cbind(1,x)) %*% diag(w) %*% cbind(1,x))^(-1) %*%
t(cbind(1,x)) %*% diag(w) %*% y
This leads to:
[,1]
[1,] 38.92461
[2,] 11.52764
However, the lm-Funktion leads to different results:
lm(y~x, weights = w)
Call:
lm(formula = y ~ x, weights = w)
Coefficients:
(Intercept) x
37.896 -5.437
I am quite sure it must have something to do with the cbind(1,x)
-part, as (t(cbind(x)) %*% diag(w) %*% cbind(x))^(-1) %*% t(cbind(x)) %*% diag(w) %*% y
leads to the same results as lm(y~x -1, weights = w)
. Does anyone see what I did wrong while trying to calculate the coefficients manually?