Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
giocoal committed Jan 5, 2023
2 parents 3f0fb5b + 4340915 commit 1e76ae7
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 35 deletions.
120 changes: 85 additions & 35 deletions Analisi predittive/Previsioni_RandomForest_PreCovid.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -155,56 +155,106 @@ f_mod2 <- function(Y, h, newxreg = NULL) {
## Previsioni

```{r}
# Parametri globali
h = 73
initial = 538
window = NULL
# # Parametri globali
# h = 73
# initial = 538
# window = NULL
#
# # Calcolo tempo di computazione
# start.time <- Sys.time()
# print(start.time)
#
# # Pre processing
#
# rf_r1 <- xts(r1[, c("lordototale", "Festivo", "Pioggia")], as.Date(as.character(r1$data), format = "%Y-%m-%d"))
#
# rf_r1$Festivo[rf_r1$Festivo=="False"] <- 0
# rf_r1$Festivo[rf_r1$Festivo=="True"] <- 1
# rf_r1$Pioggia[rf_r1$Pioggia==""] <- 0
# rf_r1$Pioggia[rf_r1$Pioggia=="True"] <- 1
# yday <- yday(time(rf_r1))
# wday <- wday(time(rf_r1), week_start = getOption("lubridate.week.start", 1))
# data <- cbind(rf_r1, yday, wday)
#
# # CV su MODELLO 2
#
# e_2 <- tsCV_RandomForest(my_xts = data[,1], xreg = data[,-1], forecastfunction = f_mod2, h=h, initial = initial, window = window)
# e2 <- e_2$e
# e2_percentage <- e_2$e_percentage
# e2_estimate <- e_2$y_estimate
# e2_groundtruth <- e_2$y_groundtruth
#
# end.time <- Sys.time()
# print(end.time)
# time.taken <- end.time - start.time
# print(time.taken)
```

# Calcolo tempo di computazione
start.time <- Sys.time()
print(start.time)
```{r}
# covid_predictions <- e2_estimate[539] # Prima riga non nulla di e2_estimate
# covid_predictions <- as.data.frame(t(covid_predictions))
#
# prediction_dates <- seq(as.Date("2020-02-24"), as.Date("2020-05-06"), by = 1)
#
# covid_predictions <- cbind(covid_predictions, prediction_dates)
#
# rownames(covid_predictions) <- NULL
# colnames(covid_predictions) <- c("Previsioni", "Date")
```

# Pre processing
```{r}
# previsioni <- xts(covid_predictions[, c("Previsioni")], as.Date(as.character(covid_predictions$Date), format = "%Y-%m-%d"))
```

rf_r1 <- xts(r1[, c("lordototale", "Festivo", "Pioggia")], as.Date(as.character(r1$data), format = "%Y-%m-%d"))
```{r}
# previsioni <- cbind(previsioni, rf_r1$lordototale[prediction_dates])
# previsioni$perdite <- previsioni$previsioni - previsioni$lordototale
```

rf_r1$Festivo[rf_r1$Festivo=="False"] <- 0
rf_r1$Festivo[rf_r1$Festivo=="True"] <- 1
rf_r1$Pioggia[rf_r1$Pioggia==""] <- 0
rf_r1$Pioggia[rf_r1$Pioggia=="True"] <- 1
yday <- yday(time(rf_r1))
wday <- wday(time(rf_r1), week_start = getOption("lubridate.week.start", 1))
data <- cbind(rf_r1, yday, wday)
```{r}
# write.csv(previsioni, "../Dati ristoranti/previsioni_RF.csv")
```

# CV su MODELLO 2
```{r}
previsioni <- read.csv("..\\Dati ristoranti\\previsioni_RF.csv")
previsioni <- previsioni[-c(1)]
```

e_2 <- tsCV_RandomForest(my_xts = data[,1], xreg = data[,-1], forecastfunction = f_mod2, h=h, initial = initial, window = window)
e2 <- e_2$e
e2_percentage <- e_2$e_percentage
e2_estimate <- e_2$y_estimate
e2_groundtruth <- e_2$y_groundtruth
```{r}
# Analisi quantitativa

# Perdita medie e totale
perdita_media <- mean(previsioni$perdite)
perdita_totale <- sum(previsioni$perdite)

# Calcolo dell'incasso totale dell'anno precedente
r1_2019 <- r1[r1$Year == '2019', ]
incasso_2019 <- sum(r1_2019$lordototale)

end.time <- Sys.time()
print(end.time)
time.taken <- end.time - start.time
print(time.taken)
percentage <- perdita_totale/incasso_2019*100
```
```{r}
covid_predictions <- e2_estimate[539] # Prima riga non nulla di e2_estimate
covid_predictions <- as.data.frame(t(covid_predictions))

prediction_dates <- seq(as.Date("2020-02-24"), as.Date("2020-05-06"), by = 1)
Togliere commento seguente se vogliamo tenere anche la variabile "perdite"

covid_predictions <- cbind(covid_predictions, prediction_dates)
```{r}
# previsioni <- previsioni[-c(3)]
```

rownames(covid_predictions) <- NULL
colnames(covid_predictions) <- c("Previsioni", "Date")
```{r}
prediction_dates <- seq(as.Date("2020-02-24"), as.Date("2020-05-06"), by = 1)
previsioni <- cbind(prediction_dates, previsioni)
```

```{r}
previsioni <- xts(covid_predictions[, c("Previsioni")], as.Date(as.character(covid_predictions$Date), format = "%Y-%m-%d"))
previsioni <- melt(previsioni, id.vars = "prediction_dates")
```

```{r}
plot(previsioni)
p <- ggplot(previsioni,
aes(x = prediction_dates, y = value, col = variable)) + geom_line()
p + labs(x = "Data", y='Vendite') + ggtitle("Previsioni RF") +
theme(legend.title = element_blank(),
legend.position = c(0.9, 0.18),
legend.background = element_rect(fill = "white", color = "black"))
```

74 changes: 74 additions & 0 deletions Dati ristoranti/previsioni_RF.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"","previsioni","lordototale","perdite"
"1",10741.842,7367.814,3374.028
"2",11124.14445,6204.735,4919.40945
"3",12113.415675,8036.415,4077.000675
"4",12509.694,8750.565,3759.129
"5",19010.862,12779.289,6231.573
"6",29833.83225,16312.725,13521.10725
"7",25051.3479,13912.641,11138.7069
"8",11823.80895,9332.523,2491.28595
"9",11759.193,9952.038,1807.155
"10",12037.466025,9823.788,2213.678025
"11",12168.4095,10418.382,1750.0275
"12",19053.79335,10842.417,8211.37635
"13",28431.278775,14958.135,13473.143775
"14",25374.117375,5275.449,20098.668375
"15",11332.0377,4063.419,7268.6187
"16",11559.5964,3292.812,8266.7844
"17",11962.539,3007.584,8954.955
"18",12426.183,0,12426.183
"19",18807.22125,0,18807.22125
"20",30037.9212,0,30037.9212
"21",25068.771,0,25068.771
"22",11643.534,0,11643.534
"23",11944.3302,0,11944.3302
"24",12079.26,0,12079.26
"25",12491.91045,0,12491.91045
"26",18986.7645,0,18986.7645
"27",30428.50725,0,30428.50725
"28",24939.702,0,24939.702
"29",11321.092575,0,11321.092575
"30",11634.948,0,11634.948
"31",12010.6206,0,12010.6206
"32",12400.7814,0,12400.7814
"33",18816.894,0,18816.894
"34",30413.745,0,30413.745
"35",25486.17075,0,25486.17075
"36",11422.35,0,11422.35
"37",11816.5014,0,11816.5014
"38",12037.243275,2.7,12034.543275
"39",12501.644625,0,12501.644625
"40",19010.862,0,19010.862
"41",30882.3876,0,30882.3876
"42",25092.6228,0,25092.6228
"43",11355.319125,0,11355.319125
"44",11778.993,0,11778.993
"45",12067.029,0,12067.029
"46",12659.8734,0,12659.8734
"47",18956.575125,0,18956.575125
"48",30659.157,0,30659.157
"49",25370.7165,0,25370.7165
"50",11736.6489,0,11736.6489
"51",11978.7138,0,11978.7138
"52",12291.03,0,12291.03
"53",12657.87,0,12657.87
"54",19028.05425,0,19028.05425
"55",30979.611,0,30979.611
"56",27361.974375,0,27361.974375
"57",11321.092575,0,11321.092575
"58",11790.603,0,11790.603
"59",12311.3655,0,12311.3655
"60",12725.0757,0,12725.0757
"61",18946.08225,0,18946.08225
"62",30596.6754,0,30596.6754
"63",25969.8501,0,25969.8501
"64",11482.5015,0,11482.5015
"65",11635.56,0,11635.56
"66",12324.825,0,12324.825
"67",12615.3666,0,12615.3666
"68",19255.073625,0,19255.073625
"69",31109.896125,0,31109.896125
"70",26392.996125,0,26392.996125
"71",11375.89785,0,11375.89785
"72",11872.4742,0,11872.4742
"73",12371.206275,0,12371.206275

0 comments on commit 1e76ae7

Please sign in to comment.