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)
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
<- define_pattern_s(n_x=20, n_y=20, autocorr = FALSE)
field_20x20 <- define_pattern_s(n_x=10, n_y=20, autocorr = FALSE)
field_10x20
# create plots
<-ggplot(field_20x20, aes(x = x, y = y, fill = value)) +
p1geom_raster() +
labs(fill = "Value", title = "20x20 grid") +
xlab("X coordinates")+
ylab("Y coordinates") +
scale_fill_viridis_c()+
theme_bw()
<-ggplot(field_10x20, aes(x = x, y = y, fill = value)) +
p2geom_raster() +
labs(fill = "Value", title = "10x20 grid") +
xlab("X coordinates")+
ylab("Y coordinates") +
scale_fill_viridis_c()+
theme_bw()
+p2+coord_equal() p1
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
<- define_pattern_s(n_x=20, n_y=20, autocorr = FALSE)
no_autocorr <- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE, amplitude = 1, range = 10)
with_autocorr
# Visualise
<-ggplot(no_autocorr, aes(x = x, y = y, fill = value)) +
p1geom_raster() +
labs(fill = "Value", title = "Without autocorrelation") +
xlab("X coordinates")+
ylab("Y coordinates") +
scale_fill_viridis_c()+
theme_bw()
<-ggplot(with_autocorr, aes(x = x, y = y, fill = value)) +
p2geom_raster() +
labs(fill = "Value", title = "With autocorrelation") +
xlab("X coordinates")+
ylab("Y coordinates") +
scale_fill_viridis_c()+
theme_bw()
+p2+coord_equal() p1
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.
<- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE,range = 2, amplitude = 1)
spatial_field
# Determine duration of time series
<- set_timeline(2, "weeks")
time_df
# Set time series pattern
<- define_pattern_t(time_df,weekly_amplitude = 2, weekly_noise = 1)
time_series #> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns
#Simulate spatio-temporal data
<- simulate_spacetime(temporal_pattern = time_series,
truth 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 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.
<- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE,range = 2, amplitude = 1)
spatial_field
# Determine duration of time series
<- set_timeline(2, "weeks")
time_df
# Set time series pattern
<- define_pattern_t(time_df,weekly_amplitude = 2, weekly_noise = 1)
time_series #> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns
#Simulate spatio-temporal data
<- simulate_spacetime(temporal_pattern = time_series,
truth field = spatial_field,
pattern_adherence = 0)
ggplot(truth, aes(x=date, y=value, group=paste0(x,"-",y)))+
geom_point()+
geom_path()+
theme_bw()
<- define_pattern_s(n_x=20, n_y=20, autocorr = TRUE,range = 2, amplitude = 1)
spatial_field
# Determine duration of time series
<- set_timeline(2, "weeks")
time_df
# Set time series pattern
<- define_pattern_t(time_df,weekly_amplitude = 2, weekly_noise = 1)
time_series #> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns
#Simulate spatio-temporal data
<- simulate_spacetime(temporal_pattern = time_series,
truth field = spatial_field,
pattern_adherence = 2)
ggplot(truth, aes(x=date, y=value, group=paste0(x,"-",y)))+
geom_point()+
geom_path()+
theme_bw()