if (FALSE) {
library(sf)
library(terra)
library(caret)
########### prepare sample data:
dat <- readRDS(system.file("extdata","Cookfarm.RDS",package="CAST"))
dat <- aggregate(dat[,c("DEM","TWI", "NDRE.M", "Easting", "Northing")],
by=list(as.character(dat$SOURCEID)),mean)
pts <- st_as_sf(dat,coords=c("Easting","Northing"))
st_crs(pts) <- 26911
pts_train <- pts[1:29,]
pts_test <- pts[30:42,]
studyArea <- terra::rast(system.file("extdata","predictors_2012-03-25.tif",package="CAST"))
studyArea <- studyArea[[c("DEM","TWI", "NDRE.M", "NDRE.Sd", "Bt")]]
########### Distance between training data and new data:
dist <- plot_geodist(pts_train,studyArea)
########### Distance between training data, new data and test data:
#mapview(pts_train,col.regions="blue")+mapview(pts_test,col.regions="red")
dist <- plot_geodist(pts_train,studyArea,testdata=pts_test)
########### Distance between training data, new data and CV folds:
folds <- createFolds(1:nrow(pts_train),k=3,returnTrain=FALSE)
dist <- plot_geodist(x=pts_train, modeldomain=studyArea, cvfolds=folds)
## or use nndm to define folds
AOI <- as.polygons(rast(studyArea), values = F) |>
st_as_sf() |>
st_union() |>
st_transform(crs = st_crs(pts_train))
nndm_pred <- nndm(pts_train, AOI)
dist <- plot_geodist(x=pts_train, modeldomain=studyArea,
cvfolds=nndm_pred$indx_test, cvtrain=nndm_pred$indx_train)
########### Distances in the feature space:
plot_geodist(x=pts_train, modeldomain=studyArea,
type = "feature",variables=c("DEM","TWI", "NDRE.M"))
dist <- plot_geodist(x=pts_train, modeldomain=studyArea, cvfolds = folds, testdata = pts_test,
type = "feature",variables=c("DEM","TWI", "NDRE.M"))
############ Example for a random global dataset
############ (refer to figure in Meyer and Pebesma 2022)
library(sf)
library(rnaturalearth)
library(ggplot2)
### 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 = FALSE, col = FALSE) +
labs(x = NULL, y = NULL)
### plot distances:
dist <- plot_geodist(pts_random,co.ee,showPlot=FALSE)
dist$plot+scale_x_log10(labels=round)
}
Run the code above in your browser using DataLab