R语言如何做马尔科夫转换模型markov switching m

作者: 拓端tecdat | 来源:发表于2020-04-05 22:43 被阅读0次

原文链接:http://tecdat.cn/?p=6962

假设 有时间序列数据,如下所示。经验表明,目标变量y似乎与解释变量x有关。然而,乍一看,y的水平在中间移动,所以它似乎并不总是有固定的关系(背后有多个状态)。

need-to-insert-img

上面的样本数据创建如下。数据根据时间改变x和y之间的关系。

<- rpois(500, lambda = 10) \ny1 <- x * 4 + 20 \ny2 <- x * 2 + 60 \n\n \nnoise <- rnorm(1:500, mean = 10, sd = 5)\ny1 <- y1 + noise\ny2 <- y2 + noise\n\n y <- c(y1[1:200], y2[201:400], y1[401:500])\n observed <- data.frame(x = x, y = y)","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">x <- rpois(500, lambda = 10) y1 <- x * 4 + 20 y2 <- x * 2 + 60 noise <- rnorm(1:500, mean = 10, sd = 5) y1 <- y1 + noise y2 <- y2 + noise y <- c(y1[1:200], y2[201:400], y1[401:500]) observed <- data.frame(x = x, y = y)

need-to-insert-img

x和y1,y2之间的关系如下图所示。如果您知道x和y有两种状态,则x和y看起来像这样。

数据

在马尔可夫转换模型中,观察数据被认为是从几个状态生成的,并且如上所示很好地分离。

need-to-insert-img

观察到的数据

need-to-insert-img

创建马尔可夫转换模型

模型公式

|t|) \n# (Intercept) 45.7468 1.7202 26.59 <2e-16 ***\n# x 3.2262 0.1636 19.71 <2e-16 ***\n# ---\n# Signif. codes: \n# 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n# \n# Residual standard error: 11.51 on 498 degrees of freedom\n# Multiple R-squared: 0.4383, Adjusted R-squared: 0.4372 \n# F-statistic: 388.7 on 1 and 498 DF, p-value: < 2.2e-16","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"> # Call: # lm(formula = y ~ x, data = observed) # # Residuals: # Min 1Q Median 3Q Max # -24.303 -9.354 -1.914 9.617 29.224 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 45.7468 1.7202 26.59 <2e-16 *** # x 3.2262 0.1636 19.71 <2e-16 *** # --- # Signif. codes: # 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 11.51 on 498 degrees of freedom # Multiple R-squared: 0.4383, Adjusted R-squared: 0.4372 # F-statistic: 388.7 on 1 and 498 DF, p-value: < 2.2e-16

need-to-insert-img

参数的含义是

k:马尔可夫转换模型的状态数。在这里,它被指定为后面有两个状态。

sw:使用逻辑指定每个参数在状态更改时是否更改

p:AR模型系数

family:(在GLM的情况下)概率分布族

|t|) \n# (Intercept)(S) 69.3263 4.0606 17.0729 <2e-16 ***\n# x(S) 2.1795 0.1187 18.3614 <2e-16 ***\n# y_1(S) -0.0103 0.0429 -0.2401 0.8103 \n# ---\n# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n# \n# Residual standard error: 4.99756\n# Multiple R-squared: 0.6288\n# \n# Standardized Residuals:\n# Min Q1 Med Q3 Max \n# -1.431396e+01 -2.056292e-02 -1.536781e-03 -1.098923e-05 1.584478e+01 \n# \n# Regime 2 \n# ---------\n# Estimate Std. Error t value Pr(>|t|) \n# (Intercept)(S) 30.2820 1.7687 17.1210 <2e-16 ***\n# x(S) 3.9964 0.0913 43.7722 <2e-16 ***\n# y_1(S) -0.0045 0.0203 -0.2217 0.8245 \n# ---\n# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n# \n# Residual standard error: 4.836684\n# Multiple R-squared: 0.8663\n# \n# Standardized Residuals:\n# Min Q1 Med Q3 Max \n# -13.202056966 -0.771854514 0.002211602 1.162769110 12.417873232 \n# \n# Transition probabilities:\n# Regime 1 Regime 2\n# Regime 1 0.994973376 0.003347279\n# Regime 2 0.005026624 0.996652721","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"> # Markov Switching Model # # AIC BIC logLik # 3038.846 3101.397 -1513.423 # # Coefficients: # # Regime 1 # --------- # Estimate Std. Error t value Pr(>|t|) # (Intercept)(S) 69.3263 4.0606 17.0729 <2e-16 *** # x(S) 2.1795 0.1187 18.3614 <2e-16 *** # y_1(S) -0.0103 0.0429 -0.2401 0.8103 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 4.99756 # Multiple R-squared: 0.6288 # # Standardized Residuals: # Min Q1 Med Q3 Max # -1.431396e+01 -2.056292e-02 -1.536781e-03 -1.098923e-05 1.584478e+01 # # Regime 2 # --------- # Estimate Std. Error t value Pr(>|t|) # (Intercept)(S) 30.2820 1.7687 17.1210 <2e-16 *** # x(S) 3.9964 0.0913 43.7722 <2e-16 *** # y_1(S) -0.0045 0.0203 -0.2217 0.8245 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 4.836684 # Multiple R-squared: 0.8663 # # Standardized Residuals: # Min Q1 Med Q3 Max # -13.202056966 -0.771854514 0.002211602 1.162769110 12.417873232 # # Transition probabilities: # Regime 1 Regime 2 # Regime 1 0.994973376 0.003347279 # Regime 2 0.005026624 0.996652721

need-to-insert-img

输出中的制度1和制度2表示后面的两个状态 。

|t|) \n# (Intercept)(S) 69.3263 4.0606 17.0729 <2e-16 ***\n# x(S) 2.1795 0.1187 18.3614 <2e-16 ***\n# y_1(S) -0.0103 0.0429 -0.2401 0.8103 ","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"># Regime 1 # --------- # Estimate Std. Error t value Pr(>|t|) # (Intercept)(S) 69.3263 4.0606 17.0729 <2e-16 *** # x(S) 2.1795 0.1187 18.3614 <2e-16 *** # y_1(S) -0.0103 0.0429 -0.2401 0.8103

need-to-insert-img

y1 <- x * 4 + 20可以看到Regime 2与之兼容。

可以说从调整后的R平方值整体上有所改善。

|t|) \n# (Intercept)(S) 30.2820 1.7687 17.1210 <2e-16 ***\n# x(S) 3.9964 0.0913 43.7722 <2e-16 ***\n# y_1(S) -0.0045 0.0203 -0.2217 0.8245 ","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"># Regime 2 # --------- # Estimate Std. Error t value Pr(>|t|) # (Intercept)(S) 30.2820 1.7687 17.1210 <2e-16 *** # x(S) 3.9964 0.0913 43.7722 <2e-16 *** # y_1(S) -0.0045 0.0203 -0.2217 0.8245

need-to-insert-img

模型

对于每个regime,目标变量+指定的解释变量和处于该状态的概率以阴影绘制

need-to-insert-img

每个时间点的概率

need-to-insert-img

每次获取状态和更改点

如果你想知道你在某个特定时间点所在的regime,那么就选择那个时刻概率最高的 。

probable\n [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n [30] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n...","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">> probable [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 [30] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

need-to-insert-img

异常值/变化点是Regime更改的时间

分享:

相关文章

网友评论

    本文标题:R语言如何做马尔科夫转换模型markov switching m

    本文链接:https://www.haomeiwen.com/subject/tchmphtx.html