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

## Let's first take a look at today's data.
```{r}
library(glmnet)
library(MASS)
data(Boston)
names(Boston)
?Boston
```


## Why don't we try a super tiny lambda? What is the difference between OLS and Lasso?
```{r} 
#——————————————-
# Model
#——————————————-
    lambda <- 0.01
    
    # standard linear regression without intercept(-1)
    li.eq <- lm(medv~., Boston) 
    
    # lasso
    la.eq <- glmnet(Boston[,-14], Boston[,14], lambda=lambda, 
                    family="gaussian", 
                    intercept = F, alpha=1) 
    
```

## Can you interpret the difference between OLS and Lasso?
```{r}
print(cbind(li.eq$coefficients[-1],
la.eq$beta))
```




```{r} 
#——————————————-
# Results (lambda=0.01)
#——————————————-
    df.comp <- data.frame(
       
        Linear  = li.eq$coefficients,
        Lasso   = c(0,la.eq$beta[,1])
       
    )
    df.comp
```

## Let's try lambda as 0.1 
```{r}
#——————————————-
# Results (lambda=0.1)
#——————————————-
    lambda <- 0.1
    
    # lasso
    la.eq <- glmnet(Boston[,-14], Boston[,14], lambda=lambda, 
                    family="gaussian", 
                    intercept = F, alpha=1) 
    
    df.comp <- data.frame(
       
        Linear  = li.eq$coefficients,
        Lasso   = c(0,la.eq$beta[,1])
      #  Ridge   = ri.eq$beta[,1]
    )
    df.comp

```

## We can also run Lasso with lots of Lambda values. The plot tells us how shrinkage works for all variables in our data.
```{r}    
#————————————————
# Shrinkage of coefficients 
# (rangle lambda input or without lambda input)
#————————————————
    
    # lasso
    la.eq <- glmnet(Boston[,-14], Boston[,14], family="gaussian", 
                    intercept = F, alpha=1) 
   
    # plot
plot(x=log(la.eq$lambda),y =t(la.eq$beta)[,1],type="l",lty=1,col=1,
     ylim=c(min(la.eq$beta),max(la.eq$beta)),
     xlab="log of lambda",ylab="coefficient")
for(i in 2:ncol(t(la.eq$beta))){
   lines(x=log(la.eq$lambda),y =t(la.eq$beta)[,i] ,type = "l",lty=i,
         col=i)
}

 
   
```
## Let's try selecting the optimal lambda

```{r}
 
mod_cv <- cv.glmnet(x=as.matrix(Boston[,-14]), y=Boston[,14], family="gaussian",
                        intercept = F, alpha=1)
plot(mod_cv)

```


## Let's take a look at the optimal lambda value.
```{r}
 print(paste(mod_cv$lambda.min,
                log(mod_cv$lambda.min)))
```
 
 