## Simulating Small Data Set Bias and Variance
# Jeff Hebert 8/2/2016

cnt <- 26
set.seed(123)
x <- runif(cnt,0,10)
y <- x + rnorm(cnt, mean = 0, sd = 1)
folds <- as.integer(cut(1:cnt, 4))

plot(x,y, col = folds, pch = 19)

model <- lm(y ~ x)
abline(model, lwd = 3)

for(fold in folds){
  idx <- folds == fold
  x_tr <- x[idx]
  y_tr <- y[idx]
  x_te <- x[-idx]
  y_te <- y[-idx]
  
  model <- lm(y_tr ~ x_tr)
  abline(model, col = fold)
}