Installer des Packages¶
In [ ]:
# Pour le datascience
library(tidyverse, warn.conflicts = FALSE)
In [4]:
library(readxl)
In [5]:
# Aussi foreign
library(haven, warn.conflicts = FALSE)
In [8]:
library(labelled, warn.conflicts = FALSE)
Tabuler les statistiques¶
In [78]:
# Lire un fichier csv
dat <- read.csv(choose.files())
In [24]:
# Tableaux des fréquence
Freq(dat$size)
| level | freq | perc | cumfreq | cumperc |
|---|---|---|---|---|
| <chr> | <int> | <dbl> | <int> | <dbl> |
| Petit | 6492 | 0.3322926 | 6492 | 0.3322926 |
| Moyen | 6482 | 0.3317807 | 12974 | 0.6640733 |
| Grand | 6563 | 0.3359267 | 19537 | 1.0000000 |
In [25]:
# Tableau des modilités
table(dat$size)
Petit Moyen Grand 6492 6482 6563
In [26]:
# Tableau trié
Margins(table(dat$size), ord = "desc")
A Freq: 3 × 5 level freq perc cumfreq cumperc <chr> <int> <dbl> <int> <dbl> Grand 6563 0.3359267 6563 0.3359267 Petit 6492 0.3322926 13055 0.6682193 Moyen 6482 0.3317807 19537 1.0000000
In [27]:
# Tableau des proportions
prop.table(table(dat$size))
Petit Moyen Grand
0.3322926 0.3317807 0.3359267
In [37]:
# Pourcentage
PercTable(size ~ sexe, data = dat, rfrq = "000", margins = c(1,2))
sexe Homme Femme Sum size Petit 3'261 3'231 6'492 Moyen 3'216 3'266 6'482 Grand 3'292 3'271 6'563 Sum 9'769 9'768 19'537
In [38]:
# Affichage du tableau
tab <- with(dat, table(size, h_pres, sexe))
dimnames(tab) # Les trois dimension
- $size
-
- 'Petit'
- 'Moyen'
- 'Grand'
- $h_pres
-
- 'Petit'
- 'Grand'
- $sexe
-
- 'Homme'
- 'Femme'
In [39]:
# Sélection de deux dimension
margin.table(tab, margin = c(1,3))
sexe size Homme Femme Petit 3261 3231 Moyen 3216 3266 Grand 3292 3271
In [40]:
# Sélection de deux dimension
marginSums(tab, margin = c(1,3))
sexe size Homme Femme Petit 3261 3231 Moyen 3216 3266 Grand 3292 3271
In [41]:
# Sélection de deux dimension
ftable(tab, row.vars = c(2, 3))
size Petit Moyen Grand
h_pres sexe
Petit Homme 1162 1166 1158
Femme 1161 1135 1189
Grand Homme 2099 2050 2134
Femme 2070 2131 2082
In [42]:
# Sélection de deux dimentions
ftable(tab, row.vars = c("size", "h_pres"))
sexe Homme Femme
size h_pres
Petit Petit 1162 1161
Grand 2099 2070
Moyen Petit 1166 1135
Grand 2050 2131
Grand Petit 1158 1189
Grand 2134 2082
In [43]:
# Dans DescTools
Desc(tab, verbose = "high", expected = TRUE, plotit = FALSE)
------------------------------------------------------------------------------
tab (table)
Summary:
n: 19'537, 3-dim table: 3 x 2 x 2
Chi-squared test for independence of all factors:
X-squared = 3.384, df = 7, p-value = 0.8474
sexe Homme Femme Sum
size h_pres
Petit Petit 1162 1161 2323
Grand 2099 2070 4169
Moyen Petit 1166 1135 2301
Grand 2050 2131 4181
Grand Petit 1158 1189 2347
Grand 2134 2082 4216
Sum Petit 3486 3485 6971
Grand 6283 6283 12566
In [44]:
# Permutation des dimensions
aperm(tab, c(2,3,1))
, , size = Petit
sexe
h_pres Homme Femme
Petit 1162 1161
Grand 2099 2070
, , size = Moyen
sexe
h_pres Homme Femme
Petit 1166 1135
Grand 2050 2131
, , size = Grand
sexe
h_pres Homme Femme
Petit 1158 1189
Grand 2134 2082
In [45]:
# Sélections de dimensions
Rev(tab, margin = c(1, 3)) #reversing level
, , sexe = Femme
h_pres
size Petit Grand
Grand 1189 2082
Moyen 1135 2131
Petit 1161 2070
, , sexe = Homme
h_pres
size Petit Grand
Grand 1158 2134
Moyen 1166 2050
Petit 1162 2099
In [46]:
# Zoomer sur la dimension 1
apply(tab, 1, sum)
- Petit
- 6492
- Moyen
- 6482
- Grand
- 6563
In [47]:
# Zoomer sur les dimensions 2 et 3
apply(tab, c(2,3), sum)
| Homme | Femme | |
|---|---|---|
| Petit | 3486 | 3485 |
| Grand | 6283 | 6283 |
In [48]:
# Zoomer sur les dimension size et h_pres
apply(tab, c("size", "h_pres"), sum)
| Petit | Grand | |
|---|---|---|
| Petit | 2323 | 4169 |
| Moyen | 2301 | 4181 |
| Grand | 2347 | 4216 |
In [49]:
# Alterner les dimensions
aperm(tab, c(2,3,1))
, , size = Petit
sexe
h_pres Homme Femme
Petit 1162 1161
Grand 2099 2070
, , size = Moyen
sexe
h_pres Homme Femme
Petit 1166 1135
Grand 2050 2131
, , size = Grand
sexe
h_pres Homme Femme
Petit 1158 1189
Grand 2134 2082
In [50]:
# Mettre en forme
CollapseTable(tab, h_pres = c("Léger", "Lourd "), size = c(1,2,2))
, , sexe = Homme
h_pres
size Léger Lourd
1 1162 2099
2 2324 4184
, , sexe = Femme
h_pres
size Léger Lourd
1 1161 2070
2 2324 4213
In [51]:
# Tableau de fréquence
(mytab <- xtabs(Freq ~ size + h_pres + sexe, data = tab))
, , sexe = Homme
h_pres
size Petit Grand
Petit 1162 2099
Moyen 1166 2050
Grand 1158 2134
, , sexe = Femme
h_pres
size Petit Grand
Petit 1161 2070
Moyen 1135 2131
Grand 1189 2082
In [52]:
# Mise en forme
structable(mytab)
h_pres Petit Grand
size sexe
Petit Homme 1162 2099
Femme 1161 2070
Moyen Homme 1166 2050
Femme 1135 2131
Grand Homme 1158 2134
Femme 1189 2082
In [53]:
# Tableau marginale sur deux dimensions
margin.table(mytab, c(1,2))
h_pres size Petit Grand Petit 2323 4169 Moyen 2301 4181 Grand 2347 4216
In [58]:
# Proportions marginale sur deux dimensions
(ptab <- prop.table(mytab, c(2,3)))
, , sexe = Homme
h_pres
size Petit Grand
Petit 0.33 0.33
Moyen 0.33 0.33
Grand 0.33 0.34
, , sexe = Femme
h_pres
size Petit Grand
Petit 0.33 0.33
Moyen 0.33 0.34
Grand 0.34 0.33
In [59]:
# Tableau sur trois dimensions
cb <- array(tab, dim = c(3,3,2))
Abind(cb[,,1], cb[,,2], along = 3)
- 1162
- 1166
- 1158
- 2099
- 2050
- 2134
- 1161
- 1135
- 1189
- 2070
- 2131
- 2082
- 1162
- 1166
- 1158
- 2099
- 2050
- 2134
In [60]:
# Tableaux de fréquences
aggregate(cbind(day, wind)~size, data = dat, FUN = sum)
| size | day | wind |
|---|---|---|
| <fct> | <dbl> | <dbl> |
| Petit | 1e+05 | 325295 |
| Moyen | 1e+05 | 324780 |
| Grand | 1e+05 | 327740 |
In [61]:
# Mise en forme
tab[] <- Format(tab, big.mark = "'", digits = 0, zero.form = "-",
na.form = "Missing")
tab[] <- StrAlign(tab, "\\r")
tab
, , sexe = Homme
h_pres
size Petit Grand
Petit 1'162 2'099
Moyen 1'166 2'050
Grand 1'158 2'134
, , sexe = Femme
h_pres
size Petit Grand
Petit 1'161 2'070
Moyen 1'135 2'131
Grand 1'189 2'082
In [62]:
# Mise en forme
ptab[] <- Format(ptab, digits = 1, fmt = "%")
ptab
, , sexe = Homme
h_pres
size Petit Grand
Petit 33.3% 33.4%
Moyen 33.4% 32.6%
Grand 33.2% 34.0%
, , sexe = Femme
h_pres
size Petit Grand
Petit 33.3% 32.9%
Moyen 32.6% 33.9%
Grand 34.1% 33.1%
Graphics dans base R¶
In [69]:
# Diagramme en bars
barplot(table(dat$status)[order(table(dat$status))], col = "khaki", las = 2)
In [70]:
# Bars groupés
with(dat, barplot(table(sexe, status), col = c("khaki","green"), beside = TRUE, legend = TRUE))
In [71]:
with(dat, boxplot(wind~status))
In [72]:
plot(wind~day, data = dat)
In [73]:
hist(dat$wind, main = "L'histogramme")
In [74]:
# Diagramme circulaire
pie(table(dat$status), col = c("hotpink","khaki","chartreuse"))