I'm trying to use the autoKrige() function in the automap package for a simple application of universal kriging. I have an irregularly spaced grid of measurements, and I want to interpolate between them on a fine spatial scale. Example code:
library('automap')
# create an irregularly spaced grid
y <-x <-c(-5,-4,-2,-1,-0.5,0,0.5,1,2,4,5)
grid <-expand.grid(x,y)
names(grid) <-c('x', 'y')
# create some measurements, greatest in the centre, with some noise
vals <-apply(grid,1, function(x) {12/(0.1+sqrt(x[1]^2 + x[2]^2))+rnorm(1,2,1.5)})
# get data into sp format
s <-SpatialPointsDataFrame(grid, data.frame(vals))
# make some prediction locations and get them into sp format
pred <-expand.grid(seq(-5,5,by=0.5), seq(-5,5,by=0.5))
pred <-cbind(pred[,1], pred[,2]) # this seems to be needed, not sure why
pred <-SpatialPoints(pred)
# try universal kriging
surf <-autoKrige(vals~x+y, s, new_data=pred)
This results in the error:
Error in gstat.formula.predict(d$formula, newdata, na.action = na.action, :
NROW(locs) != NROW(X): this should not occur
I have tried making new_data have the same number of rows as the original data, and have even tried making the co-ordinates in new_data exactly the same as the original data, but I still get this error. I'm new to geostatistics techniques so apologies if I'm making a basic mistake. Can anyone advise where I'm going wrong? Thanks.