r-spatial / gstat

Spatial and spatio-temporal geostatistical modelling, prediction and simulation
http://r-spatial.github.io/gstat/
GNU General Public License v2.0
196 stars 50 forks source link

krigeSimCE function doesn't return the simulated value when newdata has no "data" attribute #117

Open xyzyc opened 2 years ago

xyzyc commented 2 years ago

addAttrToGeom doesn't change the newdata. I think it should be newdata = addAttrToGeom(newdata, as.data.frame(sims)) krigeSimCE = function (formula, data, newdata, model, n = 1, ext = 2) { stopifnot(is(model, "variogramModel")) stopifnot(gridded(newdata)) if (!missing(data)) stopifnot(identical(data@proj4string@projargs, newdata@proj4string@projargs)) varName <- all.vars(formula[[2]]) condSim <- TRUE if (missing(data)) { condSim <- FALSE message("[No data provided: performing unconditional simulation.]") } else { message("[Performing conditional simulation.]") } covMat <- ceWrapOnTorusCalcCovRow1(newdata, model, ext = ext) sims <- ceSim(covMat, n, newdata@grid@cells.dim, newdata@grid.index) colnames(sims) <- paste0(varName, ".sim", 1:n) if (!condSim) { if ("data" %in% slotNames(newdata)) newdata@data <- cbind(newdata@data, sims) else newdata = addAttrToGeom(newdata, as.data.frame(sims)) return(newdata) } obsMeanField <- krige(formula, data, newdata, model) simMeanObsLoc <- krigeMultiple(as.formula(paste0("var1.pred ~", formula[[3]])), obsMeanField, data, model, sims) simMeanFields <- krigeMultiple(as.formula(paste0(varName, "~", formula[[3]])), data, newdata, model, simMeanObsLoc) sims <- obsMeanField@data$var1.pred + sims - simMeanFields if ("data" %in% slotNames(newdata)) { newdata@data <- cbind(newdata@data, sims) return(newdata) } newdata = addAttrToGeom(newdata, as.data.frame(sims)) }

edzer commented 2 years ago

@BenGraeler ?

xyzyc commented 2 years ago

I mean the "newdata = " part is missing in the original code.