关于数据挖掘:R语言面板平滑转换回归PSTR分析案例实现

33次阅读

共计 7423 个字符,预计需要花费 19 分钟才能阅读完成。

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

原文出处:拓端数据部落公众号

建模过程包含三个阶段:表述,预计和评估,本文帮忙用户进行模型表述、预计,进行 PSTR 模型评估。

在程序包中实现了集群依赖性和异方差性一致性测验。

还实现了 wild bootstrap 和 cluster wild bootstrap 测验。

并行计算(作为选项)在某些函数中实现,尤其是 bootstrap 测验。因而,该程序包适宜在超级计算服务器上运行多个外围的工作。

数据

“Hansen99”数据集来提供示例。

初始化

能够通过执行创立 PSTR 类的新对象

 
 #> Summary of the model:
#> ---------------------------------------------------------------------------
#>   time horizon sample size = 14,  number of individuals = 560
#> ---------------------------------------------------------------------------
#> Dependent variable:  inva
#> ---------------------------------------------------------------------------
#> Explanatory variables in the linear part:
#>   dt\_75 dt\_76 dt\_77 dt\_78 dt\_79 dt\_80 dt\_81 dt\_82 dt\_83 dt\_84 dt\_85 dt\_86 dt_87 vala debta cfa sales
#> ---------------------------------------------------------------------------
#> Explanatory variables in the non-linear part:
#>   vala debta cfa sales
#> ---------------------------------------------------------------------------
#> Potential transition variable(s) to be tested:
#>   vala
#> ###########################################################################
#> ***************************************************************************
#> Results of the linearity (homogeneity) tests:
#> ***************************************************************************
#> Sequence of homogeneity tests for selecting number of switches 'm':
#> ***************************************************************************
#> ###########################################################################

因变量是“inva”,第 4 列到第 20 列的数据中的变量是线性局部的解释变量,非线性局部中的解释变量是“indep_k”中的四个,潜在的转换变量是“vala”(Tobin 的 Q)。

以下代码执行线性测验

 #> ###########################################################################
#> ***************************************************************************
#> Results of the linearity (homogeneity) tests:
#> ---------------------------------------------------------------------------
#> LM tests based on transition variable 'vala'
#>   m  LM\_X PV  LM\_F PV HAC\_X        PV HAC\_F        PV
#>   1 125.3  0 28.99  0 30.03 4.819e-06 6.952 1.396e-05
#> ***************************************************************************
#> Sequence of homogeneity tests for selecting number of switches 'm':
#> ---------------------------------------------------------------------------
#> LM tests based on transition variable 'vala'
#>   m  LM\_X PV  LM\_F PV HAC\_X        PV HAC\_F        PV
#>   1 125.3  0 28.99  0 30.03 4.819e-06 6.952 1.396e-05
#> ***************************************************************************
#> ###########################################################################

能够看到函数“LinTest”获取 PSTR 对象“pstr”并返回后果。因为解决包中 PSTR 对象的函数通过增加新的 atrributes 来更新对象。当然能够创立新的 PSTR 对象来获取返回值,以便保留模型的不同设置的后果。

 能够通过运行以下代码来执行 wild bootstrap 和 wild cluster bootstrap。

预计

当确定要用于预计的转换变量时,在本例中为“inva”,能够预计 PSTR 模型

 print(pstr,"estimates")

默认状况下,应用“optim”办法“L-BFGS-B”,但能够通过更改优化办法进行估算

 print(pstr,"estimates")
#> ###########################################################################
 #> ###########################################################################
#> ***************************************************************************
#> Results of the PSTR estimation:
#> ---------------------------------------------------------------------------
#> Transition variable 'vala' is used in the estimation.
#> ---------------------------------------------------------------------------
#> Parameter estimates in the linear part (first extreme regime) are
#>        dt\_75\_0   dt\_76\_0   dt\_77\_0   dt\_78\_0  dt\_79\_0  dt\_80\_0   dt\_81\_0
#> Est  -0.002827 -0.007512 -0.005812 0.0003951 0.002464 0.006085 0.0004164
#> s.e.  0.002431  0.002577  0.002649 0.0027950 0.002708 0.002910 0.0029220
#>        dt\_82\_0   dt\_83\_0    dt\_84\_0  dt\_85\_0   dt\_86\_0   dt\_87\_0  vala_0
#> Est  -0.007802 -0.014410 -0.0009146 0.003467 -0.001591 -0.008606 0.11500
#> s.e.  0.002609  0.002701  0.0030910 0.003232  0.003202  0.003133 0.04073
#>       debta\_0   cfa\_0  sales_0
#> Est  -0.03392 0.10980 0.002978
#> s.e.  0.03319 0.04458 0.008221
#> ---------------------------------------------------------------------------
#> Parameter estimates in the non-linear part are
#>        vala\_1 debta\_1    cfa\_1  sales\_1
#> Est  -0.10370 0.02892 -0.08801 0.005945
#> s.e.  0.03981 0.04891  0.05672 0.012140
#> ---------------------------------------------------------------------------
#> Parameter estimates in the second extreme regime are
#>      vala_{0+1} debta_{0+1} cfa_{0+1} sales_{0+1}
#> Est    0.011300    -0.00500   0.02183    0.008923
#> s.e.   0.001976     0.01739   0.01885    0.004957
#> ---------------------------------------------------------------------------
#> Non-linear parameter estimates are
#>       gamma        c_1
#> Est  0.6299 -0.0002008
#> s.e. 0.1032  0.7252000
#> ---------------------------------------------------------------------------
#> Estimated standard deviation of the residuals is 0.04301
#> ***************************************************************************
#> ###########################################################################

 还实现了线性面板回归模型的预计。

 print(pstr0,"estimates")
#> ###########################################################################
#> ## PSTR 1.2.4 (Orange Panel)
#> ###########################################################################
#> ***************************************************************************
#> A linear panel regression with fixed effects is estimated.
#> ---------------------------------------------------------------------------
#> Parameter estimates are
#>          dt\_75     dt\_76     dt\_77    dt\_78    dt\_79    dt\_80    dt_81
#> Est  -0.007759 -0.008248 -0.004296 0.002356 0.004370 0.008246 0.004164
#> s.e.  0.002306  0.002544  0.002718 0.002820 0.002753 0.002959 0.002992
#>          dt\_82     dt\_83    dt\_84    dt\_85    dt\_86     dt\_87     vala
#> Est  -0.005294 -0.010040 0.006864 0.009740 0.007027 0.0004091 0.008334
#> s.e.  0.002664  0.002678 0.003092 0.003207 0.003069 0.0030080 0.001259
#>          debta     cfa    sales
#> Est  -0.016380 0.06506 0.007957
#> s.e.  0.005725 0.01079 0.002412
#> ---------------------------------------------------------------------------
#> Estimated standard deviation of the residuals is 0.04375
#> ***************************************************************************
#> ###########################################################################

评估

能够基于预计的模型进行评估测试请留神,在“EvalTest”中,每次只有一个转换变量用于非线性测试。这与“LinTest”函数不同,后者能够采纳多个转换变量。这就是为什么我将后果保留到新的 PSTR 对象“pstr1”而不是笼罩的起因。通过这样做,我能够在新对象中保留来自不同转换变量的更多测试后果。

iB = 5000
cpus = 50

## wild bootstrap time-varyint 评估测验
pstr = WCB_TVTest(use=pstr,iB=iB,parallel=T,cpus=cpus)

## wild bootstrap 异质性评估测验
pstr1 = WCB_HETest(use=pstr1,vq=pstr$mQ\[,1\],iB=iB,parallel=T,cpus=cpus)

请留神,评估函数不承受线性面板回归模型中返回的对象“pstr0”,因为评估测试是针对预计的 PSTR 模型设计的,而不是线性模型。

可视化

估算 PSTR 模型后,能够绘制预计的转换函数

还能够依据转换变量绘制系数曲线,标准误差和 p 值。

绘图plot_response,形容了 PSTR 模型的因变量和一些解释性变量。

咱们能够看到,如果没有非线性,对变量的响应是一条直线。如果变量和转换变量是不同的,咱们能够绘制曲面,z 轴为响应,x 轴和 y 轴为两个变量。如果变量和转换变量雷同,则变为曲线。

咱们通过运行来制作图表

x 轴上的数字看起来不太好,因为很难找到转折点的地位。

ggplot2 软件包容许咱们手动绘制数字。

当初咱们十分分明地看到,大概 0.5 的转折点将曲线切割成两种状态,并且两种状态的行为齐全不同。该图表是对于托宾 Q 对预期投资的滞后影响。低 Q 值公司(其后劲被金融市场评估为低)可能不太违心扭转他们将来的投资打算,或者可能会扭转。

相干文章

1.R 语言多元 Logistic 逻辑回归 利用案例

2. 面板平滑转移回归 (PSTR) 剖析案例实现剖析案例实现 ”)

3.matlab 中的偏最小二乘回归(PLSR)和主成分回归(PCR)

4.R 语言泊松 Poisson 回归模型剖析案例

5.R 语言回归中的 Hosmer-Lemeshow 拟合优度测验

6.r 语言中对 LASSO 回归,Ridge 岭回归和 Elastic Net 模型实现

7. 在 R 语言中实现 Logistic 逻辑回归

8.python 用线性回归预测股票价格

9.R 语言如何在生存剖析与 Cox 回归中计算 IDI,NRI 指标

正文完
 0