Skip to content

Universal Kriging with factor covariate

5 messages · Edzer Pebesma, piero campa

#
Hi list,
I am applying Universal Kriging (a.k.a. Regression-kriging, etc), and
whether I use a factor variable as covariate (to feed the linear regression
model by lm() call), I get this error:

Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "krige", for signature
"call", "SpatialPointsDataFrame"

The same is for krige.cv.
I managed to run UK by manually adding to my Spatial object explicitly the
dummy 0/1 variables for each category used in the formula of the regression
model; but I wonder if I am doing something wrong or actually the krige
method does not support factor covariates.

Thank you,
Piero Campalani
#
krige()  supports the use of factors. Mailing the error message without
the command that generated it doesn't help us forward. Most useful would
be a small reproducable example.
On 02/09/2011 08:32 PM, piero campa wrote:

  
    
#
Ok sure.
I will hereby simulate the interpolation of a target variable "points"
(whose data is attached) via a single factor predictor "classes" created
randomly over the region of interest:

# libraries
library(sp)
library(gstat)
library(automap)

# Import the point pattern from the txt file and turn it to Spatial
# (set properly the path variable)
points <- read.table(path, header=TRUE)
coordinates(points)<- ~ LON+LAT

# Create grid enclosing the points and fill it with random factor data 
grid <- GridTopology(c(9,43.5), c(.1,.1), c(40, 20))
SPgrid <- SpatialGridDataFrame(grid, data.frame(classes=round(runif(40*20,
1, 10))))
SPgrid$classes <- as.factor(SPgrid$classes)

# Overlay the factor covariate onto the points
ov <- overlay(SPgrid, points)
points$CL <- ov$classes

# Create the linear regression model
lm <- lm(pm10 ~ CL, points)
summary(lm)

# Fit the variogram of the residuals
rvar <- autofitVariogram(residuals(lm) ~ 1, points)

# Regression Kriging
pm10.rk <- krige(lm$call$formula, points, SPgrid, rvar)
--> HERE I HAVE ERROR:
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "krige", for signature
"call", "SpatialPointsDataFrame"

Hope this helps getting closer to the problem,
Piero

http://r-sig-geo.2731867.n2.nabble.com/file/n6011444/2009-045.ARPA.txt
2009-045.ARPA.txt
#
On 02/10/2011 03:28 PM, piero campa wrote:
You wrongly assumed that lm$call$formula is a formula. Try:

krige(as.formula(lm$call$formula), points, #etc

or simply:

krige(pm10 ~ CL, points, ...

Hth,

  
    
#
That's it. Thank you!
Piero

PS:
To let the example work actually before calling krige() you should run this
command:
names(SPgrid) <- "CL"

Moreover the right krige call is the following:
pm10.rk <- krige(as.formula(lm$call$formula), points, SPgrid,
rvar$var_model)