You can copy and paste the following code and run them in R.

First, install the latest PSTR package by running

install("devtools") # if you have devtools installed, you can skip this line.
devtools::install_github("yukai-yang/PSTR") # install PSTR
library(PSTR) # load PSTR

Now the package as well as the data set are loaded. You can take a look at the data

Hansen99
## # A tibble: 7,840 x 20
##    cusip  year    inva dt_75 dt_76 dt_77 dt_78 dt_79 dt_80 dt_81 dt_82
##    <int> <int>   <dbl> <int> <int> <int> <int> <int> <int> <int> <int>
##  1  1030    74 0.10500     0     0     0     0     0     0     0     0
##  2  1030    75 0.04006     1     0     0     0     0     0     0     0
##  3  1030    76 0.09213     0     1     0     0     0     0     0     0
##  4  1030    77 0.13567     0     0     1     0     0     0     0     0
##  5  1030    78 0.16206     0     0     0     1     0     0     0     0
##  6  1030    79 0.17429     0     0     0     0     1     0     0     0
##  7  1030    80 0.14632     0     0     0     0     0     1     0     0
##  8  1030    81 0.17356     0     0     0     0     0     0     1     0
##  9  1030    82 0.06026     0     0     0     0     0     0     0     1
## 10  1030    83 0.07199     0     0     0     0     0     0     0     0
## # ... with 7,830 more rows, and 9 more variables: dt_83 <int>,
## #   dt_84 <int>, dt_85 <int>, dt_86 <int>, dt_87 <int>, vala <dbl>,
## #   debta <dbl>, cfa <dbl>, sales <dbl>

Consider the panel data set that we used in the application. We build the PSTR model by running

pstr = NewPSTR(Hansen99, dep='inva', indep=4:20, indep_k=c('vala','debta','cfa','sales'),
    tvars=c('vala','debta','cfa','sales'), iT=14)

where Hansen99 is the panel data set, inva is the dependent variable “investment”, the independent variables consist of two parts: linear part and nonlinear part, the four variables vala (Tobin’s Q), debta (debt), cfa (cash) and sales (sales) enter both linear and nonlinear part, and they are the potential transition variables tvars, time sample size is 14, the return value pstr contains all the information about the PSTR model.

Since we know the linearity test results, let us skip the testing part, run the estimation

pstr = EstPSTR(use=pstr, im=1, iq=1, useDelta=TRUE, par=c(1.6,.5), method='CG')

The number of switch im=1. The firt potential transition variable iq=1, vala (Tobin’s Q) see tvars, is used for estimation. The optimization method CG (conjugate gradients method) is employed. The PSTR object pstr is reused and overwritten such that it contains the estimation results while the other model information remains unchanged. The \(\delta\) is used in the estimation instead of \(\gamma\).

We do not do grid search here, provided that the grid search has been conducted beforehand which gives the initial value of the optimization par=c(1.6,.5). The grid search can be done by building the grid using expand.grid function in R and then evaluate the nonlinear target function by using apply function. Since there are not so many nonlinear parameters, only \(\delta\) and \(c\), the algorithm is straightforward in the sense that only two lines R code, expand.grid and apply.

Here are the results from the estimation

print(pstr,"estimates")
## ###########################################################################
## ## PSTR 1.2.0 'Orange Panel' from GitHub
## ###########################################################################
## ***************************************************************************
## 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.004332 -0.007436 -0.005040 0.001092 0.003012 0.006406 0.001119
## s.e.  0.002502  0.002586  0.002738 0.002799 0.002697 0.002862 0.002944
##        dt_82_0   dt_83_0  dt_84_0  dt_85_0  dt_86_0   dt_87_0   vala_0
## Est  -0.007943 -0.014010 0.000836 0.004903 0.000628 -0.006537 -0.01722
## s.e.  0.002677  0.002757 0.003085 0.003234 0.003104  0.003192  0.06771
##       debta_0   cfa_0   sales_0
## Est  -0.06389 0.08598 -0.000464
## s.e.  0.02035 0.03117  0.004060
## ---------------------------------------------------------------------------
## Parameter estimates in the non-linear part are
##       vala_1 debta_1    cfa_1 sales_1
## Est  0.02403 0.06768 -0.04671 0.01033
## s.e. 0.06772 0.02021  0.03373 0.00494
## ---------------------------------------------------------------------------
## Parameter estimates in the second extreme regime are
##      vala_{0+1} debta_{0+1} cfa_{0+1} sales_{0+1}
## Est    0.006810    0.003784   0.03927    0.009865
## s.e.   0.001193    0.011720   0.01235    0.003116
## ---------------------------------------------------------------------------
## Non-linear parameter estimates are
##      gamma    c_1
## Est  4.953 0.4949
## s.e. 1.211 0.2538
## ---------------------------------------------------------------------------
## Estimated standard deviation of the residuals is 0.04323
## ***************************************************************************
## ###########################################################################

We see that the estimated smoothness parameter \(\gamma = 4.953\) and the location \(c = 0.4949\).

In the following, I am going to show the new plotting function plot_response, which depicts the relationship between \([\phi_0 + \phi_1 g_{it}(q_{it} ; \gamma, c)] x_{it}\) (I called it response), some explanatory variable \(x_{it}\) and the transition variable \(q_{it}\) in the PSTR model.

The response \([\phi_0 + \phi_1 g_{it}(q_{it} ; \gamma, c)] x_{it}\) is actually the contribution that the varabile \(x_{it}\) makes to the conditional expectation of the dependent \(y_{it}\) 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_{it}\) and the transition variable \(q_{it}\) are distinct, with z-axis the response, x- and y- axises the two variables. And it becomes a curve if the variable \(x_{it}\) and the transition variable \(q_{it}\) are identical.

We make the graph by running

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

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

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

ret$vala

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).

ret$vala + ggplot2::scale_x_log10(breaks=c(.02,.05,.1,.2,.5,1,2,5,10,20)) +
    ggplot2::labs(x="Tobin's Q")

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

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.

ret$cfa
ret$sales