unable to find an inherited method for function ‘krige’ for signature ‘"lm", "SpatialPointsDataFrame"’

1,830 views
Skip to first unread message

Dave Dauphine

unread,
Oct 29, 2014, 7:41:42 PM10/29/14
to davi...@googlegroups.com
Hi!

Can 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 a coordinate system (CRS)?

Thanks!


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)

Eric Holmes

unread,
Oct 29, 2014, 8:44:20 PM10/29/14
to davi...@googlegroups.com
Hi Dave,

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!


library(gstat)
library(maptools)

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

##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)

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--
Eric Holmes
Research Ecologist
Center for Watershed Sciences
University of California, Davis

Dave Dauphine

unread,
Oct 30, 2014, 1:29:59 PM10/30/14
to davi...@googlegroups.com
woohoo, thanks so much eric, D-RUG to the rescue again!

You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/taywzPxWvu4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+...@googlegroups.com.

Dave Dauphine

unread,
Oct 30, 2014, 2:01:39 PM10/30/14
to davi...@googlegroups.com
Hmmm, 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"’ 

Eric Holmes

unread,
Oct 30, 2014, 2:22:12 PM10/30/14
to davi...@googlegroups.com
The error looks like it is not handling the linear model in the first argument of the krige function.  It is hard to diagnose without sample data, but you could try something like this:

krige(formula = logAs1 ~ Elev_m + Basin, data = clean3145, model = as.rvgm, newdata = grd)

Dave Dauphine

unread,
Oct 30, 2014, 3:01:35 PM10/30/14
to davi...@googlegroups.com
merci encore Eric!

Dave Dauphine

unread,
Oct 30, 2014, 3:11:35 PM10/30/14
to davi...@googlegroups.com
still a similar error:   unable to find an inherited method for function ‘krige’ for signature ‘"formula", "missing"’

attached my code and data if your kindness still abounds!

clean3145.csv
lm_oct30.R

Dave Dauphine

unread,
Oct 30, 2014, 4:05:52 PM10/30/14
to davi...@googlegroups.com
here's the code again, not sure the most recent changes were saved on the previous
lm_oct30.R

Eric Holmes

unread,
Oct 30, 2014, 7:18:52 PM10/30/14
to davi...@googlegroups.com
Hey Dave,

So I am not sure if this is what you are looking for, but at least it runs and will give you something to tweak.  I removed some duplicate coordinates at the top of the code as this can cause an error in the krige function (https://stat.ethz.ch/pipermail/r-sig-geo/2012-July/015540.html).

library(gstat)
library(maptools)
library(raster)

clean3145 = read.csv("clean3145.csv")
clean3145 = clean3145[duplicated(paste(clean3145$UTMEM, clean3145$UTMNM, sep="")) != TRUE,]

coordinates(clean3145)=~UTMEM+UTMNM
summary(clean3145)

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

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

coordinates(grd) <- ~UTMEM + UTMNM

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)
class(as.rvgm)

#Regression Kriging (rk)
library(sp)

AS.rk = krige(logAs1 ~ UTMEM + UTMNM, clean3145, grd, model = as.rvgm)

plot(raster(AS.rk))


Dave Dauphine

unread,
Oct 30, 2014, 8:19:51 PM10/30/14
to davi...@googlegroups.com
Thanks so much for sticking with me Eric!

Could you get it to work? It's getting past the krige function for me now, but with a new error message after that:


AS.rk = krige(logAs1 ~ UTMEM + UTMNM, clean3145, grd, model = as.rvgm)
[using universal kriging]
> plot(raster(AS.rk))
Error in plot(raster(AS.rk)) : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': Error: could not find function "raster"

Erica Rettig

unread,
Oct 30, 2014, 8:24:47 PM10/30/14
to davi...@googlegroups.com
I assume raster package isn't loaded?

library(raster)
Thanks,
Erica


Erica Rettig
PhD Candidate
Graduate Group in Ecology, UC Davis

Dave Dauphine

unread,
Oct 31, 2014, 12:24:03 PM10/31/14
to davi...@googlegroups.com
silly me! 

Eric had the code to load it in there but I never installed it, and the warning message got buried in the rest of the output. Thanks Erica!

Now I'll see if it still works with a GAM instead of a LM.

Dave Dauphine

unread,
Oct 31, 2014, 2:42:50 PM10/31/14
to davi...@googlegroups.com
Now could someone help me flip the color scheme? It has high arsenic values in green and low in red. The opposite would be more intuitive, with bad=red. 

I tried to figure this out from the help files, but I'm such a rookie. 

Erica Rettig

unread,
Oct 31, 2014, 3:05:08 PM10/31/14
to davi...@googlegroups.com
Take a look at package RColorBrewer. It makes it easy to set up any color scheme you'd like.

Dave Dauphine

unread,
Oct 31, 2014, 3:09:42 PM10/31/14
to davi...@googlegroups.com
nevermind, found it, thanks again Eric and Erica!

plot(raster(AS.rk), col= colorRampPalette(c("blue", "green", "yellow", "red"))(100)) 

Dave Dauphine

unread,
Oct 31, 2014, 4:28:17 PM10/31/14
to davi...@googlegroups.com
Hi again!

I'm really happy it's working with the log linear model, but when I try to run it with my GAM I get:

> AS.rk = krige(gam3145, clean3145, grd, as.rvgm)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘krige’ for signature ‘"gam", "SpatialPointsDataFrame"’


Here's the entire code:
library(mgcv)
library(gstat)
library(maptools)
library(raster)
library(sp)

#Load database and tell R which variables are coordinates
clean3145 = read.csv("clean3145.csv")
clean3145 = clean3145[duplicated(paste(clean3145$UTMEM, clean3145$UTMNM, sep="")) != TRUE,]
coordinates(clean3145)=~UTMEM+UTMNM
summary(clean3145)

#Create sampling grid. 
#Higher resolution parameter seems to REDUCE resolution
x.range <- as.integer(range(clean3145@coords[, 1]))
y.range <- as.integer(range(clean3145@coords[, 2]))
resolution <- 1000
grd <- expand.grid(UTMEM = seq(from = x.range[1], to = x.range[2], by = resolution), 
                   UTMNM = seq(from = y.range[1],to = y.range[2], by = resolution))
coordinates(grd) <- ~UTMEM + UTMNM
gridded(grd) <- TRUE


#Plot experimental variogram for GAM residuals 
gam3145<-gam(As1 ~ s(UTMEM, UTMNM) + Elev_m + Basin, family=Gamma(link=log), clean3145)
residuals(gam3145)
variogram(residuals(gam3145)~UTMEM+UTMNM, clean3145)

#Fit exponential variogram model
As.rev=variogram(residuals(gam3145)~UTMEM+UTMNM, clean3145)
as.rvgm=fit.variogram(As.rev, vgm(nugget=0.5, model="Exp", range=10000, sill=1.5))
plot(As.rev, as.rvgm, plot.nu=T)
str(as.rvgm)
class(as.rvgm)

#Regression Kriging (rk)
#This shows a map of the residuals, not arsenic itself. 
AS.rk = krige(residuals(gam3145) ~ UTMEM + UTMNM, clean3145, grd, model = as.rvgm)
plot(raster(AS.rk), col= colorRampPalette(c("blue", "green", "yellow", "red"))(100)) 


#These codes didn't work. 
AS.rk = krige(gam3145 ~ UTMEM + UTMNM, clean3145, grd, model = as.rvgm)
AS.rk = krige(gam3145 ~ UTMEM + UTMNM, clean3145, grd, model = as.rvgm)
AS.rk = krige(residuals(gam3145), clean3145, grd, model = as.rvgm)
AS.rk = krige(gam3145, clean3145, grd, as.rvgm)

Dave Dauphine

unread,
Oct 31, 2014, 4:39:12 PM10/31/14
to davi...@googlegroups.com
now I see it's not working with the linear model (logArsenic=river basin +elevation] either, just the log Arsenic values themselves (logAs1). 

That's a great start, but I'm still trying to do regression kriging. 

Happy Halloween and thanks again for all the help so far!
Reply all
Reply to author
Forward
0 new messages