Hello everyone
I was wondering if I could some get help to figure out the problem below:
I'm trying to run this code available on Feenstra (2014, In Search of the Armington Elasticity) documentation
But stata issued the following error message: invalid syntax(198)
I was wondering if I could some get help to figure out the problem below:
I'm trying to run this code available on Feenstra (2014, In Search of the Armington Elasticity) documentation
But stata issued the following error message: invalid syntax(198)
Code:
*2SLS IV
forvalues i=1/`indnum' {
preserve
keep if loopindustry==`i'
*for Sato-Vartia price indicator
qui tab ccode, gen(c_I_)
foreach var of varlist x1sv_ij x2sv_ij {
gen `var'hat = `var'
sum `var'hat if loopind == `i'
*replace `var'hat = `var'hat - r(mean) if loopind == `i'
}
*Run 2sls regression for sigma
ivregress 2sls ysv_ij (x1sv_ijhat x2sv_ijhat = c_I_*) , small
gen conssv=_b[_cons]
gen thetasv1=_b[x1sv_ijhat]
gen thetasv2=_b[x2sv_ijhat]
estat vce /* VAR-COV TO BE USED TO CALCULATE CI'S FOR SIGMA */
gen rho1sv = .5 + sqrt(.25 - 1/(4+thetasv2^2/thetasv1)) if thetasv2>0
replace rho1sv = .5 - sqrt(.25 - 1/(4+thetasv2^2/thetasv1)) if thetasv2<0
gen sigmasv = 1 + (2*rho1sv-1)/((1-rho1sv)*thetasv2)
******************************************************
* Get standard error of Sigma
* using Taylor series expansion aka Delta method
* (not used in published paper)
******************************************************
gen sigmasvse=0
cap if thetasv2>0 {
testnl 1 + (2* (.5 + sqrt(.25 - 1/(4+_b[x2sv_ijhat]^2/_b[x1sv_ijhat]))) -1)/((1- (.5 + sqrt(.25 - 1/(4+_b[x2sv_ijhat]^2/_b[x1sv_ijhat]))) )* _b[x2sv_ijhat] )=0
replace sigmasvse=sigmasv/sqrt(r(F))
}
cap if thetasv2<0 {
testnl 1 + (2* (.5 - sqrt(.25 - 1/(4+_b[x2sv_ijhat]^2/_b[x1sv_ijhat]))) -1)/((1- (.5 - sqrt(.25 - 1/(4+_b[x2sv_ijhat]^2/_b[x1sv_ijhat]))) )* _b[x2sv_ijhat] )=0
replace sigmasvse=sigmasv/sqrt(r(F))
}
mkmat groupcom industrycountry thetasv1 thetasv2 rho1sv sigmasv sigmasvse , matrix(SV`i')
matrix bSV`i'=nullmat(bSV`i')\SV`i'
* Generate optimal weights
gen uhatsv = ysv_ij - conssv - thetasv1 * x1sv_ij - thetasv2 * x2sv_ij
* Generate squared error term: we subtract the mean of error by country and good.
egen uhatsvmean = mean(uhatsv)
replace uhatsv = uhatsv-uhatsvmean
gen uhatsv2=uhatsv^2
drop if uhatsv2==.
qui regress uhatsv2 c_I_*, noc
predict uhatsv2hat
gen ssvhat=sqrt(uhatsv2hat)
* Weight data and reestimate
gen ones=1
foreach var of varlist ysv_ij x1sv_ijhat x2sv_ijhat ones {
gen `var'svstar = `var'/ssvhat
}
ivregress 2sls ysv_ijsvstar onessvstar (x1sv_ijhatsvstar x2sv_ijhatsvstar = c_I_*) , small noc
gen thetasv1w=_b[x1sv_ijhatsvstar ]
gen thetasv2w=_b[x2sv_ijhatsvstar ]
estat vce
gen rho1svw = .5 + sqrt(.25 - 1/(4+thetasv2w^2/thetasv1w)) if thetasv2w>0
replace rho1svw = .5 - sqrt(.25 - 1/(4+thetasv2w^2/thetasv1w)) if thetasv2w<0
gen sigmasvw = 1 + (2*rho1svw-1)/((1-rho1svw)*thetasv2w)
sum groupcom num thetasv1w thetasv2w rho1svw sigmasvw
*standard error of sigma using delta method (not used in published version of the paper)
cap gen sigmasvwse=0
cap if thetasv2w >0 {
testnl 1 + (2* (.5 + sqrt(.25 - 1/(4+_b[x2sv_ijhatsvstar ]^2/_b[x1sv_ijhatsvstar ]))) -1)/((1- (.5 + sqrt(.25 - 1/(4+_b[x2sv_ijhatsvstar ]^2/_b[x1sv_ijhatsvstar ]))) )* _b[x2sv_ijhatsvstar ] )=0
replace sigmasvwse=sigmasvw/sqrt(r(F))
}
cap if thetasv2w<0 {
testnl 1 + (2* (.5 + sqrt(.25 - 1/(4+_b[x2sv_ijhatsvstar ]^2/_b[x1sv_ijhatsvstar ]))) -1)/((1- (.5 + sqrt(.25 - 1/(4+_b[x2sv_ijhatsvstar ]^2/_b[x1sv_ijhatsvstar ]))) )* _b[x2sv_ijhatsvstar ] )=0
replace sigmasvwse=sigmasvw/sqrt(r(F))
}
mkmat thetasv1w thetasv2w rho1svw sigmasvw sigmasvwse ssvhat , matrix(SVw`i')
matrix bSVw`i'=nullmat(bSVw`i')\SVw`i'
restore
}

Comment