-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_diagnostics.qmd
145 lines (118 loc) · 3.23 KB
/
03_diagnostics.qmd
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
title: "Diagnostics"
author: "Martin Řanda"
format:
html:
toc: true
toc-location: left
toc-depth: 4
theme: flatly
highlight-style: oblivion
code-fold: true
self-contained: true
engine: knitr
---
```
## Author: Martin Řanda
## Year: 2023
##
## R Version 4.2.2
##
## forecast 8.21
## xts 0.13.0
## zoo 1.8.11
## dplyr 1.1.0
## librarian 1.8.1
```
## Setup
Load packages and setup
```{r}
if (!require(librarian)) install.packages("librarian")
librarian::shelf(dplyr, xts, forecast)
for (pkg in check_attached()) cat(paste(pkg, packageVersion(pkg), "\n"))
# Set timezone to CET, locale to English, and working directory to filepath
Sys.setenv(TZ = "CET")
Sys.setlocale("LC_TIME", "English")
```
Load all in-sample fits
```{r}
nn1_fit <- readRDS("results/insample/nn1_fit.rds")
nn48_fit <- readRDS("results/insample/nn48_fit.rds")
sarimax101_fit <- readRDS("results/insample/sarimax101_fit.rds")
sarimax202_fit <- readRDS("results/insample/sarimax202_fit.rds")
trees_fit <- readRDS("results/insample/trees_fit.rds")
# Create residuals
nn1_fit$resid <- with(nn1_fit, load_mw - fit)
nn48_fit$resid <- with(nn48_fit, load_mw - fit)
sarimax101_fit$resid <- with(sarimax101_fit, load_mw - fit)
sarimax202_fit$resid <- with(sarimax202_fit, load_mw - fit)
trees_fit$resid <- with(trees_fit, load_mw - fit)
fit_list <- list(
nn1_fit,
nn48_fit,
sarimax101_fit,
sarimax202_fit,
trees_fit
)
names(fit_list) <- c("nn1_fit", "nn48_fit", "sarimax101_fit", "sarimax202_fit", "trees_fit")
```
Imported data may have different dimensions due to the in-sample set being different in most cases
```{r}
sapply(fit_list, dim)
```
## Diagnostics
```{r}
lags <- 1:7
boxtests <- vector("list", length = length(names(fit_list)))
names(boxtests) <- names(fit_list)
archtests <- vector("list", length = length(names(fit_list)))
names(archtests) <- names(fit_list)
jbtests <- vector("list", length = length(names(fit_list)))
names(jbtests) <- names(fit_list)
for (n in names(fit_list)) {
j <- 0
box <- rep(NA, length(lags))
arch <- rep(NA, length(lags))
for (l in lags) {
j <- j + 1
cat("\n", l, "lag", n)
box[j] <- Box.test(fit_list[[n]]$resid, lag = l, type = "Ljung-Box")$p.value
cat("\nLB statistic", Box.test(fit_list[[n]]$resid, lag = l, type = "Ljung-Box")$statistic)
arch[j] <- Box.test(fit_list[[n]]$resid^2, lag = l, type = "Box-Pierce")$p.value
cat("\nARCH statistic", Box.test(fit_list[[n]]$resid^2, lag = l, type = "Box-Pierce")$statistic)
jbtest <- JarqueBeraTest(fit_list[[n]]$resid)$p.value
}
boxtests[[n]] <- box
archtests[[n]] <- arch
jbtests[[n]] <- jbtest
}
```
```{r}
boxtests
```
```{r}
archtests
```
```{r}
jbtests
```
```{r}
par(mfrow = c(2, 3))
for (n in names(fit_list)) {
plot(Acf(fit_list[[n]]$resid, plot = F), main = n)
}
```
```{r}
par(mfrow = c(2, 3))
for (n in names(fit_list)) {
h <- hist(fit_list[[n]]$resid, breaks = 100, main = n, freq = F, col = "white")
x <- seq(min(fit_list[[n]]$resid), max(fit_list[[n]]$resid), length = 1000)
y <- dnorm(x, mean = mean(fit_list[[n]]$resid), sd = sd(fit_list[[n]]$resid))
lines(x, y, col = "purple", lwd = 2)
}
par(mfrow = c(2, 3))
for (n in names(fit_list)) {
qqnorm(fit_list[[n]]$resid, main = n)
qqline(fit_list[[n]]$resid)
}
```