-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariogram_tutorial1.rmd
More file actions
147 lines (114 loc) · 5.2 KB
/
variogram_tutorial1.rmd
File metadata and controls
147 lines (114 loc) · 5.2 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
---
title: "Semivariograms part 1"
author: "Daniel M. Parker (w/ help from ChatGPT)"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_float: false
number_sections: false
code_folding: "hide"
theme: cosmo
highlight: textmate
---
# **Introduction to Semivariograms**
A semivariogram describes how data values change with increasing distance. It helps quantify **spatial dependence**.
## **Spatial Dependence and Tobler's Law**
Tobler’s First Law of Geography states:
> "Everything is related to everything else, but near things are more related than distant things."
In semivariograms:
- **Short distances** → Similar values (low variance).
- **Large distances** → Increasing dissimilarity.
---
# **Semivariogram Components**
The key parameters of a semivariogram are:
- **Nugget (`C₀`)**: Variance at distance = 0 (measurement error or microscale variation).
- **Sill (`C`)**: Total variance when the semivariogram levels off.
- **Range (`A`)**: Distance at which spatial autocorrelation disappears.
### **A basic semivariogram**
```{r semivariogram-plot, echo=FALSE}
library(ggplot2)
# Define parameters
distances <- seq(0, 100, by=5) # Distance values
nugget <- 15 # Nugget above zero
sill <- 160 # Sill where semivariance levels off
range_value <- 30 # Range where spatial dependence disappears
# Spherical model approximation
spherical_variogram <- function(h, nugget, sill, range_value) {
ifelse(h < range_value,
nugget + (sill - nugget) * (1.5 * (h / range_value) - 0.5 * (h / range_value)^3),
sill)
}
# Compute semivariance values
semivariance <- sapply(distances, spherical_variogram, nugget = nugget, sill = sill, range_value = range_value)
# Create a data frame
data <- data.frame(Distance = distances, Semivariance = semivariance)
# Plot using ggplot2
library(ggplot2)
ggplot(data, aes(x = Distance, y = Semivariance)) +
geom_line(color = "blue", linewidth = 1.2) + # Semivariogram line
geom_hline(yintercept = sill, linetype = "dashed", color = "red") + # Sill
geom_hline(yintercept = nugget, linetype = "dashed", color = "green") + # Nugget
geom_vline(xintercept = range_value, linetype = "dashed", color = "purple") + # Range
annotate("text", x = 5, y = nugget + 5, label = "Nugget", color = "green", fontface = "bold") +
annotate("text", x = 80, y = sill - 5, label = "Sill", color = "red", fontface = "bold") +
annotate("text", x = range_value + 2, y = sill / 2, label = "Range", color = "purple", fontface = "bold", angle = 90) +
labs(title = "Improved Semivariogram Diagram", x = "Distance", y = "Semivariance") +
ylim(0, 170) + # Set y-axis range from 0 to 170
theme_minimal()
```
---
# **Basic Variogram Statistics**
### **1. Sill - Nugget**
- Measures the structured spatial variance.
- **Large value** → Strong spatial structure.
- **Small value** → Weak spatial dependence.
### **2. Relative Nugget Effect (Nugget/Sill)**
- **High (~1)** → Mostly random noise.
- **Low (~0)** → Strong spatial structure.
### **3. Range-to-Distance Ratio (Range / Max Distance)**
- **Close to 1** → Strong correlation across the study area.
- **Close to 0** → Correlation fades quickly.
### **4. Partial Sill (Sill - Nugget)**
- Measures the variance **explained by spatial dependence**.
### **5. Proportion of Spatially Structured Variation**
- **Formula:** Partial Sill / Sill.
- **Close to 1** → Mostly structured.
- **Close to 0** → Mostly random.
### **6. Mean Variogram Value Over Specific Lag Distances**
- Helps analyze how spatial dependence changes at different distances.
### **7. Relative Range (Range / Study Area Size)**
- **Close to 1** → Correlation spans the entire study area.
- **Much less than 1** → Limited spatial dependence.
- **This will be discussed briefly at the end of the tutorial.**
### **8. Anisotropy**
- Sometimes spatial dependence varies by direction, a phenomenon known as **anisotropy**.
- To measure anisotropy, we can compute variograms along different angles (e.g., **0°, 45°, 90°**).
- If spatial correlation differs by direction, we can quantify it using **anisotropy ratios**.
---
# **Working Through a Real Example: The Meuse Dataset**
```{r empirical-variogram, message=FALSE}
library(sp)
library(gstat)
data(meuse, package = "sp")
coordinates(meuse) <- ~x+y # Define spatial coordinates
# Compute the Empirical Variogram
variogram_model <- variogram(zinc ~ 1, meuse)
plot(variogram_model, main = "Empirical Semivariogram")
# Fit a Variogram Model
vgm_model_nugget <- fit.variogram(variogram_model,
vgm(psill = 10000, model = "Nug", range = 0, add.to =
vgm(psill = 150000, model = "Sph", range = 600)))
print(vgm_model_nugget)
plot(variogram_model, vgm_model_nugget, main = "Fitted Semivariogram Model")
```
# Next Step
For practice with your own CSV data (and side-by-side comparison of correlograms and semivariograms), see:
- [Spatial Dependence Part 2 repo](https://github.com/parker-group/spatial_depend_2)
- [Live tutorial site](https://parker-group.github.io/spatial_depend_2/)
### **Rendering the RMarkdown File**
Run in R:
```r
rmarkdown::render("variogram_tutorial1.Rmd")
```
This will create a Markdown file that you can push to GitHub.