Dear Statlist,
I'm currently following the useful presentation of Maurizio Pisati, titled "Exploratory spatial data analysis using Stata". I have generated my measures of local spatial autocorrelation, and would now like to plot them on a scatterplot and map them. However, I'm running into some issues when trying to generate a scatterplot. As the presentation states;
I have tried to following the code which is outlined but I'm not sure if my commands are correct
MY variable is count and the weight matrix is called W.
I wonder should the above section be amended to;
Any help would be appreciated
Sean
I'm currently following the useful presentation of Maurizio Pisati, titled "Exploratory spatial data analysis using Stata". I have generated my measures of local spatial autocorrelation, and would now like to plot them on a scatterplot and map them. However, I'm running into some issues when trying to generate a scatterplot. As the presentation states;
We use Stata command genmsp { a simple wrapper to spatlsa
(source code in Appendix) { to generate the variables required for
drawing the Moran scatterplot and the corresponding cluster map
(Anselin 1995)
(source code in Appendix) { to generate the variables required for
drawing the Moran scatterplot and the corresponding cluster map
(Anselin 1995)
Code:
*Setting the matrix
spatwmat using leamatrix.dta, name(W) standardize
*Testing for spatial autocorrelation
spatgsa count pop19p unemploymentrate higherpropsp, w(W) moran geary
*Testing for local spatial autocorrealtion
spatlsa count, w(W) moran id(geogdesc) sort
*Generating the scatterplot
program genmsp, sortpreserve
version 12.1
syntax varname, Weights(name) [Pvalue(real 0.05)]
unab Y : `varlist'
tempname W
matrix `W' = `weights'
tempvar Z
qui summarize `Y'
qui generate `Z' = (`Y' - r(mean)) / sqrt( r(Var) * ( (r(N)-1) / r(N) ) )
qui cap drop std_`Y'
qui generate std_`Y' = `Z'
tempname z Wz
qui mkmat `Z', matrix(`z')
matrix `Wz' = `W'*`z'
matrix colnames `Wz' = Wstd_`Y'
qui cap drop Wstd_`Y'
qui svmat `Wz', names(col)
qui spatlsa `Y', w(`W') moran
tempname M
matrix `M' = r(Moran)
matrix colnames `M' = __c1 __c2 __c3 zval_`Y' pval_`Y'
qui cap drop __c1 __c2 __c3
qui cap drop zval_`Y'
qui cap drop pval_`Y'
qui svmat `M', names(col)
qui cap drop __c1 __c2 __c3
qui cap drop msp_`Y'
qui generate msp_`Y' = .
qui replace msp_`Y' = 1 if std_`Y'<0 & Wstd_`Y'<0 & pval_`Y'<`pvalue'
qui replace msp_`Y' = 2 if std_`Y'<0 & Wstd_`Y'>0 & pval_`Y'<`pvalue'
qui replace msp_`Y' = 3 if std_`Y'>0 & Wstd_`Y'<0 & pval_`Y'<`pvalue'
qui replace msp_`Y' = 4 if std_`Y'>0 & Wstd_`Y'>0 & pval_`Y'<`pvalue'
lab def __msp 1 "Low-Low" 2 "Low-High" 3 "High-Low" 4 "High-High", modify
lab val msp_`Y' __msp
end
exit
graph twoway(scatter Wstd_Y std_Y
if pval_Y >=0.05,
msymbol(i) mlabel(name) mlabsize(*0.6) mlabpos(c))
(scatter Wstd_Y std_Y
if pval_Y < 0.05,
msymbol(i) mlabel(name) mlabsize(*0.6) mlabpos(c)
mlabcol(red))
(lfit Wstd_Y std_Y),
yline(0, lpatttern(--)) xline(0, lpattern(--))
xlabel(-2(1)4, labsize(*0.8)) xline(0, lpattern(--))
ylabel(-2(1)3, angle(0) labsize(*0.8))
ytitle("{it:Wz}") legend(off) scheme(s1color)
I wonder should the above section be amended to;
Code:
syntax count, Weights(W) [Pvalue(real 0.05)] unab Y : `count' tempname W matrix `W' = `W' tempvar Z
Sean

Comment