rruntime-errorautomap

R: error with autofitVariogram (automap package)


Using autofitVariogram() function from automap package I have generate following error:

Error in vgm_list[[which.min(SSerr_list)]] : attempt to select less than one element in get1index

Example code:

model <- as.formula(Value ~ Elevation)
data <- matrix(c(11.07,42.75,5,62.5,
                 8.73,45.62,234,75,
                 12.62,44.03,12,75,
                 10.87,45.38,67,75,
                 8.79,42.53,64,75),
               nrow = 5, byrow = TRUE)
data <- as.data.frame(data)
names(data) <- c('Lon', 'Lat', 'Elevation', 'Value')
library('sp')
coordinates(data) = ~Lon+Lat
library('automap')
autofitVariogram(model, data)

What causes this error? Do interpolated values cause some kind of 'singularity'?

Thx!


Solution

  • This error is caused by the fact that gstat cannot generate an experimental variogram given this number of observations:

    library(gstat)
    library(sp)
    
    data <- matrix(c(11.07,42.75,5,62.5,
                     8.73,45.62,234,75,
                     12.62,44.03,12,75,
                     10.87,45.38,67,75,
                     8.79,42.53,64,75),
                   nrow = 5, byrow = TRUE)
    data <- as.data.frame(data)
    names(data) <- c('Lon', 'Lat', 'Elevation', 'Value')
    coordinates(data) = ~Lon+Lat
    variogram(Value ~ Elevation, data)
    ## NULL
    

    When given insufficient observations, gstat::variogram returns NULL. This in turn causes autofitVariogram to fail.

    The solution is to simply have more data if you want to use kriging. A rule of thumb is that you need about 30 observations to generate a meaningful variogram to fit a variogram model to.