Created
October 18, 2017 13:31
-
-
Save 3inar/4d8b15458b1cec6def5c7874f1890fda to your computer and use it in GitHub Desktop.
2017-10-18 untitled from rstudio
This file contains 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
library(plyr) | |
experiment <- raply(10000, function() { | |
x <- runif(100, -1, 5) | |
y <- 1 + 2* x + rnorm(100,sd = 16) | |
split_x <- split(x, ceiling(seq_along(x)/5)) | |
split_y <- split(y, ceiling(seq_along(y)/5)) | |
beta_1 <- coef(lm(y~x)) | |
beta_2 <- colMeans(aaply(1:length(split_x), 1, function(i) { | |
xx <- split_x[[i]] | |
yy <- split_y[[i]] | |
coef(lm(yy~xx)) | |
})) | |
c(loss_total=sqrt(sum((c(1,2) - beta_1)^2)), | |
loss_split=sqrt(sum((c(1,2) - beta_2)^2))) | |
}, .progress="text") | |
colMeans(experiment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment