clear
set more on
set matsize 800
* help spatreg - `findit spatreg' if necessary
help spatreg
more
spatwmat using http://www-personal.umich.edu/~franzese/oxford_w.dta, name(W) standardize eigenval(E)
use http://www-personal.umich.edu/~franzese/oxford_data.dta, clear
more
* All Vars=average 1986-1990 in 48 contiguous US states
* DepVar=ben95=maximum monthly AFDC benefit
* IndVars=
* rskpovpc=state’s poverty rate
* wage95=average monthly wage in the retail sector
* instcoad=government ideology (0=right to 100=left)
* ipcfold=degree of interparty competition (.5=competitive to 1.0=non-competitive
* teitrend=tax effort (revenues as a percentage of tax “capacity”)
* match=portion of AFDC benefits paid by the federal government
* Spatial Weights=W=row-standardized binary contiguity-weights matrix.
* Calculate global indicators of spatial association
spatgsa ben95, w(W) m g two
more
* Calculate local indicators of spatial association
spatlsa ben95, w(W) m g id(statenm) two
more
* OLS regression...
regress ben95 rskpovpc wage95 instcoad ipcfold teitrend match
more
* ...then check spatial diagnostics and LM tests
spatdiag, weights(W)
more
* the spatial-error model; note the LM tests...
spatreg ben95 rskpovpc wage95 instcoad ipcfold teitrend match, weights(W) eigenval(E) model(error)
more
* the spatial-lag model; note the LM tests, compare to LMs from spatial-error model...
spatreg ben95 rskpovpc wage95 instcoad ipcfold teitrend match, weights(W) eigenval(E) model(lag)
more
*****Calculate Cross-Sectional Spatial Effects*****
***First with Standard Errors by Parametric Bootstrap***
local nobs = 48 /* Number of Observations in the Cross-Section */
local Y ben95 /* Dependent Variable */
local X ipcfold /* Independent Variable for Counterfactual Analysis: ipc */
local rho _b[rho:_cons] /* Estimated coefficient on Spatial Lag */
local beta _b[`Y':`X'] /* Estimated coefficient on Independent Variable of Interest */
matrix coeff = [`rho',`beta'] /* Convert rho and beta estimates to a vector called `coeff' */
matrix eye = I(`nobs') /* Make an NxN identity matrix called `eye' */
***Standard Errors by Parametric Simulation***
matrix V = e(V) /* Estimated VCov(B) as matrix `V' */
matrix VCVM = [V[8,8], V[8,4] \ V[4,8], V[4,4]] /* Extract relevant components of `V', NOTE: rho is last */
local draw = 800 /* Number of simulations to draw */
drop _all /* Clear some space in memory */
corr2data rho_drw beta_drw, n(`draw') means(coeff) cov(VCVM) /* Draw your simulated coefficient estimates */
mkmat rho_drw-beta_drw, matrix(draws) /* Convert those simulated coefficient estimates to matrices */
drop _all /* Clear some space in memory */
local i = 1
while `i' < `draw'+1 {
matrix estcf1 = eye - draws[`i',1]*W /* Make matrix `estcf1' as I-rhoW -- looping to get 1 per draw */
matrix estcf = draws[`i',2]*inv(estcf1) /* Make matrix `estcf' as inverse(I-rhoW)b -- looping to get 1 per draw */
matrix estcf_col = estcf[1...,1] /* NOTE: change column indicator for each unit -- it gives response across all N to d(X) in that unit, now 1 */
svmat estcf_col, n(estcf_col`i') /* Save those `draws' simulations of those N estimates as variables estcf_col`i' */
mat drop estcf1 estcf estcf_col /* Drop these matrices to clear some space in memory */
local i = `i' + 1
}
mkmat estcf_col11-estcf_col`draw'1, matrix(estcf_set) /* the simulations are currently cols and N responses rows */
matrix estcf_set_tr = estcf_set' /* so these two lines convert to matrix and transpose */
drop _all /* Clear some space in memory */
svmat estcf_set_tr, n(estcf) /* then save that matrix as N variables of `draws' simulations */
* then generate whatever descriptive statistics of those estimates you may wish *
* NOTE: if have enough memory, can leave untransposed and use egen meancf=rowmean() etc. as seen before *
more
egen estcf1on8mean=mean(estcf8) /* mean of simulated est'd responses 8 to 1 */
egen estcf1on8med=median(estcf8) /* median of simulated est'd responses 8 to 1 */
egen estcf1on8tenth=pctile(estcf8) , p(10) /* tenth percentile of simulated est'd responses 8 to 1 */
egen estcf1on8sd=sd(estcf8) /* sd of simulated est'd responses 8 to 1 */
gen estcf1on8tstats=estcf1on8mean/estcf1on8sd /* t-stat of simulated est'd responses 8 to 1 */
summarize estcf1on8mean estcf1on8med estcf1on8tenth estcf1on8sd estcf1on8tstats
* plot histogram of those simulated est'd responses 8 to 1
more
hist estcf8
more
***Standard Errors by Delta Method***
matrix estcf1_DM = eye - `rho'*W /* ...same as before... */
matrix estcf_DM = `beta'*inv(estcf1_DM) /* ...same as before... */
matrix d_cf_rho1 = estcf_DM*W*inv(estcf1_DM) /* ...this is d(estcf)d(rho)... */
matrix d_cf_beta1 = inv(estcf1_DM) /* ...this is d(estcf)d(beta)... */
matrix V = e(V) /* Estimated VCov(B) as matrix `V' */
matrix VCVM = [V[8,8], V[8,4] \ V[4,8], V[4,4]] /* Extract relevant components of `V', NOTE: rho is last */
svmat estcf_DM, n(estcf_DM) /* save estimated-effects matrix as variables */
*** NOTE: In this formulation, each column or variable of estcf_DM is effect on all N of d(X) in that column unit ***
local i = 1
while `i' < `nobs'+1 {
matrix d_cf_rho = d_cf_rho1[1...,`i'] /* `i'th column of d(estcf)d(rho) */
matrix d_cf_beta = d_cf_beta1[1...,`i'] /* `i'th column of d(estcf)d(beta) */
matrix d_cf = [d_cf_rho, d_cf_beta] /* arrange as vector */
matrix delta1 = d_cf*VCVM*d_cf' /* DM estimate VCov of these N responses to d(x) in `i' */
matrix DMvariances = vecdiag(delta1)' /* diagonal elements of which are the est'd variances those N responses */
svmat DMvariances, n(DMvariances`i') /* convert those variance estimates to a variable */
local i = `i' + 1 /* loop and you get all Nx{i=1..N} estimated variances for all Nx{i=1..N} responses */
}
local i = 1
while `i' < `nobs'+1 {
gen DMse`i'=(DMvariances`i'1)^.5 /* convert those NxN of variance estimates to standard errors */
gen DMtstats`i'=estcf_DM`i'/DMse`i' /* calculate NxN matrix of t-statistics */
local i = `i' + 1
}
drop DMvariance* /* Clear some space in memory by dropping the variances */
list estcf_DM1 DMse1 DMtstats1 if _n<48.5
* Compare to estimates and estimated certainties from parametric bootstrap *
summarize estcf1on8mean estcf1on8sd estcf1on8tstats