# PSTR **Repository Path**: fangyebing/PSTR ## Basic Information - **Project Name**: PSTR - **Description**: an R package for testing, estimating and evaluating the Panel Smooth Transition Regression (PSTR) model. - **Primary Language**: Unknown - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2026-02-24 - **Last Updated**: 2026-02-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README PSTR version 1.2.5 (Orange Panel) ================================= [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/PSTR)](https://cran.r-project.org/package=PSTR) ![](http://cranlogs.r-pkg.org/badges/grand-total/PSTR) ![](http://cranlogs.r-pkg.org/badges/PSTR) ![](http://cranlogs.r-pkg.org/badges/last-week/PSTR) The PSTR package implements the Panel Smooth Transition Regression (PSTR) modelling. You can find the package on CRAN, see [PSTR@CRAN](https://CRAN.R-project.org/package=PSTR) The modelling procedure consists of three stages: Specification, Estimation and Evaluation. The package offers tools helping the users to conduct model specification tests, to do PSTR model estimation, and to do model evaluation. The cluster-dependency and heteroskedasticity-consistent tests are implemented in the package. The wild bootstrap and cluster wild bootstrap tests are also implemented. Parallel computation (as an option) is implemented in some functions, especially the bootstrap tests. Therefore, the package suits tasks running many cores on super-computation servers. How to install -------------- You can either install the stable version from CRAN ``` r install.packages("PSTR") ``` or install the development version from GitHub ``` r devtools::install_github("yukai-yang/PSTR") ``` provided that the package “devtools” has been installed beforehand. Example ------- After installing the package, you need to load (attach better say) it by running the code ``` r library(PSTR) ``` You can first check the information and the current version number by running ``` r version() #> PSTR version 1.2.5 (Orange Panel) ``` Then you can take a look at all the available functions and data in the package ``` r ls( grep("PSTR", search()) ) #> [1] "EstPSTR" "EvalTest" "Hansen99" #> [4] "LinTest" "NewPSTR" "plot_coefficients" #> [7] "plot_response" "plot_target" "plot_transition" #> [10] "sunspot" "version" "WCB_HETest" #> [13] "WCB_LinTest" "WCB_TVTest" ``` ### The data In the package, a data set called “Hansen99” is offered to give prompt example. For details of the data set, you can run ``` r ?Hansen99 ``` ### Initialization You can create a new object of the class PSTR by doing ``` r pstr = NewPSTR(Hansen99, dep='inva', indep=4:20, indep_k=c('vala','debta','cfa','sales'), tvars=c('vala'), im=1, iT=14) print(pstr) #> ########################################################################### #> ## PSTR 1.2.5 (Orange Panel) #> ########################################################################### #> *************************************************************************** #> 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': #> *************************************************************************** #> ########################################################################### ``` It says that the data set “Hansen99” is used, the dependent variable is “inva”, the variables in the data from column 4 to 20 are the explanatory variables in the linear part (though you can write down the names of them), the explanatory variables in the nonlinear part are the four ones in “indep\_k”, and the potential transition variable is “vala” (Tobin’s Q). Now you can see that the “NewPSTR” is basically defining the settings of the model. Note that you can print the object of the class PSTR. By default, it gives you a summary of the PSTR model. They are mainly about which one is the dependent variable, which ones are explanatory variables and etc.. ### Specification The following code does linearity tests ``` r pstr = LinTest(use=pstr) print(pstr, "tests") #> ########################################################################### #> ## PSTR 1.2.5 (Orange Panel) #> ########################################################################### #> *************************************************************************** #> 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 #> *************************************************************************** #> ########################################################################### ``` You can see that the function “LinTest” takes the PSTR object “pstr” and overwrites it when return. This is the way I recommend as the functions handling the PSTR object in the package update the object by adding new atrributes or members. However, the same function will change the values of the attributes it adds. You can of course create new PSTR objects to take the return values in order to save the results from different settings of the model. You can do the wild bootstrap and wild cluster bootstrap by running the following code. (Warning! Don’t run it except that you have at least 50 cores!) ``` r iB = 5000 # the number of repetitions in the bootstrap library(snowfall) pstr = WCB_LinTest(use=pstr,iB=iB,parallel=T,cpus=50) ``` It takes a long long time to run the bootstrap. This function is developed for those who work on some super-computation server with many cores and a large memory. Note that you will have to attach the “snowfall” package manually. But of course, you can try the function on your personal computer by reducing the number of repetitions and the cores. ``` r pstr = WCB_LinTest(use=pstr,iB=4,parallel=T,cpus=2) ``` ### Estimation When you determine which transition variable to use for the estimation, in this case “inva”, you can estimate the PSTR model ``` r pstr = EstPSTR(use=pstr,im=1,iq=1,useDelta=T,par=c(-0.462,0), vLower=4, vUpper=4) print(pstr,"estimates") ``` By default, the “optim” method “L-BFGS-B” is used, but you can change the method for estimation by doing ``` r pstr = EstPSTR(use=pstr,im=1,iq=1,useDelta=T,par=c(-0.462,0), method="CG") print(pstr,"estimates") #> ########################################################################### #> ## PSTR 1.2.5 (Orange Panel) #> ########################################################################### #> *************************************************************************** #> 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 #> *************************************************************************** #> ########################################################################### ``` The argument “useDelta” determines the type of the initial value for the smoothness parameter. By default “useDelta = F” means that the first initial value in “par” is the “gamma” instead of “delta”. Here we use the settings “useDelta = T” and “par = c(1.6, .5)” means that the first value of “par” is the “delta” and its value is 1.6. Note that “delta” and “gamma” has the relationship “gamma = exp(delta)”. Thus, the following two sentences are equivalent ``` r pstr = EstPSTR(use=pstr,im=1,iq=1,useDelta=T,par=c(-0.462,0), method="CG") pstr = EstPSTR(use=pstr,im=1,iq=1,par=c(exp(-0.462),0), method="CG") ``` Note that the estimation of a linear panel regression model is also implemented. The user can do it by simply running ``` r pstr0 = EstPSTR(use=pstr) print(pstr0,"estimates") #> ########################################################################### #> ## PSTR 1.2.5 (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 #> *************************************************************************** #> ########################################################################### ``` ### Evaluation The evaluation tests can be done based on the estimated model ``` r ## evaluatio tests pstr1 = EvalTest(use=pstr,vq=pstr$mQ[,1]) ``` Note that in the “EvalTest”, only one transition variable is taken each time for the no remaining nonlinearity test. This is different from the “LinTest” function which can take several transition variables. This is the reason why I save the results into new PSTR objects “pstr1” instead of overwriting. By doing so, I can save more test results from different transition variables in new objects. The user can also do the wild bootstrap and wild cluster bootstrap in the following way, provided that he or she has the super-computation resources. ``` r iB = 5000 cpus = 50 ## wild bootstrap time-varyint evaluation test pstr = WCB_TVTest(use=pstr,iB=iB,parallel=T,cpus=cpus) ## wild bootstrap heterogeneity evaluation test pstr1 = WCB_HETest(use=pstr1,vq=pstr$mQ[,1],iB=iB,parallel=T,cpus=cpus) ``` Note that the evaluation functions do not accept the returned object “pstr0” from a linear panel regression model, as the evaluation tests are designed for the estimated PSTR model but not a linear one. ### Plotting After estimating the PSTR model, you can plot the estimated transition function by running ``` r plot_transition(pstr) ``` ![](rmd_files/README-plot_trans1-1.png) or a better plot with more arguments ``` r plot_transition(pstr, fill='blue', xlim=c(-2,20), color = "dodgerblue4", size = 2, alpha=.3) + ggplot2::geom_vline(ggplot2::aes(xintercept = pstr$c - log(1/0.95 - 1)/pstr$gamma),color='blue') + ggplot2::labs(x="customize the label for x axis",y="customize the label for y axis", title="The Title",subtitle="The subtitle",caption="Make a caption here.") ``` ![](rmd_files/README-plot_trans2-1.png) You can also plot the curves of the coefficients, the standard errors and the p-values against the transition variable. ``` r ret = plot_coefficients(pstr, vars=1:4, length.out=100, color="dodgerblue4", size=2) ret[[1]] ``` ![](rmd_files/README-plot_coef-1.png) The plotting function `plot_response`, which depicts the relationship between which I called response, some explanatory variable *x**i**t* and the transition variable *q**i**t* in the PSTR model. The response \[*ϕ*0 + *ϕ*1*g**i**t*(*q**i**t*; *γ*, *c*)\]*x**i**t* is actually the contribution that the varabile *x**i**t* makes to the conditional expectation of the dependent *y**i**t* through the smooth transition mechanism. We can see that the response against the variable is a straight line if there is no nonlinearity. We can plot a surface if the variable *x**i**t* and the transition variable *q**i**t* are distinct, with z-axis the response, x- and y- axises the two variables. And it becomes a curve if the variable *x**i**t* and the transition variable *q**i**t* are identical. We make the graph by running ``` r ret = plot_response(obj=pstr, vars=1:4, log_scale = c(F,T), length.out=100) ``` `ret` takes the return value of the function. We make the graphs for all the four variables in nonlinear part by using `vars=1:4` (variable names can also be used for specification). Note that we do not do it for the variables in the linear part, as they produce straight lines or planes. `log_scale` is a 2-vector of booleans specifying, for each graph, whether the first (some variable in the nonlinear part) or the second (the transition variable) should be log scaled. `length.out` gives the number of points in the grid for producing the surface or curve. A `length.out` of 100 points looks fine enough. You may think of “what if I don’t wanna make all the variables log scaled?”. The solution is to make the graphs separately by running something like ``` r ret1 = plot_response(obj=pstr, vars=1, log_scale = c(F,T), length.out=100) ret2 = plot_response(obj=pstr, vars=2, log_scale = c(T,T), length.out=100) ``` Let us take a look the elements in `ret` ``` r attributes(ret) #> $names #> [1] "vala" "debta" "cfa" "sales" ``` We see that `ret` is a list containing elements whose names are the variables’ names that we specified when running `plot_response`. Yes, but they are now plottable objects in the sense that you can simply plot them by running ``` r ret$vala ``` ![](rmd_files/README-vala-1.png) The numbers on the x-axis look not so good as it is difficult to find where the turning-point is. The `ggplot2` package allows us to manually paint the numbers (the PSTR package collaborates very well with some prevailling packages), and even the label on x-axis (and many more). ``` r ret$vala + ggplot2::scale_x_log10(breaks=c(.02,.05,.1,.2,.5,1,2,5,10,20)) + ggplot2::labs(x="Tobin's Q") ``` ![](rmd_files/README-vala2-1.png) Now we see very clearly that the turning-point approximately 0.5 cut the curve into two regimes, and the two regimes behave so differently. This graph is about the lagged Tobin’s Q’s contribution to the expected investment. Low Q firms (whose potentials are evaluated to be low by the financial market) look rather reluctant to change their future investment plan, or maybe get changed. Then let us proceed to the surfaces. Check the response from the debta by running ``` r ret$debta ``` The graph is “living” and you can scracth on it by using your mouse. “vala\_y” shows that the y-axis is the Q, and “debta\_x” shows that the x-axis is the debt. The tool bar on up-right helps you to rotate, pan, zoom and save the graph. Note that the transition variable Q is in log scale while debt is not. It is very clear that low Q firms’ future investment will be affected by the current debt situation. The more debt there is, the less investment there will be. However, it is not the case for high Q firms who has good potential and is not sensitive to the debt. The following two living graphs are for the cash flow and the sales. ``` r ret$cfa ``` ``` r ret$sales ```   ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/Lianxh_装饰黄线.png) > Stata连享会   [主页](https://www.lianxh.cn/news/46917f1076104.html) || [视频](http://lianxh.duanshu.com) || [推文](https://www.lianxh.cn/news/d4d5cd7220bc7.html) || [知乎](https://www.zhihu.com/people/arlionn/) ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/连享会-草料主页-一码平川600.png)   ## 1. 连享会课程 > **免费公开课:** > - [直击面板数据模型](https://lianxh.duanshu.com/#/brief/course/7d1d3266e07d424dbeb3926170835b38) - 连玉君,时长:1小时40分钟,[课程主页](https://gitee.com/arlionn/PanelData) > - [Stata 33 讲](https://lianxh.duanshu.com/#/brief/course/b22b17ee02c24015ae759478697df2a0) - 连玉君, 每讲 15 分钟. [课程主页](https://gitee.com/lianxh/Stata33) > - [Stata 小白的取经之路](https://lianxh.duanshu.com/#/brief/course/137d1b7c7c0045e682d3cf0cb2711530) - 龙志能,时长:2 小时,[课程主页](https://gitee.com/arlionn/StataBin) > - 部分直播课 [课程资料下载](https://gitee.com/arlionn/Live) (PPT,dofiles等)   > ### ⛳ [课程主页](https://www.lianxh.cn/news/46917f1076104.html) ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/lianxhbottom01.png) ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/lianxhbottom02.png) ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/lianxhbottom03.png) > ### ⛳ [课程主页](https://www.lianxh.cn/news/46917f1076104.html) ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/Lianxh_装饰黄线.png)   ## 2. 资源分享 ### 视频公开课 - [连享会码云:100多个精选计量项目](https://www.lianxh.cn/news/944a69d75cec9.html) | [新浪视频](https://weibo.com/tv/show/1034:4479228373303338) - [五分钟 Markdown]() | [新浪视频](https://weibo.com/tv/show/1034:4484204327796746) - [连老师给你的-听课建议](https://www.lianxh.cn/news/69706e871c9ad.html) - [连享会 · Stata 33 讲 - 免费听](http://lianxh-pc.duanshu.com/course/detail/b22b17ee02c24015ae759478697df2a0) - 连玉君, 每讲 15 分钟. [课件](https://gitee.com/arlionn/stata101) - [直击面板数据模型](http://lianxh-pc.duanshu.com/course/detail/7d1d3266e07d424dbeb3926170835b38) - 连玉君,时长:1小时40分钟 - [Stata 33 讲](http://lianxh-pc.duanshu.com/course/detail/b22b17ee02c24015ae759478697df2a0) - 连玉君, 每讲 15 分钟. [课程主页](https://gitee.com/arlionn/stata101) - [Stata小白的取经之路](https://gitee.com/arlionn/StataBin),龙志能,上海财经大学,[去听课](https://lianxh.duanshu.com/#/brief/course/137d1b7c7c0045e682d3cf0cb2711530) ### Stata - [连享会推文](https://www.lianxh.cn) | [直播视频](http://lianxh.duanshu.com) - **计量专题课程**: [Stata暑期班/寒假班](https://gitee.com/lianxh/text) | [专题课程](https://gitee.com/arlionn/Course) - Stata专栏:[最新推文](https://www.lianxh.cn) | [知乎](https://www.zhihu.com/people/arlionn/) | [CSDN](https://blog.csdn.net/arlionn) - Books and Journal: [计量Books](https://quqi.gblhgk.com/s/880197/hmpmu2ylAcvHnXwY) | [SJ-PDF](https://quqi.gblhgk.com/s/880197/eipgoUi6Gd1FDZRu) | [Stata Journal-在线浏览](https://www.lianxh.cn/news/12ffe67d8d8fb.html) - Stata Guys:[Ben Jann](http://www.soz.unibe.ch/about_us/personen/prof_dr_jann_ben/index_eng.html) ### Data - [CSMAR-国泰安](http://www.gtarsc.com/#/datacenter/singletable) | [Wind-万德](https://www.wind.com.cn/Default.html) | [Resset-锐思](http://www.resset.cn/databases) - [常用数据库](https://www.lianxh.cn/news/0b65fd5165c2c.html) - [人文社科开放数据库](https://www.lianxh.cn/news/6f06c914acde8.html) - [徐现祥教授-IRE-官员交流、方言等](https://www.lianxh.cn/news/8c9f81a5f19ee.html) - [知乎-Data](https://www.zhihu.com/question/20179699/answer/681756635) ### Papers - 学术论文复现 - [论文重现网站](https://www.lianxh.cn/news/e87e5976686d5.html) - [Google学术](https://ac.scmor.com/) | [统一入口:虫部落学术搜索](http://scholar.chongbuluo.com/) | [微软学术](https://academic.microsoft.com/home) - [iData - 期刊论文下载](https://www.cn-ki.net/) - [ CNKI ](http://scholar.cnki.net/) | [百度学术](http://xueshu.baidu.com/) | [Google学术](https://scholar.glgoo.org/) | [Sci-hub ](http://www.sci-hub.cc/), [2](http://sci-hub.ac/), [3](http://sci-hub.bz/), [4](http://sci-hub.ac/) - Stata论文重现: [Harvard dataverse][harvd] | [JFE][jfe] | [github][git1] | [Yahoo-github][yahoogit] - 学者主页(提供了诸多论文的原始数据和 dofiles):[Angrist][Ang1] || [Daron Acemoglu][acem] || [Ross Levine][ross] || [Esther Duflo][Duflo] || [Imbens](https://scholar.harvard.edu/imbens/software) || [Raj Chetty](http://www.rajchetty.com/) [harvd]:https://dataverse.harvard.edu/dataverse [jfe]:http://jfe.rochester.edu/data.htm [Ang1]:http://economics.mit.edu/faculty/angrist/data1/data [acem]:http://economics.mit.edu/faculty/acemoglu/data [ross]:http://faculty.haas.berkeley.edu/ross_levine/papers.htm [duflo]:http://economics.mit.edu/faculty/eduflo/papers [git1]:https://github.com/search?utf8=%E2%9C%93&q=stata&type= [yahoogit]:https://search.yahoo.com/search;_ylt=AwrBT8di2LBZqyEAuG9XNyoA;_ylc=X1MDMjc2NjY3OQRfcgMyBGZyA3lmcC10LTQ3MwRncHJpZAMEbl9yc2x0AzAEbl9zdWdnAzAEb3JpZ2luA3NlYXJjaC55YWhvby5jb20EcG9zAzAEcHFzdHIDBHBxc3RybAMwBHFzdHJsAzE0BHF1ZXJ5A3N0YXRhJTIwZ2l0aHViBHRfc3RtcAMxNTA0NzYxODcz?p=stata+github&fr2=sb-top&fr=yfp-t-473&fp=1   --- >#### 关于我们 - **Stata连享会** 由中山大学连玉君老师团队创办,定期分享实证分析经验。[直播间](http://lianxh.duanshu.com) 有很多视频课程,可以随时观看。 - [连享会-主页](https://www.lianxh.cn) 和 [知乎专栏](https://www.zhihu.com/people/arlionn/),300+ 推文,实证分析不再抓狂。   > ⛳ **`lianxh` 命令发布了:** > 随时搜索连享会推文、Stata 资源,安装命令如下: >   `. ssc install lianxh` > 使用详情参见帮助文件 (有惊喜): >   `. help lianxh`   ![](https://fig-lianxh.oss-cn-shenzhen.aliyuncs.com/横条-远山03-窄版.jpg)