Skip to content

Instantly share code, notes, and snippets.

@3inar
Created November 2, 2017 17:12
Show Gist options
  • Save 3inar/2cb1615f2ef88a11f1d51a87e23edc76 to your computer and use it in GitHub Desktop.
Save 3inar/2cb1615f2ef88a11f1d51a87e23edc76 to your computer and use it in GitHub Desktop.
2017-11-02 untitled from rstudio
library(plyr)
library(MASS)
test <- raply(50000, {
dat <- mvrnorm(200, mu = c(0,1), Sigma = matrix(c(1,.3,.3,1), nrow=2))
y <- dat[,1]
x <- dat[,2]
# throw away most of the data, introduce huge bias
y <- y[x > 1]
x <- x[x > 1]
lm_fit <- lm(y~x)
mu <- mean(y)
lmu <- coef(lm_fit)[1] + coef(lm_fit)[2]
c(mu, lmu)
}, .progress="text")
mus <- test[,1]
lmus <- test[,2]
# There is really a striking improvement by using the correlation
mean((mus)^2)
mean((lmus)^2)
# corrected model not too bad on average
colMeans(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment