Spatial analysis: generate movement density sequence from animal location data

52 views Asked by At

I read the articel Animal Movement Prediction Based on Predictive Recurrent Neural Network. The authors use {spatialEco} to generate movement density sequences. I tried to generate the KDE (kernel density estimate), but I do not know how to create the images that will be used in PRNN++ model for movement predictions.

library(sf)
library(dplyr)
library(spatialEco)
library(terra)
library(tmap)
library(tidyverse)
library(RColorBrewer)
# Load your geo_record.RData
load("C:/Users/user/Desktop/Movedata/geo_record.RData")

# Assuming geo_record is a data frame with columns: coords.x1, coords.x2, and geometry.y
geo_data <- as.data.frame(geo_record)

colnames(geo_data)[which(colnames(geo_data) == "geometry.y")] <- "PolygonArea"

movement_points_sf <- geo_data[1:9445,] %>%
  rename(x = coords.x1,
         y = coords.x2) %>%
  select(-9) %>%
  mutate(longitude = x,
         latitude = y) %>%
  st_as_sf(., 
           coords = c("x", "y"), 
           crs = 4326, 
           agr = "constant") %>%
  st_transform(3857)

# Grouping sizes to consider


# Set parameters
bandwidth <- as.integer(mean(geo_data$distance, na.rm=TRUE)) # Bandwidth for the Gaussian Kernel
resolution <- 500   # Resolution of the raster

breaks <- seq(from = min(movement_points_sf$DateTime), to = max(movement_points_sf$DateTime), by = "30 days")

# Create the 'group_30_days' column using cut
movement_points_sf$group_30_days <- cut(movement_points_sf$DateTime, breaks, labels = FALSE, include.lowest = TRUE)
# Replace NA values with 18
movement_points_sf$group_30_days <- ifelse(is.na(movement_points_sf$group_30_days), 18, movement_points_sf$group_30_days)
# Create a list to store movement density sequences for different group sizes
movement_density_list <- list()

# Generate movement density sequences for different day groups
for (group_30_day in unique(movement_points_sf$group_30_days)) {
  
  # Filter points for the current day group
  grouped_animal_sf <- movement_points_sf[movement_points_sf$group_30_days == group_30_day,]
  
  
  
  print(summary(grouped_animal_sf))
  # Check if the group has sufficient data
  if (nrow(grouped_animal_sf) >= 3) {
    # Generate movement density sequence
    movement_density <- sf.kde(x = grouped_animal_sf, y= grouped_animal_sf$timelag,
                               bw = bandwidth, 
                               standardize = TRUE, 
                               res = resolution)
    
    # Add the movement density sequence to the list
    movement_density_list[[as.character(group_30_day)]] <- movement_density
  } else {
    cat("Skipping day group", day_group, "due to insufficient data.\n")
  }
}
0

There are 0 answers