***********************************************************************
*
*		DOMENICO DEPALO 	- Tor Vergata University, Rome - 25Jan08
*
*		This version: 	- La Sapienza University, Rome - 14Mar09
***********************************************************************

program define sroot, rclass
	version 6.0, missing
	syntax varname(ts) [if] [in] [, TRend noCONstant /*
		*/ Lags(int -1) REGress SEASon(varlist) GENerate(string) RESiduals(string)]

    	if "`generate'" != "" {
        capture confirm new variable `generate'
        if _rc {
                di in r "`generate' already exists: " _c
                di in r "specify new variable with generate( ) option"
                exit 110
        	}
	  local gcount: word count `generate'
        if `gcount'!=3  {
                di in r "generate( ) option requires three new variables. You specified `gcount'."
                exit 198
        	}

        }

    	if "`residuals'" != "" {
        capture confirm new variable `residuals'
        if _rc {
                di in r "`residuals' already exists"
                exit 110
        	}
	}

	if "`drift'" != "" {
		if "`constan'" != "" {
noi di as error "cannot specify drift if constant is excluded"
			exit 198
		}
		if "`trend'" != "" {
noi di as error "cannot specify drift if time trend is included"
			exit 198
		}
	}

	if      "`constan'" != "" & "`trend'" == "" & "`season'" == "" { local case 1 }			// no deterministic part
	else if "`constan'" == "" & "`trend'" == "" & "`drift'" == "" & "`season'" == "" { 			// only constant
		local case 2
	}
	else if "`constan'" == "" & "`trend'" == "" & "`season'" != "" {
		local case 3													// constant + seasonal dummies
		local count: word count `season'
		if `count'==1 {
			tempvar d_
			qui tab `season',gen(`d_')
			local ds  "`d_'1 `d_'2 `d_'3"
		}
		if `count'>1 {
			local one: word 1 of `season'
			local two: word 2 of `season'
			local thr: word 3 of `season'
			local fou: word 4 of `season'
			tempvar tempd d_
			qui gen `tempd'=1 if `one'==1
			qui replace `tempd'=2 if `two'==1
			qui replace `tempd'=3 if `thr'==1
			qui replace `tempd'=4 if `fou'==1
			qui tab `tempd',gen(`d_')
			local ds  "`d_'1 `d_'2 `d_'3"
		}
	}

	else if "`constan'" == "" & "`trend'" != "" & "`season'" == "" { 						// constant + trend
		local case 4
		tempvar tt
		qui gen `tt'=_n
		local ds  "`tt'"
	}
	else if "`constan'" == "" & "`trend'" != "" & "`season'" != "" { 						// constant + trend + seasonal dummies
		local case 5
		tempvar tt
		qui gen `tt'=_n
		local count: word count `season'
		if `count'==1 {
			tempvar d_
			qui tab `season',gen(`d_')
			local ds  "`d_'1 `d_'2 `d_'3 `tt'"
		}
		if `count'>1 {
			local one: word 1 of `season'
			local two: word 2 of `season'
			local thr: word 3 of `season'
			local fou: word 4 of `season'
			tempvar tempd d_
			qui gen `tempd'=1 if `one'==1
			qui replace `tempd'=2 if `two'==1
			qui replace `tempd'=3 if `thr'==1
			qui replace `tempd'=4 if `fou'==1
			qui tab `tempd',gen(`d_')
			local ds  "`d_'1 `d_'2 `d_'3 `tt'"
		}
	}

	else {
		noi di in red "cannot choose trend if constant is excluded"
		exit 198
	}

        marksample touse
	_ts tvar panelvar `if' `in', sort onepanel
	markout `touse' `tvar'
        local samp "if `touse'==1"
	qui tsset
	if r(unit1) != "q" {
		di in red "Only quarterly data are allowed"
		exit
		}


	* BUILDS THE TRANSFORMED VARIABLES
	tempvar y1 y2 y3 y4
	qui gen `y1'=`varlist'+l.`varlist'+l2.`varlist'+l3.`varlist'
	qui gen `y2'=-(`varlist'-l.`varlist'+l2.`varlist'-l3.`varlist')
	qui gen `y3'=-(`varlist'-l2.`varlist')
	qui gen `y4'=`varlist'-l4.`varlist'

	if "`generate'"!="" {
		local word1: word 1 of `generate'
		local word2: word 2 of `generate'
		local word3: word 3 of `generate'
		local word4: word 4 of `generate'
		qui gen `word1'=`y1'
		qui gen `word2'=`y2'
		qui gen `word3'=`y3'
		qui label var `word1' "Freq. 0 of `varlist'"
		qui label var `word2' "Semi-annual freq. of `varlist'"
		qui label var `word3' "Annual freq of `varlist'"
	}


	forvalues i=1(1)`lags' {
		local cova "`cova' l`i'.`y4'"
	}

	local xmat "l.`y1' l.`y2' l2.`y3' l.`y3' `cova' `ds'"

	if "`constan'"!="" {local cc "noconstant"}

	quietly {
		regress `y4' `xmat',`cc'
	    	if "`residuals'" != "" {
			predict `residuals', resid
			label var `residuals' "Residuals from Seasonal Unit Root test"
		}

		local T = e(N)
		local n = e(N) - e(df_r)
		test L.`y1'=0
		local Z1t = sign(_b[L.`y1'])*sqrt(r(F))

		test L.`y2'=0
		local Z2t = sign(_b[L.`y2'])*sqrt(r(F))

		test L2.`y3'=0
		local Z3t = sign(_b[L2.`y3'])*sqrt(r(F))

		test L.`y3'=0
		local Z4t = sign(_b[L.`y3'])*sqrt(r(F))

		test L2.`y3'=0
		test L.`y3'=0,accum
		local Z34 = r(F)
		test L.`y2'=0,accum
		local Z24 = r(F)
		test L.`y1'=0,accum
		local Z14 = r(F)

        	local b1 "_b[L.`y1']"
        	local s1 "_se[L.`y1']"
        	local b2 "_b[L.`y2']"
        	local s2 "_se[L.`y2']"
        	local b3 "_b[L2.`y3']"
        	local s3 "_se[L2.`y3']"
        	local b4 "_b[L.`y3']"
        	local s4 "_se[L.`y3']"

		tempname tab
		mat `tab'=(0,0)
		forvalues ll=1/`lags' {
			if `ll'==1 {
				mat `tab'=( _b[L`ll'.`y4'],_se[L`ll'.`y4'])
			}
			if `ll'>1 {
				mat `tab'=(`tab' \ _b[L`ll'.`y4'],_se[L`ll'.`y4'])
			}
		}

		tempname matseas
		mat `matseas'=(0,0)
		if `case'==3|`case'==5 {
			mat `matseas'=( _b[`d_'1],_se[`d_'1] \_b[`d_'2],_se[`d_'2] \ _b[`d_'3],_se[`d_'3])
		}
	}

	GetCrit `case' `T' `varname'

	noi di in gr _n "HEGY test for SEASONAL unit roots" /*
		*/ _col(52) "Number of obs   = " in ye %9.0g `T'

	di in gr _col (19) "Test" /*
		*/ _col(32)  "1% Critical" /*
		*/ _col(50)  "5% Critical" /*
		*/ _col(67) "10% Critical"
	di in gr _col (16) "Statistic" /*
		*/ _col(36)  "Value" /*
		*/ _col(54)  "Value" /*
		*/ _col(72) "Value"
	di in gr in smcl "{hline 78}"

	local Zt1=crthegy[1,1]
	local Zt5=crthegy[1,3]
	local Zt10=crthegy[1,4]
	di in gr " Z(t) - Fr 0" /*
		*/ _col(15) in ye %10.3f `Z1t' /*
		*/ _col(33) %10.3f  `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'

	local Zt1=crthegy[2,1]
	local Zt5=crthegy[2,3]
	local Zt10=crthegy[2,4]
	di in gr " Z(t) - Fr 1/2" /*
		*/ _col(15) in ye %10.3f `Z2t' /*
		*/ _col(33) %10.3f  `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'

	local Zt1=crthegy[3,1]
	local Zt5=crthegy[3,3]
	local Zt10=crthegy[3,4]
	di in gr " Z(t) - L.Ann." /*
		*/ _col(15) in ye %10.3f `Z3t' /*
		*/ _col(33) %10.3f  `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'

	local Zt1=crthegy[4,1]
	local Zt5=crthegy[4,3]
	local Zt10=crthegy[4,4]
	di in gr " Z(t) - Annual" /*
		*/ _col(15) in ye %10.3f `Z4t' /*
		*/ _col(33) %10.3f  `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'

	local Zt1=crthegy[5,4]
	local Zt5=crthegy[5,2]
	local Zt10=crthegy[5,1]
	di in gr " Joint Annual" /*							// joint test
		*/ _col(15) in ye %10.3f `Z34' /*
		*/ _col(33) %10.3f `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'

	local Zt1=crthegy[6,1]
	local Zt5=crthegy[6,2]
	local Zt10=crthegy[6,3]
	di in gr " All SEAS. fr." /*
		*/ _col(15) in ye %10.3f `Z24' /*
		*/ _col(33) %10.3f `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'

	local Zt1=crthegy[7,1]
	local Zt5=crthegy[7,2]
	local Zt10=crthegy[7,3]
	di in gr " All freq." /*
		*/ _col(15) in ye %10.3f `Z14' /*
		*/ _col(33) %10.3f `Zt1' /*
		*/ _col(51) %10.3f `Zt5' /*
		*/ _col(69) %10.3f `Zt10'




       if "`regress'" != "" {
                di
		if "`trend'"=="" {
			tempvar tt
			qui gen `tt'=_n in 1
		}
                        DispReg `case' `varlist' `lags' `b1' `s1' `b2' `s2' `b3' `s3' `b4' `s4' `tab' `matseas' `tt'
        }

	ret scalar Z1t     = `Z1t'
	ret scalar Z2t     = `Z2t'
	ret scalar Z3t     = `Z3t'
	ret scalar Z4t     = `Z4t'
	ret scalar Z34  	 = `Z34'
	ret scalar Z24  	 = `Z24'
	ret scalar Z14  	 = `Z14'

	ret scalar N       = `T'
	ret scalar lags    = `lags'

	cap drop `y4'
	cap drop `xmat'
end

program define GetCrit, rclass

	args case N
tempname uno1 uno2 uno3 uno4 uno5 uno6 uno7 crthegy

if `N'<=48 			{
	local r=1
}
if `N'<=100 & `N'>48	{
	local r=2
}
if `N'<=136 & `N'>100	{
 	local r=3
}
if `N'>136  		{
	local r=4
}


* freq 0

	if `case'==1{
		mat	`uno1'= 	    ( -2.72,	-2.29,	-1.95,	-1.59	\	/*
		  */	     -2.60,		-2.26,	-1.97,	-1.61	\	/*
		  */	     -2.62,		-2.25,	-1.93,	-1.59 \	/*
		  */	     -2.62,		-2.23,	-1.94,	-1.62 )
	}

	if `case'==2{
	mat	`uno1'= 	    ( -3.66,	-3.25,	-2.96,	-2.62	\	/*
		  */	     -3.47, 	-3.14,	-2.88,	-2.58	\	/*
		  */	     -3.51, 	-3.17,	-2.89,	-2.58	\	/*
		  */	     -3.48,  	-3.13,	-2.87,	-2.57 )
	}
	if `case'==3{
	mat	`uno1'= 	    ( -3.77,	-3.39,	-3.08,	-2.72 \	/*
		  */	     -3.55, 	-3.22,	-2.95,	-2.63 \	/*
		  */	     -3.56, 	-3.23,	-2.94,	-2.62 \	/*
		  */	     -3.51, 	-3.18,	-2.91,	-2.59)
	}
	if `case'==4{
		mat	`uno1'= 	    ( -4.23,	-3.85,	-3.56,	-3.21\	/*
		  */	     -4.07, 	-3.73,	-3.47,	-3.16\	/*
		  */	     -4.09, 	-3.75,	-3.46,	-3.16\	/*
		  */	     -4.05, 	-3.70,	-3.44,	-3.15)
	}
	if `case'==5{
	mat	`uno1'= 	    ( -4.46,	-4.04,	-3.71,	-3.37\	/*
		  */	     -4.09, 	-3.80,	-3.53,	-3.32\	/*
		  */	     -4.15, 	-3.80,	-3.52,	-3.21\	/*
		  */	     -4.05, 	-3.74,	-3.49,	-3.18)
	}

* fre 1/2

	if `case'==1{
	mat	`uno2'= 	    ( -2.67,	-2.27,	-1.95,	-1.60\	/*
		  */	     -2.61, 	-2.22,	-1.92,	-1.57\	/*
		  */	     -2.60, 	-2.23,	-1.94,	-1.61\	/*
		  */	     -2.60, 	-2.24,	-1.95,	-1.61)
	}
	if `case'==2{
	mat	`uno2'= 	    ( -2.68,      -2.27,	-1.95,	-1.60\	/*
		  */	     -2.61, 	-2.24,	-1.95,	-1.60\	/*
		  */	     -2.60, 	-2.21,	-1.91,	-1.58\	/*
		  */	     -2.58, 	-2.22,	-1.92,	-1.59)
	}
	if `case'==3{
	mat	`uno2'= 	    ( -3.75, 	-3.37,	-3.04,	-2.69\	/*
		  */	     -3.60, 	-3.22,	-2.94,	-2.63\	/*
		  */	     -3.49, 	-3.15,	-2.90,	-2.59\	/*
		  */	     -3.50, 	-3.16,	-2.89,	-2.60)
	}
	if `case'==4{
	mat	`uno2'= 	    ( -2.65, 	-2.24,	-1.91,	-1.57\	/*
		  */	     -2.58, 	-2.24,	-1.94,	-1.60\	/*
		  */	     -2.65, 	-2.25,	-1.96,	-1.63\	/*
		  */	     -2.59,  	-2.25,	-1.95,	-1.62)
	}
	if `case'==5{
	mat	`uno2'= 	    ( -3.80,	-3.41,	-3.08, 	-2.73\	/*
		  */	     -3.60,  	-3.22,	-2.94,	-2.63\	/*
		  */	     -3.57,  	-3.18,	-2.93,	-2.61\	/*
		  */	     -3.52,  	-3.18,	-2.91,	-2.60)
	}

* fre 1/4

	if `case'==1{
		mat	`uno3'= 	    (-2.66, 	-2.23,	-1.93,	-1.52\	/*
		  */	     -2.55, 	-2.18,	-1.90,	-1.53\	/*
		  */	     -2.58,       -2.21,	-1.92,	-1.56\	/*
		  */	     -2.58, 	-2.34,	-1.92,	-1.55)
	}
	if `case'==2{
	mat	`uno3'= 	    (-2.64, 	-2.23,	-1.90,	-1.55\	/*
		  */	     -2.61, 	-2.23,	-1.90,	-1.54\	/*
		  */	     -2.53, 	-2.18,	-1.88,	-1.53\	/*
		  */	     -2.57, 	-2.21,	-1.90,	-1.53)
	}
	if `case'==3{
	mat	`uno3'= 	    (-4.31, 	-3.92,	-3.61,	-3.24\	/*
		  */	     -4.06, 	-3.72,	-3.44,	-3.14\	/*
		  */	     -4.06, 	-3.72,	-3.44,	-3.11\	/*
		  */	     -4.00, 	-3.67,	-3.38,	-3.07)
	}
	if `case'==4{
	mat	`uno3'= 	    (-2.68, 	-2.27, 	-1.92,	-1.52\	/*
		  */	     -2.56, 	-2.19,   	-1.89,	-1.54\	/*
		  */	     -2.56, 	-2.20,	-1.90,	-1.52\	/*
		  */	     -2.58, 	-2.21,	-1.92,	-1.56)
	}
	if `case'==5{
	mat	`uno3'= 	    (-4.46, 	-4.02,	-3.66,	-3.28\	/*
		  */	     -4.12, 	-3.76,	-3.48,	-3.14\	/*
		  */	     -4.05, 	-3.72,	-3.44,	-3.12\	/*
		  */	     -4.04, 	-3.69,	-3.41,	-3.10)
	}

*fre\ 3/4

	if `case'==1{
		mat	`uno4'= 	    (-2.51,		-2.11,	-1.76,	-1.35\	/*
		  */	     -2.43,		-2.01,	-1.68,	-1.32\	/*
		  */	     -2.44,		-1.99,	-1.68,	-1.31\	/*
		  */	     -2.43,		-1.98,	-1.65,	-1.3)
	}
	if `case'==2{
	mat	`uno4'= 	    (-2.44,		-2.06,	-1.72,	-1.33\	/*
		  */	     -2.38,		-1.99,	-1.68,	-1.3\		/*
		  */	     -2.36,		-1.98,	-1.68,	-1.31\	/*
		  */	     -2.36,		-1.98,	-1.66,	-1.29)
	}
	if `case'==3{
	mat	`uno4'= 	    (-2.86,		-2.37,	-1.98,	-1.53\	/*
		  */	     -2.78,		-2.32,	-1.96,	-1.53\	/*
		  */	     -2.72,		-2.31,	-1.96,	-1.52\	/*
		  */	     -3.74,		-2.33,	-1.96,	-1.54)
	}
	if `case'==4{
	mat	`uno4'= 	    (-2.41,		-2.05,	-1.7	,	-1.33\	/*
		  */	     -3.38,		-1.97,	-1.65,	-1.28\	/*
		  */	     -2.36,		-1.97,	-1.64,	-1.29\	/*
		  */	     -2.35,		-1.97,      -1.66,	-1.29)
	}
	if `case'==5{
	mat	`uno4'= 	    (-2.75,		-2.26,	-1.91,	-1.48\	/*
      	  */       -2.76,		-2.32,	-1.94,	-1.51\	/*
	        */       -2.71,		-2.78,	-1.94,	-1.51\	/*
		  */	     -2.65,		-2.27,	-1.92,	-1.48)
	}

* freq 3--4
	if `case'==1{
	mat	`uno5'=	( 2.45,	3.26,	4.04,	5.02\	/*
		  */	    2.39,	3.12,	3.89,	4.89\		/*
		  */	    2.41,	3.14,	3.86,	4.81\		/*
		  */	    2.42,	3.16,	3.92,	4.81)
	}
	if `case'==2{
	mat	`uno5'=	( 2.32,	3.04,	3.78,	4.78\		/*
		  */	    2.35,	3.08,	3.81,	4.77\		/*
		  */	    2.36,	3.00,	3.70,	4.73\		/*
		  */	    2.37,	3.12,	3.86,	4.76)
	}
	if `case'==3{
	mat	`uno5'=	( 5.50,	6.60,	7.68,	9.22\		/*
		  */	    5.56,	6.57,	7.72,	8.74\		/*
		  */	    5.56,	6.63,	7.66,	8.92\		/*
		  */	    5.56,	6.61,	7.53,	8.93)
	}
	if `case'==4{
	mat	`uno5'=	( 2.23,	2.95,	3.70,	4.64\		/*
		  */	    2.31,	2.98,	3.71,	4.70\		/*
		  */	    2.33,	3.04,	3.69,	4.57\		/*
		  */	    2.34,	3.07,	3.76,	4.66)
	}
	if `case'==5{
	mat	`uno5'=	( 5.37,	6.55,	7.70,	9.27\		/*
		  */	    5.52,	6.60,	7.52,	8.79\		/*
		  */	    5.55,	6.62,	7.59,	8.77\		/*
		  */	    5.56,	6.57,	7.56,	8.96)
	}

* freq 2--4
	if `case'==1{
	mat	`uno6'=	(2.80,2.21\ /*
			*/ 2.76,2.21\ /*
			*/ 2.72,2.18\ /*
			*/ 2.73,2.22\ /*
			*/ 2.76,2.19)
	}
	if `case'==2{
	mat	`uno6'=	(2.63,2.17\ /*
			*/ 2.74,2.18\ /*
			*/ 2.74,2.21\ /*
			*/ 2.75,2.19\ /*
			*/ 2.69,2.15)
	}
	if `case'==3{
	mat	`uno6'=	(6.60,5.16 \ /*
			*/ 6.05,5.18\ /*
			*/ 6.04,5.16\ /*
			*/ 5.99,5.14\ /*
			*/ 5.95,5.15\ )
	}
	if `case'==4{
	mat	`uno6'=	(2.67,2.14\ /*
			*/ 2.76,2.15\ /*
			*/ 2.75,2.15\ /*
			*/ 2.72,2.17\ /*
			*/ 2.70,2.16)
	}
	if `case'==5{
	mat	`uno6'=	(6.09,5.13\ /*
			*/ 5.99,5.13\ /*
			*/ 5.91,5.14\ /*
			*/ 5.89,5.10\ /*
			*/ 5.90,5.10)
	}
	mat `uno6'=(J(5,1,.),`uno6',J(5,1,.))

* freq 1--4
	if `case'==1{
	mat	`uno7'=	( 2.62,2.11\ /*
				*/ 2.55,2.11\ /*
				*/ 2.53,2.06\ /*
				*/ 2.56,2.12\ /*
				*/ 2.53,2.07 )
	}
	if `case'==2{
	mat	`uno7'=	( 3.47,2.86\ /*
				*/ 3.37,2.86\ /*
				*/ 3.38,2.81\ /*
				*/ 3.32,2.81\ /*
				*/ 3.29,2.79 )
	}
	if `case'==3{
	mat	`uno7'=	( 5.96,5.02\ /*
				*/ 5.74,4.95\ /*
				*/ 5.69,4.90\ /*
				*/ 5.58,4.87\ /*
				*/ 5.55,4.86 )
	}
	if `case'==4{
	mat	`uno7'=	( 4.28,3.60\ /*
				*/ 4.26,3.58\ /*
				*/ 4.13,3.55\ /*
				*/ 4.12,3.55\ /*
				*/ 4.09,3.54 )
	}
	if `case'==5{
	mat	`uno7'=	( 6.53,5.71\ /*
				*/ 6.47,5.68\ /*
				*/ 6.33,5.56\ /*
				*/ 6.38,5.61\ /*
				*/ 6.27,5.55 )
	}
	mat `uno7'=(J(5,1,.),`uno7',J(5,1,.))

matrix crthegy  = (`uno1'[`r',1..4] )
matrix crthegy  = (crthegy  \ `uno2'[`r',1..4])
matrix crthegy  = (crthegy  \ `uno3'[`r',1..4])
matrix crthegy  = (crthegy  \ `uno4'[`r',1..4])
matrix crthegy  = (crthegy  \ `uno5'[`r',1..4])
matrix crthegy  = (crthegy  \ `uno6'[`r',1..4])
matrix crthegy  = (crthegy  \ `uno7'[`r',1..4])


end


program define DispReg
        args case dvar lags b1 s1 b2 s2 b3 s3 b4 s4 tab matseas tt
        di in smcl in gr "{hline 13}{c TT}{hline 64}"
        di in smcl in gr abbrev("`dvar'",12) _col(14) "{c |}" /*
                */ _col(21) "Coef." _col(29) "Std. Err." _col(44) "t" /*
                */ _col(49) "P>|t|" _col(59) "[95% Conf. Interval]"
        di in smcl in gr "{hline 13}{c +}{hline 64}"
        di in smcl in gr %12s abbrev("`dvar'",12) _col(14) "{c |}"

	di in smcl in gr _col(7) "Freq.0 {c |}" in ye /*
                */ _col(17) %9.0g `b1' /*
                */ _col(28) %9.0g `s1' /*
                */ _col(38) %8.2f `b1'/`s1' /*
                */ _col(48) %6.3f tprob(e(df_r),`b1'/`s1') /*
                */ _col(58) %9.0g `b1' - invt(`e(df_r)',$S_level/100)*`s1' /*
                */ _col(70) %9.0g `b1' + invt(`e(df_r)',$S_level/100)*`s1'

	di in smcl in gr _col(5) "Freq.1/2 {c |}" in ye /*
                */ _col(17) %9.0g `b2' /*
                */ _col(28) %9.0g `s2' /*
                */ _col(38) %8.2f `b2'/`s2' /*
                */ _col(48) %6.3f tprob(e(df_r),`b2'/`s2') /*
                */ _col(58) %9.0g `b2' - invt(`e(df_r)',$S_level/100)*`s2' /*
                */ _col(70) %9.0g `b2' + invt(`e(df_r)',$S_level/100)*`s2'

	di in smcl in gr _col(5) "L.Annual {c |}" in ye /*
                */ _col(17) %9.0g `b3' /*
                */ _col(28) %9.0g `s3' /*
                */ _col(38) %8.2f `b3'/`s3' /*
                */ _col(48) %6.3f tprob(e(df_r),`b3'/`s3') /*
                */ _col(58) %9.0g `b3' - invt(`e(df_r)',$S_level/100)*`s3' /*
                */ _col(70) %9.0g `b3' + invt(`e(df_r)',$S_level/100)*`s3'

	di in smcl in gr _col(7) "Annual {c |}" in ye /*
                */ _col(17) %9.0g `b4' /*
                */ _col(28) %9.0g `s4' /*
                */ _col(38) %8.2f `b4'/`s4' /*
                */ _col(48) %6.3f tprob(e(df_r),`b4'/`s4') /*
                */ _col(58) %9.0g `b4' - invt(`e(df_r)',$S_level/100)*`s4' /*
                */ _col(70) %9.0g `b4' + invt(`e(df_r)',$S_level/100)*`s4'

	if `lags' >= 1 {
			local bv=`tab'[1,1]
			local sv=`tab'[1,2]
		di in smcl in gr _col(10) "LD. {c |}" in ye /*
			*/ _col(17) %9.0g `bv' /*
			*/ _col(28) %9.0g `sv' /*
			*/ _col(38) %8.2f `bv'/`sv' /*
			*/ _col(48) %6.3f tprob(e(df_r),`bv'/`sv') /*
			*/ _col(58) %9.0g `bv' - invt(`e(df_r)',/*
			*/ $S_level/100)*`sv' /*
			*/ _col(70) %9.0g `bv' + invt(`e(df_r)',/*
			*/ $S_level/100)*`sv'
	}

	local i 2
	while `i' <= `lags' {
			local bv=`tab'[`i',1]
			local sv=`tab'[`i',2]
		di in smcl in gr %12s "L`i'D." " {c |}" in ye /*
			*/ _col(17) %9.0g `bv' /*
			*/ _col(28) %9.0g `sv' /*
			*/ _col(38) %8.2f `bv'/`sv' /*
			*/ _col(48) %6.3f tprob(e(df_r),`bv'/`sv') /*
			*/ _col(58) %9.0g `bv' - invt(`e(df_r)',/*
			*/ $S_level/100)*`sv' /*
			*/ _col(70) %9.0g `bv' + invt(`e(df_r)',/*
			*/ $S_level/100)*`sv'
		local i = `i'+1
	}
	if `case'==4 |  `case'==5 {
	        local vv "`tt'"
      	  local bv "_b[`vv']"
	        local sv "_se[`vv']"
		di in smcl in gr %12s "_trend" _col(14) "{c |}" in ye /*
            	    */ _col(17) %9.0g `bv' /*
	                */ _col(28) %9.0g `sv' /*
      	          */ _col(38) %8.2f `bv'/`sv' /*
            	    */ _col(48) %6.3f tprob(e(df_r),`bv'/`sv') /*
	                */ _col(58) %9.0g `bv' - invt(`e(df_r)',$S_level/100)*`sv' /*
      	          */ _col(70) %9.0g `bv' + invt(`e(df_r)',$S_level/100)*`sv'
	}
	if `case'==3 | `case'==5 {
		forvalues i=1/3 {
			local bv=`matseas'[`i',1]
			local sv=`matseas'[`i',2]
		di in smcl in gr %12s "Q`i'" " {c |}" in ye /*
			*/ _col(17) %9.0g `bv' /*
			*/ _col(28) %9.0g `sv' /*
			*/ _col(38) %8.2f `bv'/`sv' /*
			*/ _col(48) %6.3f tprob(e(df_r),`bv'/`sv') /*
			*/ _col(58) %9.0g `bv' - invt(`e(df_r)',/*
			*/ $S_level/100)*`sv' /*
			*/ _col(70) %9.0g `bv' + invt(`e(df_r)',/*
			*/ $S_level/100)*`sv'
		}
	}
	if `case'!=1 {
	        local vv "_cons"
      	  local bv "_b[`vv']"
	        local sv "_se[`vv']"
		di in smcl in gr %12s "_cons" _col(14) "{c |}" in ye /*
            	    */ _col(17) %9.0g `bv' /*
	                */ _col(28) %9.0g `sv' /*
      	          */ _col(38) %8.2f `bv'/`sv' /*
            	    */ _col(48) %6.3f tprob(e(df_r),`bv'/`sv') /*
	                */ _col(58) %9.0g `bv' - invt(`e(df_r)',$S_level/100)*`sv' /*
      	          */ _col(70) %9.0g `bv' + invt(`e(df_r)',$S_level/100)*`sv'
	}
		di in smcl in gr "{hline 13}{c BT}{hline 64}"
end