---
title: "Overfitting with linear regression"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


We'll reproduce the example given on page 11 of _Statistical Learning with Sparsity: the Lasso and Generalizations_ by Hastie, Tibshirani, and Wainwright. (Code by Seth Flaxman).

```{r, fig.width=10 }
data = read.csv("crime.txt",sep="\t")
names(data) = c("overall","violent","funding","hs","not-hs","college","college4")
library(glmnet)
library(plotmo) 
data$overall = log10(data$overall)
par(mfrow=c(1,2))
fit.lasso = glmnet(data.matrix(data[,3:7]),data$overall)
plot.glmnet(fit.lasso,xvar="lambda")
fit.l2 = glmnet(data.matrix(data[,3:7]),data$overall,alpha=0)
plot.glmnet(fit.l2,xvar="lambda")

fit.lasso = cv.glmnet(data.matrix(data[,3:7]),data$overall)
plot(fit.lasso)
fit.l2 = cv.glmnet(data.matrix(data[,3:7]),data$overall,alpha=0)
plot(fit.l2)

```
