Spatial Autocorrelation between time series in R

772 Views Asked by At

So I'm using the spdep package in R to do this. I need to use the localmoran function in it to get clusters of hot/cold spots in the study area.

After going through the reference guide, I got it to work.

But since the data is a time series, now I'm not quite certain. My data is about rainfall by the way, measured at 7 cities over the years 2006-2016.

For the simplest example I can think of I will scale down this problem. Suppose I have this dataframe, let's call it test.

           city1        city2        city3
day_1    1.09765      0.89634     37.09863
day_2   14.98641      0.97531      5.97451
day_3    1.97551     12.18713      7.97512

According to the reference guide, I need to make a listw object, which I did.

coord <- read_csv("myCSV.csv") #this is just a matrix of the x and y coordinates of the cities
coordinates(coord) <- ~ y + x
proj4string(coord) <- CRS("+init=epsg:4326")
mapview(coord)
as.data.frame(coord)

knn <- knearneigh(coord, k=2, longlat = NULL, RANN = TRUE)

nb <- knn2nb(knn, row.names = NULL, sym = TRUE)

listw <- nb2listw(nb, style="W")

The localmoran function accepts 2 parameters at the minimum, a numeric vector and a listw object. The numeric vector must be the same length as the listw object (in the case of this scaled down example, 3).

My problem now is that since it accepts a numeric vector, I can only pass in one row like so:

localmoran(as.numeric(test[1, ]), listw)

But that only takes into account the first day in the time series. I'd have to loop through the data to get the localmoran index of the other days, like so.

for(i in c(1:3)) {
  final <- as.data.frame(localmoran(as.numeric(test[i, ]), listw))
}

Now I'm going to depart a little bit by talking about the example in the reference guide. The example only takes ONE numeric vector, likely just ONE instance of that variable. But since mine was taken at multiple instances (days), I deviate a lot from the example, and now I'm both confused and afraid.

And again departing a little bit further by talking about statistics, since I need just ONE value for localmoran that spans the entire time span, how do I collapse the localmoran values at each day into one? Averaging? Fisher's Transformation? etc?

Does the spdep package even consider time? Is there a package somewhere in R that does use time?

I am scared.

Thanks to anyone who replies.

(Please help me)

0

There are 0 best solutions below