# Making data table using linear kriging interpolation with variogram in R

I have a table of data with such structure:

``````longitude_for_data = c(32,68,89,145,176, -14, -42)
latitude_for_data  = c(22,98,21,13 , 16,-134,-102)
Z_for_data         = c(30,40,60,70 , 20,  40, 100)
data_to_analise    = cbind(longitude_for_data,latitude_for_data,Z_for_data)
``````

I want to make grid and fill it using linear kriging with variogram(as a variant -simple kriging):

``````longitude    = seq(-180,180,by=5.0)
latitude     = seq(-180,180,by=2.5)
lat_long_grid= expand.grid(longitude,latitude)
``````

as a result I want to get something like that:

``````   lon  lat Z-kriging
1 -180 -180 32
2 -175 -180 35
3 -170 -180 38
4 -165 -180 39
5 -160 -180 40
6 -155 -180 41
``````

How should I use kriging function to recieve that data table?

• some notes: 1. latitudes outside -90 to +90 degrees are invalid. Are these geographic lat-long coordinates or something else? 2. if they are lat-long degrees then you need to make sure your variogram and kriging is using the great circle distance, otherwise you will end up with a discontinous prediction at the pole and distortions everywhere else. Mar 17, 2019 at 10:23
• I saw this peoblem,thank you. It was my mistake Mar 18, 2019 at 20:51

Package sf also provides sf::st_make_grid

``````library(sf)
library(gstat)

data <- as.data.frame(data_to_analise)
colnames(data)[1:3] <- c("longitude", "latitude", "z")

st.sf <- st_as_sf(x = data, coords = c("longitude", "latitude"), crs=NA)
colnames(st.sf) <- "z"

vgm1 <- gstat::variogram(z~1, st.sf)
fit1 <- gstat::fit.variogram(vgm1, model = gstat::vgm("Gau")) # fit model
krig <- gstat::krige(z~1, st.sf, gbbx, model=fit1)

``````
• You can skip the intermediate `sf` and `data.table` representation by making a data frame and using `sp::coordinates(data)=c("longitude","latitude")`. At least until `gstat` can take `sf` objects! Mar 17, 2019 at 10:25

Thank you very much for your help. I solved problem in such way:

``````library(gstat)
library(sp)
longitude_for_data = c(32,68,89,145,176, -14, -42)
latitude_for_data  = c(22, 8,21,13 , 16,- 34,-12)
Z_for_data         = c(10,20,30,40 , 50,  60, 70)
data_together      = cbind(longitude_for_data,latitude_for_data,Z_for_data)
data_to_analise    = as.data.frame(data_together)
coordinates(data_to_analise) <- ~longitude_for_data + latitude_for_data
proj4string(data_to_analise) <- CRS("+init=epsg:4326")

longitude    = seq(-180,180,by=5.0)
latitude     = seq(-90 ,90 ,by=2.5)

lat_long_together= expand.grid(longitude,latitude)
lat_long_grid    =as.data.frame(lat_long_together)
colnames(lat_long_grid)[1:2] <- c("longitude", "latitude")
coordinates(lat_long_grid) <- ~longitude + latitude
proj4string(lat_long_grid) <- CRS("+init=epsg:4326")
m <- vgm(.59, "Sph", 874, .04)

result= krige(Z_for_data~1, data_to_analise, lat_long_grid, model = m)
``````