edzer / trajectories

Handling and analyzing trajectory and movement data
31 stars 15 forks source link

Calculating chi maps from simulated corridors #23

Open bniebuhr opened 5 years ago

bniebuhr commented 5 years ago

Dear trajectories developers (and users),

First of all, congrats for the package. The general ideia is simple and great, and I found this chi square maps particularly interesting, since I haven't seen that in a spatial form before. I have simulated ecological corridors, for a given species, in two different land use scenarios. I am willing to compare where their simulated tracks differ using chi maps. The case is a little different from the vignette, since we do not have individuals, but simulated paths in different scenarios. Also, we don't have time (it is implicit), but I thought about considering each scenario as times far away from each other, so that they may be compared. Do you think this is possible?

This is what I did:

Each simulated corridor is a shapefile. I have 1000 simulated corridors for each land use scenario. First I resampled each of them as equally distance points along the paths, so that each corridor may be considered as sequences of points and the spatial point processes may be applied.

# Sample of code, after importing corridors
nn  <-  round(gLength(corridor) / 300)
pts <- spsample(corridor, n = nn, type = "regular")

# Here I added time in a random way, but I am not sure how should I have done that  
t0 <- as.POSIXct(as.Date('2019-01-02 00:00', tz = 'UTC')) + 100*j # j is the index for corridor number
df <- data.frame(scenario = rep(1, nn), corr = rep(j, nn), t = t0 + 1:nn)
spdf <- SpatialPointsDataFrame(pts, df) 

# This piece of code was included inside a loop over all corridors

I did not know how to include time, so I considered each location as 1s for the previous, and each corridor stating in different moments in time, to avoid overlap. Now, I had a list with all 1000 corridors for a single land use scenario: spdf.l.landuse1. What I did was to transform each corridor into a STIDF and then into a Track:

track.landuse1 <- list()

for(j in 1:length(spdf.l.landuse1)) {
  print(j)
  sti <- STIDF(sp = SpatialPoints(spdf.l.landuse1[[j]]@coords, proj4string = spdf.l.landuse1[[j]]@proj4string), 
               time = spdf.l.landuse1[[j]]$t, data = spdf.l.landuse1[[j]]@data[,1:2])
  track.landuse1[[j]] <- Track(sti)
}

Finally, I transformed that into a Tracks object:

tracks.landuse1 <- Tracks(track.landuse1)

I could successfully plot that (below), and it is now consider as a series of 1000 tracks of the same object. However, I guess that it would be better to make a TracksCollection directly, considering each corridor as a Track of a different individual. This way, we could consider all tracks of a given lan use scenario as simultaneous movements of different individuals, and then compare them with the other land use scenario by choosing a very different moment in time for the second land use scenario. However, I couldn't transform the tracks directly into a TracksCollection object.

tracks_plot

Still, I tried to move on.

All the analyses I tried to perform then (movement smoothing and chi maps), presented the same error: Here is the code and the error:

# movement smoothing
b <- Track.idw(tracks.landuse1, timestamp = '20 days')
plot(b, main="", ribwid=0.04, ribsep=0.02, cex.axis=1.5)

# chi maps
ch <- chimaps(tracks.landuse1, timestamp = "20 mins", rank = 1)

# Error:
Error in if (!ivs[i] == 0 && !ivs[i] == nrow(X)) { : 
  missing value where TRUE/FALSE needed

Do you know what it may mean?

Moradii commented 5 years ago

Dear bniebuhr, Sorry for the late response. I am not sure if I truly understood how you generated time for your data, however, this error is to do with your time vector. Important note: In order to apply chimaps or Tarck.idw to your data, tracks MUST overlap in time. I guess your data does not satisfy this assumption.

I hope this helps, Mehdi