ethanancell / autocart

A modified regression tree R package so that spatial autocorrelation can be tracked at every step of the splitting
Other
3 stars 0 forks source link

Error when using the predictAutocart function #1

Closed nikosGeography closed 10 months ago

nikosGeography commented 10 months ago

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)

And a small dataset: df at the coarse scale

structure(list(ntl = c(9.30040073394775, 11.9007129669189, 14.8292026519775, 
22.660041809082, 29.5889263153076, 25.4422950744629, 12.4187831878662, 
14.5059461593628, 17.2589492797852, 19.2943172454834, 22.8597564697266, 
26.4619369506836, 20.466739654541, 12.6458320617676, 8.45075988769531, 
15.537691116333, 18.1966533660889, 21.089527130127, 23.0335674285889, 
23.2898216247559, 22.2773590087891, 15.4010229110718, 8.8380880355835, 
6.50943851470947, 5.40108203887939, 16.5031356811523, 18.3980255126953, 
22.246208190918, 24.5911998748779, 22.7725372314453, 18.2652282714844, 
12.0395975112915, 7.46130132675171, 5.6843376159668, 4.64033031463623, 
4.2633113861084, 3.84385180473328, 14.2161617279053, 16.6740818023682, 
18.7508201599121), x = c(590831.190717, 591279.537123, 591727.883529, 
592176.229935, 592624.576341, 593072.922747, 590382.844311, 590831.190717, 
591279.537123, 591727.883529, 592176.229935, 592624.576341, 593072.922747, 
593521.269153, 593969.615559, 590382.844311, 590831.190717, 591279.537123, 
591727.883529, 592176.229935, 592624.576341, 593072.922747, 593521.269153, 
593969.615559, 594417.961965, 590382.844311, 590831.190717, 591279.537123, 
591727.883529, 592176.229935, 592624.576341, 593072.922747, 593521.269153, 
593969.615559, 594417.961965, 594866.308371, 595314.654777, 589934.497905, 
590382.844311, 590831.190717), y = c(1046931.8837234, 1046931.8837234, 
1046931.8837234, 1046931.8837234, 1046931.8837234, 1046931.8837234, 
1046482.67767234, 1046482.67767234, 1046482.67767234, 1046482.67767234, 
1046482.67767234, 1046482.67767234, 1046482.67767234, 1046482.67767234, 
1046482.67767234, 1046033.47162128, 1046033.47162128, 1046033.47162128, 
1046033.47162128, 1046033.47162128, 1046033.47162128, 1046033.47162128, 
1046033.47162128, 1046033.47162128, 1046033.47162128, 1045584.26557021, 
1045584.26557021, 1045584.26557021, 1045584.26557021, 1045584.26557021, 
1045584.26557021, 1045584.26557021, 1045584.26557021, 1045584.26557021, 
1045584.26557021, 1045584.26557021, 1045584.26557021, 1045135.05951915, 
1045135.05951915, 1045135.05951915), pop = c(115.982627868652, 
163.046188354492, 166.612487792969, 230.805419921875, 397.9189453125, 
346.222076416016, 216.45051574707, 319.855773925781, 332.337463378906, 
362.700164794922, 332.499053955078, 362.675933837891, 264.628631591797, 
79.2443466186523, 41.6016006469727, 322.608520507812, 385.973754882812, 
420.628143310547, 448.584808349609, 409.322448730469, 386.374938964844, 
223.634094238281, 61.4649925231934, 39.9268226623535, 29.5225238800049, 
388.606536865234, 394.851287841797, 469.120300292969, 462.510070800781, 
447.713745117188, 358.718414306641, 150.031448364258, 53.2851371765137, 
37.5556297302246, 25.7356834411621, 22.6045246124268, 21.3207778930664, 
302.825500488281, 352.126251220703, 330.586334228516), tirs = c(35.7904319763184, 
35.5492362976074, 35.7807312011719, 39.0898895263672, 41.6156806945801, 
41.2339782714844, 36.6361389160156, 39.2393684387207, 39.1627655029297, 
38.3003997802734, 36.7696571350098, 41.1911926269531, 39.5097808837891, 
34.033390045166, 32.0809364318848, 39.2834510803223, 39.7354354858398, 
39.3034629821777, 39.5740509033203, 40.9525299072266, 41.2353057861328, 
41.0979080200195, 36.1454811096191, 32.9211463928223, 32.0329780578613, 
38.9966468811035, 39.2352828979492, 38.2555313110352, 39.4596939086914, 
41.9373664855957, 41.2251472473145, 39.757926940918, 35.4037094116211, 
34.8434524536133, 32.0887222290039, 31.3112964630127, 31.1533107757568, 
36.7114944458008, 37.6256294250488, 40.1147270202637), agbh = c(0.333513855934143, 
0.261405438184738, 0.40682926774025, 1.72421264648438, 5.82199287414551, 
4.52047300338745, 2.90418720245361, 2.80315780639648, 2.89598417282104, 
4.89105415344238, 1.37093961238861, 3.21543741226196, 3.07588648796082, 
0.0431563816964626, 0, 4.13494634628296, 6.7757363319397, 6.25349521636963, 
5.93588924407959, 4.38852214813232, 3.45881748199463, 2.54845213890076, 
0.103349439799786, 9.96726157609373e-05, 0, 4.95470190048218, 
5.19267606735229, 5.90742206573486, 7.91220712661743, 4.95516443252563, 
5.30534744262695, 0.989896059036255, 0.0697554647922516, 3.49294277839363e-05, 
0, 0, 0, 4.07474231719971, 5.1733283996582, 4.12800073623657), 
    pan = c(0.0891453698277473, 0.0890742540359497, 0.0877679884433746, 
    0.102928072214127, 0.114713497459888, 0.114911779761314, 
    0.104776054620743, 0.10668558627367, 0.10292674601078, 0.10744946449995, 
    0.0998126193881035, 0.111372709274292, 0.106109090149403, 
    0.0854731053113937, 0.0814806967973709, 0.112093240022659, 
    0.108950510621071, 0.107454411685467, 0.117015667259693, 
    0.116096191108227, 0.116933159530163, 0.103121899068356, 
    0.0866129174828529, 0.0803965851664543, 0.0810855776071548, 
    0.111800409853458, 0.10805993527174, 0.114930763840675, 0.114835068583488, 
    0.11956112831831, 0.113284841179848, 0.0996793359518051, 
    0.0868340879678726, 0.085090272128582, 0.0786930993199348, 
    0.0762152001261711, 0.0743404626846313, 0.112218298017979, 
    0.110829949378967, 0.111362040042877), ebbi = c(-0.203431189060211, 
    -0.212402805685997, -0.185238182544708, -0.0988051146268845, 
    3.19014470733237e-05, -0.00763109512627125, -0.146157652139664, 
    -0.0807789862155914, -0.100166477262974, -0.0677725672721863, 
    -0.148621186614037, -0.0544637739658356, -0.0743090361356735, 
    -0.189550191164017, -0.21031779050827, -0.0653634816408157, 
    -0.0406451188027859, -0.0659414157271385, -0.0368063598871231, 
    -0.0318984687328339, -0.0370265059173107, -0.0385789573192596, 
    -0.144565030932426, -0.214086100459099, -0.212783172726631, 
    -0.0686189830303192, -0.0486442930996418, -0.073246955871582, 
    -0.0360262431204319, -0.00416535977274179, -0.00955549068748951, 
    -0.0603046119213104, -0.150201722979546, -0.148539438843727, 
    -0.224361553788185, -0.218697503209114, -0.174144372344017, 
    -0.113888561725616, -0.0950845927000046, -0.0560582205653191
    ), nbai = c(-0.331021785736084, -0.331500232219696, -0.315080016851425, 
    -0.220386937260628, -0.12703500688076, -0.132590219378471, 
    -0.240305781364441, -0.194486483931541, -0.204664692282677, 
    -0.173288330435753, -0.266736775636673, -0.158681407570839, 
    -0.20735476911068, -0.351041853427887, -0.379130870103836, 
    -0.163192421197891, -0.156988397240639, -0.168419718742371, 
    -0.145624682307243, -0.141041740775108, -0.146409213542938, 
    -0.179768040776253, -0.312719285488129, -0.370071947574615, 
    -0.372571140527725, -0.170532166957855, -0.164590373635292, 
    -0.168181583285332, -0.142223194241524, -0.108024932444096, 
    -0.127554059028625, -0.204025864601135, -0.311274319887161, 
    -0.32052081823349, -0.376269638538361, -0.381402969360352, 
    -0.36227211356163, -0.198074206709862, -0.188638836145401, 
    -0.169653460383415), bui = c(-0.983844220638275, -1.02463090419769, 
    -0.93182498216629, -0.674426913261414, -0.361363857984543, 
    -0.345341473817825, -0.752945125102997, -0.571697235107422, 
    -0.666130244731903, -0.54601389169693, -0.774644017219543, 
    -0.44717937707901, -0.549114406108856, -0.957173347473145, 
    -1.029869556427, -0.529634475708008, -0.496254473924637, 
    -0.555450260639191, -0.422383278608322, -0.421728104352951, 
    -0.408168137073517, -0.533238112926483, -0.827063977718353, 
    -1.00881314277649, -1.02372491359711, -0.545444130897522, 
    -0.525832056999207, -0.537757158279419, -0.424047559499741, 
    -0.331437677145004, -0.371618568897247, -0.571419775485992, 
    -0.843616724014282, -0.843125402927399, -1.04571378231049, 
    -1.06696498394012, -1.008953332901, -0.650723695755005, -0.610480606555939, 
    -0.517879784107208), baei = c(1.79934537410736, 1.95380806922913, 
    1.814617395401, 1.45860064029694, 1.4552595615387, 1.51364576816559, 
    1.64155614376068, 1.57110023498535, 1.58765614032745, 1.64881181716919, 
    1.44768130779266, 1.55464541912079, 1.59049713611603, 1.56058430671692, 
    1.66681015491486, 1.48719477653503, 1.57111728191376, 1.57332634925842, 
    1.50978779792786, 1.45768415927887, 1.53166818618774, 1.41120994091034, 
    1.55846965312958, 1.60126781463623, 1.63928639888763, 1.46712744235992, 
    1.5229240655899, 1.54084098339081, 1.52842891216278, 1.48102486133575, 
    1.48502039909363, 1.52153074741364, 1.53831076622009, 1.57097470760345, 
    1.61375546455383, 1.72585415840149, 1.8508288860321, 1.54927504062653, 
    1.51909959316254, 1.44485485553741)), row.names = c(NA, 40L
), class = "data.frame")

df at the fine spatial scale

structure(list(x = c(591430, 591570, 591710, 591850, 591990, 
592130, 592270, 592410, 592550, 592690, 590870, 591010, 591150, 
591290, 591430, 591570, 591710, 591850, 591990, 592130, 592270, 
592410, 592550, 592690, 592830, 592970, 593110, 590590, 590730, 
590870, 591010, 591150, 591290, 591430, 591570, 591710, 591850, 
591990, 592130, 592270), y = c(1047270, 1047270, 1047270, 1047270, 
1047270, 1047270, 1047270, 1047270, 1047270, 1047270, 1047130, 
1047130, 1047130, 1047130, 1047130, 1047130, 1047130, 1047130, 
1047130, 1047130, 1047130, 1047130, 1047130, 1047130, 1047130, 
1047130, 1047130, 1046990, 1046990, 1046990, 1046990, 1046990, 
1046990, 1046990, 1046990, 1046990, 1046990, 1046990, 1046990, 
1046990), pop = c(84.68310546875, 87.4688873291016, 93.2934951782227, 
100.192832946777, 150.629028320312, 166.00260925293, 194.891296386719, 
374.901824951172, 410.844604492188, 430.042236328125, 74.5113830566406, 
82.6916656494141, 110.063613891602, 114.303413391113, 81.7273254394531, 
84.1179962158203, 86.7010803222656, 89.8166122436523, 131.820037841797, 
160.486907958984, 196.371215820312, 389.782135009766, 419.978332519531, 
435.8349609375, 459.345397949219, 428.688568115234, 406.798767089844, 
99.4420852661133, 100.27188873291, 99.412971496582, 114.892074584961, 
157.941528320312, 171.486511230469, 135.167922973633, 138.209838867188, 
129.149673461914, 136.842330932617, 164.459045410156, 188.529479980469, 
222.487121582031), tirs = c(33.0379028320312, 33.3250160217285, 
36.0936126708984, 33.9710235595703, 33.9881134033203, 36.8284873962402, 
39.8944511413574, 40.9711265563965, 39.7508964538574, 39.7132949829102, 
33.3147621154785, 33.9710235595703, 35.6766166687012, 35.2664527893066, 
33.5847854614258, 33.2122230529785, 35.8030815124512, 34.3811874389648, 
34.4973983764648, 37.3241004943848, 42.4135322570801, 43.7773208618164, 
39.9969940185547, 39.5868301391602, 41.7641105651855, 43.8559379577637, 
42.3861885070801, 34.6170272827148, 33.3797073364258, 35.0135192871094, 
33.728343963623, 35.5877456665039, 36.1722297668457, 34.5828475952148, 
33.5403518676758, 34.5931015014648, 34.6477928161621, 35.3108863830566, 
38.3358345031738, 43.3158912658691), agbh = c(0, 0, 0, 0, 0.000842788314912468, 
0.184929564595222, 0.644176840782166, 2.15136694908142, 4.45567417144775, 
7.13089323043823, 0, 0, 0, 0, 0, 0, 0, 5.64114307053387e-05, 
0.0127771990373731, 0.725773990154266, 3.55535674095154, 5.19202709197998, 
5.41635751724243, 8.69432830810547, 13.5945768356323, 10.256046295166, 
3.07656741142273, 0.0317663364112377, 0.0112131293863058, 0, 
0, 0.000264027214143425, 0.169368401169777, 0.0483647808432579, 
0.00482060015201569, 0, 0.00185703730676323, 0.0203782226890326, 
1.10964846611023, 3.95193958282471), pan = c(0.0807808265089989, 
0.0810955241322517, 0.0853318572044373, 0.0848961174488068, 0.0853802710771561, 
0.0949180722236633, 0.102955006062984, 0.111306630074978, 0.113727398216724, 
0.103414945304394, 0.0812649801373482, 0.0799335613846779, 0.0854528918862343, 
0.0865180343389511, 0.0811197310686111, 0.0814828425645828, 0.0865906551480293, 
0.0849203243851662, 0.0853318572044373, 0.092473104596138, 0.115179851651192, 
0.11435679346323, 0.118036352097988, 0.0975809097290039, 0.113267451524734, 
0.112323351204395, 0.111282423138618, 0.0809744894504547, 0.0810229033231735, 
0.0844603851437569, 0.0792557448148727, 0.0853560641407967, 0.0964915677905083, 
0.0847508758306503, 0.081579677760601, 0.084266722202301, 0.0846782550215721, 
0.0863243713974953, 0.0954748466610909, 0.11210548132658), ebbi = c(-0.251711845397949, 
-0.230355352163315, -0.172974780201912, -0.251617819070816, -0.270763069391251, 
-0.19197490811348, -0.070182591676712, -0.0302768126130104, -0.0390738770365715, 
-0.0568303316831589, -0.29997530579567, -0.282559186220169, -0.20842307806015, 
-0.234125927090645, -0.272604018449783, -0.260400325059891, -0.157657369971275, 
-0.253427445888519, -0.287487298250198, -0.202935218811035, 0.0455458723008633, 
0.0632750391960144, -0.0222451984882355, -0.049715232104063, 
-0.00644376873970032, 0.0641650184988976, -0.0183720476925373, 
-0.238951086997986, -0.258594989776611, -0.277442455291748, -0.27830383181572, 
-0.216851815581322, -0.160907313227654, -0.219639122486115, -0.274559587240219, 
-0.22369422018528, -0.22627380490303, -0.261305004358292, -0.176320463418961, 
0.0299501214176416), nbai = c(-0.418968826532364, -0.392554819583893, 
-0.330964118242264, -0.390339821577072, -0.395646899938583, -0.286705285310745, 
-0.213901907205582, -0.177690178155899, -0.145884290337563, -0.179487586021423, 
-0.426024168729782, -0.435121357440948, -0.34683084487915, -0.38117840886116, 
-0.430814981460571, -0.419372320175171, -0.356248944997787, -0.393164962530136, 
-0.391421049833298, -0.294367790222168, -0.114015020430088, -0.107484519481659, 
-0.139944717288017, -0.180391356348991, -0.124917015433311, -0.10764454305172, 
-0.130295693874359, -0.39233860373497, -0.402235358953476, -0.398480594158173, 
-0.426811307668686, -0.342672497034073, -0.27313768863678, -0.352631151676178, 
-0.428731501102448, -0.398615896701813, -0.381396234035492, -0.380956500768661, 
-0.274761527776718, -0.118230007588863), bui = c(-1.20509171485901, 
-1.16822242736816, -0.935697615146637, -1.15229475498199, -1.18216443061829, 
-0.937914848327637, -0.59693431854248, -0.465738475322723, -0.480972111225128, 
-0.56506210565567, -1.26940608024597, -1.23849582672119, -1.04437160491943, 
-1.09911727905273, -1.22503197193146, -1.22437858581543, -0.948225677013397, 
-1.13534832000732, -1.18317627906799, -0.956433415412903, -0.315382063388824, 
-0.272441625595093, -0.393160134553909, -0.588842809200287, -0.415748178958893, 
-0.299552708864212, -0.353374898433685, -1.12385618686676, -1.17933559417725, 
-1.18108081817627, -1.23666405677795, -1.06072950363159, -0.81386935710907, 
-1.06584239006042, -1.21908521652222, -1.10717940330505, -1.08904707431793, 
-1.09874415397644, -0.896753132343292, -0.345923602581024), baei = c(2.19398903846741, 
2.16542887687683, 1.72719323635101, 2.00616931915283, 1.96136748790741, 
1.59306180477142, 1.44566011428833, 1.38681972026825, 1.38774847984314, 
1.44682884216309, 2.19097185134888, 2.15053915977478, 2.12012434005737, 
2.15052032470703, 2.12018632888794, 2.20533895492554, 1.98911929130554, 
1.94120621681213, 1.93479013442993, 1.62273609638214, 1.24096488952637, 
1.23243093490601, 1.58627140522003, 1.55595648288727, 1.36783933639526, 
1.21888029575348, 1.58364117145538, 1.98974299430847, 2.13756370544434, 
1.98630011081696, 2.16506409645081, 2.07235240936279, 1.57500052452087, 
1.93455445766449, 2.10221886634827, 1.99757599830627, 1.91563332080841, 
1.75048124790192, 1.66478312015533, 1.27246117591858)), row.names = c(62L, 
63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 211L, 212L, 213L, 
214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 224L, 
225L, 226L, 227L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L, 
370L, 371L, 372L, 373L, 374L), class = "data.frame")

If needed I can share the whole dataset.

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.

nikosGeography commented 10 months ago

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).