0

Could anyone tell me why I'm getting this "Error in (function (classes, fdef, mtable)"?

I see something about a second "grid" database in the R examples. Do I need to create this? So far my target variables, predictors, and coordinates are all in the same database.

Or do I need to specify coordinate system (CRS)?

Thank you.

library(gstat)
library(maptools)

clean3145 = read.csv(file="clean3145.csv")
coordinates(clean3145)=~UTMEM+UTMNM
summary(clean3145)

#Variogram of LM residuals
logAs<-lm(logAs1 ~ Elev_m + Basin, clean3145)
residuals(logAs)
variogram(residuals(logAs)~UTMEM+UTMNM, clean3145)
plot(variogram(residuals(logAs)~UTMEM+UTMNM, clean3145), plot.nu=T, pch="+")

#Fit variogram
As.rev=variogram(residuals(logAs)~UTMEM+UTMNM, clean3145)
as.rvgm=fit.variogram(As.rev, vgm(nugget=0.25, model="Exp", range=15000, sill=0.6))
plot(As.rev, as.rvgm, plot.nu=T)
str(as.rvgm)

#Regression Kriging (rk)
library(sp)
AS.rk=krige(logAs, clean3145, as.rvgm)

2 Answers2

0

You are correct in thinking you need the grd. This will give you the sampling grid for your kriging interpolation. I modified your code to include a sample grid. You may need to experiment with the resolution parameter to get it to work for you. Good luck!

##Create sampling grid
x.range <- as.integer(range(clean3145@coords[, 1]))
y.range <- as.integer(range(clean3145@coords[, 2]))

resolution <- 4
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = resolution), 
               y = seq(from = y.range[1],to = y.range[2], by = resolution))

coordinates(grd) <- ~x + y
gridded(grd) <- TRUE

#Variogram of LM residuals
logAs<-lm(logAs1 ~ Elev_m + Basin, clean3145)
residuals(logAs)
variogram(residuals(logAs)~UTMEM+UTMNM, clean3145)
plot(variogram(residuals(logAs)~UTMEM+UTMNM, clean3145), plot.nu=T, pch="+")

#Fit variogram
As.rev=variogram(residuals(logAs)~UTMEM+UTMNM, clean3145)
as.rvgm=fit.variogram(As.rev, vgm(nugget=0.25, model="Exp", range=15000, sill=0.6))
plot(As.rev, as.rvgm, plot.nu=T)
str(as.rvgm)

#Regression Kriging (rk)
library(sp)
AS.rk=krige(logAs, clean3145, as.rvgm, newdata = grd)
  • hmm, still getting the same error for some reason! AS.rk=krige(logAs, clean3145, as.rvgm, newdata = grd) Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘krige’ for signature ‘"lm", "SpatialPointsDataFrame"’ – Dave Dauphiné Oct 30 '14 at 18:10
0

Use as the first argument to krige the formula logAs1 ~ Elev_m + Basin, or in any case a formula, instead of the lm object logAs.

Edzer Pebesma
  • 3,814
  • 16
  • 26