Hello,
As I am spatially interpolating rainfall data using the kriging method, I found negative values as provided below.
suppressPackageStartupMessages({
library(tmap)
library(tidyverse)
library(rgdal)
library(rgeos)
library(spatstat)
library(maptools)
library(raster)
library(gstat)
library(sp)
library(gsheet)
})
P_month=gsheet2tbl('https://docs.google.com/spreadsheets/d/1rXB9rG4aRX8Hzyc_O97QcAII9AsiZDlgQ-1JsnMf6ww/edit?usp=sharing')
coordinates(P_month) <- ~lon+lat
proj4string(P_month) <- CRS("+init=epsg:28992")
States=readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_USA_1_sp.rds","rb"))
W <- States %>%
subset(NAME_1%in%c("Alabama","Arkansas", "Florida", "Georgia", "Kentucky", "Louisiana", "Mississippi","Tennessee"))
P_month@bbox <- W@bbox
th <- as(dirichlet(as.ppp(P_month)), "SpatialPolygons")
proj4string(th) <- proj4string(P_month)
grd <- as.data.frame(spsample(P_month, "regular", n=1000000))
names(grd) <- c("X", "Y")
coordinates(grd) <- c("X", "Y")
gridded(grd) <- TRUE
fullgrid(grd) <- TRUE
proj4string(P_month) <- proj4string(P_month)
proj4string(grd) <- proj4string(P_month)
P_month$X <- coordinates(P_month)[,1]
P_month$Y <- coordinates(P_month)[,2]
f.1 <- as.formula(rain ~ X + Y)
var.smpl <- variogram(f.1, P_month, cloud = FALSE)
dat.fit <- fit.variogram(var.smpl, vgm(c("Exp", "Sph", "Gau", "Mat")))
dat.krg <- krige( f.1, P_month, grd, dat.fit)
r.m <- mask(raster(dat.krg), W)
plot(var.smpl, dat.fit)
tm_shape(r.m) +tm_raster(n=5, midpoint = NA)
I assume one of the main reasons is due to the warning from
> dat.fit <- fit.variogram(var.smpl, vgm(c("Exp", "Sph", "Gau", "Mat")))
Warning messages:
1: In fit.variogram(object, x, fit.sills = fit.sills, fit.ranges = fit.ranges, :
No convergence after 200 iterations: try different initial values?
2: In fit.variogram(object, x, fit.sills = fit.sills, fit.ranges = fit.ranges, :
No convergence after 200 iterations: try different initial values?
3: In fit.variogram(object, x, fit.sills = fit.sills, fit.ranges = fit.ranges, :
No convergence after 200 iterations: try different initial values?
I will be using multiple data with a function. So, changing initial values every time won't be feasible.
So, How can I avoid getting negative values of interpolated rainfall?