Suppose we have a data in the long form where each observation is the crime rate in a city in a year. What I want is a heatmap where each cell is the average correlation of crimes of cities in the two states. For example, if Texas has 4 cities and California 5 then it will average of 20 numbers.
I know I can achieve the heatmap of the average crime rates of different states this way:
But what I want is different. I want to average over all pairwise city-wise correlations grouped by states.
I know I can achieve the heatmap of the average crime rates of different states this way:
Code:
collapse (mean) crime , by(year state)
reshape wide crime , i(y) j(region) string
pwcorr crimeAL-crimeWY
return list
matrix corrmatrix = r(C)
heatplot corrmatrix , lower nodiag title("Correlation Matrix Of Average Crime Rates") color(hcl diverging, intensity(.7))

Comment