1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
%\documentclass{article}
\documentclass[fontsize=2pt]{scrreprt}
\usepackage{fullpage}

\begin{document}


<<echo=False>>=
library(xtable)

eletemp = read.table(file = "sahko.csv", sep = ";", dec = ",", skip = 1)
ele = ts(eletemp[[1]][1:816], start = 1, frequency = 24)
temp = ts(eletemp[[2]], start = 1, frequency = 24)
temp816 = ts(eletemp[[2]][1:816], start = 1, frequency = 24)
temp24 = ts(eletemp[[2]][817:840], start = c(35,1), frequency = 24)
d = 1
S = 24
D = 1
Dw = 1
dele = ele
dtemp = temp

#Differencing
if (d > 0) {
  dele = diff(dele, lag = 1, differences = d)
  dtemp = diff(dtemp, lag = 1, differences = d)
}
if (D > 0) {
  dele = diff(dele, lag = S, differences = D)
  dtemp = diff(dtemp, lag = S, differences = D)
}
if (Dw > 0) {
  dele = diff(dele, lag = 168, differences = Dw)
  dtemp = diff(dtemp, lag = 168, differences = Dw)
}
#MODEL 1 (p, q, P, Q) = (0, 1, 1, 1)
portmStart = 30;
plotStart = 30;
p = 0
q = 1
P = 1
Q = 1
malli = arima(  ele, 
    order = c(p,d,q), 
    seasonal = list(order = c(P, D, Q), period = S), 
    method = "CSS")

createPortmanteuBox <- function()
{
  myData<-data.frame()
  for (h in 1:20)
  {
    for ( p in 0:2)
    {
      for (q in 0:1)
      {
        for (Q in 0:1)
        {
          for (P in 0:1)
          {
            test<-  Box.test(window(malli$residuals,start=portmStart), 
              lag = h, type = "Ljung-Box", fitdf = p + q + P + Q)
    
            #Bad
            lag<-h
            myF<-paste(p,q,Q,P)
            myP<-test$p.value
            myData<-rbind(myData, data.frame(lag,myF,myP))
            #print(paste(h,p,q,Q,P,test$p.value))
          }
        }
      }
    }
  }
  myData
}


myData<-createPortmanteuBox()
maDat<-  matrix(ncol=24,nrow=20, 
  myData[order(as.factor(myData$lag)),]$myP)
xtable(maDat)
#matplot(maDat)

@

\end{document}