diff --git a/404.html b/404.html index eb232d13..350a2b39 100644 --- a/404.html +++ b/404.html @@ -80,7 +80,7 @@
### AOA for which the spatial CV error applies:
-AOA <- aoa(predictors_sp,ffsmodel_LLO)
-
-plot(prediction_ffs,main="prediction for the AOA \n(spatial CV error applied)")
+AOA <- aoa(predictors_sp,ffsmodel_LLO)
##
+ |
+ | | 0%
+ |
+ | | 1%
+ |
+ |= | 1%
+ |
+ |= | 2%
+ |
+ |== | 2%
+ |
+ |== | 3%
+ |
+ |== | 4%
+ |
+ |=== | 4%
+ |
+ |=== | 5%
+ |
+ |==== | 5%
+ |
+ |==== | 6%
+ |
+ |===== | 7%
+ |
+ |===== | 8%
+ |
+ |====== | 8%
+ |
+ |====== | 9%
+ |
+ |======= | 9%
+ |
+ |======= | 10%
+ |
+ |======= | 11%
+ |
+ |======== | 11%
+ |
+ |======== | 12%
+ |
+ |========= | 12%
+ |
+ |========= | 13%
+ |
+ |========== | 14%
+ |
+ |========== | 15%
+ |
+ |=========== | 15%
+ |
+ |=========== | 16%
+ |
+ |============ | 17%
+ |
+ |============ | 18%
+ |
+ |============= | 18%
+ |
+ |============= | 19%
+ |
+ |============== | 19%
+ |
+ |============== | 20%
+ |
+ |============== | 21%
+ |
+ |=============== | 21%
+ |
+ |=============== | 22%
+ |
+ |================ | 22%
+ |
+ |================ | 23%
+ |
+ |================ | 24%
+ |
+ |================= | 24%
+ |
+ |================= | 25%
+ |
+ |================== | 25%
+ |
+ |================== | 26%
+ |
+ |=================== | 26%
+ |
+ |=================== | 27%
+ |
+ |=================== | 28%
+ |
+ |==================== | 28%
+ |
+ |==================== | 29%
+ |
+ |===================== | 29%
+ |
+ |===================== | 30%
+ |
+ |===================== | 31%
+ |
+ |====================== | 31%
+ |
+ |====================== | 32%
+ |
+ |======================= | 32%
+ |
+ |======================= | 33%
+ |
+ |======================== | 34%
+ |
+ |======================== | 35%
+ |
+ |========================= | 35%
+ |
+ |========================= | 36%
+ |
+ |========================== | 37%
+ |
+ |========================== | 38%
+ |
+ |=========================== | 38%
+ |
+ |=========================== | 39%
+ |
+ |============================ | 39%
+ |
+ |============================ | 40%
+ |
+ |============================ | 41%
+ |
+ |============================= | 41%
+ |
+ |============================= | 42%
+ |
+ |============================== | 42%
+ |
+ |============================== | 43%
+ |
+ |=============================== | 44%
+ |
+ |=============================== | 45%
+ |
+ |================================ | 45%
+ |
+ |================================ | 46%
+ |
+ |================================= | 46%
+ |
+ |================================= | 47%
+ |
+ |================================= | 48%
+ |
+ |================================== | 48%
+ |
+ |================================== | 49%
+ |
+ |=================================== | 49%
+ |
+ |=================================== | 50%
+ |
+ |=================================== | 51%
+ |
+ |==================================== | 51%
+ |
+ |==================================== | 52%
+ |
+ |===================================== | 52%
+ |
+ |===================================== | 53%
+ |
+ |===================================== | 54%
+ |
+ |====================================== | 54%
+ |
+ |====================================== | 55%
+ |
+ |======================================= | 55%
+ |
+ |======================================= | 56%
+ |
+ |======================================== | 57%
+ |
+ |======================================== | 58%
+ |
+ |========================================= | 58%
+ |
+ |========================================= | 59%
+ |
+ |========================================== | 59%
+ |
+ |========================================== | 60%
+ |
+ |========================================== | 61%
+ |
+ |=========================================== | 61%
+ |
+ |=========================================== | 62%
+ |
+ |============================================ | 62%
+ |
+ |============================================ | 63%
+ |
+ |============================================= | 64%
+ |
+ |============================================= | 65%
+ |
+ |============================================== | 65%
+ |
+ |============================================== | 66%
+ |
+ |=============================================== | 67%
+ |
+ |=============================================== | 68%
+ |
+ |================================================ | 68%
+ |
+ |================================================ | 69%
+ |
+ |================================================= | 69%
+ |
+ |================================================= | 70%
+ |
+ |================================================= | 71%
+ |
+ |================================================== | 71%
+ |
+ |================================================== | 72%
+ |
+ |=================================================== | 72%
+ |
+ |=================================================== | 73%
+ |
+ |=================================================== | 74%
+ |
+ |==================================================== | 74%
+ |
+ |==================================================== | 75%
+ |
+ |===================================================== | 75%
+ |
+ |===================================================== | 76%
+ |
+ |====================================================== | 76%
+ |
+ |====================================================== | 77%
+ |
+ |====================================================== | 78%
+ |
+ |======================================================= | 78%
+ |
+ |======================================================= | 79%
+ |
+ |======================================================== | 79%
+ |
+ |======================================================== | 80%
+ |
+ |======================================================== | 81%
+ |
+ |========================================================= | 81%
+ |
+ |========================================================= | 82%
+ |
+ |========================================================== | 82%
+ |
+ |========================================================== | 83%
+ |
+ |=========================================================== | 84%
+ |
+ |=========================================================== | 85%
+ |
+ |============================================================ | 85%
+ |
+ |============================================================ | 86%
+ |
+ |============================================================= | 87%
+ |
+ |============================================================= | 88%
+ |
+ |============================================================== | 88%
+ |
+ |============================================================== | 89%
+ |
+ |=============================================================== | 89%
+ |
+ |=============================================================== | 90%
+ |
+ |=============================================================== | 91%
+ |
+ |================================================================ | 91%
+ |
+ |================================================================ | 92%
+ |
+ |================================================================= | 92%
+ |
+ |================================================================= | 93%
+ |
+ |================================================================== | 94%
+ |
+ |================================================================== | 95%
+ |
+ |=================================================================== | 95%
+ |
+ |=================================================================== | 96%
+ |
+ |==================================================================== | 96%
+ |
+ |==================================================================== | 97%
+ |
+ |==================================================================== | 98%
+ |
+ |===================================================================== | 98%
+ |
+ |===================================================================== | 99%
+ |
+ |======================================================================| 99%
+ |
+ |======================================================================| 100%
+
+plot(prediction_ffs,main="prediction for the AOA \n(spatial CV error applied)")
plot(AOA$AOA,col=c("grey","transparent"),add=T)
++#spplot(prediction_ffs,main="prediction for the AOA \n(spatial CV error applied)")+ #spplot(AOA$AOA,col.regions=c("grey","transparent")) ### AOA for which the random CV error applies: -AOA_random <- aoa(predictors_sp,model) -plot(prediction,main="prediction for the AOA \n(random CV error applied)") +AOA_random <- aoa(predictors_sp,model)
+## + | + | | 0% + | + | | 1% + | + |= | 1% + | + |= | 2% + | + |== | 2% + | + |== | 3% + | + |== | 4% + | + |=== | 4% + | + |=== | 5% + | + |==== | 5% + | + |==== | 6% + | + |===== | 7% + | + |===== | 8% + | + |====== | 8% + | + |====== | 9% + | + |======= | 9% + | + |======= | 10% + | + |======= | 11% + | + |======== | 11% + | + |======== | 12% + | + |========= | 12% + | + |========= | 13% + | + |========== | 14% + | + |========== | 15% + | + |=========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============ | 18% + | + |============= | 18% + | + |============= | 19% + | + |============== | 19% + | + |============== | 20% + | + |============== | 21% + | + |=============== | 21% + | + |=============== | 22% + | + |================ | 22% + | + |================ | 23% + | + |================ | 24% + | + |================= | 24% + | + |================= | 25% + | + |================== | 25% + | + |================== | 26% + | + |=================== | 26% + | + |=================== | 27% + | + |=================== | 28% + | + |==================== | 28% + | + |==================== | 29% + | + |===================== | 29% + | + |===================== | 30% + | + |===================== | 31% + | + |====================== | 31% + | + |====================== | 32% + | + |======================= | 32% + | + |======================= | 33% + | + |======================== | 34% + | + |======================== | 35% + | + |========================= | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |========================== | 38% + | + |=========================== | 38% + | + |=========================== | 39% + | + |============================ | 39% + | + |============================ | 40% + | + |============================ | 41% + | + |============================= | 41% + | + |============================= | 42% + | + |============================== | 42% + | + |============================== | 43% + | + |=============================== | 44% + | + |=============================== | 45% + | + |================================ | 45% + | + |================================ | 46% + | + |================================= | 46% + | + |================================= | 47% + | + |================================= | 48% + | + |================================== | 48% + | + |================================== | 49% + | + |=================================== | 49% + | + |=================================== | 50% + | + |=================================== | 51% + | + |==================================== | 51% + | + |==================================== | 52% + | + |===================================== | 52% + | + |===================================== | 53% + | + |===================================== | 54% + | + |====================================== | 54% + | + |====================================== | 55% + | + |======================================= | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |======================================== | 58% + | + |========================================= | 58% + | + |========================================= | 59% + | + |========================================== | 59% + | + |========================================== | 60% + | + |========================================== | 61% + | + |=========================================== | 61% + | + |=========================================== | 62% + | + |============================================ | 62% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================= | 65% + | + |============================================== | 65% + | + |============================================== | 66% + | + |=============================================== | 67% + | + |=============================================== | 68% + | + |================================================ | 68% + | + |================================================ | 69% + | + |================================================= | 69% + | + |================================================= | 70% + | + |================================================= | 71% + | + |================================================== | 71% + | + |================================================== | 72% + | + |=================================================== | 72% + | + |=================================================== | 73% + | + |=================================================== | 74% + | + |==================================================== | 74% + | + |==================================================== | 75% + | + |===================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 76% + | + |====================================================== | 77% + | + |====================================================== | 78% + | + |======================================================= | 78% + | + |======================================================= | 79% + | + |======================================================== | 79% + | + |======================================================== | 80% + | + |======================================================== | 81% + | + |========================================================= | 81% + | + |========================================================= | 82% + | + |========================================================== | 82% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |=========================================================== | 85% + | + |============================================================ | 85% + | + |============================================================ | 86% + | + |============================================================= | 87% + | + |============================================================= | 88% + | + |============================================================== | 88% + | + |============================================================== | 89% + | + |=============================================================== | 89% + | + |=============================================================== | 90% + | + |=============================================================== | 91% + | + |================================================================ | 91% + | + |================================================================ | 92% + | + |================================================================= | 92% + | + |================================================================= | 93% + | + |================================================================== | 94% + | + |================================================================== | 95% + | + |=================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 96% + | + |==================================================================== | 97% + | + |==================================================================== | 98% + | + |===================================================================== | 98% + | + |===================================================================== | 99% + | + |======================================================================| 99% + | + |======================================================================| 100%
-+plot(prediction,main="prediction for the AOA \n(random CV error applied)") plot(AOA_random$AOA,col=c("grey","transparent"),add=T)
@@ -154,7 +154,7 @@+#spplot(prediction,main="prediction for the AOA \n(random CV error applied)")+ #spplot(AOA_random$AOA,col.regions=c("grey","transparent"))
The figure shows in grey areas that are outside the area of @@ -556,7 +1195,7 @@
Further reading
Generate Response
generate_random_response <- function(raster, predictornames = -names(raster), seed = sample(seq(1000), 1)){ +names(raster), seed = sample(seq(1000), 1)){ operands_1 = c("+", "-", "*", "/") operands_2 = c("^1","^2") @@ -178,7 +178,7 @@
Generate Responsee = paste0("raster$", expression, collapse = " ") response = eval(parse(text = e)) - names(response) <- "response" + names(response) <- "response" return(response) }
mask <- predictors[[1]]
-values(mask)[!is.na(values(mask))] <- 1
-mask <- st_as_sf(as.polygons(mask))
+values(mask)[!is.na(values(mask))] <- 1
+mask <- st_as_sf(as.polygons(mask))
mask <- st_make_valid(mask)
set.seed(15)
@@ -221,9 +221,9 @@ Prepare data
-trainDat <- extract(predictors,samplepoints,na.rm=FALSE)
-trainDat$response <- extract(response,samplepoints,na.rm=FALSE, ID=FALSE)$response
-trainDat <- na.omit(trainDat)
trainDat <- extract(predictors,samplepoints,na.rm=FALSE)
+trainDat$response <- extract(response,samplepoints,na.rm=FALSE, ID=FALSE)$response
+trainDat <- na.omit(trainDat)
set.seed(10)
-model <- train(trainDat[,names(predictors)],
+model <- train(trainDat[,names(predictors)],
trainDat$response,
method="rf",
importance=TRUE,
@@ -275,9 +275,9 @@ Predict and calculate error
-prediction <- predict(predictors,model,na.rm=T)
+prediction <- predict(predictors,model,na.rm=T)
truediff <- abs(prediction-response)
-plot(rast(list(prediction,response)),main=c("prediction","reference"))
## [1] "aoa"
+AOA <- aoa(predictors, model)
+##
+ |
+ | | 0%
+ |
+ |==== | 5%
+ |
+ |======= | 10%
+ |
+ |========== | 15%
+ |
+ |============== | 20%
+ |
+ |================== | 25%
+ |
+ |===================== | 30%
+ |
+ |======================== | 35%
+ |
+ |============================ | 40%
+ |
+ |================================ | 45%
+ |
+ |=================================== | 50%
+ |
+ |====================================== | 55%
+ |
+ |========================================== | 60%
+ |
+ |============================================== | 65%
+ |
+ |================================================= | 70%
+ |
+ |==================================================== | 75%
+ |
+ |======================================================== | 80%
+ |
+ |============================================================ | 85%
+ |
+ |=============================================================== | 90%
+ |
+ |================================================================== | 95%
+ |
+ |======================================================================| 100%
-names(AOA)
## [1] "parameters" "DI" "AOA"
+class(AOA)
+## [1] "aoa"
+names(AOA)
## [1] "parameters" "DI" "AOA"
+
print(AOA)
## DI:
## class : SpatRaster
@@ -333,7 +377,7 @@ AOA Calculation## AOA Threshold: 0.3221291
Plotting the aoa
object shows the distribution of DI
values within the training data and the DI of the new data.
+plot(AOA)
The most output of the
aoa
function are two raster data: @@ -346,7 +390,7 @@AOA Calculationparameters list entry.
We can plot the DI as well as predictions onyl in the AOA:
-+plot(truediff,col=viridis(100),main="true prediction error") plot(AOA$DI,col=viridis(100),main="DI") plot(prediction, col=viridis(100),main="prediction for AOA") @@ -378,28 +422,28 @@
AOA for spatially clustered data?
To show how this looks like, we use 15 spatial locations and simulate 5 data points around each location.
-+-set.seed(25) samplepoints <- clustered_sample(mask,75,15,radius=25000) plot(response,col=viridis(100)) plot(samplepoints,col="red",add=T,pch=3)
-trainDat <- extract(predictors,samplepoints,na.rm=FALSE) -trainDat$response <- extract(response,samplepoints,na.rm=FALSE)$response +
+trainDat <- na.omit(trainDat)+trainDat <- extract(predictors,samplepoints,na.rm=FALSE) +trainDat$response <- extract(response,samplepoints,na.rm=FALSE)$response trainDat <- data.frame(trainDat,samplepoints) -trainDat <- na.omit(trainDat)
We first train a model with (in this case) inappropriate random cross-validation.
-+set.seed(10) -model_random <- train(trainDat[,names(predictors)], +model_random <- train(trainDat[,names(predictors)], trainDat$response, method="rf", importance=TRUE, trControl = trainControl(method="cv")) -prediction_random <- predict(predictors,model_random,na.rm=TRUE) +prediction_random <- predict(predictors,model_random,na.rm=TRUE) print(model_random)
## Random Forest ## @@ -419,14 +463,14 @@
AOA for spatially clustered data?## RMSE was used to select the optimal model using the smallest value. ## The final value used for the model was mtry = 4.
…and a model based on leave-cluster-out cross-validation.
-+folds <- CreateSpacetimeFolds(trainDat, spacevar="parent",k=10) set.seed(15) -model <- train(trainDat[,names(predictors)], +model <- train(trainDat[,names(predictors)], trainDat$response, method="rf", importance=TRUE, - tuneGrid = expand.grid(mtry = c(2:length(names(predictors)))), + tuneGrid = expand.grid(mtry = c(2:length(names(predictors)))), trControl = trainControl(method="cv",index=folds$index)) print(model)
-## Random Forest @@ -448,19 +492,325 @@
AOA for spatially clustered data?## ## RMSE was used to select the optimal model using the smallest value. ## The final value used for the model was mtry = 6.
+-prediction <- predict(predictors,model,na.rm=TRUE)
+prediction <- predict(predictors,model,na.rm=TRUE)
The AOA is then calculated (for comparison) using the model validated by random cross-validation, and second by taking the spatial clusters into account and calculating the threshold based on minimum distances to a nearest training point not located in the same cluster. This is done in the aoa function, where the folds used for cross-validation are automatically extracted from the model.
- -+++AOA_spatial <- aoa(predictors, model)
+## + | + | | 0% + | + |= | 1% + | + |== | 3% + | + |=== | 4% + | + |==== | 5% + | + |===== | 7% + | + |====== | 8% + | + |======= | 9% + | + |======= | 11% + | + |======== | 12% + | + |========= | 13% + | + |========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============= | 19% + | + |============== | 20% + | + |=============== | 21% + | + |================ | 23% + | + |================= | 24% + | + |================== | 25% + | + |=================== | 27% + | + |==================== | 28% + | + |===================== | 29% + | + |===================== | 31% + | + |====================== | 32% + | + |======================= | 33% + | + |======================== | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |=========================== | 39% + | + |============================ | 40% + | + |============================= | 41% + | + |============================== | 43% + | + |=============================== | 44% + | + |================================ | 45% + | + |================================= | 47% + | + |================================== | 48% + | + |=================================== | 49% + | + |=================================== | 51% + | + |==================================== | 52% + | + |===================================== | 53% + | + |====================================== | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |========================================= | 59% + | + |========================================== | 60% + | + |=========================================== | 61% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================== | 65% + | + |=============================================== | 67% + | + |================================================ | 68% + | + |================================================= | 69% + | + |================================================= | 71% + | + |================================================== | 72% + | + |=================================================== | 73% + | + |==================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |======================================================= | 79% + | + |======================================================== | 80% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |============================================================ | 85% + | + |============================================================= | 87% + | + |============================================================== | 88% + | + |=============================================================== | 89% + | + |=============================================================== | 91% + | + |================================================================ | 92% + | + |================================================================= | 93% + | + |================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 97% + | + |===================================================================== | 99% + | + |======================================================================| 100%
++AOA_random <- aoa(predictors, model_random)
+## + | + | | 0% + | + |= | 1% + | + |== | 3% + | + |=== | 4% + | + |==== | 5% + | + |===== | 7% + | + |====== | 8% + | + |======= | 9% + | + |======= | 11% + | + |======== | 12% + | + |========= | 13% + | + |========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============= | 19% + | + |============== | 20% + | + |=============== | 21% + | + |================ | 23% + | + |================= | 24% + | + |================== | 25% + | + |=================== | 27% + | + |==================== | 28% + | + |===================== | 29% + | + |===================== | 31% + | + |====================== | 32% + | + |======================= | 33% + | + |======================== | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |=========================== | 39% + | + |============================ | 40% + | + |============================= | 41% + | + |============================== | 43% + | + |=============================== | 44% + | + |================================ | 45% + | + |================================= | 47% + | + |================================== | 48% + | + |=================================== | 49% + | + |=================================== | 51% + | + |==================================== | 52% + | + |===================================== | 53% + | + |====================================== | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |========================================= | 59% + | + |========================================== | 60% + | + |=========================================== | 61% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================== | 65% + | + |=============================================== | 67% + | + |================================================ | 68% + | + |================================================= | 69% + | + |================================================= | 71% + | + |================================================== | 72% + | + |=================================================== | 73% + | + |==================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |======================================================= | 79% + | + |======================================================== | 80% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |============================================================ | 85% + | + |============================================================= | 87% + | + |============================================================== | 88% + | + |=============================================================== | 89% + | + |=============================================================== | 91% + | + |================================================================ | 92% + | + |================================================================= | 93% + | + |================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 97% + | + |===================================================================== | 99% + | + |======================================================================| 100%
plot(AOA_spatial$DI,col=viridis(100),main="DI") plot(prediction, col=viridis(100),main="prediction for AOA \n(spatial CV error applies)") plot(AOA_spatial$AOA,col=c("grey","transparent"),add=TRUE,plg=list(x="topleft",box.col="black",bty="o",title="AOA")) @@ -477,7 +827,7 @@
AOA for spatially clustered data? -
+@@ -488,16 +838,16 @@grid.arrange(plot(AOA_spatial) + ggplot2::ggtitle("Spatial CV"), plot(AOA_random) + ggplot2::ggtitle("Random CV"), ncol = 2)
Comparison prediction erro
Since we used a simulated response variable, we can now compare the prediction error within the AOA with the model error, assuming that the model error applies inside the AOA but not outside.
-++RMSE(values(prediction)[values(AOA_spatial$AOA)==1], + values(response)[values(AOA_spatial$AOA)==1])###for the spatial CV: -RMSE(values(prediction)[values(AOA_spatial$AOA)==1], - values(response)[values(AOA_spatial$AOA)==1])
-## [1] 3308.808
+-RMSE(values(prediction)[values(AOA_spatial$AOA)==0], - values(response)[values(AOA_spatial$AOA)==0])
+RMSE(values(prediction)[values(AOA_spatial$AOA)==0], + values(response)[values(AOA_spatial$AOA)==0])
-## [1] 10855.31
+model$results
-## mtry RMSE Rsquared MAE RMSESD RsquaredSD MAESD ## 1 2 3227.421 0.9382904 2740.529 2335.609 0.06774290 2168.398 @@ -505,16 +855,16 @@
Comparison prediction erro ## 3 4 2677.002 0.9570317 2349.310 1690.078 0.04208035 1549.323 ## 4 5 2587.598 0.9486190 2282.064 1595.276 0.05220790 1410.225 ## 5 6 2494.756 0.9425158 2190.718 1507.700 0.07431001 1289.825
++RMSE(values(prediction_random)[values(AOA_random$AOA)==1], + values(response)[values(AOA_random$AOA)==1])###and for the random CV: -RMSE(values(prediction_random)[values(AOA_random$AOA)==1], - values(response)[values(AOA_random$AOA)==1])
-## [1] 1365.329
+-RMSE(values(prediction_random)[values(AOA_random$AOA)==0], - values(response)[values(AOA_random$AOA)==0])
+RMSE(values(prediction_random)[values(AOA_random$AOA)==0], + values(response)[values(AOA_random$AOA)==0])
-## [1] 3959.685
+model_random$results
## mtry RMSE Rsquared MAE RMSESD RsquaredSD MAESD ## 1 2 1088.1729 0.9956237 790.2191 595.2632 0.004567068 407.8754 @@ -538,13 +888,626 @@
Relationship be level, which then allows limiting predictions using a threshold. Note that we used a multi-purpose CV to estimate the relationship between the DI and the RMSE here (see details in the paper). -
-+++ window.size = 5, length.out = 5)DI_RMSE_relation <- DItoErrormetric(model, AOA_spatial$parameters, multiCV=TRUE, - window.size = 5, length.out = 5) -plot(DI_RMSE_relation)
+## + | + | | 0% + | + |= | 1% + | + |== | 3% + | + |=== | 4% + | + |==== | 5% + | + |===== | 7% + | + |====== | 8% + | + |======= | 9% + | + |======= | 11% + | + |======== | 12% + | + |========= | 13% + | + |========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============= | 19% + | + |============== | 20% + | + |=============== | 21% + | + |================ | 23% + | + |================= | 24% + | + |================== | 25% + | + |=================== | 27% + | + |==================== | 28% + | + |===================== | 29% + | + |===================== | 31% + | + |====================== | 32% + | + |======================= | 33% + | + |======================== | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |=========================== | 39% + | + |============================ | 40% + | + |============================= | 41% + | + |============================== | 43% + | + |=============================== | 44% + | + |================================ | 45% + | + |================================= | 47% + | + |================================== | 48% + | + |=================================== | 49% + | + |=================================== | 51% + | + |==================================== | 52% + | + |===================================== | 53% + | + |====================================== | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |========================================= | 59% + | + |========================================== | 60% + | + |=========================================== | 61% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================== | 65% + | + |=============================================== | 67% + | + |================================================ | 68% + | + |================================================= | 69% + | + |================================================= | 71% + | + |================================================== | 72% + | + |=================================================== | 73% + | + |==================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |======================================================= | 79% + | + |======================================================== | 80% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |============================================================ | 85% + | + |============================================================= | 87% + | + |============================================================== | 88% + | + |=============================================================== | 89% + | + |=============================================================== | 91% + | + |================================================================ | 92% + | + |================================================================= | 93% + | + |================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 97% + | + |===================================================================== | 99% + | + |======================================================================| 100% +## + | + | | 0% + | + |= | 1% + | + |== | 3% + | + |=== | 4% + | + |==== | 5% + | + |===== | 7% + | + |====== | 8% + | + |======= | 9% + | + |======= | 11% + | + |======== | 12% + | + |========= | 13% + | + |========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============= | 19% + | + |============== | 20% + | + |=============== | 21% + | + |================ | 23% + | + |================= | 24% + | + |================== | 25% + | + |=================== | 27% + | + |==================== | 28% + | + |===================== | 29% + | + |===================== | 31% + | + |====================== | 32% + | + |======================= | 33% + | + |======================== | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |=========================== | 39% + | + |============================ | 40% + | + |============================= | 41% + | + |============================== | 43% + | + |=============================== | 44% + | + |================================ | 45% + | + |================================= | 47% + | + |================================== | 48% + | + |=================================== | 49% + | + |=================================== | 51% + | + |==================================== | 52% + | + |===================================== | 53% + | + |====================================== | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |========================================= | 59% + | + |========================================== | 60% + | + |=========================================== | 61% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================== | 65% + | + |=============================================== | 67% + | + |================================================ | 68% + | + |================================================= | 69% + | + |================================================= | 71% + | + |================================================== | 72% + | + |=================================================== | 73% + | + |==================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |======================================================= | 79% + | + |======================================================== | 80% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |============================================================ | 85% + | + |============================================================= | 87% + | + |============================================================== | 88% + | + |=============================================================== | 89% + | + |=============================================================== | 91% + | + |================================================================ | 92% + | + |================================================================= | 93% + | + |================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 97% + | + |===================================================================== | 99% + | + |======================================================================| 100% +## + | + | | 0% + | + |= | 1% + | + |== | 3% + | + |=== | 4% + | + |==== | 5% + | + |===== | 7% + | + |====== | 8% + | + |======= | 9% + | + |======= | 11% + | + |======== | 12% + | + |========= | 13% + | + |========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============= | 19% + | + |============== | 20% + | + |=============== | 21% + | + |================ | 23% + | + |================= | 24% + | + |================== | 25% + | + |=================== | 27% + | + |==================== | 28% + | + |===================== | 29% + | + |===================== | 31% + | + |====================== | 32% + | + |======================= | 33% + | + |======================== | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |=========================== | 39% + | + |============================ | 40% + | + |============================= | 41% + | + |============================== | 43% + | + |=============================== | 44% + | + |================================ | 45% + | + |================================= | 47% + | + |================================== | 48% + | + |=================================== | 49% + | + |=================================== | 51% + | + |==================================== | 52% + | + |===================================== | 53% + | + |====================================== | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |========================================= | 59% + | + |========================================== | 60% + | + |=========================================== | 61% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================== | 65% + | + |=============================================== | 67% + | + |================================================ | 68% + | + |================================================= | 69% + | + |================================================= | 71% + | + |================================================== | 72% + | + |=================================================== | 73% + | + |==================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |======================================================= | 79% + | + |======================================================== | 80% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |============================================================ | 85% + | + |============================================================= | 87% + | + |============================================================== | 88% + | + |=============================================================== | 89% + | + |=============================================================== | 91% + | + |================================================================ | 92% + | + |================================================================= | 93% + | + |================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 97% + | + |===================================================================== | 99% + | + |======================================================================| 100% +## + | + | | 0% + | + |= | 1% + | + |== | 3% + | + |=== | 4% + | + |==== | 5% + | + |===== | 7% + | + |====== | 8% + | + |======= | 9% + | + |======= | 11% + | + |======== | 12% + | + |========= | 13% + | + |========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============= | 19% + | + |============== | 20% + | + |=============== | 21% + | + |================ | 23% + | + |================= | 24% + | + |================== | 25% + | + |=================== | 27% + | + |==================== | 28% + | + |===================== | 29% + | + |===================== | 31% + | + |====================== | 32% + | + |======================= | 33% + | + |======================== | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |=========================== | 39% + | + |============================ | 40% + | + |============================= | 41% + | + |============================== | 43% + | + |=============================== | 44% + | + |================================ | 45% + | + |================================= | 47% + | + |================================== | 48% + | + |=================================== | 49% + | + |=================================== | 51% + | + |==================================== | 52% + | + |===================================== | 53% + | + |====================================== | 55% + | + |======================================= | 56% + | + |======================================== | 57% + | + |========================================= | 59% + | + |========================================== | 60% + | + |=========================================== | 61% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================== | 65% + | + |=============================================== | 67% + | + |================================================ | 68% + | + |================================================= | 69% + | + |================================================= | 71% + | + |================================================== | 72% + | + |=================================================== | 73% + | + |==================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |======================================================= | 79% + | + |======================================================== | 80% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |============================================================ | 85% + | + |============================================================= | 87% + | + |============================================================== | 88% + | + |=============================================================== | 89% + | + |=============================================================== | 91% + | + |================================================================ | 92% + | + |================================================================= | 93% + | + |================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 97% + | + |===================================================================== | 99% + | + |======================================================================| 100%
+plot(DI_RMSE_relation)
-expected_RMSE = terra::predict(AOA_spatial$DI, DI_RMSE_relation) +
+expected_RMSE = terra::predict(AOA_spatial$DI, DI_RMSE_relation) # account for multiCV changing the DI threshold updated_AOA = AOA_spatial$DI > attr(DI_RMSE_relation, "AOA_threshold") @@ -573,21 +1536,21 @@
Data and preprocessing -
-dat <- readRDS(system.file("extdata","Cookfarm.RDS",package="CAST")) +
+head(trainDat)+dat <- readRDS(system.file("extdata","Cookfarm.RDS",package="CAST")) # calculate average of VW for each sampling site: -dat <- aggregate(dat[,c("VW","Easting","Northing")],by=list(as.character(dat$SOURCEID)),mean) +dat <- aggregate(dat[,c("VW","Easting","Northing")],by=list(as.character(dat$SOURCEID)),mean) # create sf object from the data: pts <- st_as_sf(dat,coords=c("Easting","Northing")) ##### Extract Predictors for the locations of the sampling points -studyArea <- rast(system.file("extdata","predictors_2012-03-25.tif",package="CAST")) -st_crs(pts) <- crs(studyArea) -trainDat <- extract(studyArea,pts,na.rm=FALSE) -pts$ID <- 1:nrow(pts) -trainDat <- merge(trainDat,pts,by.x="ID",by.y="ID") +studyArea <- rast(system.file("extdata","predictors_2012-03-25.tif",package="CAST")) +st_crs(pts) <- crs(studyArea) +trainDat <- extract(studyArea,pts,na.rm=FALSE) +pts$ID <- 1:nrow(pts) +trainDat <- merge(trainDat,pts,by.x="ID",by.y="ID") # The final training dataset with potential predictors and VW: -head(trainDat)
## ID DEM TWI BLD NDRE.M NDRE.Sd Bt Easting Northing ## 1 1 788.1906 4.304258 1.42 -0.051189531 0.2506899 0.0000 493384 5180587 ## 2 2 788.3813 3.863605 1.29 -0.046459336 0.1754623 0.0000 493514 5180567 @@ -618,7 +1581,7 @@
Model training and prediction
+-predictors <- c("DEM","NDRE.Sd","TWI","Bt") response <- "VW" @@ -648,13 +1611,13 @@
Prediction
+ +plot(stretch(studyArea[[predictors]]))@@ -662,10 +1625,97 @@AOA estimation
Next we’re limiting the predictions to the AOA. Predictions outside the AOA should be excluded.
--AOA <- aoa(studyArea,model) - -#### Plot results: +
++AOA <- aoa(studyArea,model)
+## + | + | | 0% + | + |== | 2% + | + |=== | 5% + | + |===== | 7% + | + |======= | 10% + | + |======== | 12% + | + |========== | 14% + | + |============ | 17% + | + |============= | 19% + | + |=============== | 21% + | + |================= | 24% + | + |================== | 26% + | + |==================== | 29% + | + |====================== | 31% + | + |======================= | 33% + | + |========================= | 36% + | + |=========================== | 38% + | + |============================ | 40% + | + |============================== | 43% + | + |================================ | 45% + | + |================================= | 48% + | + |=================================== | 50% + | + |===================================== | 52% + | + |====================================== | 55% + | + |======================================== | 57% + | + |========================================== | 60% + | + |=========================================== | 62% + | + |============================================= | 64% + | + |=============================================== | 67% + | + |================================================ | 69% + | + |================================================== | 71% + | + |==================================================== | 74% + | + |===================================================== | 76% + | + |======================================================= | 79% + | + |========================================================= | 81% + | + |========================================================== | 83% + | + |============================================================ | 86% + | + |============================================================== | 88% + | + |=============================================================== | 90% + | + |================================================================= | 93% + | + |=================================================================== | 95% + | + |==================================================================== | 98% + | + |======================================================================| 100%
+#### Plot results: plot(AOA$DI,col=viridis(100),main="DI with sampling locations (red)") plot(pts,zcol="ID",col="red",add=TRUE) @@ -713,7 +1763,7 @@
Further reading
data("splotdata") -predictors <- rast(system.file("extdata","predictors_chile.tif",package="CAST")) +predictors <- rast(system.file("extdata","predictors_chile.tif",package="CAST")) splotdata <- st_drop_geometry(splotdata)
+prediction_random <- predict(predictors,model_random,na.rm=TRUE)set.seed(10) -model_random <- train(splotdata[,names(predictors)], +model_random <- train(splotdata[,names(predictors)], splotdata$Species_richness, method="rf", importance=TRUE, ntrees = 50, trControl = trainControl(method="cv")) -prediction_random <- predict(predictors,model_random,na.rm=TRUE)
+Parallel AOA by dividing the new data @@ -129,20 +129,344 @@
Parallel AOA by dividing the new cores). This is especially useful for very large prediction areas, e.g. in global mapping. +
model_random_trainDI = trainDI(model_random)
+## + | + | | 0% + | + | | 1% + | + |= | 1% + | + |= | 2% + | + |== | 2% + | + |== | 3% + | + |== | 4% + | + |=== | 4% + | + |=== | 5% + | + |==== | 5% + | + |==== | 6% + | + |===== | 7% + | + |===== | 8% + | + |====== | 8% + | + |====== | 9% + | + |======= | 9% + | + |======= | 10% + | + |======= | 11% + | + |======== | 11% + | + |======== | 12% + | + |========= | 12% + | + |========= | 13% + | + |========= | 14% + | + |========== | 14% + | + |========== | 15% + | + |=========== | 15% + | + |=========== | 16% + | + |============ | 17% + | + |============ | 18% + | + |============= | 18% + | + |============= | 19% + | + |============== | 19% + | + |============== | 20% + | + |============== | 21% + | + |=============== | 21% + | + |=============== | 22% + | + |================ | 22% + | + |================ | 23% + | + |================= | 24% + | + |================= | 25% + | + |================== | 25% + | + |================== | 26% + | + |=================== | 26% + | + |=================== | 27% + | + |=================== | 28% + | + |==================== | 28% + | + |==================== | 29% + | + |===================== | 29% + | + |===================== | 30% + | + |===================== | 31% + | + |====================== | 31% + | + |====================== | 32% + | + |======================= | 32% + | + |======================= | 33% + | + |======================= | 34% + | + |======================== | 34% + | + |======================== | 35% + | + |========================= | 35% + | + |========================= | 36% + | + |========================== | 37% + | + |========================== | 38% + | + |=========================== | 38% + | + |=========================== | 39% + | + |============================ | 39% + | + |============================ | 40% + | + |============================ | 41% + | + |============================= | 41% + | + |============================= | 42% + | + |============================== | 42% + | + |============================== | 43% + | + |============================== | 44% + | + |=============================== | 44% + | + |=============================== | 45% + | + |================================ | 45% + | + |================================ | 46% + | + |================================= | 47% + | + |================================= | 48% + | + |================================== | 48% + | + |================================== | 49% + | + |=================================== | 49% + | + |=================================== | 50% + | + |=================================== | 51% + | + |==================================== | 51% + | + |==================================== | 52% + | + |===================================== | 52% + | + |===================================== | 53% + | + |====================================== | 54% + | + |====================================== | 55% + | + |======================================= | 55% + | + |======================================= | 56% + | + |======================================== | 56% + | + |======================================== | 57% + | + |======================================== | 58% + | + |========================================= | 58% + | + |========================================= | 59% + | + |========================================== | 59% + | + |========================================== | 60% + | + |========================================== | 61% + | + |=========================================== | 61% + | + |=========================================== | 62% + | + |============================================ | 62% + | + |============================================ | 63% + | + |============================================= | 64% + | + |============================================= | 65% + | + |============================================== | 65% + | + |============================================== | 66% + | + |=============================================== | 66% + | + |=============================================== | 67% + | + |=============================================== | 68% + | + |================================================ | 68% + | + |================================================ | 69% + | + |================================================= | 69% + | + |================================================= | 70% + | + |================================================= | 71% + | + |================================================== | 71% + | + |================================================== | 72% + | + |=================================================== | 72% + | + |=================================================== | 73% + | + |=================================================== | 74% + | + |==================================================== | 74% + | + |==================================================== | 75% + | + |===================================================== | 75% + | + |===================================================== | 76% + | + |====================================================== | 77% + | + |====================================================== | 78% + | + |======================================================= | 78% + | + |======================================================= | 79% + | + |======================================================== | 79% + | + |======================================================== | 80% + | + |======================================================== | 81% + | + |========================================================= | 81% + | + |========================================================= | 82% + | + |========================================================== | 82% + | + |========================================================== | 83% + | + |=========================================================== | 84% + | + |=========================================================== | 85% + | + |============================================================ | 85% + | + |============================================================ | 86% + | + |============================================================= | 86% + | + |============================================================= | 87% + | + |============================================================= | 88% + | + |============================================================== | 88% + | + |============================================================== | 89% + | + |=============================================================== | 89% + | + |=============================================================== | 90% + | + |=============================================================== | 91% + | + |================================================================ | 91% + | + |================================================================ | 92% + | + |================================================================= | 92% + | + |================================================================= | 93% + | + |================================================================== | 94% + | + |================================================================== | 95% + | + |=================================================================== | 95% + | + |=================================================================== | 96% + | + |==================================================================== | 96% + | + |==================================================================== | 97% + | + |==================================================================== | 98% + | + |===================================================================== | 98% + | + |===================================================================== | 99% + | + |======================================================================| 99% + | + |======================================================================| 100%
+print(model_random_trainDI)
-## DI of 703 observation ## Predictors: bio_1 bio_4 bio_5 bio_6 bio_8 bio_9 bio_12 bio_13 bio_14 bio_15 elev ## ## AOA Threshold: 0.1941761
+-saveRDS(model_random_trainDI, "path/to/file")
+saveRDS(model_random_trainDI, "path/to/file")
If you have a large raster, you divide it into multiple smaller tiles and apply the trainDI object afterwards to each tile.
--r1 = crop(predictors, c(-75.66667, -67, -30, -17.58333)) -r2 = crop(predictors, c(-75.66667, -67, -45, -30)) -r3 = crop(predictors, c(-75.66667, -67, -55.58333, -45)) +
+r1 = crop(predictors, c(-75.66667, -67, -30, -17.58333)) +r2 = crop(predictors, c(-75.66667, -67, -45, -30)) +r3 = crop(predictors, c(-75.66667, -67, -55.58333, -45)) plot(r1[[1]],main = "Tile 1") @@ -152,7 +476,7 @@
Parallel AOA by dividing the new
Use the
-trainDI
argument in theaoa
function to specify, that you want to use a previously computed trainDI object.+aoa_r1 = aoa(newdata = r1, trainDI = model_random_trainDI) plot(r1[[1]], main = "Tile 1: Predictors") @@ -162,27 +486,27 @@
Parallel AOA by dividing the new
You can now run the aoa function in parallel on the different tiles! Of course you can use for favorite parallel backend for this task, here we use mclapply from the
-parallel
package.+-library(parallel) tiles_aoa = mclapply(list(r1, r2, r3), function(tile){ aoa(newdata = tile, trainDI = model_random_trainDI) }, mc.cores = 3)
+plot(tiles_aoa[[1]]$AOA, main = "Tile 1") plot(tiles_aoa[[2]]$AOA, main = "Tile 2") plot(tiles_aoa[[3]]$AOA, main = "Tile 3")
For larger tasks it might be useful to save the tiles to you hard-drive and load them one by one to avoid filling up your RAM.
-@@ -143,6 +145,14 @@+@@ -195,7 +519,7 @@# Simple Example Code for raster tiles on the hard drive tiles = list.files("path/to/tiles", full.names = TRUE) tiles_aoa = mclapply(tiles, function(tile){ - current = terra::rast(tile) + current = terra::rast(tile) aoa(newdata = current, trainDI = model_random_trainDI) }, mc.cores = 3)
Parallel AOA by dividing the new
ArgumentsDItoErrormetric) +
Value
@@ -155,14 +165,16 @@Value
DI+ SpatRaster, stars object or data frame. Dissimilarity index of newdata
LPD ++ SpatRaster, stars object or data frame. Local Point Density of newdata.
AOA -+ SpatRaster, stars object or data frame. Area of Applicability of newdata. - AOA has values 0 (outside AOA) and 1 (inside AOA)
SpatRaster, stars object or data frame. Area of Applicability of newdata. AOA has values 0 (outside AOA) and 1 (inside AOA)
Details
-The Dissimilarity Index (DI) and the corresponding Area of Applicability (AOA) are calculated. +
The Dissimilarity Index (DI), the Local Data Point Density (LPD) and the corresponding Area of Applicability (AOA) are calculated. If variables are factors, dummy variables are created prior to weighting and distance calculation.
Interpretation of results: If a location is very similar to the properties of the training data it will have a low distance in the predictor variable space @@ -188,7 +200,7 @@
See also
@@ -200,15 +212,15 @@Exampleslibrary(viridis) # prepare sample data: -dat <- readRDS(system.file("extdata","Cookfarm.RDS",package="CAST")) -dat <- aggregate(dat[,c("VW","Easting","Northing")],by=list(as.character(dat$SOURCEID)),mean) +dat <- readRDS(system.file("extdata","Cookfarm.RDS",package="CAST")) +dat <- aggregate(dat[,c("VW","Easting","Northing")],by=list(as.character(dat$SOURCEID)),mean) pts <- st_as_sf(dat,coords=c("Easting","Northing")) -pts$ID <- 1:nrow(pts) +pts$ID <- 1:nrow(pts) set.seed(100) pts <- pts[1:30,] -studyArea <- rast(system.file("extdata","predictors_2012-03-25.tif",package="CAST"))[[1:8]] -trainDat <- extract(studyArea,pts,na.rm=FALSE) -trainDat <- merge(trainDat,pts,by.x="ID",by.y="ID") +studyArea <- rast(system.file("extdata","predictors_2012-03-25.tif",package="CAST"))[[1:8]] +trainDat <- extract(studyArea,pts,na.rm=FALSE) +trainDat <- merge(trainDat,pts,by.x="ID",by.y="ID") # visualize data spatially: plot(studyArea) @@ -218,15 +230,17 @@
Examples# train a model: set.seed(100) variables <- c("DEM","NDRE.Sd","TWI") -model <- train(trainDat[,which(names(trainDat)%in%variables)], +model <- train(trainDat[,which(names(trainDat)%in%variables)], trainDat$VW, method="rf", importance=TRUE, tuneLength=1, trControl=trainControl(method="cv",number=5,savePredictions=T)) print(model) #note that this is a quite poor prediction model -prediction <- predict(studyArea,model,na.rm=TRUE) +prediction <- predict(studyArea,model,na.rm=TRUE) plot(varImp(model,scale=FALSE)) #...then calculate the AOA of the trained model for the study area: -AOA <- aoa(studyArea,model) +AOA <- aoa(studyArea, model) +#... or if preferred calculate the aoa and the LPD of the study area: +AOA <- aoa(studyArea, model, LPD = TRUE, maxLPD = 1) plot(AOA) #### @@ -257,13 +271,13 @@
Examplesrsmp_cv <- rsmp("cv", folds = 5L)$instantiate(task) ## predict: -prediction <- predict(studyArea,lrn$model,na.rm=TRUE) +prediction <- predict(studyArea,lrn$model,na.rm=TRUE) ### Estimate AOA AOA <- aoa(studyArea, - train = as.data.frame(task$data()), + train = as.data.frame(task$data()), variables = task$feature_names, - weight = data.frame(t(lrn$importance())), + weight = data.frame(t(lrn$importance())), CVtest = rsmp_cv$instance[order(row_id)]$fold) } @@ -274,7 +288,7 @@
Examples -
Developed by Hanna Meyer, Carles Milà, Marvin Ludwig, Jan Linnenbrink.
+Developed by Hanna Meyer, Carles Milà, Marvin Ludwig, Jan Linnenbrink, Fabian Schumacher.