if (FALSE) {
library(CAST)
library(sf)
library(terra)
library(caret)
library(rnaturalearth)
library(ggplot2)
data(splotdata)
studyArea <- rnaturalearth::ne_countries(continent = "South America", returnclass = "sf")
########### Distance between training data and new data:
dist <- geodist(splotdata, studyArea)
plot(dist)
########### Distance between training data, new data and test data (here Chile):
plot(splotdata[,"Country"])
dist <- geodist(splotdata[splotdata$Country != "Chile",], studyArea,
testdata = splotdata[splotdata$Country == "Chile",])
plot(dist)
########### Distance between training data, new data and CV folds:
folds <- createFolds(1:nrow(splotdata), k=3, returnTrain=FALSE)
dist <- geodist(x=splotdata, modeldomain=studyArea, cvfolds=folds)
plot(dist)
########### Distances in the feature space:
predictors <- terra::rast(system.file("extdata","predictors_chile.tif", package="CAST"))
dist <- geodist(x = splotdata,
modeldomain = predictors,
type = "feature",
variables = c("bio_1","bio_12", "elev"))
plot(dist)
dist <- geodist(x = splotdata[splotdata$Country != "Chile",],
modeldomain = predictors, cvfolds = folds,
testdata = splotdata[splotdata$Country == "Chile",],
type = "feature",
variables=c("bio_1","bio_12", "elev"))
plot(dist)
############ Example for a random global dataset
############ (refer to figure in Meyer and Pebesma 2022)
### Define prediction area (here: global):
ee <- st_crs("+proj=eqearth")
co <- ne_countries(returnclass = "sf")
co.ee <- st_transform(co, ee)
### Simulate a spatial random sample
### (alternatively replace pts_random by a real sampling dataset (see Meyer and Pebesma 2022):
sf_use_s2(FALSE)
pts_random <- st_sample(co.ee, 2000, exact=FALSE)
### See points on the map:
ggplot() + geom_sf(data = co.ee, fill="#00BFC4",col="#00BFC4") +
geom_sf(data = pts_random, color = "#F8766D",size=0.5, shape=3) +
guides(fill = "none", col = "none") +
labs(x = NULL, y = NULL)
### plot distances:
dist <- geodist(pts_random,co.ee)
plot(dist) + scale_x_log10(labels=round)
}
Run the code above in your browser using DataLab