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?
y? – whuber Aug 25 '17 at 16:50vcov.lmrefers tosummary.lmwhich shows the role ofrdf("residual degrees of freedom") in the estimateRmakes. – whuber Aug 25 '17 at 17:02Rwill automatically detect and remove--and (2) counting categorical variables. That's why the variable count is originally computed by the fitting procedure (such aslm) and passed along to other procedures likesummary.lmandvcov.lm. You will notice, too, thatsolveis not invoked: for numeric accuracy,Ruses a QR decomposition performed bylm. – whuber Aug 25 '17 at 17:18