I have a data.frame containing the pixel values of several satellite data at a coarse spatial scale. Using the autocart function I created a model at the coarse spatial scale and then I tried to apply it at a finer spatial scale using the predictAutocartfunction but I getting this error:
Error: Index out of bounds: [index='block.data.bui'].
I have checked the column names of both data.frames (the one I used to create the model and the one I used to make predictions at the finer spatial resolution) and they are same.
Any ideas why I am getting this error?
Below is the code I used:
library(terra)
library (autocart)
wd <- "path/"
provoliko <- "EPSG:7767"
v <- vect(paste0(wd, "mumbai_7767.shp"))
# Load the data
block.data <- read.csv(paste0(wd, "block.data.csv"))
# df with the coordinates
sint <- subset(block.data, select = c(x, y))
block.data <- subset(block.data, select = c(ntl, x, y, pop, tirs, agbh, pan, ebbi, nbai, bui, baei))
# Extract the response vector in the regression tree
response <- as.matrix(block.data$ntl)
# Create a dataframe for the predictors used in the model
predictors <- data.frame(block.data$pop, block.data$tirs, block.data$agbh, block.data$pan, block.data$ebbi,
block.data$nbai, block.data$bui, block.data$baei)
# Create the matrix of locations so that autocart knows where our observations are located
locations <- as.matrix(cbind(block.data$x, block.data$y))
alpha <- 0.60
beta <- 0.20
my_control <- autocartControl(distpower = 2)
snow_model <- autocart(response, predictors, locations, alpha, beta, my_control)
rsds <- as.data.frame(cbind(sint, block.data$ntl - snow_model$prediction))
rsds <- rast(rsds, type = "xyz")
crs(rsds) <- provoliko
rsds <- crop(rsds, ext(v))
rsds <- mask(rsds, v)
writeRaster(rsds, paste0(wd, "autocart_resids.tif"), overwrite = TRUE)
# predictors at the fine spatial scale
pop = rast(paste0(wd, "pop.tif"))
tirs = rast(paste0(wd, "tirs.tif"))
agbh = rast(paste0(wd, "agbh.tif"))
pan = rast(paste0(wd, "pan.tif"))
ebbi = rast(paste0(wd, "ebbi.tif"))
baei = rast(paste0(wd, "baei.tif"))
nbai = rast(paste0(wd, "nbai.tif"))
bui = rast(paste0(wd, "bui.tif"))
stackedImages <- terra::as.data.frame(c(pop, tirs, agbh, pan, ebbi, nbai, bui, baei),
xy = TRUE,
na.rm = TRUE)
names(stackedImages)[3:10] <- c("pop", "tirs", "agbh", "pan", "ebbi", "nbai", "bui", "baei")
# Create a dataframe for the predictors used in the model
predictors_fine <- data.frame(stackedImages$pop, stackedImages$tirs, stackedImages$agbh, stackedImages$pan,
stackedImages$ebbi, stackedImages$nbai, stackedImages$bui, stackedImages$baei)
# Create the matrix of locations so that autocart knows where our observations are located
locations_fine <- as.matrix(cbind(stackedImages$x, stackedImages$y))
test_predictions <- predictAutocart(snow_model, predictors_fine) # here is the error
p <- as.data.frame(cbind(predictors_fine$x, predictors_fine$y, test_predictions))
p <- rast(p, type = "xyz")
crs(p) <- provoliko
p <- crop(p, ext(v))
p <- mask(p, v)
writeRaster(p, paste0(wd, "autocart_pred.tif"), overwrite = TRUE)
I am using R 4.3.1 and RStudio2023.09.1+494 "Desert Sunflower" Release (cd7011dce393115d3a7c3db799dda4b1c7e88711, 2023-10-16) for windows Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2023.09.1+494 Chrome/116.0.5845.190 Electron/26.2.4 Safari/537.36.
I solved by renaming the data.frame containing the high resolution data from stackedImages to block.data (i.e., the same as the data.frame containing the low resolution data).
I have a
data.frame
containing the pixel values of several satellite data at a coarse spatial scale. Using theautocart
function I created a model at the coarse spatial scale and then I tried to apply it at a finer spatial scale using thepredictAutocart
function but I getting this error:Error: Index out of bounds: [index='block.data.bui'].
I have checked the column names of both data.frames (the one I used to create the model and the one I used to make predictions at the finer spatial resolution) and they are same.
Any ideas why I am getting this error?
Below is the code I used:
And a small dataset: df at the coarse scale
df at the fine spatial scale
If needed I can share the whole dataset.
I am using
R
4.3.1 andRStudio
2023.09.1+494 "Desert Sunflower" Release (cd7011dce393115d3a7c3db799dda4b1c7e88711, 2023-10-16) for windows Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2023.09.1+494 Chrome/116.0.5845.190 Electron/26.2.4 Safari/537.36.