R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(knitr)

library(readxl)
library(readxl)
uber_data <- read_excel("~/Desktop/L3 - IEG/S1/Analitica de datos /Travail final/uber_data.xlsx")
View(uber_data)

head(uber_data)
## # A tibble: 6 × 11
##   city_id period_start        wait_time treat commute trips_pool trips_express
##   <chr>   <dttm>              <chr>     <lgl> <lgl>        <dbl>         <dbl>
## 1 Boston  2018-02-19 07:00:00 2 mins    FALSE TRUE          1415          3245
## 2 Boston  2018-02-19 09:40:00 5 mins    TRUE  FALSE         1461          2363
## 3 Boston  2018-02-19 12:20:00 2 mins    FALSE FALSE         1362          2184
## 4 Boston  2018-02-19 15:00:00 5 mins    TRUE  TRUE          1984          3584
## 5 Boston  2018-02-19 17:40:00 2 mins    FALSE FALSE         1371          2580
## 6 Boston  2018-02-19 20:20:00 5 mins    TRUE  FALSE         1401          2022
## # ℹ 4 more variables: rider_cancellations <dbl>, total_driver_payout <dbl>,
## #   total_matches <dbl>, total_double_matches <dbl>
summary(uber_data)
##    city_id           period_start                  wait_time        
##  Length:126         Min.   :2018-02-19 07:00:00   Length:126        
##  Class :character   1st Qu.:2018-02-22 18:20:00   Class :character  
##  Mode  :character   Median :2018-02-26 05:40:00   Mode  :character  
##                     Mean   :2018-02-26 05:40:00                     
##                     3rd Qu.:2018-03-01 17:00:00                     
##                     Max.   :2018-03-05 04:20:00                     
##    treat          commute          trips_pool   trips_express 
##  Mode :logical   Mode :logical   Min.   : 771   Min.   :1638  
##  FALSE:63        FALSE:106       1st Qu.:1248   1st Qu.:2225  
##  TRUE :63        TRUE :20        Median :1397   Median :2428  
##                                  Mean   :1408   Mean   :2516  
##                                  3rd Qu.:1580   3rd Qu.:2662  
##                                  Max.   :2241   Max.   :4507  
##  rider_cancellations total_driver_payout total_matches  total_double_matches
##  Min.   : 95.0       Min.   :18770       Min.   :1588   Min.   : 728        
##  1st Qu.:147.8       1st Qu.:24722       1st Qu.:2172   1st Qu.:1060        
##  Median :166.0       Median :27353       Median :2361   Median :1282        
##  Mean   :177.7       Mean   :28237       Mean   :2536   Mean   :1322        
##  3rd Qu.:187.2       3rd Qu.:30586       3rd Qu.:2714   3rd Qu.:1482        
##  Max.   :355.0       Max.   :48600       Max.   :5005   Max.   :2739
str(uber_data)
## tibble [126 × 11] (S3: tbl_df/tbl/data.frame)
##  $ city_id             : chr [1:126] "Boston" "Boston" "Boston" "Boston" ...
##  $ period_start        : POSIXct[1:126], format: "2018-02-19 07:00:00" "2018-02-19 09:40:00" ...
##  $ wait_time           : chr [1:126] "2 mins" "5 mins" "2 mins" "5 mins" ...
##  $ treat               : logi [1:126] FALSE TRUE FALSE TRUE FALSE TRUE ...
##  $ commute             : logi [1:126] TRUE FALSE FALSE TRUE FALSE FALSE ...
##  $ trips_pool          : num [1:126] 1415 1461 1362 1984 1371 ...
##  $ trips_express       : num [1:126] 3245 2363 2184 3584 2580 ...
##  $ rider_cancellations : num [1:126] 256 203 118 355 181 135 103 150 131 284 ...
##  $ total_driver_payout : num [1:126] 34458 29764 27437 44995 27584 ...
##  $ total_matches       : num [1:126] 3372 2288 2283 4035 2200 ...
##  $ total_double_matches: num [1:126] 1476 1275 962 2021 979 ...
groupe_controle <- uber_data %>%
  filter(treat == "control") %>%
  select(trips_pool, trips_express)

groupe_traitement <- uber_data %>%
  filter(treat == "traitement") %>%
  select(trips_pool, trips_express)

###Pregunta 1

resultats <- bind_rows(
  groupe_controle %>% summarise(moyenne_trips = mean(trips_pool + trips_express)),
  groupe_traitement %>% summarise(moyenne_trips = mean(trips_pool + trips_express))
) %>%
mutate(heure_categorie = rep(c("Heure de pointe", "Heure creuse"), each = n() / 2),
       wait_time = rep(c(2, 5), each = n() / 2))

ggplot(resultats, aes(x = heure_categorie, y = moyenne_trips, fill = factor(wait_time))) +
  geom_bar(stat = "identity", position = position_dodge()) +
  labs(title = "Effet de l'allongement des temps d'attente",
       x = "Catégorie d'heures",
       y = "Moyenne des trips") +
  scale_fill_manual(values = c("2" = "blue", "5" = "red")) +
  theme_minimal()
## Warning: Removed 2 rows containing missing values (`geom_bar()`).

###Pregunta suite

uber_data$wait_time <- as.numeric(gsub("[^0-9]", "", uber_data$wait_time))

summary(uber_data$wait_time)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     2.0     2.0     3.5     3.5     5.0     5.0
model <- lm(wait_time ~ trips_pool + trips_express + rider_cancellations + total_driver_payout + total_matches + total_double_matches, data = uber_data)

summary(model)
## 
## Call:
## lm(formula = wait_time ~ trips_pool + trips_express + rider_cancellations + 
##     total_driver_payout + total_matches + total_double_matches, 
##     data = uber_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4674 -0.7560 -0.1047  0.8765  2.3950 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           4.757e+00  8.260e-01   5.759 6.74e-08 ***
## trips_pool            1.237e-03  5.471e-04   2.260 0.025624 *  
## trips_express        -1.385e-03  7.875e-04  -1.759 0.081166 .  
## rider_cancellations   2.237e-02  3.309e-03   6.762 5.38e-10 ***
## total_driver_payout  -1.925e-04  4.048e-05  -4.756 5.59e-06 ***
## total_matches        -7.257e-05  6.191e-04  -0.117 0.906880    
## total_double_matches  1.610e-03  4.466e-04   3.605 0.000457 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.097 on 119 degrees of freedom
## Multiple R-squared:  0.4949, Adjusted R-squared:  0.4694 
## F-statistic: 19.43 on 6 and 119 DF,  p-value: 1.062e-15
ggplot(uber_data, aes(x = wait_time, y = total_matches)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Impact des temps d'attente sur le nombre total de correspondances",
       x = "Temps d'attente",
       y = "Nombre total de correspondances")
## `geom_smooth()` using formula = 'y ~ x'

###También analizaren general el impacto de cambiar los tiempos de espera.El CEO está interesado en saber si el análisis apoya la ampliación de los tiempos de espera a 5 minutos para las horas pico.

uber_data$period_start <- as.POSIXct(uber_data$period_start, format="%Y-%m-%d %H:%M:%S")

uber_data$hour <- hour(uber_data$period_start)

peak_hours <- c(7, 9, 12, 17, 20)
uber_data_peak <- uber_data %>% filter(hour %in% peak_hours)

average_wait_time <- mean(uber_data_peak$wait_time)

cat("Moyenne des temps d'attente pendant les heures de pointe : ", round(average_wait_time, 2), " minutes\n")
## Moyenne des temps d'attente pendant les heures de pointe :  3.5  minutes
if (average_wait_time <= 5) {
  cat("L'extension des temps d'attente à 5 minutes est soutenue.\n")
} else {
  cat("L'extension des temps d'attente à 5 minutes n'est pas recommandée.\n")
}
## L'extension des temps d'attente à 5 minutes est soutenue.

###Pregunta2

uber_data$period_start <- as.POSIXct(uber_data$period_start)

uber_data$hour_category <- ifelse(hour(uber_data$period_start) %in% c(7, 9, 12, 17, 20), "Peak", "Off-Peak")

uber_data <- uber_data %>%
  mutate(profit_per_trip = total_driver_payout / total_matches)

profit_difference <- uber_data %>%
  group_by(hour_category) %>%
  summarise(mean_profit_difference = mean(profit_per_trip))

print(profit_difference)
## # A tibble: 2 × 2
##   hour_category mean_profit_difference
##   <chr>                          <dbl>
## 1 Off-Peak                        11.1
## 2 Peak                            11.5

##Pregunta 3 - Traitement vs contrôle

uber_data$treat <- ifelse(uber_data$treat, "Traitement", "Contrôle")


benefits_analysis <- uber_data %>%
  group_by(treat) %>%
  summarise(mean_benefit = mean(total_driver_payout),
            median_benefit = median(total_driver_payout),
            total_trips = sum(total_matches + total_double_matches))

print(benefits_analysis)
## # A tibble: 2 × 4
##   treat      mean_benefit median_benefit total_trips
##   <chr>             <dbl>          <dbl>       <dbl>
## 1 Contrôle         29291.         28042.      246983
## 2 Traitement       27183.         26018.      239150
ggplot(uber_data, aes(x = treat, y = total_driver_payout)) +
  geom_bar(stat = "summary", fun = "mean", fill = "skyblue", color = "black") +
  labs(title = "Bénéfices par groupe de traitement et de contrôle",
       x = "Groupe",
       y = "Bénéfice moyen") +
  theme_minimal()

Pregunta 4

uber_data$period_start <- as.POSIXct(uber_data$period_start)

uber_data$hour_category <- ifelse(hour(uber_data$period_start) %in% c(7, 9, 12, 17, 20), "Peak", "Off-Peak")

uber_data$group_category <- ifelse(uber_data$treat == 1, "Treatment", "Control")

cancellation_data <- uber_data %>%
  select(group_category, hour_category, rider_cancellations)

cancellation_summary <- cancellation_data %>%
  group_by(group_category, hour_category) %>%
  summarise(total_cancellations = sum(rider_cancellations))
## `summarise()` has grouped output by 'group_category'. You can override using
## the `.groups` argument.
print(cancellation_summary)
## # A tibble: 2 × 3
## # Groups:   group_category [1]
##   group_category hour_category total_cancellations
##   <chr>          <chr>                       <dbl>
## 1 Control        Off-Peak                     9846
## 2 Control        Peak                        12549
ggplot(cancellation_summary, aes(x = hour_category, y = total_cancellations, fill = group_category)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Annulations de trajets par groupe de traitement et heure",
       x = "Heure de la journée",
       y = "Nombre total d'annulations") +
  scale_fill_manual(values = c("Treatment" = "blue", "Control" = "red"))