Skip to content

Instantly share code, notes, and snippets.

@vankesteren
Last active March 11, 2021 08:28
Show Gist options
  • Select an option

  • Save vankesteren/945f476216cd4cc2b91f8ac2415fe88f to your computer and use it in GitHub Desktop.

Select an option

Save vankesteren/945f476216cd4cc2b91f8ac2415fe88f to your computer and use it in GitHub Desktop.
All the interesting regression quantities
# Randomly generate some data
S <- rWishart(1, 10, diag(10))[,,1] / 10
X <- MASS::mvrnorm(100, rep(0, 10), S)
b <- runif(10, -1, 1)
y <- X %*% b + rnorm(100, sd = sqrt(b %*% S %*% b))
# all the interesting regression quantities!
n <- nrow(X) # sample size
p <- ncol(X) # parameters in model matrix
xtxi <- solve(crossprod(X)) # inverse covariance matrix
xty <- crossprod(X, y) # covariance of X with y
bhat <- xtxi %*% xty # beta estimates
yres <- y - X %*% bhat # residuals
rss <- crossprod(yres) # residual sum of squares
rdf <- n - p # degrees of freedom
acov <- as.numeric(rss / rdf) * xtxi # asymptotic covmat of beta
bse <- sqrt(diag(acov)) # standard error of beta
tb <- bhat/bse # t scores of beta
pval <- 2 * (1 - pt(tb, df = rdf)) # p values of beta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment