Last active
December 19, 2018 19:48
-
-
Save JonasMoss/041eb03089b772c6573a80ec4ddce314 to your computer and use it in GitHub Desktop.
An example of strange R squared values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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