You are on page 1of 5

library(readxl)

Servicios <- read_excel("C:\\Users\\jogarzon\\Documents\\Modelo Forecast R\\


Servicios.xlsx",
col_types = c("date", "text", "text",
"text", "text", "text", "numeric"))

library(WriteXLS)
library(uroot)
library(tidyr)
library(tidyverse)
library(lubridate)
library(forecast)
library(dplyr)
library(data.table)
Base <- unite(Servicios,Clave,c(2:6), sep = ";",remove = TRUE) %>%
data.frame() %>%
spread(Clave,Servicios)

#Porbar con 25, 28,29,33,34, 37, 6200, 7300 , 7900, 118


###Estacionaria: 36,38
####"ASISTENCIA EN VIAJE ABA DE CHUBB SEGUROS MEXICO;Asistencia Vial;Arrastre de
Grúa;VIAL MORAS;SISE"
####"ASISTENCIA ZURICH VIAL;Asistencia Vial;Arrastre de Grúa;VIAL MORAS;SISE"
###"ASISTENCIA CLUB GNP;Asistencia Vial;Arrastre de Grúa;VIAL MORAS;SISE"
###"ASISTENCIA EN VIAJES ATLAS (MOTOS);Asistencia Vial;Arrastre de Grúa;VIAL
MORAS;SISE"
###"NISSAN ASSISTANCE;Asistencia Vial;Arrastre de Grúa;VIAL PERIFERICO;SISE"

###"ASISTENCIA EN VIAJE ABA DE CHUBB SEGUROS MEXICO;Asistencia Vial;Suministro de


Gasolina;VIAL MORAS;SISE"
myts <- ts(Base["ASISTENCIA EN VIAJE ABA DE CHUBB SEGUROS MEXICO;Asistencia
Vial;Suministro de Gasolina;VIAL MORAS;SISE"],end = c(2020,11), frequency = 12)

myts

?auto.arima
pl <- autoplot(myts)
pl

Arima <- auto.arima(y = myts, test = "adf", ic ="aicc",trace = TRUE)


summary(Arima)

FCST <- forecast(Arima,25,level = c(99.5))

pl2 <- autoplot(FCST)


pl2
###################################################################################
####
#LOOP

Base [is.na(Base)] <- 0


myts <- ts(Base[2:ncol(myts)],end = c(2020,11), frequency = 12 )

M <- 24
PREFCST.list2<- list()
PREFCST.list <- list()
FCSTF <- list()
FCST.list <- list()

for (j in 1:ncol(myts)) {

ifelse(is.na(myts[,j][[nrow(myts)]]),
FCSTF[[j]] <- rep(c(0),M),
ifelse(myts[,j][[nrow(myts)]] <= 5 || myts[,j][[nrow(myts)]] > sum( myts[,j]
[[nrow(myts)-3]] + myts[,j][[nrow(myts)-2]] + myts[,j][[nrow(myts)-1]], na.rm =
TRUE) ,
FCSTF[[j]] <- rep(c(myts[,j][[nrow(myts)]]),M),
{
automatic.services <-auto.arima(myts[,j],test = "adf", ic ="aicc")
PREFCST.list[[j]]<- forecast(automatic.services,M, level = c(99.5))
PREFCST<- c(PREFCST.list[[j]]$mean)
ifelse ( (PREFCST[M]) < 0,
{
automatic.services <-auto.arima(myts[,j],stationary = TRUE)
PREFCST.list2[[j]] <- forecast(automatic.services,M, level = c(99.5))
PREFCST2<- c(PREFCST.list2[[j]]$mean)
ifelse ( (PREFCST2[M]) < 0,
{
automatic.services <-auto.arima(myts[,j], approximation = TRUE)
FCST.list[[j]] <- forecast(automatic.services,M, level = c(99.5))
FCSTF[[j]]<- c(FCST.list[[j]]$mean)},
{
automatic.services <-auto.arima(myts[,j],stationary = TRUE)
FCST.list[[j]] <- forecast(automatic.services,M, level = c(99.5))
FCSTF[[j]] <- c(FCST.list[[j]]$mean)})},
{
automatic.services <-auto.arima(myts[,j],test = "adf", ic ="aicc")
FCST.list[[j]] <- forecast(automatic.services,M, level = c(99.5))
FCSTF[[j]] <- c(FCST.list[[j]]$mean)})}))

print(j)
}

names <- c(colnames(myts))


x <- data.frame(myts) %>%
`names<-`(names)

FCST2 <- data.frame(FCSTF)

Base_Forecast <- `names<-`(FCST2,names)

Base_Forecast2 <- replace(Base_Forecast, Base_Forecast < 0, 0)


Base_Forecast3 <- bind_rows(x,Base_Forecast2)
Fechas <- seq(as.Date("2017/01/01"), by = "month", length.out =
nrow(Base_Forecast3))

df_transpose <- data.frame(t(Base_Forecast3[-1])) %>%


`names<-`(Fechas)
df_transpose [is.na(df_transpose)] <- 0

datos2 <- gather(datos, key= "clave",value = "servicios",2:ncol(datos))

setwd("C:\\Users\\jogarzon\\Documents\\Modelo Forecast R")


write.csv(df_transpose,"Resultado.csv")

Forecast Diario

library(readxl)
Servicios <- read_excel("C:\\Users\\jogarzon\\OneDrive - IKE ASISTENCIA COLOMBIA
S.A\\Forecast\\Mexico\\Diario\\Rewards Real.xlsx",
col_types = c("date", "text", "text", "numeric"))

library(astsa)
library(stats)
library(WriteXLS)
library(uroot)
library(tidyr)
library(tidyverse)
library(lubridate)
library(forecast)
library(dplyr)
library(data.table)

Base <- select(Servicios,c(Mes,Cuenta, Llamadas))%>%


data.frame() %>%
spread(Cuenta,Llamadas)

#Porbar con 25, 28,29,33,34, 37, 6200, 7300 , 7900, 118


###Estacionaria: 36,38
####"ASISTENCIA EN VIAJE ABA DE CHUBB SEGUROS MEXICO;Asistencia Vial;Arrastre de
Grúa;VIAL MORAS;SISE"
####"ASISTENCIA ZURICH VIAL;Asistencia Vial;Arrastre de Grúa;VIAL MORAS;SISE"
###"ASISTENCIA CLUB GNP;Asistencia Vial;Arrastre de Grúa;VIAL MORAS;SISE"
###"ASISTENCIA EN VIAJES ATLAS (MOTOS);Asistencia Vial;Arrastre de Grúa;VIAL
MORAS;SISE"
###"NISSAN ASSISTANCE;Asistencia Vial;Arrastre de Grúa;VIAL PERIFERICO;SISE"

###"ASISTENCIA EN VIAJE ABA DE CHUBB SEGUROS MEXICO;Asistencia Vial;Suministro de


Gasolina;VIAL MORAS;SISE"
myts <- ts(Base[13], end = c(2021,01,31), frequency = 30)

REAL <- myts[(length(myts)-(364)):(length(myts)-31)]


X<-sarima.for(myts,2,1,2,2,2,2,30, n.ahead=334)
X$pred
sarima(myts , 2,0,0)
myts

?auto.arima
pl <- autoplot(myts)
pl
#revisar esta funcion approximation = TRUE
##auto.arima(y = myts, ic = c("aic"), test = "adf", seasonal.test = "ch", start.p =
2, start.q = 2, start.P = 1, start.Q = 1, max.order = 5)
#parallel = TRUE,stepwise = FALSE,num.cores =4
Arima <- auto.arima(myts, trace = TRUE)
summary(Arima)
FCST <- forecast(Arima,334,level = c(99.5))

Y<-bind_cols(FCST$mean,X$pred,REAL,seq(as.Date("2021/02/01"), by = "day",
length.out = 334)) %>%
data.frame() %>% `names<-`(names) %>%
gather(key= "Modelo",value = "Llamadas",1:3)

names<- c("ARIMA","SARIMA","REAL","Fecha")

ggplot(Y, aes(x=Fecha, y=Llamadas, group = Modelo, colour =Modelo )) +


geom_line() +
geom_point( size=2, shape=21, fill="white") +
theme_minimal()

autoplot(FCST)

Transponer

Sub Transponer()

n = Cells(1, 1).CurrentRegion.Rows.Count
m = Cells(1, 1).CurrentRegion.Columns.Count

bd = Val(InputBox("Número de columnas a repetir:", "BASE", 3))


t = m - bd 'rotulos

Application.ScreenUpdating = False

Range(Cells(2, 1), Cells(n, bd)).Select


Selection.Copy
For i = 1 To t - 1
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Activate
ActiveSheet.Paste
Next

For j = 2 To t
Cells(2, bd + j).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Cells(2, bd + 1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Activate
ActiveSheet.Paste

Next
'rotulos
For k = 1 To t
For jj = 1 To n - 1
Cells(1 + ((n - 1) * (k - 1)) + jj, bd + 2) = Cells(1, bd + k)
Next
Next

Cells(1, bd + 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With

Range("A1").Select
Selection.Copy
Cells(1, bd + 1).Select
ActiveSheet.Paste
Cells(1, bd + 2).Select
ActiveSheet.Paste
Application.CutCopyMode = False

Cells(1, bd + 1) = "Servicios"
Cells(1, bd + 2) = "Mes"

End Sub

You might also like