---
title: "TP 3 correction"
output: html_notebook
---

```{r}
library(tidyverse)
```

### Chargement des données


```{r}
diet = read_csv("./diet.csv")
```
On vérifie le formats des variables
```{r}
glimpse(diet)
summary(diet)
diet = diet %>% mutate(gender = factor(gender)) %>% mutate(Diet = factor(Diet)) %>% arrange(Person) # on réordonne suivant l'identifiant

glimpse(diet)
```
Il y a 2 valeurs manquantes dans `gender`
```{r}
summary(diet$gender)
ggplot(diet,aes(x=Height,color=gender)) + geom_density()
```
On impute les 2 valeurs manquantes
```{r}
diet$gender[diet$Person==25] = 0
diet$gender[diet$Person==26] = 1
```
On définit le label
```{r}
diet = diet %>% mutate(weight_loss = -pre.weight+weight6weeks)
```
Visualisation des dépendances entre le label et `diet` et `gender`
```{r}
#boxplot(weight_loss ~ Diet + gender, data=diet)
ggplot(diet, aes(x=Diet, y=weight_loss)) +
  geom_boxplot()
ggplot(diet, aes(x=gender, y=weight_loss)) +
  geom_boxplot()
ggplot(diet, aes(x=Diet, y=weight_loss, fill=gender)) +
  geom_boxplot()
```
de même, numériquement
```{r}
diet %>% group_by(Diet) %>% summarise(mean(weight_loss))
diet %>% group_by(gender) %>% summarise(mean(weight_loss))
diet %>% group_by(gender,Diet) %>% summarise(mean(weight_loss))
```


```{r}
diet %>% group_by(Diet) %>% summarise(avg = mean(weight_loss))
```
On fait un premier modèle suivant `diet`
```{r}
fit_diet_v1 = lm(weight_loss ~ Diet, data = diet)
summary(fit_diet_v1)
model.matrix(fit_diet_v1)
```
La même chose sans intercept.
```{r}
fit_diet_v2 = lm(weight_loss ~ Diet - 1, data = diet)
summary(fit_diet_v2)
model.matrix(fit_diet_v2)
```
ON vérifie l'hypothèse de normalité des erreurs et l'absence d'individus aberrants
```{r}
plot(fit_diet_v2)
anova(fit_diet_v2)
```


On crée une variable `Diet2` puis on estime dans les modèles avec cette nouvelle variable (version avec et sans intercept)
```{r}
diet = diet %>% mutate(Diet2 = (Diet != 3))
fit_diet_v2 = lm(weight_loss ~ Diet2, data = diet)
summary(fit_diet_v2)

fit_diet_v2bis = lm(weight_loss ~ Diet2 - 1, data = diet)
summary(fit_diet_v2bis)
```
On vérifie que la perte de poids dépend de `Diet2`
```{r}
anova(fit_diet_v2bis)
```
On regarde maintenant la dépendance entre la perte de poids et `gender`
```{r}
fit_diet_v3 = lm(weight_loss ~ gender -1, data = diet)
summary(fit_diet_v3)
```
On essaye dans un modèle d'anova à 2 facteurs avec effet croisé
```{r}
fit_diet_v4 = lm(weight_loss ~ Diet2 * gender-1 , data = diet)
summary(fit_diet_v4)
```
Une autre façon de faire la même chose : estimer dans 2 modèles l'un pour les hommes, l'autre pour les femmes. Attention ici, comme on travaille sur des sous-données, certaines estimations seront moins précises que dans le modèle avec effets croisés.
```{r}
diet_gender1 = diet %>% filter(gender == "1")
summary(lm(weight_loss ~Diet2-1,diet_gender1))
```
```{r}
diet_gender0 = diet %>% filter(gender == "0")
summary(lm(weight_loss ~Diet2-1,diet_gender0))
```


```{r}

Knee_pain = read.table("./Knee_pain.txt",header = TRUE)

glimpse(Knee_pain)

Knee_pain = mutate(Knee_pain ,TreatmentType = factor(TreatmentType))
Knee_pain = mutate(Knee_pain, TreatmentType = mapvalues(TreatmentType,c("1","2"), c("Cold","Hot")))
glimpse(Knee_pain)
attach(Knee_pain)

```
```{r}
sum(Knee_pain$NumberOfRespondersToTreatment)
```

### Premiers graphiques
```{r}
ggplot(Knee_pain, aes(TreatmentType,NumberOfRespondersToTreatment)) + geom_boxplot(aes(fill = TreatmentType))
ggplot(Knee_pain, aes(DiagnosisType,NumberOfRespondersToTreatment)) + geom_boxplot(aes(fill = DiagnosisType))
```


```{r}
ggplot(Knee_pain,aes(NumberOfRespondersToTreatment)) + geom_histogram()

```
 

```{r}
plot(cars)
```

Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).
