---
title: '`glmnet` in R'
author: 'Torben'
output:
  html_document: default
---

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

Load `glmnet` to make the functionalities available.
```{r}
library(glmnetUtils)
library(tidyverse)
```

Ressources on `glmnet`

* The package vignette `vignette("glmnet_beta")` is highly recommendable. 
* Many questions have already been asked _and_ answered at https://stackoverflow.com/questions/tagged/glmnet.

## Example

```{r}
crime <- read_csv("crime.csv", col_types = cols())
```

Note: Due to different scales of the variables, the estimated parameters may be different in size simply due to
different units. Hence, The `glmnet` function automatically standardises both the response 
(for `family = "gaussian"`) `y` and the covariates `x`.

```{r}
crime_lasso <- glmnet(`crime rate` ~ ., alpha = 1, data = crime) ## alpha = 1: LASSO
plot(crime_lasso)
```

```{r}
library(plotmo) # for plot_glmnet # install.packages("plotmo")
plot_glmnet(crime_lasso, xvar = "norm")
```

```{r}
crime_ridge <- glmnet(`crime rate` ~ ., alpha = 0, data = crime) ## alpha = 0: Ridge Regression
plot(crime_ridge)
plot_glmnet(crime_ridge, xvar = "norm")
```

# What should $\lambda$ be?

```{r}
cv_glmnet_lasso <- cv.glmnet(`crime rate` ~ ., alpha = 1, data = crime)
plot(cv_glmnet_lasso)
```


```{r}
cv_glmnet_ridge <- cv.glmnet(`crime rate` ~ ., alpha = 0, data = crime) ## alpha = 0: Ridge
plot(cv_glmnet_ridge)

```

# Elastic net approach: What should $\alpha$ be?

```{r}
cv_glmnet_enet <- cva.glmnet(`crime rate` ~ ., data = crime)
```


```{r}
plot(cv_glmnet_enet)
```

```{r}
minlossplot(cv_glmnet_enet)
```

# Other types of regression

There are many different types of regressions one would be interested in:

* Logistic regression (binary outcomes): `family = "binomial"`
* Count regression (Poisson distribution): `family = "poisson"`
* Multiclass (multinomial data): `family = "multinomial"`
* Survival analysis (Cox proportional hazard model): `family = "cox"`
* Multivariate normal (multivariate Gaussian): `family = "mgaussian"`



