-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercices.Rmd
216 lines (171 loc) · 6.47 KB
/
exercices.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
---
title: "Exercices"
author: Xavier Raynaud <br/> [email protected]
output:
xaringan::moon_reader:
css: ["SU_xaringan.css"]
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T, fig.width=4, fig.height=4, cache = F)
library(knitr)
library(ggthemes)
library(ggplot2)
library(cowplot)
library(gridExtra)
library(GGally)
library(plotly)
library(ggfortify)
```
class: left, top
**Les camemberts: une bonne idée ? **
```{r camemberts, echo=F, fig.width=12}
plot_grid(
ggplot(data.frame(x=letters[1:3],y=rep(100/3,3)),aes(x="",y=y)) + geom_bar(aes(fill=x),stat="identity")+coord_polar("y")+scale_fill_discrete(guide=F),
ggplot(data.frame(x=letters[1:3],y=c(30,34,36)),aes(x="",y=y)) + geom_bar(aes(fill=x),stat="identity")+coord_polar("y")+scale_fill_discrete(guide=F),
ggplot(data.frame(x=letters[1:3],y=c(36,34,30)),aes(x="",y=y)) + geom_bar(aes(fill=x),stat="identity")+coord_polar("y")+scale_fill_discrete(guide=F),
ggplot(data.frame(x=letters[1:3],y=rep(10/3,3)),aes(x=x,y=y)) + geom_bar(aes(fill=x),stat="identity")+scale_fill_discrete(guide=F),
ggplot(data.frame(x=letters[1:3],y=c(30,34,36)),aes(x=x,y=y)) + geom_bar(aes(fill=x),stat="identity")+scale_fill_discrete(guide=F),
ggplot(data.frame(x=letters[1:3],y=c(36,34,30)),aes(x=x,y=y)) + geom_bar(aes(fill=x),stat="identity")+scale_fill_discrete(guide=F),nrow = 2)
```
---
**Chercher des outliers**
```{r sparrows, fig.width=12,fig.height=7,message=F,warning=F}
read.table("data/SparrowsElphick.txt",h=T)-> sparrows
sparrows = sparrows[sparrows$SpeciesCode==1 & sparrows$Sex!=0,]
sparrows = subset(sparrows,select = -c(SpeciesCode))
sparrows$Sex=as.factor(sparrows$Sex)
ggpairs(sparrows) # pas très informatif mais quelques points semblent sortir du lot...
```
---
**Chercher des outliers**
```{r longsparrows, fig.width=12,fig.height=7}
library(tidyr) # On va transformer le tableau (wide) en tableau long
sparrows$order=1:dim(sparrows)[1]
longsparrows = gather(sparrows,"variable","value",
wingcrd,culmen, flatwing,head,nalospi, tarsus,wt)
ggplot(longsparrows,aes(x=value,y=order)) +
geom_point() + facet_wrap(~variable, scales = "free")
```
---
**Chercher des outliers**
```{r longsparrows2, fig.width=12,fig.height=7}
# On peut identifier les points problématiques avec plotly
ggplotly(
ggplot(longsparrows,aes(x=value,y=order)) +
geom_point() + facet_wrap(~variable, scales = "free"),
width=750,height=400
)
```
---
**Visualiser la distribution de variables**
```{r histograms1,message=F}
ggplot(sparrows,aes(x=culmen)) + geom_histogram()
```
---
**Visualiser la distribution *conditionelle* de variables**
```{r histograms2,message=F,fig.width=8}
ggplot(sparrows,aes(x=culmen)) + geom_histogram() +facet_grid(Sex~1)
```
---
**Visualiser l'hétéroscedasticité**
```{r boxplots}
ggplot(sparrows,aes(x=Sex,y=culmen)) + geom_boxplot()
```
---
**Visualiser l'hétéroscedasticité**
```{r boxplots2,fig.width=8}
ggplot(sparrows,aes(x=Sex,y=culmen)) + geom_boxplot() + facet_grid(~Month)
```
---
**A partir du jeu de données `mtcars` visualiser s'il y a une relation entre la masse des voitures (`wt`), leur consommation (`mpg`) et leur cylindrée (`cyl`).**
---
**A partir du jeu de données `mtcars` visualiser s'il y a une relation entre la masse des voitures (`wt`), leur consommation (`mpg`) et leur cylindrée (`cyl`).**
```{r}
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(colour=as.factor(cyl)))
```
---
**A partir du jeu de données `mtcars` visualiser s'il y a une relation entre la masse des voitures (`wt`), leur consommation (`mpg`) et leur cylindrée (`cyl`).**
```{r}
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(colour=as.factor(cyl)))
```
---
**Ajouter une droite de regression (modèle linéaire) sur ce graphique.**
---
**Ajouter une droite de regression (modèle linéaire) sur ce graphique.**
Deux options:
1. Faire la regression à la main et récupérer les valeurs des coefficients
```{r}
coef(lm(mpg~wt,data=mtcars))
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(colour=as.factor(cyl))) +
geom_abline(intercept=37.285,slope=-5.344)
```
---
**Ajouter une droite de regression (modèle linéaire) sur ce graphique.**
Deux options:
2. Utiliser `geom_smooth`
```{r}
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(colour=as.factor(cyl))) +
geom_smooth(method="lm")
```
---
**Ajouter une droite de regression (modèle linéaire) sur ce graphique.**
Les deux régressions sont identiques
```{r,fig.show='hold'}
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(colour=as.factor(cyl))) +
scale_x_continuous(limits = c(0,6))+ scale_y_continuous(limits=c(0,35)) +
geom_abline(intercept=37.285,slope=-5.344)
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(colour=as.factor(cyl))) +
scale_x_continuous(limits = c(0,6))+ scale_y_continuous(limits=c(0,35)) +
geom_smooth(method="lm")
```
---
**Evaluer la qualité de la regression.**
---
**Evaluer la qualité de la regression.**
```{r, fig.width=12,fig.height=7}
autoplot(lm(mpg~wt,data=mtcars))
```
---
**Appliquer une transformation pour faire la regression**
---
**Appliquer une transformation pour faire la regression**
```{r}
model = lm(mpg~log(wt),data=mtcars)
newx = seq(min(mtcars$wt),max(mtcars$wt),length.out=100)
newdata = as.data.frame(cbind(newx=newx, predict(model,newdata=data.frame(wt=newx),interval="confidence")))
ggplot(newdata,aes(x=newx)) +
geom_ribbon(aes(ymin=lwr,ymax=upr),fill=8)+
geom_line( aes(y=fit)) +
geom_point(data=mtcars,aes(x=wt,y=mpg))
```
---
**Représenter les prédits vs observé**
```{r predvsobs}
ggplot(data.frame(x=mtcars$mpg,y=predict(model)),aes(x=x,y=y)) +
geom_point() +
geom_abline(intercept=0,slope = 1)
```
---
**Représenter les prédits vs observé**
```{r predvsobs2}
ggplot(data.frame(x=mtcars$mpg,y=predict(model)),aes(x=x,y=y)) +
geom_point() + geom_abline(intercept=0,slope = 1)+
geom_text(label=row.names(mtcars),nudge_x = 0.5,hjust="left")
```
---
**Peut-on représenter les données, la régression et les graphs diagnostiques dans un même montage ?**
---
**Peut-on représenter les données, la régression et les graphs diagnostiques dans un même montage ?**
```{r, fig.width=12,fig.height=7}
grid.arrange(
grobs=
c(list(ggplot(data=mtcars,aes(x=wt,y=mpg)) + geom_point() + geom_smooth(method="lm")),
autoplot(lm(mpg~wt,data=mtcars))@plots),
nrow=2,layout_matrix=rbind(c(1,2,3),c(1,4,5))
)
```