joari

functions.R

Dec 11th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.79 KB | None | 0 0
  1. # Functions for this study
  2. # ==================================================================================
  3. # Load libraries
  4. library(pear)
  5. # ==================================================================================
  6. plot_order <- function(aa, cases, anoI, arquivo){
  7.   maxY <- max(aa)
  8.   validSample <- NULL
  9.   for (j in 1:length(cases)) {
  10.     if (aa[j,1] != 0) {
  11.       validSample = c(validSample, (anoIni + cases[j] - 1))
  12.     }
  13.   }
  14.   png(file=paste(arquivo, " - Model order",".png", sep=""));
  15.   par(mar = par()$mar + c(0,0,0,4))
  16.   if (aa[1,1] != 0){
  17.     plot(1:12, y = aa[1,], type = "o", pch=1,
  18.          xlab = "Month", ylab = "Model order",
  19.          ylim = c(0, maxY), main = arquivo, xaxt='n')
  20.     axis(side = 1, at = 1:12, labels=month.abb)
  21.   }
  22.   for (i in 2:length(cases)) {
  23.     if (aa[i,1] != 0) {
  24.       lines(aa[i,], type = "o", pch=i, col=rainbow(14)[i])
  25.     }
  26.   }
  27.   # ll <- seq(1:14)
  28.   # legend("topleft", inset=.05, legend = anoI, title="Sample", pch = ll, bty = "n", fill=rainbow(14))#, horiz=F)
  29.   legend("right", inset = c(-0.21,0), legend = validSample, xpd = TRUE,
  30.          horiz = FALSE, col = rainbow(14), lty = 1, bty = "n")
  31.   par(mar=c(5, 4, 4, 2) + 0.1)
  32.   dev.off()
  33. }
  34. # ==================================================================================
  35. plot_EAFh <-  function(EAF, maxEAF, anoIni, anoFim, arquivo, anoini){
  36.   tmp <- as.vector(t(EAF))
  37.   EAF_ts <- ts(tmp, end = anoFim, frequency = 12)
  38.  
  39.   png(file=paste(arquivo, "-TS-inic-", anoini, ".png", sep=""));
  40.   plot(EAF_ts, xlim = c(anoIni,anoFim), ylim = c(0, maxEAF))
  41.   title(main = arquivo)
  42.   mtext(paste0("Ano inicial = ", anoini), side = 3, line = 0.5)
  43.   #summary(EAF_ts)
  44.   #hist(EAF_ts,prob=TRUE)
  45.   dev.off()
  46.  
  47.   png(file=paste(arquivo, "-BP-inic-", anoini, ".png", sep=""));
  48.   boxplot(EAF_ts ~ cycle(EAF_ts), names=month.abb, ylim = c(0, maxEAF))#, main = arquivo)
  49.   title(main = arquivo)
  50.   mtext(paste0("Ano inicial = ", anoini), side = 3, line = 0.5)
  51.   dev.off()
  52.  
  53.   png(file=paste(arquivo, "-PePACF-inic-", anoini, ".png", sep=""));
  54.   outpepacf <- pepacf(EAF_ts, 15, plot=TRUE)
  55.   title(main = arquivo)
  56.   mtext(paste0("Ano inicial = ", anoini), side = 3, line = 0.5)
  57.   dev.off()
  58. }
  59. # ==================================================================================
  60. ajusta_parp <- function(EAF, anoFim, criterion, saida, saida_Full){
  61.   tmp <- as.vector(t(EAF))
  62.   EAF_ts <- ts(tmp, end = anoFim, frequency = 12)
  63.   parpIC <- list()
  64.   #  for (criterion in c("aic", "bic")){
  65.   # for (criterion in c("aic")){
  66.     parp<-pear(EAF_ts,ic=criterion)
  67.     # parp <- pear(EAF_ts,ic="aic")
  68.     parpIC <- c(parpIC , parp)
  69.     #parp$model.orders
  70.     # IC <- toupper(criterion)
  71.     cat(paste(formatC(parp$model.orders,digits=NULL,format="d",width=6), sep=""), file=saida, append=TRUE)
  72.     title <- paste("\n", toupper(criterion)," phi coefficients\n", sep = "" )
  73.     prt.phi(title, parp$phi, parp$model.orders, saida_Full)  
  74.     title <- paste(toupper(criterion)," phi standard deviation \n", sep = "" )
  75.     prt.phi(title, parp$se.phi, parp$model.orders, saida_Full)
  76.   # }
  77.   return(parpIC)
  78. }
  79. # ==================================================================================
  80. # print PAR(p) phi or sd.phi coefficients as formatted table
  81. prt.phi <- function(title, phi, model.orders, saida){
  82.   cat(title,file=saida,append=T);
  83.   ordMax <- max(model.orders)
  84.   cat(" month", paste(formatC("lag_",format="s",width=11), formatC(1:ordMax,format="d",flag="0",width=2),sep=""),
  85.       file=saida, fill=13*22+6, append=T);
  86.   for (i in 1:12){
  87.     cat(formatC(phi[i,1:model.orders[[i]]],digits=9,format="f",width=13), labels=paste(formatC(month.abb[i],format="s",width=6)),
  88.         file=saida, fill=13*22+6, append=T);
  89.   }
  90. }
  91. # ==================================================================================
  92. #
Advertisement
Add Comment
Please, Sign In to add comment