Spatial Methods in R

Worked examples from Pebesma and Bivand 2019

In [1]:
rm(list = ls())
In [2]:
# install.packages("starsdata", repos = "http://pebesma.staff.ifgi.de", type = "source")
# install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/", type = "source")
Installing package into ‘/home/alal/R/x86_64-pc-linux-gnu-library/3.6’
(as ‘lib’ is unspecified)

In [ ]:
library(LalRUtils)
libreq(lfe, data.table, tidyverse, magrittr, sf, 
      stars, mapview, starsdata, spacetime, spDataLarge,
       gstat, tmap, spdep, spatialreg, reticulate,
      xts, units, rnaturalearth, classInt, viridis)
theme_set(lal_plot_theme())
set.seed(42)
In [2]:
root = '/home/alal/tmp/r_spatial'
setwd(root)
      wants           loaded
 [1,] "lfe"           TRUE  
 [2,] "data.table"    TRUE  
 [3,] "tidyverse"     TRUE  
 [4,] "magrittr"      TRUE  
 [5,] "sf"            TRUE  
 [6,] "stars"         TRUE  
 [7,] "mapview"       TRUE  
 [8,] "starsdata"     TRUE  
 [9,] "spacetime"     TRUE  
[10,] "spDataLarge"   TRUE  
[11,] "gstat"         TRUE  
[12,] "tmap"          TRUE  
[13,] "spdep"         TRUE  
[14,] "spatialreg"    TRUE  
[15,] "reticulate"    TRUE  
[16,] "xts"           TRUE  
[17,] "units"         TRUE  
[18,] "rnaturalearth" TRUE  
[19,] "classInt"      TRUE  
[20,] "viridis"       TRUE  

Basics

In [6]:
nc = system.file("gpkg/nc.gpkg", package="sf") %>%
    read_sf()
nc %>%
    st_transform(32119) %>%
    select(BIR74) %>%
    plot(graticule = TRUE, axes = TRUE)
In [7]:
nc %>%
    st_transform(32119) %>%
    select(BIR74) %>%
    mapview(zcol = "BIR74", legend = TRUE, 
            col.regions = sf.colors)

Geometries

Simple Features

In [8]:
(pt = st_point(c(0,1)))
attributes(pt)
POINT (0 1)
$class =
  1. 'XY'
  2. 'POINT'
  3. 'sfg'
In [9]:
system.file("shape/storms_xyz_feature.shp", package="sf") %>%
    st_read()
Reading layer `storms_xyz_feature' from data source `/home/alal/R/x86_64-pc-linux-gnu-library/3.6/sf/shape/storms_xyz_feature.shp' using driver `ESRI Shapefile'
Simple feature collection with 71 features and 1 field
geometry type:  LINESTRING
dimension:      XYZ
bbox:           xmin: -102.2 ymin: 8.3 xmax: 0 ymax: 59.5
epsg (SRID):    NA
proj4string:    NA

Raster and Vector Datacubes

In [10]:
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x = read_stars(tif)
In [11]:
x
stars object with 3 dimensions and 1 attribute
attribute(s):
  L7_ETMs.tif    
 Min.   :  1.00  
 1st Qu.: 54.00  
 Median : 69.00  
 Mean   : 68.91  
 3rd Qu.: 86.00  
 Max.   :255.00  
dimension(s):
     from  to  offset delta                       refsys point values    
x       1 349  288776  28.5 +proj=utm +zone=25 +south... FALSE   NULL [x]
y       1 352 9120761 -28.5 +proj=utm +zone=25 +south... FALSE   NULL [y]
band    1   6      NA    NA                           NA    NA   NULL    
In [14]:
plot(x)
In [15]:
par(mfrow = c(1, 2))
plot(x, rgb = c(3,2,1), reset = FALSE, main = "RGB")    # rgb
plot(x, rgb = c(4,3,2), main = "False color (NIR-R-G)") # false color
In [16]:
log(x)
stars object with 3 dimensions and 1 attribute
attribute(s):
  L7_ETMs.tif   
 Min.   :0.000  
 1st Qu.:3.989  
 Median :4.234  
 Mean   :4.116  
 3rd Qu.:4.454  
 Max.   :5.541  
dimension(s):
     from  to  offset delta                       refsys point values    
x       1 349  288776  28.5 +proj=utm +zone=25 +south... FALSE   NULL [x]
y       1 352 9120761 -28.5 +proj=utm +zone=25 +south... FALSE   NULL [y]
band    1   6      NA    NA                           NA    NA   NULL    
In [17]:
ndvi = function(x) (x[4]-x[3])/(x[4]+x[3])
st_apply(x, c("x", "y"), ndvi)
stars object with 2 dimensions and 1 attribute
attribute(s):
     ndvi          
 Min.   :-0.75342  
 1st Qu.:-0.20301  
 Median :-0.06870  
 Mean   :-0.06432  
 3rd Qu.: 0.18667  
 Max.   : 0.58667  
dimension(s):
  from  to  offset delta                       refsys point values    
x    1 349  288776  28.5 +proj=utm +zone=25 +south... FALSE   NULL [x]
y    1 352 9120761 -28.5 +proj=utm +zone=25 +south... FALSE   NULL [y]

Proxy objects

In [19]:
granule = system.file("sentinel/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.zip", package = "starsdata")
file.size(granule)
769461997
In [20]:
base_name = strsplit(basename(granule), ".zip")[[1]]
s2 = paste0("SENTINEL2_L1C:/vsizip/", granule, "/", base_name, ".SAFE/MTD_MSIL1C.xml:10m:EPSG_32632")
(p = read_stars(s2, proxy = TRUE))
stars_proxy object with 1 attribute in file:
$`MTD_MSIL1C.xml:10m:EPSG_32632`
[1] "SENTINEL2_L1C:/vsizip//home/alal/R/x86_64-pc-linux-gnu-library/3.6/starsdata/sentinel/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.zip/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.SAFE/MTD_MSIL1C.xml:10m:EPSG_32632"

dimension(s):
     from    to offset delta                       refsys point values    
x       1 10980  3e+05    10 +proj=utm +zone=32 +datum...    NA   NULL [x]
y       1 10980  6e+06   -10 +proj=utm +zone=32 +datum...    NA   NULL [y]
band    1     4     NA    NA                           NA    NA   NULL    
In [21]:
plot(p)

Data Cubes

In [23]:
data(air) # this loads several datasets in .GlobalEnv
dim(air)
space
70
time
4383
In [24]:
d = st_dimensions(station = st_as_sfc(stations), time = dates)
(aq = st_as_stars(list(PM10 = air), dimensions = d))
stars object with 2 dimensions and 1 attribute
attribute(s):
     PM10        
 Min.   :  0.00  
 1st Qu.:  9.92  
 Median : 14.79  
 Mean   : 17.70  
 3rd Qu.: 21.99  
 Max.   :274.33  
 NA's   :157659  
dimension(s):
        from   to     offset  delta                       refsys point
station    1   70         NA     NA +proj=longlat +datum=WGS8...  TRUE
time       1 4383 1998-01-01 1 days                         Date FALSE
                                                         values
station POINT (9.585911 53.67057),...,POINT (9.446661 49.24068)
time                                                       NULL
In [25]:
image(aperm(log(aq), 2:1), main = "NA pattern (white) in PM10 station time series")
In [26]:
plot(st_as_sf(st_apply(aq, 1, mean, na.rm = TRUE)), 
     reset = FALSE, pch = 16,
    ylim = st_bbox(DE)[c(2,4)])
plot(DE, add=TRUE)
In [29]:
(a = aggregate(aq, st_as_sf(DE_NUTS1), mean, na.rm = TRUE))
a %>% filter(time >= "2008-01-01", time < "2008-01-08") %>% plot(key.pos = 4)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
although coordinates are longitude/latitude, st_intersects assumes that they are planar
stars object with 2 dimensions and 1 attribute
attribute(s):
     PM10         
 Min.   :  1.075  
 1st Qu.: 10.886  
 Median : 15.316  
 Mean   : 17.888  
 3rd Qu.: 21.811  
 Max.   :172.267  
 NA's   :25679    
dimension(s):
         from   to     offset  delta                       refsys point
geometry    1   16         NA     NA +proj=longlat +datum=WGS8... FALSE
time        1 4383 1998-01-01 1 days                         Date FALSE
                                                                    values
geometry MULTIPOLYGON (((9.65046 49....,...,MULTIPOLYGON (((10.77189 51...
time                                                                  NULL
In [30]:
plot(as.xts(a)[,4], main = DE_NUTS1$NAME_1[4])
In [31]:
plot(st_geometry(bristol_zones), axes = TRUE, graticule = TRUE)
plot(st_geometry(bristol_zones)[33], col = 'red', add = TRUE)
In [32]:
bristol_od %>% head
A tibble: 6 × 7
odallbicyclefootcar_drivertrain
<chr><chr><dbl><dbl><dbl><dbl><dbl>
E02002985E02002985209512759 0
E02002985E020029871217 3562 0
E02002985E02003036 322 110 1
E02002985E020030431411 25617
E02002985E02003049 562 436 0
E02002985E02003054 424 021 0
In [34]:
bristol_tidy <- bristol_od %>% select(-all) %>% gather("mode", "n", -o, -d)
head(bristol_tidy)
od = bristol_tidy %>% pull("o") %>% unique

Building data cube

In [35]:
nod = length(od)
mode = bristol_tidy %>% pull("mode") %>% unique
nmode = length(mode)
a = array(0L,  c(nod, nod, nmode), 
    dimnames = list(o = od, d = od, mode = mode))
a[as.matrix(bristol_tidy[c("o", "d", "mode")])] = bristol_tidy$n
order = match(od, bristol_zones$geo_code) # it happens this equals 1:102
zones = st_geometry(bristol_zones)[order]
(d = st_dimensions(o = zones, d = zones, mode = mode))
     from  to offset delta                       refsys point
o       1 102     NA    NA +proj=longlat +datum=WGS8... FALSE
d       1 102     NA    NA +proj=longlat +datum=WGS8... FALSE
mode    1   4     NA    NA                           NA FALSE
                                                                values
o    MULTIPOLYGON (((-2.510462 5...,...,MULTIPOLYGON (((-2.55007 51...
d    MULTIPOLYGON (((-2.510462 5...,...,MULTIPOLYGON (((-2.55007 51...
mode                                                 bicycle,...,train
In [36]:
(odm = st_as_stars(list(N = a), dimensions = d))
stars object with 3 dimensions and 1 attribute
attribute(s):
       N           
 Min.   :   0.000  
 1st Qu.:   0.000  
 Median :   0.000  
 Mean   :   4.802  
 3rd Qu.:   0.000  
 Max.   :1296.000  
dimension(s):
     from  to offset delta                       refsys point
o       1 102     NA    NA +proj=longlat +datum=WGS8... FALSE
d       1 102     NA    NA +proj=longlat +datum=WGS8... FALSE
mode    1   4     NA    NA                           NA FALSE
                                                                values
o    MULTIPOLYGON (((-2.510462 5...,...,MULTIPOLYGON (((-2.55007 51...
d    MULTIPOLYGON (((-2.510462 5...,...,MULTIPOLYGON (((-2.55007 51...
mode                                                 bicycle,...,train
In [37]:
plot(odm[,,33] + 1, logz = TRUE)
Warning message in st_as_sf.stars(x):
“working on the first sfc dimension only”Warning message in st_bbox.dimensions(st_dimensions(obj), ...):
“returning the bounding box of the first geometry dimension”Warning message in st_bbox.dimensions(st_dimensions(obj), ...):
“returning the bounding box of the first geometry dimension”

Manipulating Geometries

In [39]:
demo(nc, ask = FALSE, echo = FALSE)
Reading layer `nc.gpkg' from data source `/home/alal/R/x86_64-pc-linux-gnu-library/3.6/sf/gpkg/nc.gpkg' using driver `GPKG'
Simple feature collection with 100 features and 14 fields
Attribute-geometry relationship: 0 constant, 8 aggregate, 6 identity
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs
In [40]:
par(mar = rep(0,4), mfrow = c(1, 2))
plot(st_geometry(nc)[1], col = NA, border = 'black')
plot(st_convex_hull(st_geometry(nc)[1]), add = TRUE, col = NA, border = 'red')
box()
set.seed(131)
mp = st_multipoint(matrix(runif(20), 10))
plot(mp)
plot(st_voronoi(mp), add = TRUE, col = NA, border = 'red')
box()

Feature Attributes

In [41]:
system.file("gpkg/nc.gpkg", package="sf") %>%
    read_sf() %>%
    st_transform(32119) %>%
    select(BIR74, SID74, NAME) %>%
    st_centroid() %>%
    head(n = 1) -> x # save as x
Warning message in st_centroid.sf(.):
“st_centroid assumes attributes are constant over geometries of x”
In [42]:
nc <- system.file("gpkg/nc.gpkg", package="sf") %>%
    read_sf() %>%
    st_transform(32119)
nc1 <- nc %>% select(BIR74, SID74, NAME) %>%
    st_set_agr(c(BIR74 = "aggregate", SID74 = "aggregate", NAME = "identity"))

Spatial Join

In [43]:
a = st_sf(a = 1:2, geom = st_sfc(st_point(c(0,0)), st_point(c(1,1))))
b = st_sf(b = 3:4, geom = st_sfc(st_linestring(rbind(c(2,0), c(0,2))),
                                 st_point(c(1,1))))
st_join(a, b)
A sf: 3 × 3
abgeom
<int><int><POINT>
11NAPOINT (0 0)
22 3POINT (1 1)
2.12 4POINT (1 1)
In [44]:
p1 = st_polygon(list(rbind(c(0,0), c(4,0), c(4,4), c(0,4), c(0,0))))
d1 = st_sf(a = c(3,1), geom = st_sfc(p1, p1 + c(4, 0)))
d2 = st_sf(b = c(4), geom = st_sfc(p1 * .75 + c(3, 2)))
In [45]:
(i = st_intersection(d1, d2))
Warning message:
“attribute variables are assumed to be spatially constant throughout all geometries”
A sf: 2 × 3
abgeom
<dbl><dbl><POLYGON>
34POLYGON ((3 4, 4 4, 4 2, 3 ...
14POLYGON ((4 2, 4 4, 6 4, 6 ...
In [46]:
plot(d1, xlim = c(0,8), ylim = c(0, 6), col = NA, border = 1, reset = FALSE)
plot(d2, col = NA, border = 'red', add = TRUE, lwd = 2)
plot(d1, xlim = c(0,8), ylim = c(0, 6), col = NA, border = 1, lwd = 2, reset = FALSE)
plot(d2, col = NA, border = 'red', add = TRUE, lwd = 3)
plot(st_geometry(i), add = TRUE, col = grey(c(.7,.9)), , border = 'green', lwd = 1)

Reference Systems

  • a coordinate system is a set of mathematical rules for specifying how coordinates are to be assigned to points
  • a datum is a parameter or set of parameters that define the position of the origin, the scale, and the orientation of a coordinate system,
  • a geodetic datum is a datum describing the relationship of a two- or three-dimensional coordinate system to the Earth, and
  • a coordinate reference system is a coordinate system that is related to an object by a datum; for geodetic and vertical datums, the object will be the Earth.
In [3]:
(a = set_units(1:3, m))
a_km = set_units(1:3, km)
a + a_km
Units: [m]
[1] 1 2 3
Units: [m]
[1] 1001 2002 3003
In [4]:
b = set_units(4:6, s)
a / b
Units: [m/s]
[1] 0.25 0.40 0.50
In [5]:
(d = as.Date("1970-02-01"))
as.numeric(d)
31

CRS Class

In [6]:
st_crs(4326)
st_crs("+proj=longlat")
Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"
Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +ellps=WGS84 +no_defs"
In [10]:
DE = st_geometry(ne_countries(country = "germany", returnclass = "sf"))
DE.eqc = st_transform(DE, "+proj=eqc +lat_ts=51.14 +lon_0=90w")

par(mfrow = c(1, 2))
plot(DE, axes = TRUE)
plot(DE.eqc, axes = TRUE)
In [12]:
r = rnorm(100)
(cI <- classIntervals(r))
style: quantile
  one of 14,887,031,544 possible partitions of this variable into 8 classes
  [-2.99309,-1.106661) [-1.106661,-0.6166935) [-0.6166935,-0.212842) 
                    13                     12                     13 
[-0.212842,0.08979677) [0.08979677,0.4595165)  [0.4595165,0.6615581) 
                    12                     12                     13 
  [0.6615581,1.303997)    [1.303997,2.286645] 
                    12                     13 
In [13]:
(ls = st_sfc(st_linestring(rbind(c(-179.5, 52), c(179.5, 52))), crs = 4326))
st_length(ls)
LINESTRING (-179.5 52, 179.5 52)
Geometry set for 1 feature 
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: -179.5 ymin: 52 xmax: 179.5 ymax: 52
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
68677.47 [m]

Base and Grid Plots

In [14]:
system.file("gpkg/nc.gpkg", package="sf") %>% read_sf -> nc
plot(nc)
Warning message:
“plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all”
In [15]:
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x = read_stars(tif)
par(mfrow = c(1, 2))
plot(x[,1:10,1:10,1], text_values=TRUE, key.pos = NULL, col = viridis::viridis(10), 
    reset = FALSE)
plot(x[,1:10,1:10,1:3], rgb=1:3, interpolate = TRUE, reset = FALSE)
In [19]:
nc %>% st_transform(32119) %>% 
    ggplot(., aes(fill = BIR74)) + geom_sf() + theme_void() +
    scale_fill_gradientn(colors = viridis::viridis(20))
In [21]:
nc_3857 <- sf::st_transform(nc, "+init=epsg:3857")
ggplot() + 
    geom_sf(data = nc_3857[1:3, ], aes(fill = AREA)) + 
    geom_sf_label(data = nc_3857[1:3, ], aes(label = NAME))
In [24]:
system.file("tif/L7_ETMs.tif", package = "stars") %>% read_stars() -> x
g = ggplot() + 
    coord_equal() + 
    scale_fill_viridis() + 
    theme_void() +
    scale_x_discrete(expand=c(0,0)) +
    scale_y_discrete(expand=c(0,0))
g + geom_stars(data = x) + 
    facet_wrap(~band)

Geostatistics

In [6]:
files = list.files("aq", pattern = "*.csv", full.names = TRUE)
files[1:5]
  1. 'aq/DE_8_3407_2017_timeseries.csv'
  2. 'aq/DE_8_3413_2017_timeseries.csv'
  3. 'aq/DE_8_3417_2017_timeseries.csv'
  4. 'aq/DE_8_3421_2017_timeseries.csv'
  5. 'aq/DE_8_3427_2017_timeseries.csv'
In [7]:
r = lapply(files[-1], function(f) read.csv(f))
In [8]:
Sys.setenv(TZ = "UTC") # make sure times are not interpreted as DST
r = lapply(r, function(f) {
        f$t = as.POSIXct(f$DatetimeBegin) 
        f[order(f$t), ] 
    }
)
In [9]:
r = r[sapply(r, nrow) > 1000]
names(r) =  sapply(r, function(f) unique(f$AirQualityStationEoICode))
length(r) == length(unique(names(r)))
TRUE
In [10]:
r = lapply(r, function(f) xts(f$Concentration, f$t))
aq = do.call(cbind, r)
In [31]:
sel = apply(aq, 2, function(x) sum(is.na(x)) < 0.75 * 365 * 24)
aqsel = aq[, sel] # stations are in columns
aqsel %>% head()
                    DENW247 DEBW023 DESL013 DEBW033  DEST112  DEBY052 DEBB021
2017-01-01 00:00:00  43.226    30.5   38.33    39.1 37.47440 31.80421   19.76
2017-01-01 01:00:00  42.425    29.2   30.06    37.0 19.05360 30.76408   13.86
2017-01-01 02:00:00  42.189    29.2   28.17    37.9 14.42505 28.24502   11.75
2017-01-01 03:00:00  38.025    28.5   27.06    36.6 15.64640 27.04142   10.92
2017-01-01 04:00:00  39.939    26.7   20.62    36.5 12.52430 27.00222   13.13
2017-01-01 05:00:00  42.736    26.2   18.33    35.4 15.35040 28.38938   13.44
                    DEHB001  DERP021 DESH053 DENI054 DESH033  DEHE045  DERP023
2017-01-01 00:00:00   44.72 31.08788  38.423 42.0556  25.056 34.46519 34.97631
2017-01-01 01:00:00   38.75 31.18070  37.632      NA  21.522 28.98998 31.70201
2017-01-01 02:00:00   33.52 31.75698  38.612 21.5820  22.175 27.23417 30.70175
2017-01-01 03:00:00   29.54 32.57169  34.101 22.0537  22.954 26.49708 27.08300
2017-01-01 04:00:00   27.34 33.12492  34.019 22.3240  23.067 26.51819 21.97022
2017-01-01 05:00:00   30.26 32.70103  34.306 22.3994  22.723 28.55110 21.48180
                     DEBY033  DEBY111 DENI143  DEBY115  DEHE062 DEBB092
2017-01-01 00:00:00 36.04885 36.70370 39.7877 60.61040 55.08487   25.98
2017-01-01 01:00:00 34.62919 36.34808      NA 77.05551 58.47018   13.58
2017-01-01 02:00:00 32.94854 36.60906 32.5003 74.67794 54.29801   14.63
2017-01-01 03:00:00 32.20955 36.78688 26.4509 78.36523 55.23328   17.03
2017-01-01 04:00:00 30.91130 36.99146 31.8329 64.64854 49.56955   18.90
2017-01-01 05:00:00 30.47154 37.32415 33.9264 61.57118 49.36127   19.43
                     DEBY053  DEHE041 DENI031 DEHH073 DESH023 DEBE063 DEBW039
2017-01-01 00:00:00 36.63488 54.18967 28.0322  35.045  34.951   56.11    24.9
2017-01-01 01:00:00 36.87674 47.24425      NA  33.364  34.359   65.55    23.1
2017-01-01 02:00:00 36.02590 45.67839 25.9572  32.255  38.191   50.57    22.3
2017-01-01 03:00:00 36.40639 46.96411 25.4800  31.083  31.750   53.67    20.9
2017-01-01 04:00:00 36.48669 45.17942 23.7186  26.336  29.142   45.46      NA
2017-01-01 05:00:00 37.16068 47.52178 23.5214  22.902  27.530   46.79    20.9
                     DEST103  DEHE046 DESH030 DENW338 DEBB073 DEBB053 DEBW056
2017-01-01 00:00:00 43.83540 38.25651  39.243  42.087   43.67   13.72    39.3
2017-01-01 01:00:00 33.87375 38.66923  40.366  39.127   32.64   10.98      NA
2017-01-01 02:00:00 28.27585 39.57899  39.555  37.091   22.87   15.24    29.0
2017-01-01 03:00:00 27.37760 39.47057  39.794  39.392   22.98   16.90    25.9
2017-01-01 04:00:00 21.01530 39.22874  34.380      NA   21.01   13.68    24.2
2017-01-01 05:00:00 20.70240 39.11329  30.764  39.128   20.45   15.02    23.4
                    DENI070 DENI038 DESH055 DEBW031 DENW010 DEBB067 DEBB063
2017-01-01 00:00:00 25.3123 56.9538  35.116     1.0  44.773   21.30   24.66
2017-01-01 01:00:00 24.2816 55.8005  36.502     1.0  37.483   12.39   20.74
2017-01-01 02:00:00 26.0283 50.6678  37.449      NA  42.074   12.38   18.82
2017-01-01 03:00:00 25.1326      NA  33.365     0.4  33.456   10.11   17.23
2017-01-01 04:00:00      NA 40.1870  31.931     0.4  44.217    9.80   16.56
2017-01-01 05:00:00 27.9597 40.6780  30.643     0.3  45.163   11.95   14.67
                    DENW114 DENW337 DEHH047 DEHH050 DENW134  DEHE040  DEHE039
2017-01-01 00:00:00  37.018  31.705  38.458  36.348      NA 45.42385 40.90352
2017-01-01 01:00:00  38.488  37.889  38.069  32.774  44.291 47.20251 39.54010
2017-01-01 02:00:00  38.892  37.836  35.072  31.188  44.873 48.15382 37.76388
2017-01-01 03:00:00  39.509  36.218  35.952  29.023  39.599 45.72921 37.74690
2017-01-01 04:00:00  40.508  33.961  32.471  24.454  42.297 44.83478 40.97856
2017-01-01 05:00:00  43.209  32.846  28.040  20.984  42.375 46.53692 42.03893
                    DEBE018 DEUB028 DEBW147 DENI063 DEHH079 DENI051 DEHH064
2017-01-01 00:00:00   45.55   15.72    55.0 35.7591  40.605      NA  40.670
2017-01-01 01:00:00   39.69   15.97    63.5 34.8880  36.402 1.91075  41.965
2017-01-01 02:00:00   25.09   15.42    71.5 34.1970  33.659 1.63579  39.177
2017-01-01 03:00:00   22.87   16.88    65.6 32.9647  33.000 2.16219  37.745
2017-01-01 04:00:00   21.78   17.41    58.0 27.4854  29.967 2.65927  34.223
2017-01-01 05:00:00   24.99   19.93    50.7 24.5228  27.078 3.86665  29.880
                     DEHE131 DEHH081  DEBY196 DEMV023  DERP009  DERP010 DEBW098
2017-01-01 00:00:00 43.77301  40.607 35.69417    32.8 41.58068 46.04778    46.1
2017-01-01 01:00:00 44.14619  38.389 37.80980    30.1 40.37899 45.79980    51.2
2017-01-01 02:00:00 40.86524  37.048 33.98867    28.4 38.77568 41.03337    51.9
2017-01-01 03:00:00 36.45972  36.861 29.35781    27.0 40.43807 40.53645    47.8
2017-01-01 04:00:00 36.28900  37.417 25.18104    23.3 37.78670 39.14986    43.9
2017-01-01 05:00:00 37.56973  34.707 24.75849    21.3 34.20705 36.40031    44.4
                     DEST015 DENW022  DEBY032 DEBW019  DEHE001 DENW259 DENW260
2017-01-01 00:00:00 34.48490  35.060 30.11113    37.2 42.86374  40.759  59.775
2017-01-01 01:00:00 31.02770  35.566 32.18087      NA 42.65577  46.404  56.942
2017-01-01 02:00:00 32.26665  39.657 30.69143    31.9 42.75205  39.974  53.306
2017-01-01 03:00:00 32.36105  36.390 33.80033    28.7 41.93159  34.321  48.294
2017-01-01 04:00:00 31.12250  37.526 36.43603    27.9 42.02798  37.824  45.130
2017-01-01 05:00:00 31.57925  41.221 37.06125    30.3 43.04025  37.051  46.276
                    DENW064 DENW367 DEUB046  DERP013 DEBW076 DEBB044 DENI020
2017-01-01 00:00:00   2.345  44.991    0.27 46.85270    36.1   37.39 34.8170
2017-01-01 01:00:00   2.689  44.384    0.25 44.76789    36.4   33.62 26.8946
2017-01-01 02:00:00   2.547  41.961    0.01 45.09580      NA   28.02 23.4045
2017-01-01 03:00:00   3.002  41.937    0.01 45.72791    36.6   29.87 18.7576
2017-01-01 04:00:00  10.397  41.905    0.01 43.57825    36.4   29.40 21.9841
2017-01-01 05:00:00  15.656  41.178    0.22 40.63902    36.5   29.27 22.2123
                     DEST011 DEBB054 DENI042 DEHH072 DEHH015  DEBY058 DEBW125
2017-01-01 00:00:00 39.50430   62.47 39.2805  35.681      NA 33.44949    39.0
2017-01-01 01:00:00 31.03320   51.96 35.7386  33.080  40.789 38.21992    41.5
2017-01-01 02:00:00 24.52885   55.88 36.9947  31.721  39.545 34.21811    41.6
2017-01-01 03:00:00 20.14705   58.60 38.5251  30.714  37.824 30.82909    38.2
2017-01-01 04:00:00 17.45045   38.92 38.1569  26.084  35.626 30.75834    37.4
2017-01-01 05:00:00 14.79250   57.83 38.6477  22.591  31.354 32.43708    36.3
                     DEST075  DEBY089 DEBE065  DEHE061 DEBW116 DEBB045 DEBE069
2017-01-01 00:00:00 41.95185 38.87765   54.16 48.67126    68.4   44.85   50.79
2017-01-01 01:00:00 41.59185 38.28207   41.63 47.93678    76.6   24.44   46.12
2017-01-01 02:00:00 42.08420 39.69885   28.53 46.84516    72.0   21.49   39.75
2017-01-01 03:00:00 39.40900 39.36425   30.33 44.22927    80.9   22.33   35.54
2017-01-01 04:00:00 36.03370 40.41299   31.12 43.58505    72.1   23.70   27.08
2017-01-01 05:00:00 32.66150 41.36708   31.01 43.33587    63.3   22.94   25.97
                    DESN092 DEBW029  DEBY113 DESH022 DEMV021  DERP045 DESN084
2017-01-01 00:00:00  39.703    33.8 32.78220  33.126    23.5 33.65971  48.898
2017-01-01 01:00:00  40.257    32.5 32.39597  30.087    20.3 36.14664  48.609
2017-01-01 02:00:00  40.143    32.5 31.65220  29.772    22.5 34.75060  44.669
2017-01-01 03:00:00  42.055    33.6 32.09483  28.440    25.2 35.55517  43.420
2017-01-01 04:00:00  42.495    34.0 32.58430  25.186    28.6 35.21961  44.189
2017-01-01 05:00:00  42.266    33.0 32.39884  22.499    26.6 34.51007  44.035
                    DENW095 DESN004 DENW030 DENW029  DEHE024  DEHE050  DEHE049
2017-01-01 00:00:00  54.479  45.249  43.157  30.958 18.52413 22.36175 51.25910
2017-01-01 01:00:00  52.879  46.588  40.461  30.939 17.44666 35.35423 51.72083
2017-01-01 02:00:00  51.428  42.725  41.470  32.389 31.27575 41.82747 47.97628
2017-01-01 03:00:00  47.109  31.613  37.710  41.642 38.97855 43.15384 49.04113
2017-01-01 04:00:00  44.707  31.823      NA  42.302 43.73259 47.11819 53.03304
2017-01-01 05:00:00  43.904  33.583  37.193  37.968 43.32343 49.65975 53.70900
                    DEBE032 DENI068   DERP014 DESN051 DENW038 DEBE056 DEBW009
2017-01-01 00:00:00   20.02 41.3328 0.9942399  41.940  40.521   12.10    47.7
2017-01-01 01:00:00   18.03      NA 0.9942399  40.825  39.080   16.00    48.3
2017-01-01 02:00:00   15.12 43.6873 0.9942399  40.210  37.750   16.93      NA
2017-01-01 03:00:00   12.13 40.7972 0.9942399  40.479  38.083   15.66    47.5
2017-01-01 04:00:00   12.15 40.6803 0.9942399  40.940  38.488   17.22    46.0
2017-01-01 05:00:00   11.95 38.4730 0.9942399  41.940  39.325   18.00    45.7
                    DEBW010 DENW006  DEBY068 DEBB068  DETH060 DENW067 DEBW073
2017-01-01 00:00:00    43.1  50.599 40.22561   50.47 32.03858  49.304    36.2
2017-01-01 01:00:00    42.5  44.026 39.39867   51.75 32.15171  41.599    31.5
2017-01-01 02:00:00    42.0  47.662 37.59853   41.08 30.49797  32.624    30.8
2017-01-01 03:00:00    41.0  46.874 37.64728   29.48 35.74539  25.250    32.5
2017-01-01 04:00:00    39.4  47.581 36.10047   25.45 36.19359  24.654      NA
2017-01-01 05:00:00    38.3  48.217 37.22186   24.86 36.94979  30.534    29.8
                    DESH035   DEST039 DEBE034  DEST090  DEST089  DEST091
2017-01-01 00:00:00  28.616 0.9693595   44.01 34.10720 15.86890 33.85815
2017-01-01 01:00:00  29.147 0.7111755   35.20 33.24425 15.15440 27.14350
2017-01-01 02:00:00  28.986 0.7427530   25.47 33.96220 13.58410 29.27525
2017-01-01 03:00:00  27.705 1.4019800   26.94 32.48495 12.96290 29.29385
2017-01-01 04:00:00  26.489 1.8150600   25.79 32.05860 10.95970 26.00495
2017-01-01 05:00:00  25.270 3.1152500   24.87 30.00845 11.53265 24.12935
                    DENW112  DETH091  DETH020 DENW136  DEHE134  DEHE005 DESN104
2017-01-01 00:00:00  40.217 42.21442 33.78747  62.068 32.77787 47.39792  35.151
2017-01-01 01:00:00  37.562 40.50274 31.98704  66.123 34.87235 45.73116  38.211
2017-01-01 02:00:00  38.509 40.18249 29.73861  47.338 34.96411 43.18993  40.621
2017-01-01 03:00:00  37.859 40.16681 31.61659  48.508 36.18588 42.09708  29.529
2017-01-01 04:00:00  37.808 39.05669 30.28807  50.316 39.03094 41.26889  20.062
2017-01-01 05:00:00  39.701 40.57072 28.90820  49.677 40.30124 36.05763  16.375
                     DETH117 DEBW052 DEBB064 DESN093  DEHE037 DENI058 DESL010
2017-01-01 00:00:00 37.65990    25.4   32.24  38.829 48.10576 28.6103   47.73
2017-01-01 01:00:00 38.35758    21.8   27.07  39.872 47.11808 27.1911   43.87
2017-01-01 02:00:00 35.88825    22.2   24.12  37.148 46.01592 29.5701   31.18
2017-01-01 03:00:00 36.76006    28.1   22.65  34.502 38.40292 30.1858   22.41
2017-01-01 04:00:00 35.35629    32.6   23.45  33.227 37.45989 29.6568   21.47
2017-01-01 05:00:00 35.73000    35.3   23.92  35.371 38.64996 30.3264   26.17
                    DEMV007 DENW206 DENW374 DESH056  DEBY088  DEBY039 DEBW005
2017-01-01 00:00:00    24.6      NA      NA      NA 25.78332 47.00748    43.6
2017-01-01 01:00:00    25.8      NA      NA      NA 24.17246 48.76078    44.5
2017-01-01 02:00:00    26.3      NA      NA      NA 24.06157 51.25021      NA
2017-01-01 03:00:00    27.7      NA      NA      NA 22.70978 53.87538    43.9
2017-01-01 04:00:00    33.7      NA      NA      NA 20.72321 53.62586    41.3
2017-01-01 05:00:00    31.1      NA      NA      NA 20.99758 50.29898    38.7
                    DESN074 DEBE066 DESL011  DEHE020 DESN076 DEBE061  DETH072
2017-01-01 00:00:00   1.351   44.85   41.20 44.75624  24.288   56.61 36.27879
2017-01-01 01:00:00   1.043   34.80   36.85 43.42643  25.742   48.02 37.27042
2017-01-01 02:00:00   1.038   24.44   29.61 43.45604  27.712   40.77 37.26506
2017-01-01 03:00:00   1.431   23.34   27.35 42.31280  29.777   29.58 38.63355
2017-01-01 04:00:00   1.000   27.22   20.74 41.35306  28.859   32.10 38.20276
2017-01-01 05:00:00   1.041   24.10   14.78 41.14781  28.056   28.71 38.84986
                    DEBW107 DESH057 DESH008  DEBY119  DEBY120 DEBE064 DEMV004
2017-01-01 00:00:00    28.9      NA  23.684 37.28687 33.69326   54.46    19.6
2017-01-01 01:00:00    33.3      NA  23.598 38.46657 40.50094   57.90    22.5
2017-01-01 02:00:00    34.0      NA  23.128 41.17875 35.72476   48.54    24.0
2017-01-01 03:00:00    27.0      NA  22.233 38.03063 33.67797   40.77    23.8
2017-01-01 04:00:00    29.4      NA  21.352 35.98479 32.68277   43.21    22.1
2017-01-01 05:00:00    28.4      NA  20.944 34.33570 35.09572   36.81    20.4
                     DETH041 DENI028 DENW058 DEHH026  DEHE059  DEHE060 DESN052
2017-01-01 00:00:00 27.04438 36.6086  37.484  41.362 41.07379 37.37557   1.034
2017-01-01 01:00:00 25.86044 34.0742  37.240  40.849 41.00095 36.95047   1.253
2017-01-01 02:00:00 25.85614 31.8571  39.186  37.515 42.62223 35.95082   1.050
2017-01-01 03:00:00 25.33565 28.7758  37.696  36.817 43.56151 35.32750   1.017
2017-01-01 04:00:00 24.33790 26.8923  37.740  33.226 44.59536 37.66076   1.000
2017-01-01 05:00:00 24.45150 27.8498  38.285  29.418 47.78526 40.52454   1.000
                     DEHE008  DEHE026  DEBY021  DETH095 DENW381 DEHB006 DESN061
2017-01-01 00:00:00 47.61918 31.38686 42.93396 34.92598      NA   43.27  41.883
2017-01-01 01:00:00 46.97974 33.98433 42.71026 36.54903      NA   38.22  42.189
2017-01-01 02:00:00 47.41996 35.85021 43.03912 35.42514      NA   34.12  41.405
2017-01-01 03:00:00 45.90170 38.10506 44.02858 36.29026      NA   30.44  41.290
2017-01-01 04:00:00 45.74245 37.21624 43.79340 34.31637      NA   35.62  41.730
2017-01-01 05:00:00 47.97295 38.53843 41.61277 35.01185      NA   33.86  42.304
                     DERP025 DENW329  DENI067  DEBY001 DEUB004 DEBW059  DEBY063
2017-01-01 00:00:00 42.70974  28.853       NA 26.34449    1.06    36.2 43.49131
2017-01-01 01:00:00 37.43645  37.409 64.27879 26.34354    0.25    37.4 44.15668
2017-01-01 02:00:00 36.09852  40.097 60.45140 26.74219    0.26    37.6 45.58208
2017-01-01 03:00:00 37.13368  36.516 54.23810 26.98597    0.21    38.6 44.08307
2017-01-01 04:00:00 32.18294  35.502 45.40260 25.82538    0.19      NA 42.15769
2017-01-01 05:00:00 34.51453  34.236 46.16360 24.34167    0.17    41.8 40.73611
                     DERP026  DEBY028  DETH013  DEBY075 DENI029 DESH027 DENW212
2017-01-01 00:00:00 35.83964 39.45699 39.27360 37.43027 36.0460      NA  37.490
2017-01-01 01:00:00 36.39192 39.44743 38.63599 36.76011 36.4978      NA  36.164
2017-01-01 02:00:00 37.92133 41.07836 37.27694 35.95707 34.4542      NA  37.405
2017-01-01 03:00:00 32.37978 40.80686 35.40318 36.77541 32.7464      NA  38.311
2017-01-01 04:00:00 30.64722 41.01623 36.35908 37.25150      NA      NA  39.408
2017-01-01 05:00:00 28.60836 40.47800 36.99530 38.46371 32.8552      NA  39.343
                    DENW002  DEBY035 DENW181  DEHE044 DEBB029 DEBW118  DEHE043
2017-01-01 00:00:00  50.777 42.67584  30.330 36.99499   21.25    60.0 42.15602
2017-01-01 01:00:00  47.257 41.48562  16.675 37.12659   12.12    68.4 42.16577
2017-01-01 02:00:00  43.905 39.04782  18.882 38.03693   13.96    77.4 42.22551
2017-01-01 03:00:00  43.266 38.06505  15.808 33.92204   13.96    70.8 42.84770
2017-01-01 04:00:00  41.108 40.12236  15.296 34.56184   19.87    62.7 44.64082
2017-01-01 05:00:00  44.027 42.51237  17.425 35.46139   16.24    60.6 41.54668
                     DEHE028 DENW079 DENW080 DEMV026 DEBB048 DEBW156  DEHE052
2017-01-01 00:00:00 36.03849  32.420  38.992     9.8   25.91    34.0 1.225112
2017-01-01 01:00:00 38.41797  30.668  39.247    10.3   14.13    34.0 1.615130
2017-01-01 02:00:00 42.79772  31.879  38.967    11.3   13.11    38.1 1.938600
2017-01-01 03:00:00 43.79008  32.810  38.590    11.7   11.86    36.7 1.736967
2017-01-01 04:00:00 43.87318  32.730      NA    13.4   11.79    32.9 2.481479
2017-01-01 05:00:00 43.53990  34.396  40.599    16.2   11.52    30.8 4.947788
                     DERP024 DEMV012 DEMV019 DEMV020  DEBY030 DEHH008 DEHE135
2017-01-01 00:00:00 32.95756    11.8    21.6    33.8 37.93312  39.057      NA
2017-01-01 01:00:00 33.52609    11.6    22.9      NA 38.59850  36.970      NA
2017-01-01 02:00:00 33.75448    12.9    23.8    30.3 39.47229  34.043      NA
2017-01-01 03:00:00 34.26785    13.9    23.3    30.8 39.45221  33.775      NA
2017-01-01 04:00:00 33.56892    13.3    21.8    30.2 39.59178  29.913      NA
2017-01-01 05:00:00 32.61454    12.1    20.3    27.6 39.60612  26.255      NA
                     DEHE063 DEHB002 DEHB013  DERP047  DEHE112 DEBW013  DEBY189
2017-01-01 00:00:00 49.98594   41.13   35.14 31.81830 46.92968    43.4 39.34705
2017-01-01 01:00:00 46.89907   36.77   29.45 24.40538 48.52262    45.4 36.65017
2017-01-01 02:00:00 47.72698   37.35   24.98 18.26862 47.60724      NA 35.58232
2017-01-01 03:00:00 47.11620   29.39   22.24 15.86247 42.61904    45.1 32.79462
2017-01-01 04:00:00 46.45656   28.59   19.57 13.62323 41.37113    45.6 32.70094
2017-01-01 05:00:00 46.51604   30.50   25.47 13.17047 43.90932    43.0 28.83009
                     DEHE018  DERP028  DEHE013 DENW082 DEBB007 DEBW117 DENW116
2017-01-01 00:00:00 39.30296 25.24640 46.96048  32.398   42.15    50.5  36.884
2017-01-01 01:00:00 38.21227 23.62751 46.46092  42.212   38.01    55.1  34.309
2017-01-01 02:00:00 37.47739 27.90876 47.41695  43.307   31.16      NA  35.786
2017-01-01 03:00:00 38.89017 29.31313 48.41371  43.954   28.68    53.6  35.147
2017-01-01 04:00:00 33.07280 28.41286 50.50482  38.598   28.40    56.6  35.483
2017-01-01 05:00:00 29.85495 25.31351 50.73650  42.693   29.32    55.5  36.378
                      DEHE051  DENI016 DENI075  DEBY007 DEBB060 DENI041
2017-01-01 00:00:00 9.8215275  9.30380 41.6207 31.28605   16.46 21.0263
2017-01-01 01:00:00 0.5737447       NA      NA 30.56428   19.04      NA
2017-01-01 02:00:00 0.5737447  8.61789 37.2914 29.34251   22.74 22.7213
2017-01-01 03:00:00 0.5737447  9.29260 33.4501 29.64747   25.86 20.0508
2017-01-01 04:00:00 1.4090395 10.07340 33.0526 28.89223   23.39 22.4413
2017-01-01 05:00:00 1.4304615  9.42258 32.8252 28.23546   18.22 26.4321
                     DEBY013   DEST002 DENW094  DERP053  DEBY049 DENI062
2017-01-01 00:00:00 33.52979 18.654200  30.127 44.46544 38.26390      NA
2017-01-01 01:00:00 33.32425 11.605300  24.005 43.27818 38.63865 29.5112
2017-01-01 02:00:00 33.00590 13.470350  13.923 38.53671 39.28873 26.6862
2017-01-01 03:00:00 33.40838  9.268025  11.515 39.24405 39.01532 25.9394
2017-01-01 04:00:00 33.55369 11.699510  13.069 37.23989 38.11285 24.3765
2017-01-01 05:00:00 32.93611  9.136350  15.827 30.17716 37.57462 22.6946
                    DEHH016 DESN083   DEST105  DEST095  DEBY006 DEBB075
2017-01-01 00:00:00  39.011  56.029 21.239800 41.76595 29.18668   18.00
2017-01-01 01:00:00  38.297  65.485 15.705050 44.74215 29.06813   13.67
2017-01-01 02:00:00  33.171  70.252 13.826650 46.18320 28.01940   11.64
2017-01-01 03:00:00  32.961  62.064 12.326250 25.14335 27.61980   10.18
2017-01-01 04:00:00  30.207  53.280 10.422985 15.28505 27.26608    9.66
2017-01-01 05:00:00  26.728  46.610  7.292875 13.42825 27.02899   10.03
                     DEBY005 DEBE010 DEBE067 DENW024 DEBW024  DEBY004 DEUB005
2017-01-01 00:00:00 37.08419   47.50   49.59  42.178    48.8 39.08128   27.04
2017-01-01 01:00:00 37.60809   36.74   48.97  40.798      NA 39.28204   23.97
2017-01-01 02:00:00 38.11285      NA   38.12      NA    48.8 40.30018   22.04
2017-01-01 03:00:00 38.81264      NA   28.10  38.484    49.0 41.84985   19.20
2017-01-01 04:00:00 40.38527   21.26   24.73  40.391    50.1 40.70935   16.88
2017-01-01 05:00:00 42.92631   24.06   31.24  39.840    49.5 41.06976   16.14
                    DESN075 DEBB055  DEHE030 DEBY122 DEBB099 DEHH070 DEBE051
2017-01-01 00:00:00  28.668   16.54 47.72563   0.478   19.76  45.419   36.86
2017-01-01 01:00:00  30.121   10.34 47.22768   0.478   16.74  46.866   47.79
2017-01-01 02:00:00  33.927   10.22 45.37348   0.478   16.68  45.300   31.04
2017-01-01 03:00:00  30.963   11.17 43.78159   0.478   15.42  43.147   25.62
2017-01-01 04:00:00  27.941   10.95 42.68801   0.478   15.10  40.018   24.10
2017-01-01 05:00:00  30.217    9.79 41.25533   0.478   16.99  33.252   20.95
                    DESN077 DEUB030  DEST029 DEUB029 DENW101 DENW062 DEMV003
2017-01-01 00:00:00  39.187   13.28 35.20155    6.68  59.446  39.265    21.1
2017-01-01 01:00:00  35.744   11.61 28.42670    7.68  62.013  40.046    19.7
2017-01-01 02:00:00  37.102   11.57 35.40915    9.25  46.610  39.667    21.8
2017-01-01 03:00:00  39.741    9.99 31.86465    6.61  45.694  38.238    19.6
2017-01-01 04:00:00  37.198    8.57 27.05645   10.08  50.707  37.655    17.9
2017-01-01 05:00:00  42.227    8.45 15.98645   11.31  51.239  36.148    16.0
                    DENW133  DERP011 DEBW046 DESN017 DENW021  DEST066  DEBY012
2017-01-01 00:00:00      NA 38.99709    24.1  45.326  39.115 34.11035 32.30228
2017-01-01 01:00:00      NA 38.24185    17.7  48.959  39.779 31.76140 32.10152
2017-01-01 02:00:00      NA 36.34209    19.2  44.637  38.844 27.18130 32.94089
2017-01-01 03:00:00      NA 35.39058    21.7  33.927  37.687 28.13695 32.59578
2017-01-01 04:00:00      NA 34.52168    22.2  28.917  39.913 29.07900 34.02309
2017-01-01 05:00:00      NA 31.90596    20.4  32.397  39.304 29.93790 37.45608
                    DEBW120 DESN001 DEMV017 DESL012 DENW078 DEHH059 DEBB049
2017-01-01 00:00:00    43.1  38.995    20.4   41.34  37.619  40.510   26.96
2017-01-01 01:00:00      NA  42.380    21.9   31.27  36.134  38.958   13.97
2017-01-01 02:00:00    49.9  36.872    22.3   23.04  36.275  34.824   13.47
2017-01-01 03:00:00    48.0  29.835    20.7   18.90  37.036  36.385   12.25
2017-01-01 04:00:00    48.7  26.660    18.1   21.02  38.857  27.659   12.20
2017-01-01 05:00:00    47.4  20.330    15.4   21.51  39.684  24.012   11.52
                    DEHB011  DETH061  DETH043 DENW081 DESN059 DEBW099  DETH083
2017-01-01 00:00:00   32.85 21.61259 39.91254  48.428  35.075    56.7 38.19712
2017-01-01 01:00:00   26.37 23.77046 43.00333  48.257  24.728    56.6 36.20066
2017-01-01 02:00:00   26.87 26.26264 43.30110  43.825  25.837    57.1 35.24862
2017-01-01 03:00:00   25.90 27.15186 45.35599  42.489  28.859    56.9 34.36705
2017-01-01 04:00:00   23.04 28.30844 38.68375  42.950  29.089    51.8 32.75290
2017-01-01 05:00:00   24.79 25.73957 40.38033  43.553  30.733    51.1 31.04934
                     DETH042 DENW179 DENI053 DENW071 DENW182  DEBY077 DENW207
2017-01-01 00:00:00 31.99699  42.782 26.7214  35.847      NA 36.13297  50.936
2017-01-01 01:00:00 33.22318  33.056 26.1592  34.503      NA 36.55170  61.773
2017-01-01 02:00:00 27.56725  37.598 23.2826  33.112      NA 36.47809  39.316
2017-01-01 03:00:00 23.84333  42.512 22.3759  33.435      NA 36.45419  53.740
2017-01-01 04:00:00 24.20795  45.214 23.8703  34.772      NA 35.00107  57.047
2017-01-01 05:00:00 24.29774  48.550 26.9040  37.627      NA 33.92461  41.861
                     DEHE032   DEST044 DENW211 DENW068 DESL017 DESL019 DESL020
2017-01-01 00:00:00 35.87199  9.453955      NA   8.472   50.60   29.09   46.89
2017-01-01 01:00:00 35.33258  3.510690  38.228  11.732   43.77   27.85   40.14
2017-01-01 02:00:00 34.78264 19.159800  41.890  13.889   35.76   20.26   35.91
2017-01-01 03:00:00 35.01253  7.948620  38.337  13.799   24.79   13.44   26.97
2017-01-01 04:00:00 33.25269 15.119000  43.214  20.991   24.58   10.81   25.30
2017-01-01 05:00:00 31.84195 21.907100  42.481  28.282   24.40   11.34   22.58
                    DEHH068 DESH052 DEMV025 DEBW042  DEHE116  DEHE022  DEBY099
2017-01-01 00:00:00  42.137  27.109    21.3    44.4 46.90259 41.44054 31.08912
2017-01-01 01:00:00  47.701  24.724      NA    34.8 48.94117 39.91312 31.07287
2017-01-01 02:00:00  44.698  24.959    20.3    42.5 45.61465 39.19791 28.55763
2017-01-01 03:00:00  45.450  24.343    19.4    41.5 45.33828 33.94621 27.64848
2017-01-01 04:00:00  39.571  23.259    21.3    44.3 45.01734 32.11424 26.50797
2017-01-01 05:00:00  34.747  22.854    23.2    45.2 44.25575 34.20290 26.23647
                     DEBY110   DEBY109 DESN079 DEBW027 DEMV031 DEHB005 DEBW015
2017-01-01 00:00:00 34.07853 11.286536  39.301    35.2    28.0   30.41    46.2
2017-01-01 01:00:00 35.40355 10.535120  42.132    29.1      NA   26.96    46.8
2017-01-01 02:00:00 31.08817  7.784708  41.940      NA    23.6   25.83    45.7
2017-01-01 03:00:00 31.05279  5.542888  38.116    38.8    26.4   24.07    44.2
2017-01-01 04:00:00 30.81475  3.938720  31.766    39.5    30.1   23.57    43.3
2017-01-01 05:00:00 32.11300  3.269520  27.597    39.1    28.9   22.28    41.8
                     DERP016 DENI077 DESN020 DEMV022 DESN019 DEBB066  DETH026
2017-01-01 00:00:00 19.01548 44.4335  34.023    28.2  35.113   14.86 32.68233
2017-01-01 01:00:00 19.62282 45.5971  33.181    29.1  32.875   18.02 33.47162
2017-01-01 02:00:00 26.52686 45.4550  30.657    29.7  31.728   17.49 33.08042
2017-01-01 03:00:00 37.26198 46.0571  29.452    31.8  28.190   16.73 34.52330
2017-01-01 04:00:00 29.40949 46.1748  30.810    32.1  24.843   16.80 36.37948
2017-01-01 05:00:00 29.12269 46.0933  33.755    31.3  23.753   17.08 37.51187
                    DESN091 DENI071  DERP001  DETH009 DEBW112 DENW355  DERP003
2017-01-01 00:00:00  42.438 31.9407 36.84315 32.62830    28.4  35.776 33.39461
2017-01-01 01:00:00  39.091 31.4457 38.87016 32.51900    25.7  32.919 35.13319
2017-01-01 02:00:00  34.883      NA 33.23234 34.10026    24.9  33.765 35.42515
2017-01-01 03:00:00  35.400 28.1083 28.02070 33.38718    25.3  36.609 33.86792
2017-01-01 04:00:00  36.413 30.8752 24.97412 31.04992    29.1  37.607 32.34817
2017-01-01 05:00:00  36.528 32.7495 25.41866 33.85737    24.6  37.550 31.70230
                    DEBW081  DEBY009 DEMV024 DETH018  DERP007 DENW065 DENI048
2017-01-01 00:00:00    41.8 35.79168    19.4      NA 37.32141  12.797 44.1302
2017-01-01 01:00:00      NA 33.14835    18.9      NA 36.45996  10.930 33.7698
2017-01-01 02:00:00    38.8 32.75352    18.3      NA 33.27515  13.080      NA
2017-01-01 03:00:00    38.1 31.62543    17.3      NA 31.35282  19.901 30.3478
2017-01-01 04:00:00    36.9 31.13979    16.2      NA 31.43427  20.559 29.2129
2017-01-01 05:00:00    37.2 33.41889    14.9      NA 28.70097  21.015 28.4281
                    DEBE062 DENW066 DEBB065   DETH027 DENW034  DEST077  DERP022
2017-01-01 00:00:00   16.58  36.138    8.10  7.176025  42.125 40.77650 26.10352
2017-01-01 01:00:00   18.86  31.175    7.95  3.110997  39.621 15.32528 28.19878
2017-01-01 02:00:00   10.54  25.587    9.86  1.912500  36.979  8.92124 30.64404
2017-01-01 03:00:00    8.60  27.928   11.23  1.912500  41.320  8.00614 32.72736
2017-01-01 04:00:00    7.14  18.518   10.69  7.355953  39.262  8.61095 35.30913
2017-01-01 05:00:00    7.54      NA   16.27 12.758287  39.636  8.83634 36.49122
                    DESN006 DENW059 DEHH033 DEBW038 DENI011  DETH036 DEBB032
2017-01-01 00:00:00  32.493  32.759  34.562    32.4 25.2104 40.04029   25.17
2017-01-01 01:00:00  36.012  31.109  33.129      NA 24.2987 38.53716   11.59
2017-01-01 02:00:00  37.618  32.805  32.492    26.1 24.2477 38.87634   12.50
2017-01-01 03:00:00  36.719  35.042  31.370    25.0 26.7261 35.34463   14.99
2017-01-01 04:00:00  36.586  36.794  27.898    23.9 24.1483 33.79225   15.65
2017-01-01 05:00:00  35.515  37.318  24.344    24.0 25.5702 33.66287   15.47
                     DEBY020 DESN024 DENI052 DEUB001 DEBE027 DEBW084  DETH005
2017-01-01 00:00:00 34.48196  24.103 30.1100    2.40   11.16    40.5 35.12631
2017-01-01 01:00:00 32.95045  24.641 28.7858    2.13   15.42    37.7 30.75032
2017-01-01 02:00:00 32.79845  24.699 27.4834    1.91   17.16    36.1 28.14588
2017-01-01 03:00:00 30.27461  24.141 27.3405    2.09   15.07    35.2 26.24132
2017-01-01 04:00:00 29.84249  24.487 26.2040    1.64   13.06    37.7 25.11371
2017-01-01 05:00:00 29.98781  23.680 24.7114    1.50   16.46    37.1 26.85073
                     DEHE042  DEST050  DERP017 DEBW136 DENW008  DERP060 DEBE068
2017-01-01 00:00:00 45.38108 33.93755 2.774440    55.5  53.893 23.24628   50.74
2017-01-01 01:00:00 43.32147 32.44570 4.266720    48.2  54.893 20.72388   52.24
2017-01-01 02:00:00 42.46385 30.15425 3.048565    46.5  42.993 18.42690   33.99
2017-01-01 03:00:00 41.14642 28.56165 5.517837    44.2  45.469 16.26586   29.76
2017-01-01 04:00:00 40.61167 27.23615 6.212543    41.8  47.528 17.21039   28.78
2017-01-01 05:00:00 39.56596 24.81795 6.137774    40.2  45.640 16.43765   28.12
                    DETH040 DENI059 DENI060 DESN045 DENW043 DESL003  DEBY124
2017-01-01 00:00:00    6.68 27.9319 24.2916  29.203  46.761   41.09 21.09797
2017-01-01 01:00:00    7.68 25.8362 23.2249  27.769  44.830   35.53 20.89912
2017-01-01 02:00:00    9.25 25.1534      NA  28.917  45.555   30.04 21.00714
2017-01-01 03:00:00    6.61 24.8719 20.6260  30.810  40.491   26.51 21.45360
2017-01-01 04:00:00   10.08 23.6147 19.1907  30.121  41.271   23.77 21.19165
2017-01-01 05:00:00   11.31 22.4547 16.4173  27.042  43.450   17.95 19.72419
                     DEBY121 DEBW087 DENI157 DENW200 DENW189  DEST098  DEST108
2017-01-01 00:00:00 29.42568    18.2 36.8365  46.121  48.036 2.673400 28.44130
2017-01-01 01:00:00 32.34148    14.6 25.6034  39.156  51.651 4.029970 28.34505
2017-01-01 02:00:00 32.18756    16.3      NA  29.747  53.808 4.092785 28.28895
2017-01-01 03:00:00 31.45718      NA 20.1044  24.426  54.267 5.040280 22.26835
2017-01-01 04:00:00 28.99739    17.6 22.1747  20.810  43.827 4.629630 23.84585
2017-01-01 05:00:00 30.92660    19.2 23.4400  27.706  46.214 4.720950 24.36160
                    DENI043  DEBY037  DEBY031 DEHB004 DENW100 DEBW022  DEBY072
2017-01-01 00:00:00      NA 74.24200 39.59179   32.59  33.753    37.3 20.97942
2017-01-01 01:00:00 38.7174 62.63042 36.26108   30.12  42.230    36.6 21.42396
2017-01-01 02:00:00 40.5990 62.28436 36.42934   26.90  38.162    36.3 25.10552
2017-01-01 03:00:00 40.3471 60.94978 35.37582   23.31  32.586    35.1 36.44368
2017-01-01 04:00:00 38.3535 58.81695 37.25723   20.78  33.525      NA 39.83365
2017-01-01 05:00:00 36.7604 60.84653 37.65015   24.07  33.730    35.8 36.25917
                     DEBY118  DEHE011  DEST104 DEBB086  DERP020  DERP019
2017-01-01 00:00:00 38.15778 47.28270 15.45950   32.28 34.78193 31.54819
2017-01-01 01:00:00 36.91020 47.92970 15.36575   26.94 31.23602 23.98330
2017-01-01 02:00:00 36.39014 46.84868 15.04555   34.16 26.46797 21.05928
2017-01-01 03:00:00 35.42267 45.98840 17.30775   38.54 23.47990 22.32608
2017-01-01 04:00:00 33.80607 45.70119 18.91660   37.82 21.11007 21.90888
2017-01-01 05:00:00 32.50782 45.89828 14.09415   30.32 18.63623 15.83560
                     DEBY187  DERP015 DEBW122  DERP012 DEHB012 DESN025  DETH093
2017-01-01 00:00:00 32.57092 5.463808    39.6 39.78260   35.20  45.364 39.77417
2017-01-01 01:00:00 31.22105 5.102908    43.8 39.78155   31.41  46.473 36.37709
2017-01-01 02:00:00 28.64941 6.333500    40.7 38.43732   27.61  47.563 35.14458
2017-01-01 03:00:00 24.81298 5.848866    38.4 38.76446   28.15  43.872 34.05579
2017-01-01 04:00:00 24.73554 5.530460    35.2 36.63679   22.60  46.052 29.40096
2017-01-01 05:00:00 23.26617 7.503539    32.7 32.50343   26.09  45.593 26.45944
                     DERP041 DEBB083 DEBW152 DESH025  DERP046 DEBW004 DENW053
2017-01-01 00:00:00 48.10567   34.07    51.9  30.611 32.28973    39.1  33.372
2017-01-01 01:00:00 50.69691   38.51    54.4  31.494 33.02174    38.4  34.494
2017-01-01 02:00:00 48.19984   31.36    54.2  29.491 33.31210    36.9  34.169
2017-01-01 03:00:00 42.69443   30.53    50.5  28.757 33.22358    34.8  36.136
2017-01-01 04:00:00 40.55327   29.06    49.9  27.284 35.05623    34.1  37.738
2017-01-01 05:00:00 40.69074   27.44    46.0  24.051 35.46788    33.9      NA
                     DEST092 DEST102  DEBY067  DETH011 DENW188 DENW208  DEBY188
2017-01-01 00:00:00 37.58520 38.6233 35.58423 39.01051  37.371      NA 23.62085
2017-01-01 01:00:00 37.40685 37.2987 36.85284 39.12229  50.262  44.285 23.24992
2017-01-01 02:00:00 31.41765 36.1644 37.27253 38.88754  45.859  45.730 24.66289
2017-01-01 03:00:00 28.55085 34.3126 38.26199 38.76886  43.024  39.852 24.77283
2017-01-01 04:00:00 29.89080 32.5056 37.87481 37.47964  45.946  42.946 24.20783
2017-01-01 05:00:00 33.15765 30.9489 38.73521 33.44350  45.125  45.502 24.24607
                    DEBW080
2017-01-01 00:00:00    41.3
2017-01-01 01:00:00    42.5
2017-01-01 02:00:00      NA
2017-01-01 03:00:00    39.8
2017-01-01 04:00:00    37.8
2017-01-01 05:00:00    37.1
In [32]:
read.csv("AirBase_v8_stations.csv", sep = "\t", stringsAsFactors = FALSE) %>% 
    as_tibble  %>% 
    filter(country_iso_code == "DE", station_type_of_area == "rural", 
                 type_of_station == "Background") -> a2
a2.sf = st_as_sf(a2, coords = c("station_longitude_deg", "station_latitude_deg"), 
                 crs = 4326)
In [33]:
# rural
sel =  colnames(aqsel) %in% a2$station_european_code
aqsel = aqsel[, sel]
In [35]:
tb = tibble(NO2 = apply(aqsel, 2, mean, na.rm = TRUE), station_european_code = colnames(aqsel))
crs = 32632
right_join(a2.sf, tb) %>% st_transform(crs) -> no2.sf 
no2.sf %>% head()
Joining, by = "station_european_code"
A sf: 6 × 20
station_european_codestation_local_codecountry_iso_codecountry_namestation_namestation_start_datestation_end_datetype_of_stationstation_ozone_classificationstation_type_of_areastation_subcat_rural_backstreet_typestation_altitudestation_citylau_level1_codelau_level2_codelau_level2_nameEMEP_stationgeometryNO2
<chr><chr><chr><chr><chr><chr><chr><chr><chr><chr><chr><chr><int><chr><chr><chr><chr><chr><POINT [m]><dbl>
DENI031DENI031DEGermanyJadebusen 1984-06-01Backgroundruralruralunknown 2 3405000 Wilhelmshaven, Stadt no POINT (439813.9 5938977)10.450505
DEHE046DEHE046DEGermanyBad Arolsen 1999-05-11Backgroundruralruralunknown 343BAD AROLSEN/KOHLGRUND 6635002 Bad Arolsen, Stadt no POINT (495006.7 5697747) 8.935611
DEBB053DEBB053DEGermanyHasenholz 2000-11-21Backgroundruralruralregional 88BUCKOW 120645408 Buckow (Märkische Schweiz), Stadtno POINT (839844 5835576) 9.110279
DEBW031DEBW031DEGermanySchwarzwald-Süd 1984-01-01Backgroundruralruralregional904 83155012 Sulzburg, Stadt no POINT (407502 5295910) 3.438993
DEHE039DEHE039DEGermanyBurg Herzberg (Grebenau)1983-05-01Backgroundruralruralunknown 491HERZBERG 6632004 Breitenbach a. Herzberg no POINT (532394.9 5624392) 8.400254
DEUB028DEUB028DEGermanyZingst 1991-09-01Backgroundruralruralunknown 1ZINGST 13057096Zingst yesPOINT (741354.1 6038524) 5.197794
In [34]:
#> Joining, by = "station_european_code"
# load German boundaries
data(air, package = "spacetime")
de <- st_transform(st_as_sf(DE_NUTS1), crs)
ggplot() + geom_sf(data = de) +  geom_sf(data = no2.sf, mapping = aes(col = NO2))
Joining, by = "station_european_code"

Variogram

$$ \hat{\gamma}\left(h_{i}\right)=\frac{1}{2 N\left(h_{i}\right)} \sum_{j=1}^{N\left(h_{i}\right)}\left(z\left(s_{i}\right)-z\left(s_{i}+h^{\prime}\right)\right)^{2}, \quad h_{i, 0} \leq h^{\prime}<h_{i, 1} $$
In [21]:
v = variogram(NO2~1, no2.sf)
plot(v, plot.numbers = TRUE)

v0 = variogram(NO2~1, no2.sf, cutoff = 100000, width = 10000)
plot(v0, plot.numbers = TRUE)

Variogram models

In [22]:
v.m = fit.variogram(v, vgm(1, "Exp", 50000, 1))
plot(v, v.m, plot.numbers = TRUE)

Kriging

In [23]:
st_bbox(de) %>%
  st_as_stars(dx = 10000) %>%
  st_set_crs(crs) %>%
  st_crop(de) -> grd
grd
stars object with 2 dimensions and 1 attribute
attribute(s):
    values     
 Min.   :0     
 1st Qu.:0     
 Median :0     
 Mean   :0     
 3rd Qu.:0     
 Max.   :0     
 NA's   :2076  
dimension(s):
  from to  offset  delta                       refsys point values    
x    1 65  280741  10000 +proj=utm +zone=32 +datum...    NA   NULL [x]
y    1 87 6101239 -10000 +proj=utm +zone=32 +datum...    NA   NULL [y]
In [24]:
k = krige(NO2~1, no2.sf, grd, v.m)
#> [using ordinary kriging]
ggplot() + geom_stars(data = k, aes(fill = var1.pred, x = x, y = y)) + 
    geom_sf(data = st_cast(de, "MULTILINESTRING")) + 
    geom_sf(data = no2.sf)
[using ordinary kriging]

Areal Means / Block Kriging

In [25]:
a = aggregate(no2.sf["NO2"], by = de, FUN = mean)
b = krige(NO2~1, no2.sf, de, v.m)
[using ordinary kriging]
In [26]:
b$sample = a$NO2
b$kriging = b$var1.pred
b %>% select(sample, kriging) %>% gather(var, NO2, -geometry) -> b2
ggplot() + geom_sf(data = b2, mapping = aes(fill = NO2)) + facet_wrap(~var) +
     scale_fill_gradientn(colors = sf.colors(20))
In [27]:
SE = function(x) sqrt(var(x)/length(x))
a = aggregate(no2.sf["NO2"], de, SE)
In [28]:
b$sample = a$NO2
b$kriging = sqrt(b$var1.var)
b %>% select(sample, kriging) %>% gather(var, NO2, -geometry) -> b2
ggplot() + geom_sf(data = b2, mapping = aes(fill = NO2)) + facet_wrap(~var) +
     scale_fill_gradientn(colors = sf.colors(20))

Simulation

In [29]:
s = krige(NO2~1, no2.sf, grd, v.m, nmax = 30, nsim = 10)
#> drawing 10 GLS realisations of beta...
#> [using conditional Gaussian simulation]
g = ggplot() + coord_equal() +
    scale_fill_viridis() +
    theme_void() +
    scale_x_discrete(expand=c(0,0)) +
    scale_y_discrete(expand=c(0,0))
g + geom_stars(data = s[,,,1:6]) + facet_wrap(~sample)
drawing 10 GLS realisations of beta...
[using conditional Gaussian simulation]

Spatial Covariance Models with Covariates

$$ Z(s) = \sum_{j=0}^p \beta_j X_p(s) + e(s) $$
In [36]:
v = fread("pop/Zensus_Bevoelkerung_100m-Gitter.csv")
v %>% head()
A data.table: 6 × 4
Gitter_ID_100mx_mp_100my_mp_100mEinwohner
<chr><int><int><int>
100mN26840E4334143341502684050-1
100mN26840E4334243342502684050-1
100mN26840E4334343343502684050-1
100mN26840E4334443344502684050-1
100mN26840E4334543345502684050-1
100mN26841E4334143341502684150-1
In [37]:
v %>% filter(Einwohner > 0) %>% 
    select(-Gitter_ID_100m) %>%
    st_as_sf(coords = c("x_mp_100m", "y_mp_100m"), crs = 3035) %>%
    st_transform(st_crs(grd)) -> b
a = aggregate(b, st_as_sf(grd, na.rm = FALSE), sum)
In [38]:
grd$ID = 1:prod(dim(grd)) # so we can find out later which grid cell we have
ii = st_intersects(grd["ID"], st_cast(st_union(de), "MULTILINESTRING"))
Warning message in st_intersects.stars(grd["ID"], st_cast(st_union(de), "MULTILINESTRING")):
“as_points is NA: assuming here that raster cells are small polygons, not points”
In [39]:
grd_sf = st_as_sf(grd["ID"], na.rm = FALSE)[lengths(ii) > 0,]
iii = st_intersection(grd_sf, st_union(de))
Warning message:
“attribute variables are assumed to be spatially constant throughout all geometries”
In [40]:
grd$area = st_area(grd)[[1]] + units::set_units(grd$values, m^2) # NA's
grd$area[iii$ID] = st_area(iii)
In [41]:
grd$pop_dens = a$Einwohner / grd$area
sum(grd$pop_dens * grd$area, na.rm = TRUE) # verify
sum(b$Einwohner)
g + geom_stars(data = grd, aes(fill = sqrt(pop_dens), x = x, y = y))
80323301
80324282
In [42]:
(a = aggregate(grd["pop_dens"], no2.sf, mean))
stars object with 1 dimensions and 1 attribute
attribute(s):
   pop_dens         
 Min.   :0.0000034  
 1st Qu.:0.0000498  
 Median :0.0000893  
 Mean   :0.0001949  
 3rd Qu.:0.0002366  
 Max.   :0.0022390  
 NA's   :1          
dimension(s):
         from to offset delta                       refsys point
geometry    1 74     NA    NA +proj=utm +zone=32 +datum...  TRUE
                                                        values
geometry POINT (439813.9 5938977),...,POINT (456668.3 5436135)
In [43]:
no2.sf$pop_dens = st_as_sf(a)[[1]]
summary(lm(NO2~sqrt(pop_dens), no2.sf))
Call:
lm(formula = NO2 ~ sqrt(pop_dens), data = no2.sf)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.8739 -1.8221 -0.4898  1.5637  8.1100 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      4.6271     0.6933   6.674 4.62e-09 ***
sqrt(pop_dens) 321.8022    49.6603   6.480 1.04e-08 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.13 on 71 degrees of freedom
  (1 observation deleted due to missingness)
Multiple R-squared:  0.3716,	Adjusted R-squared:  0.3628 
F-statistic: 41.99 on 1 and 71 DF,  p-value: 1.039e-08
In [44]:
no2.sf = no2.sf[!is.na(no2.sf$pop_dens),]
vr = variogram(NO2~sqrt(pop_dens), no2.sf)
vr.m = fit.variogram(vr, vgm(1, "Exp", 50000, 1))
plot(vr, vr.m, plot.numbers = TRUE)
In [45]:
kr = krige(NO2~sqrt(pop_dens), no2.sf, grd["pop_dens"], vr.m)
k$kr1 = k$var1.pred
k$kr2 = kr$var1.pred
[using universal kriging]
In [46]:
st_redimension(k[c("kr1", "kr2")], 
    along = list(what = c("kriging", "residual kriging"))) %>%
    setNames("NO2") -> km
g + geom_stars(data = km, aes(fill = NO2, x = x, y = y)) + 
    geom_sf(data = st_cast(de, "MULTILINESTRING")) + 
    geom_sf(data = no2.sf) + facet_wrap(~what)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Spatiotemporal variogram

In [47]:
aqx = aq[,colnames(aq) %in% a2$station_european_code]
sfc = st_geometry(a2.sf)[match(colnames(aqx), a2.sf$station_european_code)]
st_as_stars(NO2 = as.matrix(aqx)) %>%
    st_set_dimensions(names = c("time", "station")) %>%
    st_set_dimensions("time", index(aqx)) %>%
    st_set_dimensions("station", sfc) -> no2.st
In [52]:
no2.st %>% glimpse
List of 1
 $ NO2: num [1:8760, 1:74] 28 NA 26 25.5 23.7 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:8760] "2017-01-01 00:00:00" "2017-01-01 01:00:00" "2017-01-01 02:00:00" "2017-01-01 03:00:00" ...
  .. ..$ : chr [1:74] "DENI031" "DEHE046" "DEBB053" "DEBW031" ...
 - attr(*, "dimensions")=List of 2
  ..$ time:List of 7
  .. ..$ from  : num 1
  .. ..$ to    : int 8760
  .. ..$ offset: POSIXct[1:1], format: "2017-01-01"
  .. ..$ delta : 'difftime' num 1
  .. .. ..- attr(*, "units")= chr "hours"
  .. ..$ refsys: chr "POSIXct"
  .. ..$ point : logi NA
  .. ..$ values: NULL
  .. ..- attr(*, "class")= chr "dimension"
  ..$ sfc :List of 7
  .. ..$ from  : num 1
  .. ..$ to    : int 74
  .. ..$ offset: num NA
  .. ..$ delta : num NA
  .. ..$ refsys: chr "+proj=longlat +datum=WGS84 +no_defs"
  .. ..$ point : logi TRUE
  .. ..$ values:sfc_POINT of length 74; first list element:  'XY' num [1:2] 8.09 53.6
  .. ..- attr(*, "class")= chr "dimension"
  ..- attr(*, "raster")=List of 3
  .. ..$ affine     : num [1:2] 0 0
  .. ..$ dimensions : chr [1:2] NA NA
  .. ..$ curvilinear: logi FALSE
  .. ..- attr(*, "class")= chr "stars_raster"
  ..- attr(*, "class")= chr "dimensions"
 - attr(*, "class")= chr "stars"
In [47]:
v.st = variogramST(NO2~1, no2.st[,1:(24*31)], tlags = 0:48)
In [48]:
v1 = plot(v.st)
v2 = plot(v.st, map = FALSE)
print(v1, split = c(1,1,2,1), more = TRUE)
print(v2, split = c(2,1,2,1), more = FALSE)
In [49]:
prodSumModel <- vgmST("productSum",
    space=vgm(150, "Exp", 200, 0),
    time= vgm(20, "Sph",   40, 0),
    k=2)
StAni = estiStAni(v.st, c(0,200000))
(fitProdSumModel <- fit.StVariogram(v.st, prodSumModel, fit.method = 7,
    stAni = StAni, method = "L-BFGS-B",
    control = list(parscale = c(1,10,1,1,0.1,1,10)),
    lower = rep(0.0001, 7)))
$space
A variogramModel: 2 × 9
modelpsillrangekappaang1ang2ang3anis1anis2
<fct><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
Nug 26.26254 0.0000.000011
Exp140.48760432.2530.500011
$time
A variogramModel: 2 × 9
modelpsillrangekappaang1ang2ang3anis1anis2
<fct><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
Nug 1.210356 0.000000.000011
Sph15.99463740.066180.500011
$k
k: 0.0322467170800045
$stModel
'productSum'
In [50]:
plot(v.st, fitProdSumModel, wireframe=FALSE, all=TRUE, 
     scales=list(arrows=FALSE), zlim=c(0,150))
In [51]:
plot(v.st, model=fitProdSumModel, wireframe=TRUE, all=TRUE, 
     scales=list(arrows=FALSE), zlim=c(0,185))

Spatial Autocorrelation

In [3]:
data(pol_pres15, package="spDataLarge")
head(pol_pres15[, c(1, 4, 6)])
A sf: 6 × 4
TERYTnametypesgeometry
<chr><chr><fct><MULTIPOLYGON [m]>
020101BOLESŁAWIEC Urban MULTIPOLYGON (((261089.5 38...
020102BOLESŁAWIEC Rural MULTIPOLYGON (((254150 3837...
020103GROMADKA Rural MULTIPOLYGON (((275346 3846...
020104NOWOGRODZIEC Urban/ruralMULTIPOLYGON (((251769.8 37...
020105OSIECZNICA Rural MULTIPOLYGON (((263423.9 40...
020106WARTA BOLESŁAWIECKARural MULTIPOLYGON (((267030.7 38...
In [4]:
tm_shape(pol_pres15) + tm_fill("types")
Warning message:
“The shape pol_pres15 is invalid. See sf::st_is_valid”

Neighbourhood construction

In [5]:
args(poly2nb)
function (pl, row.names = NULL, snap = sqrt(.Machine$double.eps), 
    queen = TRUE, useC = TRUE, foundInBox = NULL) 
NULL
In [6]:
system.time(nb_q <- poly2nb(pol_pres15, queen=TRUE))
   user  system elapsed 
  1.344   0.063   1.429 
In [7]:
nb_q
Neighbour list object:
Number of regions: 2495 
Number of nonzero links: 14242 
Percentage nonzero weights: 0.2287862 
Average number of links: 5.708216 
In [8]:
system.time({
  fB <- rgeos::gUnarySTRtreeQuery(as(pol_pres15, "Spatial"))
  nb_q1 <- poly2nb(pol_pres15, queen=TRUE, foundInBox=fB)
})
   user  system elapsed 
  0.938   0.031   0.970 
In [9]:
st_queen <- function(a, b = a) st_relate(a, b, pattern = "F***T****")
as.nb.sgbp <- function(x, ...) {
  attrs <- attributes(x)
  x <- lapply(x, function(i) { if(length(i) == 0L) 0L else i } )
  attributes(x) <- attrs
  class(x) <- "nb"
  x
}
In [71]:
system.time(nb_sf_q <- as.nb.sgbp(st_queen(pol_pres15)))
   user  system elapsed 
  4.312   0.000   4.375 

Preferred method : rgeos::gUnarySTRtreeQuery without overhead

In [10]:
system.time({
  fB1 <- st_intersects(st_as_sfc(lapply(st_geometry(pol_pres15), function(x) {
    st_as_sfc(st_bbox(x))[[1]]
  })))
  fB1a <- lapply(seq_along(fB1), function(i) fB1[[i]][fB1[[i]] > i])
  fB1a <- fB1a[-length(fB1a)]
  nb_sf_q1 <- poly2nb(pol_pres15, queen=TRUE, foundInBox=fB1a)
})
   user  system elapsed 
  1.078   0.000   1.110 
In [11]:
all.equal(nb_q, nb_sf_q1, check.attributes=FALSE)
TRUE

Pysal Compatibility

In [75]:
# tf <- tempfile(fileext=".gal")
# write.nb.gal(nb_q, tf)
In [ ]:
# use_python(python='/home/alal/anaconda3/envs/gds/bin/python')
# np <- import("numpy")
# libpysal <- import("libpysal")
# nb_gal_ps <- libpysal$io$open(tf)$read()
# nb_gal_ps$pct_nonzero

Graph based neighbours

In [14]:
coords <- st_centroid(st_geometry(pol_pres15), of_largest_polygon=TRUE)
suppressMessages(nb_tri <- tri2nb(coords))
nb_tri
nb_tri %>% glimpse
Neighbour list object:
Number of regions: 2495 
Number of nonzero links: 14930 
Percentage nonzero weights: 0.2398384 
Average number of links: 5.983968 
List of 2495
 $ : int [1:4] 2 4 6 78
 $ : int [1:5] 1 3 4 5 6
 $ : int [1:8] 2 5 6 58 96 100 589 591
 $ : int [1:8] 1 2 5 68 71 78 156 158
 $ : int [1:6] 2 3 4 158 588 589
 $ : int [1:7] 1 2 3 58 78 162 164
 $ : int [1:6] 8 9 10 11 53 149
 $ : int [1:6] 7 9 11 12 116 118
 $ : int [1:6] 7 8 53 118 130 133
 $ : int [1:6] 7 11 13 147 149 150
 $ : int [1:5] 7 8 10 12 13
 $ : int [1:7] 8 11 13 103 116 139 143
 $ : int [1:5] 10 11 12 103 147
 $ : int [1:4] 15 16 17 19
 $ : int [1:6] 14 16 17 18 98 605
 $ : int [1:7] 14 15 19 97 98 99 101
 $ : int [1:6] 14 15 19 554 604 605
 $ : int [1:6] 15 21 22 74 98 605
 $ : int [1:6] 14 16 17 97 548 554
 $ : int [1:6] 21 22 23 2252 2253 2316
 $ : int [1:7] 18 20 22 23 74 75 135
 $ : int [1:6] 18 20 21 605 606 2253
 $ : int [1:6] 20 21 125 135 2316 2320
 $ : int [1:5] 26 27 28 59 61
 $ : int [1:6] 28 34 42 114 132 160
 $ : int [1:7] 24 28 59 160 161 163 165
 $ : int [1:7] 24 28 29 61 111 114 117
 $ : int [1:6] 24 25 26 27 114 160
 $ : int [1:5] 27 61 64 108 111
 $ : int [1:6] 31 32 33 37 41 45
 $ : int [1:7] 30 36 37 39 40 41 42
 $ : int [1:5] 30 33 37 38 166
 $ : int [1:8] 30 32 38 45 51 66 79 155
 $ : int [1:5] 25 35 36 42 160
 $ : int [1:7] 34 36 38 80 160 163 166
 $ : int [1:6] 31 34 35 37 42 166
 $ : int [1:5] 30 31 32 36 166
 $ : int [1:7] 32 33 35 77 79 80 166
 $ : int [1:5] 31 40 42 129 132
 $ : int [1:5] 31 39 41 129 131
 $ : int [1:5] 30 31 40 45 131
 $ : int [1:6] 25 31 34 36 39 132
 $ : int [1:5] 47 48 51 52 56
 $ : int [1:5] 46 47 49 54 146
 $ : int [1:6] 30 33 41 51 54 131
 $ : int [1:6] 44 53 54 130 146 149
 $ : int [1:6] 43 44 48 49 54 56
 $ : int [1:6] 43 47 49 50 52 55
 $ : int [1:6] 44 47 48 50 146 152
 $ : int [1:5] 48 49 55 152 1335
 $ : int [1:7] 33 43 45 52 54 56 155
 $ : int [1:8] 43 48 51 55 155 1306 1797 1914
 $ : int [1:5] 7 9 46 130 149
 $ : int [1:8] 44 45 46 47 51 56 130 131
 $ : int [1:7] 48 50 52 1306 1329 1334 1335
 $ : int [1:4] 43 47 51 54
 $ : int [1:5] 58 62 96 164 165
 $ : int [1:5] 3 6 57 96 164
 $ : int [1:6] 24 26 61 62 165 167
 $ : int [1:6] 61 62 63 64 73 167
 $ : int [1:7] 24 27 29 59 60 64 167
 $ : int [1:7] 57 59 60 73 96 165 167
 $ : int [1:6] 60 64 73 75 108 136
 $ : int [1:5] 29 60 61 63 108
 $ : int [1:5] 67 68 69 70 71
 $ : int [1:5] 33 67 79 153 155
 $ : int [1:7] 65 66 69 70 76 79 153
 $ : int [1:6] 4 65 69 71 76 78
 $ : int [1:4] 65 67 68 76
 $ : int [1:5] 65 67 71 153 157
 $ : int [1:7] 4 65 68 70 156 157 159
 $ : int [1:5] 73 74 75 96 99
 $ : int [1:6] 60 62 63 72 75 96
 $ : int [1:6] 18 21 72 75 98 99
 $ : int [1:7] 21 63 72 73 74 135 136
 $ : int [1:6] 67 68 69 77 78 79
 $ : int [1:5] 38 76 78 79 80
 $ : int [1:8] 1 4 6 68 76 77 80 162
 $ : int [1:6] 33 38 66 67 76 77
 $ : int [1:6] 35 38 77 78 162 163
 $ : int [1:4] 83 2243 2248 2317
 $ : int [1:6] 83 86 91 124 2243 2276
 $ : int [1:8] 81 82 122 124 125 2243 2317 2319
 $ : int [1:5] 85 86 89 137 138
 $ : int [1:7] 84 87 89 94 137 1325 1328
 $ : int [1:6] 82 84 89 91 124 138
 $ : int [1:6] 85 89 90 1328 2210 2211
 $ : int [1:4] 90 91 2276 2280
 $ : int [1:6] 84 85 86 87 90 91
 $ : int [1:6] 87 88 89 91 2210 2280
 $ : int [1:6] 82 86 88 89 90 2276
 $ : int [1:5] 93 94 95 137 144
 $ : int [1:7] 92 95 102 105 106 144 145
 $ : int [1:6] 85 92 95 137 1303 1325
 $ : int [1:7] 92 93 94 106 1299 1300 1303
 $ : int [1:9] 3 57 58 62 72 73 99 100 101
 $ : int [1:6] 16 19 100 101 548 590
 $ : int [1:5] 15 16 18 74 99
 $ : int [1:6] 16 72 74 96 98 101
  [list output truncated]
 - attr(*, "class")= chr "nb"
 - attr(*, "region.id")= chr [1:2495] "1" "2" "3" "4" ...
 - attr(*, "tri")= logi TRUE
 - attr(*, "call")= language tri2nb(coords = coords)
 - attr(*, "sym")= logi TRUE
In [13]:
summary(unlist(nbdists(nb_tri, coords)))
table(pol_pres15$types, card(nb_q))
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
   246.6   9847.2  12151.2  13485.2  14993.5 296973.7 
                
                   1   2   3   4   5   6   7   8   9  10  11  12  13
  Rural            0   5  41 183 413 465 271 105  55  16   8   0   1
  Urban           70  58  55  34  22  25  15  13   6   0   3   1   1
  Urban/rural      2   2  15  36 109 173 162  80  23   8   1   0   0
  Warsaw Borough   0   0   0   2   3   6   4   3   0   0   0   0   0

Sphere of Influence Neighbours

In [15]:
nb_soi <- graph2nb(soi.graph(nb_tri, coords))
nb_soi
Neighbour list object:
Number of regions: 2495 
Number of nonzero links: 12792 
Percentage nonzero weights: 0.2054932 
Average number of links: 5.127054 
In [16]:
n_comp <- n.comp.nb(nb_soi)
n_comp$nc
16
In [17]:
opar <- par(mar=c(0,0,0,0)+0.5)
plot(st_geometry(pol_pres15), border="grey", lwd=0.5)
plot(nb_soi, coords=st_coordinates(coords), add=TRUE, points=FALSE, lwd=0.5)
plot(diffnb(nb_tri, nb_soi), coords=st_coordinates(coords), col="orange",
     add=TRUE, points=FALSE, lwd=0.5)
par(opar)

Distance based Neighbours

In [18]:
k1 <- knn2nb(knearneigh(coords))
k1dists <- unlist(nbdists(k1, coords))
summary(k1dists)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  246.5  6663.4  8538.0  8275.1 10123.9 17978.8 
In [20]:
nb_d18 <- dnearneigh(coords, 0, 18000)
nb_d18
nb_d183 <- dnearneigh(coords, 0, 18300)
nb_d183
Neighbour list object:
Number of regions: 2495 
Number of nonzero links: 20358 
Percentage nonzero weights: 0.3270348 
Average number of links: 8.159519 
Neighbour list object:
Number of regions: 2495 
Number of nonzero links: 21086 
Percentage nonzero weights: 0.3387296 
Average number of links: 8.451303 
In [21]:
arha <- units::set_units(st_area(pol_pres15), hectare)
aggregate(arha, list(pol_pres15$types), mean)
A data.frame: 4 × 2
Group.1x
<fct><[]>
Rural 12500.101 []
Urban 4496.825 []
Urban/rural 16849.651 []
Warsaw Borough 2886.193 []

Weights specification

B weights

In [23]:
lw_q_B <- nb2listw(nb_q, style="B")
unlist(spweights.constants(lw_q_B))
n
2495
n1
2494
n2
2493
n3
2492
nn
6225025
S0
14242
S1
28484
S2
357280

Inverse distance weights

In [24]:
gwts <- lapply(nbdists(nb_d183, coords), function(x) 1/(x/1000))
lw_d183_idw_B <- nb2listw(nb_d183, glist=gwts, style="B")
unlist(spweights.constants(lw_d183_idw_B))[c(1,6:8)]
n
2495
S0
1841.34539353165
S1
533.760569727214
S2
7264.95835219975

Measures of Spatial Autocorrelation

Moran's I

$$ I = \frac{ n \sum_{(2)} w_{ij} z_i z_j }{ S_0 \sum_{i=1}^N z_i^2} $$

where $x_i, i = 1, \dots, n$ are $n$ observations of interest, $z_i = x_i - \bar{x}$ is a demeaned variable, $\sum_{(2)} = \sum_{i=1, i \neq j}^n \sum_{j=1}^n$, $w_{ij}$ are spatial weights, and $S_0 = \sum_{(2)} w_{ij}$.

Inference made on

$ Z(I) = \frac{I - E(I) } { \sqrt{Var(I)}} $

In [25]:
set.seed(1)
x <- rnorm(nrow(pol_pres15))
mt <- moran.test(x, lw_q_B, randomisation=FALSE)
In [26]:
glance_htest <- function(ht) c(ht$estimate, "Std deviate"=unname(ht$statistic), "p.value="=unname(ht$p.value))
broom_ok <- FALSE
if (names(broom::tidy(mt))[1] == "Moran I statistic") broom_ok <- TRUE
if (broom_ok) broom::tidy(mt)[,1:5] else glance_htest(mt)
Moran I statistic
-0.00477152022494938
Expectation
-0.000400962309542903
Variance
0.000140044906283242
Std deviate
-0.369320335081977
p.value=
0.644055514956574
In [27]:
beta <- 0.5e-02
t <- st_coordinates(coords)[,1]/1000
x_t <- x + beta*t
mt <- moran.test(x_t, lw_q_B, randomisation=FALSE)
if (broom_ok) broom::tidy(mt)[,1:5] else glance_htest(mt)
Moran I statistic
0.355771472212821
Expectation
-0.000400962309542903
Variance
0.000140044906283242
Std deviate
30.0972382498515
p.value=
2.63289897373615e-199
In [28]:
lmt <- lm.morantest(lm(x_t ~ t), lw_q_B)
if (broom_ok) broom::tidy(lmt)[,1:5] else glance_htest(lmt)
Observed Moran I
-0.00477722132779921
Expectation
-0.000789185004566425
Variance
0.000139787705152573
Std deviate
-0.337306428030206
p.value=
0.632057042753561

Global Measures

In [29]:
table(pol_pres15$types)
         Rural          Urban    Urban/rural Warsaw Borough 
          1563            303            611             18 
In [30]:
jcl <- joincount.test(pol_pres15$types, listw=lw_q_B)
if (broom_ok) {
    nm <- tibble::enframe(sapply(jcl, function(t) {
      nm <- substring(names(t$statistic), 18)
      paste0(nm, ":", nm)
    }))
    mat <- cbind(nm, do.call("rbind", (lapply(jcl, broom::tidy))))
    names(mat)[3] <- "Joincount"
    names(mat)[6] <- "Std.deviate"
    names(mat)[2] <- ""
    mat[,2:6]
} else {
    mat <- t(sapply(jcl, glance_htest))
    colnames(mat)[1] <- "Joincount"
    rownames(mat) <- sapply(jcl, function(t) {  
      nm <- substring(names(t$statistic), 18)
      paste0(nm, ":", nm)
    })
    mat
}
A matrix: 4 × 5 of type dbl
JoincountExpectationVarianceStd deviatep.value=
Rural:Rural30872793.92017811126.5342033 8.73200001.251041e-18
Urban:Urban 110 104.7185351 93.2993687 0.54678312.922639e-01
Urban/rural:Urban/rural 656 426.5255306 331.759032212.59862061.074347e-36
Warsaw Borough:Warsaw Borough 41 0.3501833 0.347427768.96462030.000000e+00
In [31]:
joincount.multi(pol_pres15$types, listw=lw_q_B)
A jcmulti: 11 × 4 of type dbl
JoincountExpectedVariancez-value
Rural:Rural30872793.92017811126.5342033 8.7320000
Urban:Urban 110 104.7185351 93.2993687 0.5467831
Urban/rural:Urban/rural 656 426.5255306 331.7590322 12.5986206
Warsaw Borough:Warsaw Borough 41 0.3501833 0.3474277 68.9646203
Urban:Rural 6681083.9408630 708.2086432-15.6297121
Urban/rural:Rural23592185.76853881267.1313345 4.8664913
Urban/rural:Urban 171 423.7286419 352.1895385-13.4668567
Warsaw Borough:Rural 12 64.3925265 46.4599085 -7.6865272
Warsaw Borough:Urban 9 12.4830042 11.7580036 -1.0157509
Warsaw Borough:Urban/rural 8 25.1719985 22.3538161 -3.6319930
Jtot32273795.48557291496.3984180-14.6958878
In [ ]: