This error is commonly returned because you have duplicate locations. You can check this using the sp::zerodist
function.
To remove duplicate locations you call sp::zerodist
within a bracket index.
WeatherData <- WeatherData[-zerodist(WeatherData)[,1],]
Some has already been said by Spacedman in the comments. This warning may not pose a problem, if the variogram looks good. A good option might be to initialize some of the variogram parameters in vgm("Sph")
.
I usually take these values as default:
vario <- variogram(Temperature ~1, WU_data_spatial)
vario.fit <- fit.variogram(vario, vgm(psill=max(vario$gamma)*0.9, model = "Sph", range=max(vario$dist)/2, nugget = mean(vario$gamma)/4))
and change them (or set some of them to NA) until I reach a satisfying result. Although I don't have so much experience as to demonstrate it in this particular case. Also the results would differ on this small sample of data and the whole dataset, on which you may achieve a better fit.
Personally, I would use the package automap
and the function autofitVariogram
to do this.
library(automap)
vario.fit = autofitVariogram(Temperature~1,
WU_data_spatial,
model = c("Sph"),
kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10),
fix.values = c(NA, NA, NA),
start_vals = c(NA,NA,NA),
verbose = T)
plot(vario, vario.fit$var_model, main = "Fitted variogram")
Which gives the following result:
Looking at the dots of the empirical variogram I would think that the spatial correlation is rather poor, but again - I don't feel experienced enough to make any conclusions.
Best Answer
This is caused by the matrix library used by gstat. Historically (gstat was released as open source code in 1997) it used the LDLfactor routine in the meschach library. Around 6 months ago I factored this out this code and replaced it with the BLAS/LAPACK which are native in R. LAPACK uses Choleski decomposition. LDLfactor allows for some non-positive matrices, where Choleski will trigger an error on this.
So, your problem is apparently a non-positive definite matrix which was not detected by LDL, but was detected by Choleski.