|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cols = c(brewer.pal(6, "Dark2")) |
|
|
|
|
|
|
|
|
n_points = 2500 |
|
|
longitude = seq(from = -90, to = 90, length.out = n_points) |
|
|
latitude = rep(0, n_points) |
|
|
df = data.frame(latitude = latitude, |
|
|
longitude = longitude) |
|
|
|
|
|
parameters = data.frame(kappa = c(1, 1), |
|
|
phi = c(1.25, 17)) |
|
|
|
|
|
parameters$name = c(sprintf("phi%.2fkappa%.2f", parameters$phi, parameters$kappa)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
spatial_parameters = map2(parameters$kappa, parameters$phi,function(k, p){ |
|
|
spatial_covar_mat = varcov.spatial(coords = df, |
|
|
cov.pars = c(1, p), |
|
|
kappa = k, |
|
|
cov.model= "matern")$varcov |
|
|
spatial_covar_mat |
|
|
}) |
|
|
|
|
|
|
|
|
dist_data = as.matrix(df[,c("longitude", "latitude")], ncol = 2) |
|
|
dist_matrix = distm(dist_data, fun = distHaversine) / 1000 |
|
|
|
|
|
euclidean_dist = geosphere::distm(df[,c("longitude", "latitude")], |
|
|
fun = distHaversine) |
|
|
dimnames(euclidean_dist) = list(c(1:n_points), c(1:n_points)) |
|
|
|
|
|
euclidean_dist = scales::rescale(euclidean_dist) |
|
|
|
|
|
|
|
|
diag(dist_matrix) |
|
|
|
|
|
distance = round(dist_matrix[lower.tri(dist_matrix)], 4) |
|
|
transformed_dist = round(euclidean_dist[lower.tri(euclidean_dist)], 4) |
|
|
|
|
|
datafr <- as.data.frame(cbind(distance, transformed_dist)) |
|
|
datafr$covariance <- rep(0,1) |
|
|
|
|
|
plot_n = 1000 |
|
|
|
|
|
sample_idx = ceiling(seq(1, nrow(datafr)-1, length.out = plot_n)) |
|
|
|
|
|
plot_ss = datafr[sample_idx,] |
|
|
plot_ss$index = sample_idx |
|
|
plot_ss = plot_ss[order(plot_ss$distance),] |
|
|
|
|
|
spatialkappa_lines = lapply(spatial_parameters, function(x) { |
|
|
d = sort(c(x[lower.tri(x)]), decreasing = TRUE) |
|
|
sample_idx = seq(1, length(d), length.out = plot_n) |
|
|
d[sample_idx] |
|
|
}) |
|
|
|
|
|
legend_text = c(bquote("local:" ~ kappa == .(parameters[1,1]) ~ "; " ~ phi == .(parameters[1,2])), |
|
|
bquote("regional:" ~ kappa == .(parameters[2,1]) ~ "; " ~ phi == .(parameters[2,2]))) |
|
|
|
|
|
|
|
|
svg("output/plot_spatial_pars_km_zoomed.svg", width = 8, height = 8, dpi=300) |
|
|
plot(x = plot_ss$distance, y = plot_ss$covariance, |
|
|
type = "l", main = "Spatial parameters", col = "white", |
|
|
ylim = c(0, 1), |
|
|
xlim = c(0, 10000), |
|
|
xlab = "Distance (km)", |
|
|
ylab = "Covariance", |
|
|
frame.plot = TRUE, |
|
|
cex.main=1.7, |
|
|
axes=FALSE, |
|
|
cex.lab=1.5 |
|
|
) |
|
|
axis(1, at = seq(0,10000,by=2000), labels = seq(0,10000,by=2000), tick = TRUE, cex.axis=1.4) |
|
|
axis(2, at = seq(0,1,by=0.2), labels = seq(0,1,by=0.2), tick = TRUE, cex.axis=1.4) |
|
|
|
|
|
for(i in seq_along(spatialkappa_lines)){ |
|
|
lines(x = plot_ss$distance, y = spatialkappa_lines[[i]], col = cols[i], lwd = 2) |
|
|
} |
|
|
|
|
|
legend("topright", |
|
|
legend=legend_text, |
|
|
col=cols, lty=1, cex=1.5, lwd = 3) |
|
|
x <- dev.off() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
svg("output/plot_spatial_pars_km.svg", width = 8, height = 8, dpi=300) |
|
|
plot(x = plot_ss$distance, y = plot_ss$covariance, |
|
|
type = "l", main = "Spatial parameters", col = "white", |
|
|
ylim = c(0, 1), |
|
|
xlim = c(0, 15000), |
|
|
xlab = "Distance (km)", |
|
|
ylab = "Covariance", |
|
|
frame.plot = TRUE, |
|
|
cex.main=1.7, |
|
|
axes=FALSE, |
|
|
cex.lab=1.5) |
|
|
axis(1, at = seq(0,15000,by=2500), labels = seq(0,15000,by=2500), tick = TRUE, cex.axis=1.4) |
|
|
axis(2, at = seq(0,1,by=0.2), labels = seq(0,1,by=0.2), tick = TRUE, cex.axis=1.4) |
|
|
for(i in seq_along(spatialkappa_lines)){ |
|
|
lines(x = plot_ss$distance, y = spatialkappa_lines[[i]], col = cols[i], lwd = 2) |
|
|
} |
|
|
|
|
|
legend("topright", |
|
|
legend=legend_text, |
|
|
col=cols, lty=1, cex=1.5, lwd = 3) |
|
|
x <- dev.off() |
|
|
|
|
|
|