*
* @StarTest( options ) y start end
* does the test for linearity vs an alternative of LSTAR or ESTAR (smooth
* transition autoregression) described in Terasvirta, "Specification, Estimation
* and Evaluation of Smooth Transition Autoregressive Models", JASA 1994, vol 89,
* pp 208-218
*
* Options:
*   d     = delay on threshold variable [1]
*   p     = lags in the autoregression [1]
*   title = title for output
*
* Revision Schedule:
*  07/2005 Written by Tom Doan, Estima
*  03/2008 Updated to add comments
*
procedure StarTest y start end
type series y
type integer start end
*
option integer d      1
option integer p      1
option string  title
*
local rect[series] trans
local integer i j
local vect rss fstat signif
local rect s
local string reportheader

if .not.%defined(y) {
   display "Syntax: @STARTest(options) >>y<< start end"
   return
}
*
* Generate the cross products of the p lags of y with the first three powers of
* the threshold variable, so these will involve 2nd to 4th powers of y's.
*
dim trans(p,3)
do j=1,3
   do i=1,p
      set trans(i,j) = y{i}*(y{d}**j)
   end do i
end do j
*
* Compute the cross product matrix of the standard AR(p) regressors and the
* transformed variables.
*
cmom start end
# constant y{1 to p} trans y
dim rss(4) fstat(4) signif(4)
*
* Sweep out standard regressors
*
compute s=%sweeplist(%cmom,%seq(1,p+1))
compute rss(1)=s(%ncmom,%ncmom)
*
* Sweep out the lags, starting from the second order to the fourth. Save the sum
* of squared residuals after each.
*
do j=1,3
   compute s=%sweeplist(s,%seq(j*p+2,(j+1)*p+1))
   compute rss(j+1)=s(%ncmom,%ncmom)
end do j
*
* Compute the f-stats for the sequence of hypotheses
*
compute %ndf=%nobs-p-1
do j=1,3
   compute fstat(j+1) =((rss(j)-rss(j+1))/p)/(rss(j+1)/(%ndf-j*p))
   compute signif(j+1)=%ftest(fstat(j+1),p,%ndf-j*p)
end do j
*
* And the overall test
*
compute %cdstat=(rss(1)-rss(4))/(3*p)/(rss(4)/(%ndf-p*3))
compute %signif=%ftest(%cdstat,3*p,%ndf-p*3)
if %defined(title)
   compute reportheader=title
else
   compute reportheader="Test for STAR in series "+%l(y)+". AR length="+p+" delay="+d
report(action=define)
report(atrow=1,atcol=1,span) reportheader
report(atrow=2,atcol=1) "Test" "F-stat" "Signif"
report(atrow=3,atcol=1) "Linearity"  %cdstat %signif
report(atrow=4,atcol=1) "H01" fstat(4) signif(4)
report(atrow=5,atcol=1) "H02" fstat(3) signif(3)
report(atrow=6,atcol=1) "H03" fstat(2) signif(2)
report(action=format,width=9,atcol=2)
report(action=format,width=6,atcol=3)
report(action=show)
end

