-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZZ_learn_MSwM.R
100 lines (52 loc) · 1.78 KB
/
ZZ_learn_MSwM.R
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
# Learn package MSwM
#**********************************************************************
# Notes ####
#**********************************************************************
#**********************************************************************
# Packages ####
#**********************************************************************
library(tidyverse)
library(broom)
library(readxl)
library(magrittr)
library(stringr)
library(forcats)
library(grid)
library(gridExtra)
library(scales)
library(knitr)
# packages for time series modeling
library(astsa) # companion package
library(TSA) # companion package; arimax: flexible transfer function model
library(tseries) #
library(forecast) # Arima
library(MSwM) # Markov Switching model
# packages for
library(zoo)
library(xts)
library(timetk)
library(tidyquant)
library(lubridate)
#**********************************************************************
# Package examples ####
#**********************************************************************
data(example)
plot(ts(example))
# linear model
mod <- lm(y~x, example)
summary(mod)
x <- lm(y~1, example)
summary(x)
# Markov Switching model
mod.mswm = msmFit(mod, k = 2, p = 1, sw = c(T, T, T, T), control = list(parallel=F))
summary(mod.mswm)
plotProb(mod.mswm, which = 1)
plotProb(mod.mswm, which = 2)
plotReg(mod.mswm, expl = "x" )
# Daily Traffic Caualities by car accidents in Spain
data(traffic)
model <- glm(NDead~Temp+Prec, traffic, family = "poisson")
summary(model)
m1 <- msmFit(model, k = 2, sw = c(T, T, T), family = "poisson", control = list(parallel = F))
summary(m1)
intervals(m1)