generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
14_kriging.Rmd
235 lines (165 loc) · 6.23 KB
/
14_kriging.Rmd
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Kriging
**Learning objectives:**
- What is Kriging
- How it can be used to interpolate spatial data
- How to perform Kriging in R
## What is Kriging
```{r warning=FALSE, message=FALSE, echo=FALSE}
library(tidyverse)
theme_set(theme_minimal())
```
Kriging is a geostatistical interpolation technique that is used to estimate the value of a variable at an unobserved location based on the values of the variable at nearby locations. Kriging is based on the assumption that the spatial correlation between the values of the variable decreases with distance. Kriging is widely used in to interpolate spatial data when data are sparse or irregularly distributed.
## Types of Kriging
For example, Simple Kriging assumes the mean of the random field, μ(s), is known;
- **Simple Kriging**: Assumes that the mean of the variable is known and constant across the study area.
Formula: $Z(s_0) = \mu + \sum_{i=1}^{n} \lambda_i (Z(s_i) - \mu)$
where $\mu$ is the mean, $\lambda_i$ are the weights, and $Z(s_i)$ are the observed values.
Ordinary Kriging assumes a constant unknown mean, μ(s)=μ;
- **Ordinary Kriging**: Assumes that the mean of the variable is unknown and varies across the study area.
Formula: $Z(s_0) = \sum_{i=1}^{n} \lambda_i Z(s_i)$
where $\lambda_i$ are the weights and $Z(s_i)$ are the observed values.
Universal Kriging can be used for data with an unknown non-stationary mean structure.
- **Universal Kriging**: Assumes that the mean of the variable is unknown and varies across the study area, but can be modeled as a function of covariates.
Formula: $Z(s_0) = \sum_{i=1}^{n} \lambda_i Z(s_i) + \beta X(s_0)$
where $\lambda_i$ are the weights, $Z(s_i)$ are the observed values, $\beta$ is the coefficient for the covariate $X(s_0)$, and $X(s_0)$ is the value of the covariate at the unobserved location.
## Performing Kriging in R
To perform Kriging in R, you can use the `gstat` package, which provides functions for geostatistical analysis. The `gstat` package allows you to create a variogram model, fit the model to the data, and use the model to interpolate values at unobserved locations.
```{r}
library(gstat)
```
### Example: Simple Kriging
In this example, we will perform simple Kriging on a dataset of air quality measurements. We will estimate the air quality at unobserved locations based on the measurements at nearby monitoring stations.
Step 1: Load the air quality data and create a variogram model.
```{r warning=FALSE, message=FALSE}
library(sp)
# Load the air quality data
data(meuse)
coordinates(meuse) <- c("x", "y")
meuse%>%
as_data_frame()%>%
select(x,y,zinc)%>%
head()
```
Step 2: Visualize the data.
```{r}
meuse%>%
as_data_frame()%>%
select(x,y,zinc)%>%
ggplot(aes(x,y,color=zinc))+
geom_point()+
scale_color_viridis_c()
```
What we need is a grid of points where we want to predict the zinc concentration.
```{r}
data(meuse.grid)
meuse.grid %>%
as_data_frame()%>%
select(x,y)%>%
ggplot(aes(x,y))+
geom_point(shape=".")
```
```{r}
zinc_data <- meuse%>%
as_data_frame()%>%
select(x,y,zinc)
grid_data <- meuse.grid %>%
as_data_frame()%>%
select(x,y)
ggplot()+
geom_point(data=grid_data,aes(x,y),shape=".")+
geom_point(data=zinc_data,aes(x,y,color=zinc))+
scale_color_viridis_c()
```
### Variogram Model
We perform a Variogram analysis to understand the spatial correlation of the zinc concentration in the dataset.
Formula: $V(h) = \frac{1}{2N(h)} \sum_{i=1}^{N(h)} (Z(s_i) - Z(s_i + h))^2$
where $V(h)$ is the semivariance at lag distance $h$, $N(h)$ is the number of pairs of observations at lag distance $h$, $Z(s_i)$ is the value of the variable at location $s_i$, and $Z(s_i + h)$ is the value of the variable at location $s_i$ plus lag distance $h$.
```{r}
# Create a variogram model
vc <- variogram(log(zinc) ~ 1, meuse, cloud = TRUE)
plot(vc)
v <- variogram(log(zinc) ~ 1, meuse)
plot(v)
```
```{r}
# Fit the variogram model
fv <- fit.variogram(object = v,
model = vgm(psill = 0.5,
model = "Sph",
range = 900,
nugget = 0.1))
plot(v,fv)
```
### Perform Simple Kriging
`gstat` function to compute the Kriging predictions:
`?gstat`
```{r}
library(sf)
data(meuse)
data(meuse.grid)
meuse <- st_as_sf(meuse,
coords = c("x", "y"),
crs = 28992)
meuse.grid <- st_as_sf(meuse.grid,
coords = c("x", "y"),
crs = 28992)
```
```{r}
v <- variogram(log(zinc) ~ 1, meuse)
plot(v)
```
```{r}
fv <- fit.variogram(object = v,
model = vgm(psill = 0.5,
model = "Sph",
range = 900,
nugget = 0.1))
fv
plot(v, fv, cex = 1.5)
```
```{r}
k <- gstat(formula = log(zinc) ~ 1,
data = meuse,
model = fv)
kpred <- predict(k,meuse.grid)
```
```{r}
ggplot() +
geom_sf(data = kpred,
aes(color = var1.pred)) +
# geom_sf(data = meuse) +
viridis::scale_color_viridis(name = "log(zinc)")
ggplot() +
geom_sf(data = kpred,
aes(color = var1.var)) +
geom_sf(data = meuse) +
viridis::scale_color_viridis(name = "variance")
```
```{r}
# Perform simple Kriging
kriged <- krige(log(zinc) ~ 1, meuse,
kpred,
model = fv)
```
### Plotting the Results
```{r}
# Plot the results
plot(kriged)
```
## Summary
- Kriging is a geostatistical interpolation technique used to estimate the value of a variable at unobserved locations.
- There are different types of Kriging methods, including simple Kriging, ordinary Kriging, universal Kriging, and co-Kriging.
- In R, you can perform Kriging using the `gstat` package, which provides functions for geostatistical analysis.
## Additional Resources
- [gstat package documentation](https://cran.r-project.org/web/packages/gstat/gstat.pdf)
- [Geostatistics with R](https://www.r-bloggers.com/2016/02/geostatistics-with-r-tutorial/)
- [Introduction to Geostatistics](https://www.youtube.com/watch?v=8v9v7JcOwJc)
## Meeting Videos {-}
### Cohort 1 {-}
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary> Meeting chat log </summary>
```
LOG
```
</details>