---
title: "GAM"
author: "Mathieu RIBATET"
format: html
editor: visual
---

## 📝 First step with `mgcv`

Although, other approaches are possible, the `mgcv` package (already installed with R) is probably the best option ever! In this exercise we will learn the basics of this library. Here is a typical use:

```{r}
library(mgcv)##load the library
data(trees)

fit <- gam(Volume ~ s(Girth), family = Gamma(link = "log"), data = trees)
```

3.  Give the mathematical expression for the above model. Do we use a canonical link?

4.  Run the following piece of code and comment. (You may want to have a look at the help page of `plot.gam`).

    ```{r}
    plot(fit, resid = TRUE)
    ```

5.  Read the documentation of the `gam.check` function and run it on the above fitted model and comment.

    ```{r}
    ## Insert code here
    ```

    **Write comment here.**

6.  Read the documentation of the *`s`* function. What type of spline were we using? Refit the above model using cubic regression splines and compare the two models.

    ```{r}
    ## Insert code here
    ```

7.  Fit another model which makes use of the two available covariates, i.e., `Height` and `Girth`.

    ```{r}
    ## Insert code here
    ```

8.  What about a model with a bivariate smoother?

    ```{r}
    ## Insert code here
    ```

9.  Among all the models considered so far, which one is preferable to use?

    ```{r}
    ## Insert code here
    ```

    **Write comments here.**

## 📝 Availability of bike sharing system

With this exercise we will work on a data set available from data.gouv.fr which collects the number of bike rides per day in Nantes (using Naolib). In addition to the total number of rides, we also have access to the number of rides for short-term and long-term subscriptions.

```{r}
url <- "https://mribatet.perso.math.cnrs.fr/CentraleNantes/Data/trajets-biclooplus-nantes-metropole.csv"
data <- read.csv2(url)
```

1.  The column `Date` is currently stored as a character string, learn how to use the `as.Date` function to convert it as a `Date`.

    ```{r}
    ## Write code here
    ```

2.  It's time to do some feature engineering. Clearly, the day of the week, the month are sensible "new" covariates. Create them (and any other fantastic idea you may have).

    ```{r}
    ## Write code here
    ```

3.  Using the day of the week and month variable, fit a (sensible) GLM to predict the total number of rides.

    ```{r}
    ## Write code here
    ```

4.  Interpret results.

    ```{r}
    ## Write code
    ```

    **Comment here.**

5.  Extend your model using a GAM whose linear predictor is $$\eta(t) = \beta_0 + \sum_{j \in \{\text{tuesday}, \ldots, \text{sunday}\}} \beta_{1,j} 1_{\{\mbox{day of week}(t) = j\}}+ \sum_{j \in \{\text{Feb}, \ldots, \text{Dec}\}} 1_{\{\mbox{month}(t) = j\}} + f(\mbox{day of year}(t))$$

    By the way, why in the above equation monday and Januaray do not appear? *Hint: The `lubridate` package may be helpful.*

    ```{r}
    ## Write code here
    ```

    **Give comment here.**

6.  Perform a model checking stage and comment.

    ```{r}
    ## Write code here
    ```

    **Give comment here.**

7.  Plot the fitted spline. Does it make sense?

    ```{r}
    ## Write code here
    ```

    **Give comment here.**

8.  Read the documentation of the `s` function and learn how to use a cyclic spline. Next improve your model and plot the fitted spline.

    ```{r}
    ## Write code here
    ```

9.  How would you do to assess if people are increasingly using the bike sharing system over time?

    ```{r}
    ## Write code here
    ```

    **Give comments here.**

10. If you have time, think about comparing the behaviour for short term and long term subscription.

    ```{r}
    ## Write code here
    ```
