0

I'm trying to do the statistical analysis by using the Moran's I, but it leads me into a serious problem.

Suppose the data look like:

y indep1 indep2 coord_x coord_y District
y1 indep1 1 indep2 1 coord_x 1 coord_y 1 A
y2 indep1 2 indep2 2 coord_x 1 coord_y 1 A
y3 indep1 3 indep2 3 coord_x 1 coord_y 1 A
y4 indep1 4 indep2 4 coord_x 2 coord_y 2 B
y5 indep1 5 indep2 5 coord_x 2 coord_y 2 B

Note that the data is given as form of *.shp.

This dataframe has 2 unique districts, but total 5 rows of data. If we want to calculate the Moran's I, then the weighted matrix W is 2 by 2.

But If I run the code

ols <- lm(y~indep1+indep2, data=dataset)

We cannot calculate the

lm.morantest(ols, w) #w:weighted matrix
# returns "different length"

How can we solve that problem? IF the total number of data and the number of unique districts are different, then how can we check the spatial auto-correlation between the districts and how can we apply GWR(geographically weighted regression?) Any reference papers or your advises could be helpful.

Thank you for your help in advance.

I tried to calculate the moran's I by making the number our dependent variables same as the number of unique districts. It can be possible when aggregating the sum of dependent variables with respect to the districts.

library(reshape2)
y_agg <- dcast(shp@data, district~., value.var="dependent variable", sum)
y_agg <- y_agg$.
moran.test(y_agg, W)

But I think it is not the right way to analyze the spatial regression since all the other independent variables are ignored. How can I solve that problem? Is there any way for solving that problem without making the aggregated independent variables of my data?

Thank you.

0 Answers0