-
Notifications
You must be signed in to change notification settings - Fork 2
/
ARIMA_Forecasting.bas
75 lines (52 loc) · 2.58 KB
/
ARIMA_Forecasting.bas
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
Attribute VB_Name = "Module1"
Option Explicit
Public ServerActiveAtStart As Boolean
Sub Load_Source_Server()
Worksheets("Sheet1").Activate
rinterface.StartRServer
If rinterface.RLibraryIsAvailable("tseries") = False Then
rinterface.RRun "install.packages(c(""tseries""))"
End If
If rinterface.RLibraryIsAvailable("zoo") = False Then
rinterface.RRun "install.packages(c(""zoo""))"
End If
rinterface.RRun "library(tseries)"
rinterface.RRun "library(zoo)"
rinterface.RRun "source(file.choose())"
End Sub
Sub Plot_Weekly_TS()
'Declare Frequency Object
rinterface.PutDataframe "fre", Range("Sheet1!C5:C6"), False, False
rinterface.RunRCall "function(Data)ts(data=Data,frequency=fre[1,])", Range("Sheet1!Data")
'Find Optimal Model of Data
rinterface.GetRApply "function(Data)find.best.arima(Data)", Range("Sheet1!L2"), Range("Sheet1!Data")
rinterface.PutDataframe "z", Range("Sheet1!L1:L7"), False, False
rinterface.RunRCall "function(Data)sarima(Data,z[1,],z[2,],z[3,],z[4,],z[5,],z[6,],fre[1,])", Range("Sheet1!Data")
rinterface.InsertCurrentRPlot Range("Sheet1!BG4"), widthrescale:=0.7, heightrescale:=0.7, closergraph:=True
'Forecast n Days Ahead
rinterface.PutDataframe "days", Range("Sheet1!C7:C8"), False, False
rinterface.GetRApply "function(Data)sarima.for(Data,days,z[1,],z[2,],z[3,],z[4,],z[5,],z[6,],fre[1,])", Range("Sheet1!M2"), Range("Sheet1!Data")
rinterface.InsertCurrentRPlot Range("Sheet1!AY4"), widthrescale:=0.7, heightrescale:=0.7, closergraph:=True
Application.DisplayAlerts = False
Range("Sheet1!M2").TextToColumns Destination:=Range("N2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
"(", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
Array(6, 1)), TrailingMinusNumbers:=True
Range("Sheet1!M3").TextToColumns Destination:=Range("N3"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
"(", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
Array(6, 1)), TrailingMinusNumbers:=True
Application.DisplayAlerts = True
End Sub
Sub Clear_Forecasted_Values()
On Error Resume Next
ActiveSheet.Shapes.Range(Array("RPlot002")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("RPlot001")).Select
Selection.Delete
Range("Sheet1!O2:AP3").ClearContents
Range("Sheet1!L2:L7").ClearContents
Range("Sheet1!K5:K40").ClearContents
End Sub