I want to get the variance-covariance matrix manually, then take it's diagonal elements to derive coefficient standard errors, according to:
sqrt(diag(vcov(lm_obj)))
My code:
dummy_data <- data.frame(
x1 = rnorm(100, 0, 1),
x2 = rnorm(100, 0, 1),
y = round(runif(100, 0, 1), 0)
)
dummy_glm <- lm(y ~ x1 + x2,
data = dummy_data
)
summary(dummy_glm)
B <- as.matrix(dummy_glm$coefficients, ncol = 1)
X <- as.matrix(cbind(rep(1, times = 100), dummy_data[, c('x1', 'x2')]), ncol = 3)
## manually do regression calc
XtX <- t(X) %*% X
det(XtX) # > 0 hence there is inverse
XtX_inv <- solve(XtX)
XtX_inv_diag <- diag(XtX_inv)
# standard errors for each coefficient
st_err_B <- sqrt(XtX_inv_diag)
st_err_B_alternative <- sqrt(diag(vcov(dummy_glm)))
vcov(dummy_glm) == XtX_inv
I found my calculation of XtX_inv does not match vcov(dummy_glm). Could you please hint to a mistake?