Matevž U. Pavlič
Matevž U. Pavlič

Reputation: 1

Kriging in gstat

I have a problem with code that ran 1000 times without error.

Error message:

"Error in bb[, "max"] : subscript out of bounds"

I have no clue what bb means. I thought bbox but that is same for grid and points.

Code:

setwd("C:/Users/matev/desktop/")
getwd()
remove(list=ls())

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

#input data
DF<-read.csv(file="UpperRukaragata.csv", sep=",", dec=".", header=T)
str(DF)

plot(DF$X,DF$Y, col=DF$Z, cex=11, pch=14)

coordinates(DF)<-~X+Y    
str(DF)
summary(DF$Ta2O5)
plot(DF)

# 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]+2.5, to=x.range[2]-2.5, by=5), y=seq(from=y.range[1]+2.5, to=y.range[2]-2.5, by=10))
coordinates(grd)<-~x+y
gridded(grd)<-T
plot(grd)

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

bbox(DF)
bbox(grd)

#variogram  Z
v1<-variogram (Ta2O5~1, loc=DF)
plot(v1, type="p", plot.numbers=F, cex=0.65, pch=16)
vm1<-vgm(70, "Pen",70, 0)
(vmf1<-fit.variogram(v1, vm1))
print(plot(v1, plot.numbers = F, pch = 20, col = "darkblue", model = vmf1))

##ordinary kriging
Ta2O5<-krige(Ta2O5~1, DF, grd, model=vmf1 )

How to solve this problem?

Upvotes: 0

Views: 148

Answers (1)

Alexandre Wadoux
Alexandre Wadoux

Reputation: 181

It is not clear to me why some lines are there, comment the following:

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

Now you can run everything and it works. Simpler better.

setwd("C:/Users/matev/desktop/")
getwd()
remove(list=ls())

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

#input data
DF<-read.csv(file="UpperRukaragata.csv", sep=",", dec=".", header=T)
str(DF)

plot(DF$X,DF$Y, col=DF$Z, cex=11, pch=14)

coordinates(DF)<-~X+Y    
str(DF)
summary(DF$Ta2O5)
plot(DF)

# 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]+2.5, to=x.range[2]-2.5, by=5), y=seq(from=y.range[1]+2.5, to=y.range[2]-2.5, by=10))
coordinates(grd)<-~x+y
gridded(grd)<-T
plot(grd)

## Why doing this? comment it

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

bbox(DF)
bbox(grd)

#variogram  Z
v1<-variogram (Ta2O5~1, loc=DF)
plot(v1, type="p", plot.numbers=F, cex=0.65, pch=16)
vm1<-vgm(70, "Pen",70, 0)
(vmf1<-fit.variogram(v1, vm1))
print(plot(v1, plot.numbers = F, pch = 20, col = "darkblue", model = vmf1))

##ordinary kriging
Ta2O5<-krige(Ta2O5~1, DF, grd, model=vmf1 )

spplot(Ta2O5)

Upvotes: 1

Related Questions