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?
The
na.actionargument deals with missing values withinnewdata(notlocationsordata).This is clearly stated in
?idw / ?krige / ?predict.gstatThere is no method to deal with
NAvalues within thelocationsordata(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