Spatial and spatio-temporal dataset simulations

In this vignette

This vignette is complementary to the “The Full Game” and it provides further details on how to generate spatial fields using the package ascot.

library(ascot)
library(patchwork)
library(ggplot2)

Spatial dataset simulation

Using the function define_pattern_s you can create a spatial dataset. This function allows you to specify the number of points in the X and Y direction (using n_x and n_y), if there is any spatial autocorrelation (autocorr = TRUE), and in case of spatial autocorrelation, the spatial extent of autocorrelation (range) and variance (amplitude). Further details and examples are provided below.

# Generate fields with different number of points
field_20x20 <- define_pattern_s(n_x=20, n_y=20, autocorr = FALSE)
field_10x20 <- define_pattern_s(n_x=10, n_y=20, autocorr = FALSE) 

# create plots
p1<-ggplot(field_20x20, aes(x = x, y = y, fill = value)) +
  geom_raster() + 
  labs(fill = "Value", title = "20x20 grid") + 
  xlab("X coordinates")+
  ylab("Y coordinates") +
  scale_fill_viridis_c()+
  theme_bw()

p2<-ggplot(field_10x20, aes(x = x, y = y, fill = value)) +
   geom_raster() + 
  labs(fill = "Value", title = "10x20 grid") + 
  xlab("X coordinates")+
  ylab("Y coordinates") +
  scale_fill_viridis_c()+
  theme_bw()

p1+p2+coord_equal()

Spatial autocorrelation

When autocor=TRUE, the spatial field is generated using a exponential correlation function, with covariance parameters \(\sigma^2\) ( =defined using amplitude) and \(\phi\) (defined using range).In more details, \(\phi\) corresponds to the distance at which spatial autocorrelation stops or becomes highly variable and \(\sigma^2\) represents the variance.

# Generate Field
no_autocorr <- define_pattern_s(n_x=20, n_y=20, autocorr = FALSE)
with_autocorr <- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE, amplitude = 1, range = 10)

# Visualise
p1<-ggplot(no_autocorr, aes(x = x, y = y, fill = value)) +
  geom_raster() + 
  labs(fill = "Value", title = "Without autocorrelation") + 
  xlab("X coordinates")+
  ylab("Y coordinates") +
  scale_fill_viridis_c()+
  theme_bw()

p2<-ggplot(with_autocorr, aes(x = x, y = y, fill = value)) +
  geom_raster() + 
  labs(fill = "Value", title = "With autocorrelation") + 
  xlab("X coordinates")+
  ylab("Y coordinates") +
  scale_fill_viridis_c()+
  theme_bw()

p1+p2+coord_equal()

Spatio-temporal dataset simulation

We can now combine the temporal data (see “Temporal dataset simulation” for how to create this dataset) with the spatial data by using the function simulate_spacetime. You will need to define your temporal dataset (temporal_pattern), your spatial dataset (field) and how much variation there is in the data (pattern_adherence), which we will discuss below.

spatial_field <- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE,range = 2, amplitude = 1)

# Determine duration of time series
time_df <- set_timeline(2, "weeks")

# Set time series pattern
time_series <- define_pattern_t(time_df,weekly_amplitude = 2, weekly_noise = 1)
#> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns

#Simulate spatio-temporal data
truth <- simulate_spacetime(temporal_pattern = time_series,
                            field = spatial_field,
                            pattern_adherence = 0)

#Plot the truth data
ggplot(truth, aes(x = x, y = y, fill = value)) +
  geom_raster(hjust = 0, vjust = 0) +
  labs(fill = "Value") +
  xlab("X coordinates")+
  ylab("Y coordinates")+
  coord_equal(expand=FALSE)+
  scale_fill_viridis_c()+
  facet_wrap(~date)+
  theme_bw()

Pattern adherence

Pattern adherence sets how much variation there is in the dataset. When pattern_adherence = 0, there is no variation in the data, with the value simply being the sum of the spatial dataset and the temporal dataset. Conversely, increasing the pattern_adherence increases the amount of variation.

spatial_field <- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE,range = 2, amplitude = 1)

# Determine duration of time series
time_df <- set_timeline(2, "weeks")

# Set time series pattern
time_series <- define_pattern_t(time_df,weekly_amplitude = 2, weekly_noise = 1)
#> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns

#Simulate spatio-temporal data
truth <- simulate_spacetime(temporal_pattern = time_series,
                            field = spatial_field,
                            pattern_adherence = 0)

 ggplot(truth, aes(x=date, y=value, group=paste0(x,"-",y)))+
        geom_point()+
        geom_path()+
        theme_bw()

Timeseries of the data generated with pattern_adherence = 0. Each line correspond to one point in space over time.

spatial_field <- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE,range = 2, amplitude = 1)

# Determine duration of time series
time_df <- set_timeline(2, "weeks")

# Set time series pattern
time_series <- define_pattern_t(time_df,weekly_amplitude = 2, weekly_noise = 1)
#> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns

#Simulate spatio-temporal data
truth <- simulate_spacetime(temporal_pattern = time_series,
                            field = spatial_field,
                            pattern_adherence = 2)

 ggplot(truth, aes(x=date, y=value, group=paste0(x,"-",y)))+
        geom_point()+
        geom_path()+
        theme_bw()

Timeseries of the data generated with pattern_adherence = 2. Each line correspond to one point in space over time.