Creating data objects for interpolation methods, such as kriging in R

I have daily values ​​of temperature data in different places (X1, X2, ...), and I would like to interpolate maps with them. I created data objects with long formats, loading them from excel formatted sheets, such as:

library(reshape2)
tempdata <- read.csv("...", sep=";")
names(tempdata) <- c("date", paste("X", 1:73))
head(tempdata)
#    date  X1  X2  X3  X4  X5  X6  X7
# 1    1  7.3 6.6 6.7 5.8 6.1 6.1 5.5
# 2    2  7.5 6.6 6.6 5.6 4.8 4.7 3.9
# 3    3  8.8 7.7 7.6 7.0 7.0 6.0 5.8
# 4    4  8.5 7.4 7.5 7.0 7.3 5.9 5.5
# 5    5  7.7 6.7 6.9 6.1 6.8 5.1 4.1
# 6    6  7.5 6.7 6.8 6.0 6.4 5.0 4.1

same for longitude latitude locations X1, X2, ...:

lat.lon <- read.csv("...", sep=";")
rownames(lat.lon) <- c(paste0("X",1:73))
head(lat.lon)
#     latitude longitude
#  X1  54.1650    6.3458
#  X2  54.1667    7.4500
#  X3  54.1832    7.8856
#  X4  55.0114    8.4158
#  X5  54.5068    9.5393
#  X6  54.5214   11.0522

I combined them in the long-format format:

res <- merge(
  melt(tempdata, id.vars="date"), 
  lat.lon, 
  by.x="variable", by.y="row.names"
)
head(res)
#  variable     date value latitude longitude
#       X1        1   9.9   54.165    6.3458
#       X1        2   8.9   54.165    6.3458
#       X1        3   7.8   54.165    6.3458
#       X1        4   9.2   54.165    6.3458
#       X1        5   8.7   54.165    6.3458
#       X1        6   8.4   54.165    6.3458

from

coordinates(res) = ~longitude+latitude

I can use spplot to build them in the right places, as well as with the borders of the country:

library(maptools)
load(url('http://gadm.org/data/rda/DEU_adm0.RData'))
GE <- gadm
GE <- spChFIDs(GE, paste("GE", rownames(GE), sep = "_"))
spplot(res["value"], sp.layout = list("sp.polygons", GE), col.regions=bpy.colors(20))

I would like to use IDW for individual days of observation, but the idw methods from the packages found (for example, gstat) need other "data with a grid." How can I create such data objects to interpolate them using such methods?

+4
1

-

kpacks <- c('sp','rgdal', 'gstat', 'raster')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)

(wrld_simpl)

p.utm33n <- CRS("+init=epsg:32633") # UTM 33N Landsat Images

( )

ago <- wrld_simpl[wrld_simpl@data$NAME == 'Angola',]

UTM 33S

ago <- spTransform(ago, p.utm33n)

ago_p <- spsample(ago, type="random", n=25)    
plot(ago, col = 'grey' , axes = T)
plot(ago_p, add = T)

angola

3

tdata <- data.frame(x=rep(coordinates(ago_p)[,1], 3), 
                    y=rep(coordinates(ago_p)[,2], 3),
                    temp=runif(75, 12,35),
                    day = rep(1:3, each = 25))

, spacePointDataFrame

coordinates(tdata) <- ~x+y 

proj4string(tdata) <- CRS(proj4string(ago))

, , SpatialPixelDataBase.

rago <- raster(extent(ago))
res(rago) <- c(10000,10000)
rago[] <- 1
proj4string(rago) <- CRS(proj4string(ago))
r_ago <- mask(rago, ago)
#plot(r_ago)
grid_ago <- as(r_ago, 'SpatialPointsDataFrame')
grid_ago <- grid_ago[!is.na(grid_ago@data$layer), ]
gridded(grid_ago) <- TRUE

idw() gstat. == 1

idw_ago <- idw(temp ~ 1, tdata[tdata$day == 1, ], grid_ago, idp = 2.5)

, ,

spplot(idw_ago, "var1.pred")

spplot for idw krig

, .

library(latticeExtra)
p.dutch <- CRS("+init=epsg:28991") # Dutch National Grid EPSG:28991
load(url('http://gadm.org/data/rda/DEU_adm0.RData'))
ger <- gadm
ger <- spChFIDs(ger, paste("ger", rownames(ger), sep = "_"))
ger <- spTransform(ger, p.dutch)
ger_p <- spsample(ger, type="random", n=25)
plot(ger, col = 'yellow', border = NA, axes = T, cex.axis = 0.6)
plot(ger_p, add = T, pch = 20)

points

tdata <- data.frame(x=rep(coordinates(ger_p)[,1], 3), 
                    y=rep(coordinates(ger_p)[,2], 3),
                    temp=runif(75, 12,35),
                    day = rep(1:3, each = 25))    
coordinates(tdata) <- ~x+y 
proj4string(tdata) <- CRS(proj4string(ger))    
rger <- raster(extent(ger))
res(rger) <- c(10000,10000)
rger[] <- 1
proj4string(rger) <- CRS(proj4string(ger))
r_ger <- mask(rger, ger)
plot(r_ger)
grid_ger <- as(r_ger, 'SpatialPointsDataFrame')
grid_ger <- grid_ger[!is.na(grid_ger@data$layer), ]
gridded(grid_ger) <- TRUE
idw_ger <- idw(temp ~ 1, tdata[tdata$day == 1, ], grid_ger, idp = 2.5)
spplot(idw_ger, "var1.pred") +
latticeExtra::layer(sp.polygons(ger, fill = NA, col = 'blue')) +
latticeExtra::layer(sp.points(tdata[tdata$day == 1, ],
                                fill = NA, col = 'red'))

germany idw spplot

,

+10

Source: https://habr.com/ru/post/1530447/


All Articles