set more off
cd C:\Users\s5197351\Dropbox\Charles
insheet using Paneldata.csv, clear
cap log close
log using Paneldata.txt, replace
* trend variable
gen trend=year-1979
la var trend "Linear trend"
* Registry year: 2012 (USA), 2003 (UK), 1999(NZ), 2006 (ITA), 1980 (Finland, Sweden), 1994 (Norway), 1995 (Denmark), 1999 (Australia)
* Thus, take the median of 1999 to define registry year for non-registry countries
replace registry_period=1 if (registry_country==0 & year>=1999)
*Create interaction between registry_period and registry_country
gen rprc=registry_period*registry_country
*Revision rate
gen rev_rate=revision*100/ (primary +revision)
la var rev_rate "Revision rate"
*Create country id
encode country, gen(countryid)
* Set panel
preserve
keep if type==1
xtset countryid year, yearly


mat hausmat=J(5,2,0)
mat rowname hausmat= Y1994 Y1999 Y2003 Y2006 Y2012
mat colname hausmat= Chi2 Pvalue
local i=1
* primary and sensitivity tests
foreach num of numlist 1994 1999 2003 2006 2012 {
	replace registry_period=0 if registry_country==0
	replace registry_period=1 if (year>=`num' & registry_country==0)
	replace rprc=registry_period*registry_country
	*Hip fixed effects
	xtreg rev_rate trend registry_country registry_period rprc,fe
	est store hip_fe`num'
	*Hip-random effects
	xtreg rev_rate trend registry_country registry_period rprc
	est store hip_re`num'
	hausman hip_fe`num' hip_re`num', sigmamore
	mat hausmat[`i',1]=r(chi2)
	mat hausmat[`i',2]=r(p)
	local i=`i'+1
}
xml_tab hip_fe*, save("Result.xml") replace format(SCLR3 NCLR2)  sheet(Fixed effects - Hip)
xml_tab hip_re*, save("Result.xml") append format(SCLR3 NCLR2)  sheet(Random effects - Hip)
xml_tab hausmat, save("Result.xml") append format(SCLR3 NCLR2)  sheet(Hausman  - Hip)

***Knees
restore
keep if type==0

xtset countryid year, yearly
mat hausmat=J(5,2,0)
mat rowname hausmat= Y1994 Y1999 Y2003 Y2006 Y2012
mat colname hausmat= Chi2 Pvalue
local i=1
* primary and sensitivity tests
foreach num of numlist 1994 1999 2003 2006 2012 {
	replace registry_period=0 if registry_country==0
	replace registry_period=1 if (year>=`num' & registry_country==0)
	replace rprc=registry_period*registry_country
	*Knee fixed effects
	xtreg rev_rate trend registry_country registry_period rprc,fe
	est store knee_fe`num'
	*Knee Random effects
	xtreg rev_rate trend registry_country registry_period rprc
	est store knee_re`num'
	hausman knee_fe`num' knee_re`num', sigmamore
	mat hausmat[`i',1]=r(chi2)
	mat hausmat[`i',2]=r(p)
	local i=`i'+1
}
xml_tab knee_fe*, save("Result.xml") append format(SCLR3 NCLR2)  sheet(Fixed effects - Knee)
xml_tab knee_re*, save("Result.xml") append format(SCLR3 NCLR2)  sheet(Random effects - Knee)
xml_tab hausmat, save("Result.xml") append format(SCLR3 NCLR2)  sheet(Hausman  - Knee)

log close
