Problem to create semivariogram in Rstudio

I would like to create a semivariogram. I am using these commands:

install.packages("gstat")

library(gstat)

install.packages("sf")

library(sf)

install.packages("sp")

library(sp)

install.packages("geoR")

library(geoR)

install.packages("devtools")

library(devtools)

#library('mapping')

TheData=read.csv("C:/Users/hp/Desktop/myfile.csv")

plot(TheData$Var5,TheData$Var4)

#load the sp library
library(sp)

#remove any null data rows
TheData=na.omit(TheData)

#convert simple data frame into a spatial data frame object
coordinates(TheData)= ~ Var4+Var5

#create a bubble plot with the random values
##bubble(TheData, zcol='m_rand', fill=TRUE, do.sqrt=FALSE, maxsize=3)

#TheVariogram=variogram(m_rand~1, data=TheData)
#plot(TheVariogram)

###fit_var = gstat::fit.variogram(object = , model = )
###TheVariogramModel <- vgm(psill=0.15, model="Gau", nugget=0.0001, range=5)

f_spdf= sp::SpatialPointsDataFrame(coords = cbind(TheData$Var5,TheData$Var4), data=TheData, proj4string = sp::CRS(projargs = "+ init=epsg:32631"))

vario = gstat::variogram(object = TheData ~ 1, locations = f_spdf)

but command window shows me:

Error in validObject(.Object) :
invalid class “SpatialPointsDataFrame” object: invalid object for slot "data" in class "SpatialPointsDataFrame": got class "SpatialPointsDataFrame", should be or extend class "data.frame"

Could you please help me?

This may be due to use of coordinates() to make a spatial points data frame. I would recommend making it explicitly:

library(sp)
spdf <- SpatialPointsDataFrame(coords = TheData[,c(5,4)],
                             data = data.frame(Var_n = TheData[,"Var_n"]),
                             proj4string = CRS(projargs = "+ init=epsg:32631")

Also your code seems to be passing a data fame to variogram(). Try passing a variable instead:

library(gstat)
vario <- variogram(object = spdf@data, locations = spdf)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.