Hi all, 

 

i am trying to use categorical kriging for prediction of soil classes in an 
area. I am pretty new to using cat. kriging in R soI probably am making some 
beginers mistakes. Attached is a reproducable sample with sample data. 

 

When i try to use predict() i geta n error- I am pretty sure because some 
points are duplicated....in krige() function i use zerodist() which eliminates 
the LDL error, but i can't find a way to use it here....

 

I got this code (mostly) from the net, so i have some trouble understanding 
it...

 

thanks for any help and info about categorical kriging, 

 

m

 

setwd("C:/Users/mpavlic/Documents/2-STUDIJ/9-Analiza/2-v2/Kategoricni kriging")
remove(list=ls())

library(lattice)
library(rgdal)
library(sqldf)
library(gstat)


 #LOAD DATA
DF<-read.csv("v290.csv", dec=".", sep=";")
DF$SoilID<-as.factor(DF$SoilID)
DF<-subset(DF, DF$Z < 999)
DF->v290

## create table
#vodja.table<-table(dat [, "SoilID"])
#vodja.table

#nms<-names(vodja.table)[vodja.table<=50]      # change all faktors where count 
is < 50 to  "Drugi"
#tmp<-as.vector(dat[,"Vodja"])
#for (i in nms) {tmp[tmp==i]<-"Drugi"}

##dat[,"SoilID"] <-factor(tmp)
lvls<-levels(v290[,"SoilID"])
print(lvls)


### CREATE GSTAT OBJECT -5 classes
dat.c <- gstat(id = lvls[1], formula = I(SoilID == lvls[1]) ~ 1, loc = ~X + Y, 
data = v290, nmax = 10, set = list(order = 2, zero = 0.01))
dat.c <- gstat(dat.c, lvls[2], I(SoilID == lvls[2]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[3], I(SoilID == lvls[3]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[4], I(SoilID == lvls[4]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[5], I(SoilID == lvls[5]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[6], I(SoilID == lvls[6]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[7], I(SoilID == lvls[7]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[8], I(SoilID == lvls[8]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[9], I(SoilID == lvls[9]) ~ 1, ~X + Y, v290, nmax = 
10)
dat.c <- gstat(dat.c, lvls[10], I(SoilID == lvls[10]) ~ 1, ~X + Y, v290, nmax = 
10)


 #fit variogram
 dat.c <- gstat(dat.c, model = vgm(1, "Sph", 1200, 1), fill.all = T)
 x <- variogram(dat.c)

 # fit lmc
 dat.fit = fit.lmc(x, dat.c, fit.ranges=F, fit.sills=c(T,T,T))
 plot(x, model = dat.fit)
  

 
coordinates(v290)<-~X+Y
DF<-v290


####CREATE PREDICTION GRID
x.range <- as.integer(range(DF@coords[,1]))
y.range <- as.integer(range(DF@coords[,2]))
str(x.range)

grd<-expand.grid(x=seq(from=x.range[1], to=x.range[2], by=100),
y=seq(from=y.range[1], to=y.range[2], by=100))
coordinates(grd)<-~x+y
gridded(grd)<-T


#SAME COORDINATE NAMES
dimnames(coordinates(DF))
dimnames(coordinates(grd))
dimnames(grd@coords) <-list(NULL,c("X", "Y"))
dimnames(grd@bbox) <-list(c("X", "Y"))

#PREDICT
 z<-predict(dat.fit, newdata=grd)             # error "chfactor.c", line 130: 
singular matrix in function LDLfactor()
 str(z)

Attachment: v290.csv
Description: v290.csv

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to