-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
183 lines (147 loc) · 5.29 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "##",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE,
message = FALSE
)
```
# spEcula <img src="man/figures/logo.png" align="right" height="90"/>
<!-- badges: start -->
[](https://CRAN.R-project.org/package=spEcula)
[](https://spatlyu.r-universe.dev/spEcula)
<!-- badges: end -->
The goal of **spEcula** is to make it easier to use **R** for **spatial prediction** based on **various spatial relationships** (e.g. spatial dependence, spatial heterogeneity and geographical similarity).
## Overview
Full document of the most recent release of **spEcula** is online:
<https://spatlyu.github.io/spEcula/>
Current models and functions provided by **spEcula** are:
| **spatial prediction method** | **spEcula function** | **support status** |
|-----------------------------------|--------------------------|------------------------|
| Geographically Optimal Similarity | `gos()` | ✔️ |
| Sandwich Mapping Model | `sandwich()` | ✔️ |
## Installation
You can install the development version of *spEcula* from [*github*](https://github.com/SpatLyu/spEcula):
``` r
# install.packages("devtools")
devtools::install_github("SpatLyu/spEcula",
build_vignettes = T,
dep = T)
```
or install *spEcula* from [*r-universe*](https://spatlyu.r-universe.dev/spEcula):
```r
install.packages('spEcula',
repos = c("https://spatlyu.r-universe.dev",
"https://cran.rstudio.com/"),
dep = TRUE)
```
## Example
### Geographically Optimal Similarity (GOS) model
```{r example_gos}
library(spEcula)
data(zn)
data(grid)
zn$Zn = log(zn$Zn)
tictoc::tic()
g1 = gos(Zn ~ Slope + Water + NDVI + SOC + pH + Road + Mine,
data = zn, newdata = grid, kappa = 0.08,cores = 6)
tictoc::toc()
g1$pred = exp(g1$pred)
grid$pred = g1$pred
grid$uc99 = g1$`uncertainty99`
g1
```
```{r}
library(ggplot2)
library(cowplot)
library(viridis)
```
```{r gos_result,fig.width=9.5,fig.height=3.5}
f1 = ggplot(grid, aes(x = Lon, y = Lat, fill = pred)) +
geom_tile() +
scale_fill_viridis(option="magma", direction = -1) +
coord_equal() +
labs(fill='Prediction') +
theme_bw()
f2 = ggplot(grid, aes(x = Lon, y = Lat, fill = uc99)) +
geom_tile() +
scale_fill_viridis(option="mako", direction = -1) +
coord_equal() +
labs(fill=bquote(Uncertainty~(zeta==0.99))) +
theme_bw()
plot_grid(f1,f2,nrow = 1,label_fontfamily = 'serif',
labels = paste0('(',letters[1:2],')'),
label_fontface = 'plain',label_size = 10,
hjust = -1.5,align = 'hv') -> p
p
```
### Sandwich Mapping Model
```{r sandwich,fig.width=5.5,fig.height=3.5}
library(sf)
library(tidyverse)
library(spEcula)
simpath = system.file("extdata", "sim.gpkg", package="spEcula")
sampling = read_sf(simpath,layer = 'sim_sampling')
ssh = read_sf(simpath,layer = 'sim_ssh')
reporting = read_sf(simpath,layer = 'sim_reporting')
sampling_zone = sampling %>%
st_join(ssh['X']) %>%
st_drop_geometry()
library(ggpubr)
ggerrorplot(sampling_zone, x = "X", y = "Value",
desc_stat = "mean_sd", color = "black",
add = "violin", add.params = list(color = "darkgray")) +
geom_text(data = summarise(sampling_zone,vmean = mean(Value),.by = X),
aes(x = X, y = vmean, label = round(vmean,2)),
vjust = -0.5, hjust = -0.15, color = "black",size = 3) +
scale_x_discrete(labels = LETTERS[1:4]) +
theme(axis.title.x = element_blank())
sim_est = sandwich(sampling = sampling,stratification = ssh,reporting = reporting,
sampling_attr = 'Value',ssh_zone = 'X',reporting_id = 'Y',
weight_type = 'area')
sim_est
```
```{r sandwich_sim_est,fig.width=9.75, fig.height=3.5}
library(cowplot)
f1 = ggplot(data = sim_est, aes(fill = sandwichest_mean),
color = "darkgray") +
geom_sf() +
labs(fill='mean') +
scale_fill_gradient(low = "#f0bc9c", high = "red",
breaks = range(sim_est$sandwichest_mean)) +
theme_bw() +
theme(
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.position = 'right',
legend.background = element_rect(fill = 'transparent',color = NA)
)
f2 = ggplot(data = sim_est, aes(fill = sandwichest_standarderror),
color = "darkgray") +
geom_sf() +
labs(fill='se') +
scale_fill_gradient(low = "#b6edf0", high = "blue",
breaks = range(sim_est$sandwichest_standarderror)) +
theme_bw() +
theme(
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.position = 'right',
legend.background = element_rect(fill = 'transparent',color = NA)
)
plot_grid(f1, f2, nrow = 1,label_fontfamily = 'serif',
labels = paste0('(',letters[1:4],')'),
label_fontface = 'plain',label_size = 10,
hjust = 0.05,align = 'hv') -> p
p
```