Formation R

Analyse descriptive de données

Workflow d’un projet Data Science

EDA

Comprendre son dataset et ses caractéristiques

  • décrire les variables
  • visualiser les variables
  • analyser leurs relations

Calcul de statistiques descriptives

summary()

library(readr)

athome <- read_csv( 'data/athome.csv') 

summary(athome)

##        id              img                link              source         
##  Min.   : 199285   Length:144677      Length:144677      Length:144677     
##  1st Qu.:6532352   Class :character   Class :character   Class :character  
##  Median :6682461   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :6621094                                                           
##  3rd Qu.:6733616                                                           
##  Max.   :6788549                                                           
##                                                                            
##     title              terrain        type_annonce        type_bien        
##  Length:144677      Min.   :   0.01   Length:144677      Length:144677     
##  Class :character   1st Qu.:   1.81   Class :character   Class :character  
##  Mode  :character   Median :   2.02   Mode  :character   Mode  :character  
##                     Mean   :   2.52                                        
##                     3rd Qu.:   2.02                                        
##                     Max.   :4000.00                                        
##                     NA's   :75944                                          
##  type_bien_dtl          pieces       annee_de_construction   quartier        
##  Length:144677      Min.   : 1.00    Min.   :       6      Length:144677     
##  Class :character   1st Qu.: 3.00    1st Qu.:    2010      Class :character  
##  Mode  :character   Median : 5.00    Median :    2014      Mode  :character  
##                     Mean   : 5.25    Mean   :    2175                        
##                     3rd Qu.: 7.00    3rd Qu.:    2019                        
##                     Max.   :25.00    Max.   :20202021                        
##                     NA's   :143713   NA's   :8949                            
##     ville               etage             ascenseur      salles_de_bains 
##  Length:144677      Min.   :1.000e+00   Min.   :0.0000   Min.   : 1.00   
##  Class :character   1st Qu.:1.000e+00   1st Qu.:0.0000   1st Qu.: 1.00   
##  Mode  :character   Median :2.000e+00   Median :0.0000   Median : 1.00   
##                     Mean   :3.711e+05   Mean   :0.4866   Mean   : 1.08   
##                     3rd Qu.:3.000e+00   3rd Qu.:1.0000   3rd Qu.: 1.00   
##                     Max.   :2.008e+09   Max.   :1.0000   Max.   :14.00   
##                     NA's   :139266                       NA's   :104315  
##  salles_de_douches   wc_separes        terrasse          balcon       
##  Min.   : 1.00     Min.   :0.0000   Min.   :0.0000   Min.   :0.00000  
##  1st Qu.: 1.00     1st Qu.:0.0000   1st Qu.:1.0000   1st Qu.:0.00000  
##  Median : 1.00     Median :0.0000   Median :1.0000   Median :0.00000  
##  Mean   : 1.01     Mean   :0.4946   Mean   :0.9457   Mean   :0.01895  
##  3rd Qu.: 1.00     3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:0.00000  
##  Max.   :30.00     Max.   :1.0000   Max.   :1.0000   Max.   :1.00000  
##  NA's   :107583                                                       
##      jardin            cave          buanderie         grenier 
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :1  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:1  
##  Median :0.0000   Median :0.0000   Median :0.0000   Median :1  
##  Mean   :0.4779   Mean   :0.4961   Mean   :0.4935   Mean   :1  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1  
##                                                                
##     cheminee        cuisine_ouverte  cuisine_equipee   panneaux_solaires
##  Min.   :0.000000   Min.   :0.0000   Min.   :0.00000   Min.   :0.0000   
##  1st Qu.:0.000000   1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.0000   
##  Median :0.000000   Median :0.0000   Median :0.00000   Median :0.0000   
##  Mean   :0.003553   Mean   :0.2436   Mean   :0.02339   Mean   :0.2358   
##  3rd Qu.:0.000000   3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.0000   
##  Max.   :1.000000   Max.   :1.0000   Max.   :1.00000   Max.   :1.0000   
##                                                                         
##  acces_mobilite_reduite charges_mensuelles    caution           meuble      
##  Min.   :0.000000       Min.   :     1.0   Min.   :     1   Min.   :0.0000  
##  1st Qu.:0.000000       1st Qu.:   200.0   1st Qu.:  2900   1st Qu.:0.0000  
##  Median :0.000000       Median :   200.0   Median :  2900   Median :0.0000  
##  Mean   :0.003325       Mean   :   208.3   Mean   :  2964   Mean   :0.2436  
##  3rd Qu.:0.000000       3rd Qu.:   200.0   3rd Qu.:  2900   3rd Qu.:0.0000  
##  Max.   :1.000000       Max.   :100000.0   Max.   :150000   Max.   :1.0000  
##                         NA's   :75730      NA's   :76808                    
##  chauffage_gaz    chauffage_electrique chauffage_geothermique
##  Min.   :0.0000   Min.   :0.000000     Min.   :0.0000000     
##  1st Qu.:0.0000   1st Qu.:0.000000     1st Qu.:0.0000000     
##  Median :0.0000   Median :0.000000     Median :0.0000000     
##  Mean   :0.0344   Mean   :0.000235     Mean   :0.0005046     
##  3rd Qu.:0.0000   3rd Qu.:0.000000     3rd Qu.:0.0000000     
##  Max.   :1.0000   Max.   :1.000000     Max.   :1.0000000     
##                                                              
##  chauffage_mazout   montant_agence      piscine          place_parking   
##  Min.   :0.000000   Min.   :    0    Min.   :0.0000000   Min.   :0.0000  
##  1st Qu.:0.000000   1st Qu.: 1170    1st Qu.:0.0000000   1st Qu.:1.0000  
##  Median :0.000000   Median : 1755    Median :0.0000000   Median :1.0000  
##  Mean   :0.002737   Mean   : 2206    Mean   :0.0005322   Mean   :0.9573  
##  3rd Qu.:0.000000   3rd Qu.: 2500    3rd Qu.:0.0000000   3rd Qu.:1.0000  
##  Max.   :1.000000   Max.   :20475    Max.   :1.0000000   Max.   :1.0000  
##                     NA's   :143614                                       
##  parking_interieur  parking_exterieur     chambres         surface        
##  Min.   :0.00e+00   Min.   :0.000000   Min.   : 0.000   Min.   :     9.0  
##  1st Qu.:0.00e+00   1st Qu.:0.000000   1st Qu.: 2.000   1st Qu.:    98.0  
##  Median :0.00e+00   Median :0.000000   Median : 2.000   Median :   105.0  
##  Mean   :8.99e-05   Mean   :0.002426   Mean   : 2.504   Mean   :   147.3  
##  3rd Qu.:0.00e+00   3rd Qu.:0.000000   3rd Qu.: 3.000   3rd Qu.:   190.0  
##  Max.   :1.00e+00   Max.   :1.000000   Max.   :18.000   Max.   :566818.0  
##                                        NA's   :4157     NA's   :3810      
##       prix         
##  Min.   :       1  
##  1st Qu.:  700000  
##  Median :  700000  
##  Mean   :  674517  
##  3rd Qu.:  700000  
##  Max.   :16700000  
##  NA's   :103049
  • summary() ne permet pas la création d’une table

Dataset de statistiques descriptives détaillées

  • utiliser summarise() (manuel)
  • utiliser skim() (automatique)

Dataset de statistiques descriptives détaillées

  • utiliser summarise() (manuel)
  • utiliser skim() (automatique)

summarise()

library(dplyr)

 athome %>% 
  filter(ville=='Luxembourg') %>% 
  group_by(type_annonce) %>% 
  summarise(max_prix=max(prix, na.rm = TRUE), min_prix=min(prix, na.rm = TRUE), mean_prix=mean(prix, na.rm = TRUE), max_surface=max(surface, na.rm = TRUE), min_surface=min(surface, na.rm = TRUE), mean_surface=mean(surface, na.rm = TRUE)) 
type_annonce max_prix min_prix mean_prix max_surface min_surface mean_surface
location 125048 1 3057.511 10000 9 90.37613
vente 16700000 1 1241678.888 800 12 103.53956

skim()

library(skimr)
library(dplyr)

 athome %>% 
  filter(ville=='Luxembourg') %>% 
  select(type_annonce, prix, charges_mensuelles, annee_de_construction, pieces, surface) %>%  
  group_by(type_annonce) %>% 
  skim() #%>%
  # yank("numeric") possibilité de sortir uniquement les numeriques ou charactere

Variable type: numeric

skim_variable type_annonce n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
prix location 969 0.67 3057.51 6047.28 1 850.00 1650.0 2787.00 125048 ▇▁▁▁▁
prix vente 653 0.63 1241678.89 1019439.87 1 738116.00 1050000.0 1386428.75 16700000 ▇▁▁▁▁
charges_mensuelles location 1380 0.53 258.46 283.55 1 150.00 200.0 275.00 4253 ▇▁▁▁▁
charges_mensuelles vente 1667 0.06 348.12 531.37 30 197.50 250.0 315.00 4750 ▇▁▁▁▁
annee_de_construction location 2157 0.26 1999.80 27.22 1750 1988.00 2013.0 2018.00 2020 ▁▁▁▁▇
annee_de_construction vente 1287 0.28 1993.74 129.12 12 1988.75 2013.5 2020.00 2107 ▁▁▁▁▇
pieces location 2694 0.07 4.10 2.85 1 2.00 3.0 5.00 16 ▇▂▁▁▁
pieces vente 1644 0.08 4.78 2.87 1 3.00 4.0 6.00 15 ▇▇▃▁▁
surface location 1326 0.54 90.38 261.42 9 45.00 70.0 97.00 10000 ▇▁▁▁▁
surface vente 149 0.92 103.54 63.78 12 62.53 86.0 122.48 800 ▇▁▁▁▁

Tableau de contingence

 select_athome <- athome %>% 
  filter(ville=='Luxembourg' & quartier %in% c('Beggen', 'Eich', 'Belair', 'Merl', 'Kirchberg')) %>% 
  select(type_annonce, quartier) 

table(select_athome) %>% 
    as.data.frame.matrix

Visualiser les prix des locations par quartier

Package forcats

Avec forcats, il est possible de manipuler les variables ordinales

 select_athome2 <- select_athome %>% 
  mutate(quartier = factor(quartier, levels=c('Belair', 'Merl', 'Kirchberg','Beggen', 'Eich') ))
           
# inverser les "levels" 
 select_athome3 <- select_athome2 %>% 
  mutate(quartier = fct_rev(quartier) )

# associer l'ordre à une autre variable
 select_athome4 <-  select_athome %>% 
  mutate(ord = case_when(quartier =='Belair' ~ 1, 
                         quartier =='Merl' ~ 5, 
                         quartier =='Kirchberg' ~ 2,
                         quartier =='Beggen' ~4, 
                         quartier =='Eich' ~ 3),
         quartier = fct_reorder(quartier, ord))
pop <- pop %>% 
         mutate(county=fct_rev(county))

Code associé

library(ggridges)
library(ggplot2)
library(hrbrthemes)
library(viridis)
library(scales)

 select_athome <- athome %>% 
  filter(type_annonce=='location' & quartier %in% c('Beggen', 'Eich', 'Belair', 'Merl', 'Kirchberg')) %>% 
  select(prix, quartier) 

ggplot(select_athome, aes(x = prix, y = quartier, fill = quartier))+
  geom_density_ridges(alpha=0.6, stat="binline", bins=20) +
  scale_fill_viridis(discrete=TRUE) +
  scale_color_viridis(discrete=TRUE) +
  theme_ipsum() +
  theme(
    legend.position="none",
    panel.spacing = unit(0.1, "lines"),
    strip.text.x = element_text(size = 8)
  ) + 
  scale_x_continuous(labels = comma) +
  xlab("Prix des locations") +
  ylab(" ")

1.4.1 Instructions

  • 1) Allez dans acc2019
  • 2) Tableau descriptif des variables intéressantes
  • 3) Tableau de contengence avec espece et domcor
  • 4) Visualiser sous forme d’histogramme les accidents par rapport à l’heure et à la vitesse associée à la zone

Pondération dans votre analyse

{Hmisc} facilite la pondération :

library(Hmisc)

x  # la variable d'intérêt
weights # le poids associé

wtd.quantile(x, weights)  # weighted quantile
wtd.mean(x, weights)      # weighted mean
wtd.var(x, weights)       # weighted variance

Contact

Kevin Rosamont Prombo