0

I'm trying to create a semivariogram in R, giving it data from a raster coming from a GeoTif file. The version of Rstudio I'm running is the "Elsbeth Geranium" Release (7d165dcf, 2022-12-03) for Windows and my OS is windows 10 home.

The variogram function from the gstat package is giving me this error:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘gridded’ for signature ‘"data.frame"’

My lines of code were:

library(sp)
library(raster)
library(gstat)
A <- raster("C:/Users/Mark/EX.tif",5) #to create a raster from the fith layer of EX.tif
ptA <- rasterToPoints(A) #to extract the coordinates 
dfA <- as.data.frame(A) #to convert the raster A as a dataframe
v <- gstat::variogram(object = EX_1~1, locations = ptA, data = dfA)

I expected to create a variogram object, obtained that error message.

I hope to finally understand the variogram function, what kind of data it needs as input and how to give this input obtaining it from a GeoTif file.

Edo
  • 7,567
  • 2
  • 9
  • 19
MJHorst
  • 1
  • 3
  • you need to provide a reproducible example. check out [how to to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Edo Mar 22 '23 at 09:46
  • also: use `Sys.setenv(LANG = "en")` at the beginning of your code to get the error in english – Edo Mar 22 '23 at 09:46
  • Ok, I followed your answer to change the language of the environment to english,, now the error message is: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘gridded’ for signature ‘"data.frame"’ – MJHorst Mar 23 '23 at 09:31
  • I'll be reading the data on your link on how to make a reproducible example and I will edit my question as soon as I can – MJHorst Mar 23 '23 at 09:37

1 Answers1

0

the error means that the input class is not correct. you should include a gstat object as input, not a dataframe. Try to transform your dataframe to a spatial object, then calculate the semivariogram:

coordinates(dfA)= ~ x + y

v <- gstat::variogram(object = EX_1 ~1, data = dfA)