rregressionkrigingautomap

How to apply universal kriging with custom prediction spatial grid using autoKrige in R


I want to apply universal kriging on a dataset using the autokrige function in R. I would like to create my own custom, spatial grid for the predicted points (for the new_data argument of autokrige). I am using R version 3.2.2 (64-bit) and RStudio Version 0.99.486. The following is what I've done so far:

library(automap)
library(sp)
library(gstat)
library(raster)
library(rgdal)

data(meuse)
coordinates(meuse) <- ~x + y
proj4string(meuse) <- CRS("+init=epsg:28992")

The following code was received from stackexchange here (credit goes to Jeffrey Evans) and is used to create a custom spatial grid for the prediction values:

ext_meuse <- as(extent(meuse), "SpatialPolygons")
r_meuse <- rasterToPoints(raster(ext_meuse, resolution = 59), spatial = TRUE)
proj4string(r_meuse) <- proj4string(meuse)

I then try to apply universal kriging (regression on the 'dist' column) using autoKrige:

kriging_result = autoKrige(zinc~dist, meuse, r_meuse)

The following error is then received:

Error in model.frame.default(terms.f, newdata, na.action = na.action, : object is not a matrix In addition: Warning message: 'newdata' had 3102 rows but variable found had 1 row

Did I made a mistake with the grid creation (r_meuse)? Is there a 'better' way to create a grid for the predicted data? All the examples I have found so far uses the meuse.grid data, but I would like to apply universal kriging to other data that does not have its own grid data yet.


Solution

  • I believe the problem here is that you are performing UK without having the predictor, dist, present in r_meuse. This is a problem as that information is needed for the linear to make a prediction. So, r_meuse needs to be a SpatialPointsDataFrame with dist defined.