idw() or krige() Error: dimensions do not match when missing values

2.9k Views Asked by At

Functions idw() and krige() from gstat package keep reporting errors when either response or predictor variable contains missing values (NA), even when na.action is set to na.omit:

require(gstat)
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.grid)
gridded(meuse.grid) = ~x+y

meuse2 <- as.data.frame(meuse)
meuse2[1, 'zinc'] <- NA
meuse2 <- SpatialPointsDataFrame(SpatialPoints(meuse), meuse2)

# idw response var
int <- idw(zinc ~ 1, meuse2, meuse.grid, na.action = na.omit)
# Error: dimensions do not match: locations 310 and data 154

# krige response var
m <- vgm(.59, "Sph", 874, .04)
int <- krige(zinc ~ 1, meuse2, meuse.grid, model = m, na.action = na.omit)
# Error: dimensions do not match: locations 310 and data 154

# krige predictor var
meuse3 <- as.data.frame(meuse)
meuse3[1, 'dist'] <- NA
meuse3 <- SpatialPointsDataFrame(SpatialPoints(meuse), meuse3)
int <- krige(zinc ~ dist, meuse3, meuse.grid, model = m, na.action = na.omit)
# Error: dimensions do not match: locations 310 and data 154

Is this a bug? Do we actually have to filter our data manually and merge the results back to original data frames? Isn't there some easier solution? Why is there the na.action option then?

1

There are 1 best solutions below

0
On

The na.action argument deals with missing values within newdata (not locations or data).

This is clearly stated in ?idw / ?krige / ?predict.gstat

function determining what should be done with missing values in 'newdata'. The default is to predict 'NA'. Missing values in coordinates and predictors are both dealt with.

There is no method to deal with NA values within the locations or data (and hence the error which is basically saying that there are two more values in the locations as the data (ie. the x and y coordinate of the missing data point)

You can get it to work by removing location with the missing value

int <- idw(zinc ~ 1, meuse2[!is.na(meuse2$zinc),],newdata= meuse.grid)