Skip to content

Instantly share code, notes, and snippets.

@JonasMoss
Last active Dec 19, 2018
Embed
What would you like to do?
An example of strange R squared values.
# Create a covariance matrix for the covariates.
rho12 = -0.1
rho13 = 0.65
rho23 = -0.3
covariance = matrix(c(1, rho12, rho13,
rho12, 1, rho23,
rho13, rho23, 1), nrow = 3)
# Simulate a linear regression with all betas equal to 1.
set.seed(313)
n = 100000
x = mvtnorm::rmvnorm(n, sigma = covariance) # install.packages("mvtnorm")
y = x[, 1] + x[, 2] + x[, 3] + rnorm(n)
# Check out some R squared values. It's startling that the R^2 for x2 marginally
# is small, but the increment in the rsq when x1 is known is large.
summary(lm(y ~ x[, 1] + x[, 2] + x[, 3]))$r.sq # [1] 0.7785883
summary(lm(y ~ x[, 1] + x[, 2]))$r.sq # [1] 0.663347
summary(lm(y ~ x[, 1] + x[, 3]))$r.sq # [1] 0.581559
summary(lm(y ~ x[, 1]))$r.sq # [1] 0.536874
summary(lm(y ~ x[, 2]))$r.sq # [1] 0.08001411
summary(lm(y ~ x[, 3]))$r.sq # [1] 0.406035
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment