Skip to content

Instantly share code, notes, and snippets.

@3inar
Created October 18, 2017 13:31
Show Gist options
  • Save 3inar/4d8b15458b1cec6def5c7874f1890fda to your computer and use it in GitHub Desktop.
Save 3inar/4d8b15458b1cec6def5c7874f1890fda to your computer and use it in GitHub Desktop.
2017-10-18 untitled from rstudio
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