Skip to content

Commit

Permalink
Merge pull request #29 from ctoney/zonal_predict
Browse files Browse the repository at this point in the history
zonalBayes(): handle NA in the predictor data
  • Loading branch information
ctoney committed Aug 1, 2024
2 parents 3565b80 + 5326ed0 commit ecbd935
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions R/raster_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ zonalBayes <- function(dsn=NULL, layer=NULL, src=NULL, zoneidfld,
for (z in zoneid) {
rs_list[[z]] <- list()
for (i in 1:nMCMC) {
rs_list[[z]][[i]] <- new(RunningStats, na_rm=FALSE)
rs_list[[z]][[i]] <- new(RunningStats, na_rm = TRUE)
}
}

Expand All @@ -1447,7 +1447,7 @@ zonalBayes <- function(dsn=NULL, layer=NULL, src=NULL, zoneidfld,
nrow = x_len,
ncol = n_pred_columns)

names(m) <- c(prednames, helperidfld)
colnames(m) <- c(prednames, helperidfld)
for (i in 1:nraster) {
m[, i] <- ds_list[[i]]$read(band = 1,
xoff = xoff1,
Expand All @@ -1472,14 +1472,22 @@ zonalBayes <- function(dsn=NULL, layer=NULL, src=NULL, zoneidfld,
}

# predicted values
anyisna <- function(x) { any(is.na(x)) }
na_rows <- apply(m, 1, anyisna)
m <- m[!na_rows, , drop = FALSE]

if (nrow(m) == 0)
return()

preds <- predfun(m, xy)
if (ncol(preds) != nMCMC)

if (NROW(preds) != nMCMC)
stop("fatal: ncol(preds) != nMCMC")

# update rs objects, attrib_value from zoneidfld
for (i in seq_len(nMCMC))
preds[, i] |> rs_list[[attrib_value]][[i]]$update()
for (i in seq_len(nMCMC)) {
rs_list[[attrib_value]][[i]]$update(preds[i, ])
}

return()
}

Expand Down

0 comments on commit ecbd935

Please sign in to comment.