Search code examples
rspatial-interpolationspatial-data-frame

Creating data objects for interpolation methods such as kriging in R


I have daily meanvalues of temperature data at different locations (X1, X2, ...) and I would like to interpolate maps with them. I created a long format data objects by loading them from formatted excel 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 the latitude longitude of the 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 merged them into long-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

with

coordinates(res) = ~longitude+latitude

I can use spplot to plot them at the correct locations, also with country boundaries:

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 single days of the observations, but the idw methods from the packages Ive found (e.g. gstat) need other 'gridded' data objects. How can i create such data objects in order to interpolate them with such methods?


Solution

  • Something like this

    Load necessary packages

    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)
    

    data(wrld_simpl)

    a projected coordinate system to work with

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

    A country (I particularly like this one)

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

    Project it to UTM 33S

    ago <- spTransform(ago, p.utm33n)
    

    Sample some points within polygons

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

    angola

    Some imaginary temperature data for 3 days

    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))
    

    Manage to get it as spatialPointDataFrame object

    coordinates(tdata) <- ~x+y 
    
    proj4string(tdata) <- CRS(proj4string(ago))
    

    Since I don't know your base map, I'll use the country I pick up above The base layer must be a SpatialPixelDataBase. Ill play with a rasterLayer

    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
    

    I can now run idw() from gstat. I'll run with data from day == 1

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

    And finally plot it

    spplot(idw_ago, "var1.pred")
    

    spplot for idw krig

    Now with your data, that I was missing from your question. The same approach

    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

    Hope it helps