-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path03_exercise1.qmd
More file actions
150 lines (104 loc) · 3.71 KB
/
03_exercise1.qmd
File metadata and controls
150 lines (104 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
::: {.content-hidden unless-format="html"}
$$
\newcommand{\Exp}{\mathrm{E}}
\newcommand\given[1][]{\:#1\vert\:}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\rank}{\mathrm{rank}}
\newcommand{\bm}[1]{\boldsymbol{\mathbf{#1}}}
$$
:::
# Exercises I
### Required packages {.unnumbered}
```{r, message = FALSE, warning = FALSE, results = 'hide'}
pkgs <- c("sf", "mapview", "areal", "spdep", "spatialreg", "tmap", "viridisLite",
"ggplot2", "ggthemes", "gridExtra") # note: load spdep first, then spatialreg
lapply(pkgs, require, character.only = TRUE)
```
### Session info {.unnumbered}
```{r}
sessionInfo()
```
### Reload data from pervious session {.unnumbered}
```{r}
load("_data/msoa2_spatial.RData")
```
## General Exercises
### 1) Can you import the spatial administrative units of Germany ("Kreisgrenzen_2020_mit_Einwohnerzahl" in _data folder) and make a simple plot of the boundaries? {.unnumbered}
```{r}
# Import shape file layer
ger.sdpf <- st_read(dsn = "_data/Kreisgrenzen_2020_mit_Einwohnerzahl",
layer = "KRS_ew_20")
# Plot via ggplot
gp <- ggplot(ger.sdpf)+
geom_sf( color = "magenta", fill = NA)+
coord_sf(datum = NA)+
theme_map()
gp
```
### 2) What is the Coordinate reference system of this German shape file? {.unnumbered}
```{r}
st_crs(ger.sdpf)
```
### 3) Using the MSOA data in London from above: Please draw a map showing the distribution of share of non-EU immigrants (per_nonEU). {.unnumbered}
```{r}
gp1 <- ggplot(msoa.spdf)+
geom_sf(aes(fill = per_nonEU))+
scale_fill_viridis_c(option = "A")+
coord_sf(datum = NA)+
theme_map()+
theme(legend.position = c(.9, .4))
gp1
```
### 4) Below we will load some additional data on heat islands. {.unnumbered}
We will add some Data about [London's Urban Heat Island](https://data.london.gov.uk/dataset/london-s-urban-heat-island---average-summer/). It contains information about the mean temperature at midnight during the summer of 2011.
This is a tif file that we need to read in with stars and then transform into sf.
```{r}
library(stars)
# Read geo_tif with stars
urbclim <- read_stars("https://data.london.gov.uk/download/ae16d5af-5dce-49bc-b1e2-88bb41e8bfd0/f4e3a05d-fad7-4b56-8c42-ba2274f3bb3a/London_Tmin_midnight_2011.tif")
# urbclim <- read_stars("_data/London_Tmin_midnight_2011.tif")
# Transfer to sf
urbclim.spdf <- st_as_sf(urbclim)
names(urbclim.spdf)[1] <- "Tmin_midnight"
```
a) On which projection is the urbclim temperature data?
b) Can you please calculate the average night-time temperature for each MSOA using area weighted interpolation. Make sure that the objects are on the same projections / crs.
c) Create a map showing the temperature for each MSOA, and plot it next to the maps of non-EU immigrant residents (e.g. using `grid.arrange()`) .
```{r}
# Check projection
st_crs(urbclim.spdf)
```
```{r}
# Add id
urbclim.spdf$id <- rownames(urbclim.spdf)
# Bring on common crs
urbclim.spdf <- st_transform(urbclim.spdf, crs = st_crs(msoa.spdf))
# Use area weights interpolation to merge
msoa.spdf <- aw_interpolate(
msoa.spdf,
tid = "MSOA11CD",
source = urbclim.spdf,
sid = "id",
weight = "sum",
output = "sf",
intensive = "Tmin_midnight"
)
```
```{r}
# Make map of Temperature
gp2 <- ggplot(msoa.spdf)+
geom_sf(aes(fill = Tmin_midnight))+
scale_fill_viridis_c(option = "C")+
coord_sf(datum = NA)+
theme_map()+
theme(legend.position = c(.9, .4))
# Plot two ggplot maps next to each other
gp <- grid.arrange(gp1, gp2, nrow = 1)
gp
```
### 4) What's the correlation between the share of non-Eu residents and the termperature. {.unnumbered}
```{r}
mod1.lm <- lm(Tmin_midnight ~ per_nonEU, data = msoa.spdf)
summary(mod1.lm)
```