setwd("~/Dropbox/Energy Trade/energy-trade")
library(readxl)
library(dummies)
## dummies-1.5.6 provided by Decision Patterns
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
DatosSpotColombia <- read_excel("DatosSpotColombia.xlsx")
precio_1=DatosSpotColombia[,6:29]
precio=precio_1[1,]
for (j in 2:nrow(DatosSpotColombia)) {
precio=((cbind(precio,precio_1[j,])))
}
plot(t(precio),type = "l")

start <- as.POSIXct("1995-07-20")
interval <- 60
end=as.POSIXct("2019-03-06")
Fecha_hora=seq(from=start, by=interval*60, to=end)
Fecha_hora=Fecha_hora[-length(Fecha_hora)]
serie_tiempo=cbind.data.frame(Fecha_hora,t(precio))
serie_tiempo=cbind.data.frame(serie_tiempo,month(serie_tiempo[,1]),day(serie_tiempo[,1]),weekdays.POSIXt(serie_tiempo[,1]),week(serie_tiempo[,1]),hour(serie_tiempo[,1]),seq(1:nrow(serie_tiempo)))
colnames(serie_tiempo)=c("Fecha","precio","mes","dia_mes","dia_semana","semana","hora","tiempo")
FECHA_INICIO_NINO_1="1995-12-01"
FECHA_FINAL_NINO_1="1995-12-30"
nino_1=rep(0,nrow(serie_tiempo))
nino_1=(cbind.data.frame(serie_tiempo[,"Fecha"],nino_1))
nino_1[which(nino_1[,1]==FECHA_INICIO_NINO_1):which(nino_1[,1]==FECHA_FINAL_NINO_1),"nino_1"]=1
##########
FECHA_INICIO_NINO_2="1997-09-01"
FECHA_FINAL_NINO_2="1998-03-30"
nino_2=rep(0,nrow(serie_tiempo))
nino_2=(cbind.data.frame(serie_tiempo[,"Fecha"],nino_2))
nino_2[which(nino_2[,1]==FECHA_INICIO_NINO_2):which(nino_2[,1]==FECHA_FINAL_NINO_2),"nino_2"]=1
########
FECHA_INICIO_NINO_3="2003-01-01"
FECHA_FINAL_NINO_3="2003-09-30"
nino_3=rep(0,nrow(serie_tiempo))
nino_3=(cbind.data.frame(serie_tiempo[,"Fecha"],nino_3))
nino_3[which(nino_3[,1]==FECHA_INICIO_NINO_3):which(nino_3[,1]==FECHA_FINAL_NINO_3),"nino_3"]=1
########
FECHA_INICIO_NINO_4="2006-10-01"
FECHA_FINAL_NINO_4="2006-10-30"
nino_4=rep(0,nrow(serie_tiempo))
nino_4=(cbind.data.frame(serie_tiempo[,"Fecha"],nino_4))
nino_4[which(nino_4[,1]==FECHA_INICIO_NINO_4):which(nino_4[,1]==FECHA_FINAL_NINO_4),"nino_4"]=1
##########
FECHA_INICIO_NINO_5="2009-10-01"
FECHA_FINAL_NINO_5="2010-05-30"
nino_5=rep(0,nrow(serie_tiempo))
nino_5=(cbind.data.frame(serie_tiempo[,"Fecha"],nino_5))
nino_5[which(nino_5[,1]==FECHA_INICIO_NINO_5):which(nino_5[,1]==FECHA_FINAL_NINO_5),"nino_5"]=1
########
FECHA_INICIO_NINO_6="2015-09-01"
FECHA_FINAL_NINO_6="2016-03-30"
nino_6=rep(0,nrow(serie_tiempo))
nino_6=(cbind.data.frame(serie_tiempo[,"Fecha"],nino_6))
nino_6[which(nino_6[,1]==FECHA_INICIO_NINO_6):which(nino_6[,1]==FECHA_FINAL_NINO_6),"nino_6"]=1
########
library(dummies)
dummy_m=dummy(serie_tiempo$mes, sep = "_")
## Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts =
## FALSE): non-list contrasts argument ignored
dummy_m=dummy_m[,-1]
dummy_ds=dummy(serie_tiempo$dia_semana, sep = "_")
## Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts =
## FALSE): non-list contrasts argument ignored
dummy_ds=dummy_ds[,-1]
dummy_dm=dummy(serie_tiempo$dia_mes, sep = "_")
## Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts =
## FALSE): non-list contrasts argument ignored
dummy_dm=dummy_dm[,-1]
dummy_s=dummy(serie_tiempo$semana, sep = "_")
## Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts =
## FALSE): non-list contrasts argument ignored
dummy_s=dummy_s[,-1]
dummy_h=dummy(serie_tiempo$hora, sep = "_")
## Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts =
## FALSE): non-list contrasts argument ignored
dummy_h=dummy_h[,-1]
X=cbind(serie_tiempo[,"tiempo"],dummy_m,dummy_ds,dummy_s,dummy_h,nino_1[,2],nino_2[,2],nino_3[,2],nino_4[,2],nino_5[,2],nino_6[,2])
colnames(X)[1]="tiempo"
modelo_2=lm(serie_tiempo[,"precio"]~X)
resumen_modelo=summary(modelo_2)
coeficientes_modelo=resumen_modelo$coefficients
validacion_1=which(coeficientes_modelo[,4]>0.1)
while (length(validacion_1)>0){
quitar=(which(coeficientes_modelo[,4]>0.1)-1)
X=X[,-quitar]
modelo_2=lm(serie_tiempo[,"precio"]~X)
resumen_modelo=summary(modelo_2)
coeficientes_modelo=resumen_modelo$coefficients
validacion_1=which(coeficientes_modelo[,4]>0.1)
}
plot(serie_tiempo[,"precio"],type = "l")
lines(modelo_2$fitted.values,type = "l", lwd=1, col="blue")

#write.csv(serie_tiempo,file = "serie_tiempo.csv")
#write.csv(modelo_2$coefficients,file = "parametros_f_t.csv")
x_t=modelo_2$residuals
x_t_1=x_t[-length(x_t)]
x_t=x_t[-1]
modelo_ar_1=lm(x_t~x_t_1-1)
coeficientes_ar=modelo_ar_1$coefficients
#View(cbind(x_t,x_t_1))
#plot(x_t_1,type = "l")