-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharima.R
More file actions
56 lines (45 loc) · 2.05 KB
/
arima.R
File metadata and controls
56 lines (45 loc) · 2.05 KB
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
install.packages("TTR")
install.packages("forecast")
library("TTR")
library("forecast")
plotForecastErrors <- function(forecasterrors)
{
# make a histogram of the forecast errors:
mybinsize <- IQR(forecasterrors)/4
mysd <- sd(forecasterrors)
mymin <- min(forecasterrors) - mysd*5
mymax <- max(forecasterrors) + mysd*3
# generate normally distributed data with mean 0 and standard deviation mysd
mynorm <- rnorm(10000, mean=0, sd=mysd)
mymin2 <- min(mynorm)
mymax2 <- max(mynorm)
if (mymin2 < mymin) { mymin <- mymin2 }
if (mymax2 > mymax) { mymax <- mymax2 }
# make a red histogram of the forecast errors, with the normally distributed data overlaid:
mybins <- seq(mymin, mymax, mybinsize)
hist(forecasterrors, col="red", freq=FALSE, breaks=mybins)
# freq=FALSE ensures the area under the histogram = 1
# generate normally distributed data with mean 0 and standard deviation mysd
myhist <- hist(mynorm, plot=FALSE, breaks=mybins)
# plot the normal curve as a blue line on top of the histogram of forecast errors:
points(myhist$mids, myhist$density, type="l", col="blue", lwd=2)
}
singleMeterSeries <- ts(singleMeterData$val, frequency = 7)
plot.ts(singleMeterSeries)
# singleMeterDataSMA <- SMA(singleMeterSeries, n=5)
# plot.ts(singleMeterDataSMA)
singleMeterTsComponents <- decompose(singleMeterSeries)
plot(singleMeterTsComponents)
singleMeterTsSeasonallyAdjusted <- singleMeterSeries - singleMeterTsComponents$seasonal
plot(singleMeterTsSeasonallyAdjusted)
singleMeterForecasts <- HoltWinters(singleMeterSeries)
singleMeterForecasts
plot(singleMeterForecasts)
singleMeterForecasts$SSE
singleMeterForecasts2 <- forecast.HoltWinters(singleMeterForecasts, h=180)
singleMeterForecasts2
plot.forecast(singleMeterForecasts2)
acf(singleMeterForecasts2$residuals, lag.max=20, na.action = na.pass)
Box.test(singleMeterForecasts2$residuals, lag=20, type="Ljung-Box")
plot.ts(singleMeterForecasts2$residuals) # make a time plot
plotForecastErrors(singleMeterForecasts2$residuals[!is.na(singleMeterForecasts2$residuals)]) # make a histogram