'This program runs PDL regressions, with no constraint specified, over a number of possible lags for an individual form of profit share. The lags considered are a range of 2 to 12 for !OL, !TSAL and !TFPL.
''Change !D=_  to alter the defree of the polynomial distributions in each regression.
'This program stores in column vectors of length 1331: Durbin Watson, Akaike, Schwarz, Sum of Lag Coefficients for TSASPL, critical values of ipr and union for partial derivatives of PS wrt ipr,un and union


'Open Data
open "\\microwave\RESEARCH\GoldRA\Jordan14\Goldstein\finprsq_new.wf1"
'Create 1331 equations to store regression results for ps1 with all possible lags
for !j=1 to 64
	equation eq!j
next

'Want Sum of Lags for TSASPL, TFP4 
'First, create 1331 matrices that will contain all pdl lag coefficients for each equation, this will be continued in the lag loops
for !j=1 to 64
	vector(200) coefs!j
next
'Create space to store sum of lag coefficients and critical values for partial derivatices of each equation, this will be continued in the lag loops
vector(1331) tsaspllagsPS1
vector(1331)tfp4lagsPS1
vector(1331) dpsdunPS1
vector(1331) dpsdunionPS1
vector(1331) dpsdiprPS1
vector(1331) tstattsasplPS1
vector(1331) tstattfp4ps1
scalar z 'sum variable
scalar n 'additional sum variable
scalar w 'additional sum variable
!X=1 'equation counter X
!D=3 'degree of polynomial
!C=1 'constraint
for !OL=3 to 6		'For other lag
	for !TFPL=3 to 6 	'For tfp lag
		for !TSAL=3 to 6		'for tsa lag

smpl 1980:1 2006:4
		eq!X.ls(cov=hac) ps1 c time pdl(ipr,!OL,!D,!C) pdl(un,!OL,!D,!C) pdl(unionprq,!OL,!D,!C) pdl(unipr,!OL,!D,!C) pdl(unionipr,!OL,!D,!C) pdl(tsaspl,!TSAL,!D,!C) pdl(tfp4,!TFPL,!D,!C)
		coefs!X=eq!X.@coefs 'fills in coef vector for each equation, 1 at a time as program iterates through different lags
			for !i = 3 + (!D*7) + ((!OL+1)*5) to 2 + (!D*7) + ((1+!OL)*5) +!TSAL+1 'No constraint Degree=2, iterates over tsaspl lag coefficients
			z= coefs!X(!i) + z				'z sums the lag coefficients for tsaspl
			next
		tsaspllagsPS1(!X)=z					'the sum of lag coefficients for tsaspl is stored, for equation 1 through 1331, in the tsaspllags vector
		z=0 'reinitialize z as 0
'This calculates sum of lag coefs for tfp 4
			for !i = 3 + (!D*7) + ((!OL+1)*5) + (!TSAL+1) to 2 + (!D*7) + ((1+!OL)*5) + !TSAL + 1 + !TFPL + 1
			z = coefs!X(!i) + z
			next
		tfp4lagsPS1(!X)=z
		z=0
'This calculates the critical values of dps/dun for each equation
			for !i = 3 + (!D*7) +(!OL+1) to 2+ (!D*7) +((!OL+1)*2) 'sum of lag coefficients for un stored in z
			z= coefs!X(!i) + z
			next
			for !i = 3 +(!D*7) + ((!OL+1)*3) to 2 + (!D*7) + ((!OL+1)*4) ' sum of lag coefficients for unipr stored in n
			n= coefs!X(!i) + n
			next
		dpsdunPS1(!X) = (-z)/n						'calculation and storage of critical value of ipr
		z=0'reset calculator values
		n=0
'This calculates the critical values of dps/dunion for each equation
			for !i = 3 + (!D*7) +((!OL+1)*2) to 2+ (!D*7) +((!OL+1)*3) 'sum of lag coefficients for union stored in z
			z= coefs!X(!i) + z
			next
			for !i = 3 + (!D*7) +((!OL+1)*4) to 2+ (!D*7) +((!OL+1)*5) 'sum of lag coefficients for unionipr stored in n
			n= coefs!X(!i) + n
			next
		dpsdunionPS1(!X) = (-z)/n
		z=0
		n=0
'This calculates the critical values of dps/dipr for each equation (assumes un=6)
			for !i = 3+ (!D*7) to 2 + (!D*7) + (!OL+1) 'sum of lag coefficients for ipr stored in z
			z=coefs!X(!i) + z
			next
			for !i = 3+(!D*7) + ((!OL+1)*3) to 2 + (!D*7) + ((!OL+1)*4) 'sum of lag coefficients for unipr stored in n (assumes un = 6)
			n= coefs!X(!i) + n
			next
			n= n*6
			for !i = 3 + (!D*7) + ((!OL+1)*4) to 2 + (!D*7) + ((!OL+1)*5) 'sum of lag coefficients for unionipr stored in w
			w = coefs!X(!i) + w
			next
		dpsdiprPS1(!X)= (-z - n)/w
		z=0
		n=0
		w=0
' This calculates the sum of lagged t statistics for a number of variables
			'freeze(tablet!X) eq!X																	'stores output of equation !X in tablet!X
			'!tsa=19 + 2 + (!D*7) + ((3 + !OL+1 + 2)*5) + (3 + !TSAL + 1 + 2)
			'!tfp= 19 + 2 + (!D*7) + ((3 + !OL+1 + 2)*5) + (3 + !TSAL + 1 + 2) + (3 + !TFPL + 1 + 2)
			'tstattsasplps1(!X)=tablet!X(!tsa,6)		'stores sum of lagged t stats for tsaspl for eq!X in tstat(!X)
			'tstattfp4ps1(!X)=tablet!X(!tfp,6)			'stores for tfp4
	!X=!X+1	'move to next equation
		
next
next
next

'store some summary statistics
vector(1331) durbinwatsonPS1
for !j=1 to 64
	durbinwatsonPS1(!j)= eq!j.@dw
next
vector(1331) akaikePS1
for !j=1 to 64
	akaikePS1(!j)= eq!j.@aic
next
vector(1331) schwarzPS1
for !j=1 to 64
	schwarzPS1(!j)= eq!j.@schwarz
next
vector(1331) qstat
for !j=1 to 64
freeze(table!j) eq!j.correl(36,byser) 'creates 24 tables of correlograms
qstat(!j)= table!j(20,7)					'stores P(q-stat 14) from each table in column vector
next

