-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
executable file
·213 lines (155 loc) · 4.96 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
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
---
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%"
)
```
# sdmtools
<!-- badges: start -->
[![R-CMD-check](https://github.com/idem-lab/sdmtools/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/idem-lab/sdmtools/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/idem-lab/sdmtools/branch/main/graph/badge.svg)](https://app.codecov.io/gh/idem-lab/sdmtools?branch=main)
![GitHub License](https://img.shields.io/github/license/idem-lab/sdmtools)
[![Lifecycle:](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
![GitHub commit activity](https://img.shields.io/github/commit-activity/w/idem-lab/sdmtools)
![GitHub Downloads (all assets, all releases)](https://img.shields.io/github/downloads/idem-lab/sdmtools/total)
![GitHub last commit](https://img.shields.io/github/last-commit/idem-lab/sdmtools)
![GitHub R package version](https://img.shields.io/github/r-package/v/idem-lab/sdmtools)
![GitHub commits since latest release](https://img.shields.io/github/commits-since/idem-lab/sdmtools/latest)
[![Codecov test coverage](https://codecov.io/gh/idem-lab/sdmtools/graph/badge.svg)](https://app.codecov.io/gh/idem-lab/sdmtools)
<!-- badges: end -->
A set of helper functions to facilitate species distribution modelling.
## Installation
You can install `sdmtools` with:
``` r
install.packages(
"sdmtools",
repos = "https://idem-lab.r-universe.dev"
)
```
## Data
```{r}
library(sdmtools)
```
`raster_to_terra` — an annotated equivalence table of functions from the `raster` and `terra`. First 5 lines:
```{r, echo=FALSE}
knitr::kable(
raster_to_terra[1:5,],
format = "html",
col.names = c("raster", "terra", "comment for terra use"),
column.spec = c("c", "c", ">{\\raggedright}p{8cm}")
)
```
`global_regions` — a tibble showing the WHO region, UN region, and continent for for 249 countries and country-like things. First 5 lines:
```{r, echo=FALSE}
knitr::kable(
global_regions[1:5,],
format = "html",
#col.names = c("raster", "terra", "comment for terra use"),
column.spec = c("c", "c", ">{\\raggedright}p{8cm}")
)
```
### Data-generating functions
The package `terra` is fiddly about storing its `spat...` objects in packages, so we chose to generate example spatial data on-demand using functions, rather than storing it.
`example_raster` — an example `spatRaster`.
```{r}
library(terra)
r <- example_raster()
r
plot(r)
```
`example_vector` — an example `spatVector`.
```{r}
library(terra)
v <- example_vector()
v
plot(v)
```
`make_africa_mask` — makes a mask layer of Africa based on shapefiles from `malariaAtlas::getShp`.
Can produce either a `SpatRaster` or `SpatVector`.
```{r}
library(terra)
africa_mask <- make_africa_mask(type = "vector")
plot(africa_mask)
```
## Function examples
`rastpointplot` — simple utility to plot a raster with points over it.
```{r}
rastpointplot(r,v)
```
`source_R` — source all R files in a target directory
```r
source_R("/Users/frankenstein/project/R") # do not run
```
`import_rasts` — import all rasters from a directory into a single object
```r
rasters <- import_rasts("/data/grids/covariates") # do not run
```
`split_rast` — split a raster.
```{r}
r <- example_raster()
s <- split_rast(r, grain = 2)
s
```
```{r}
ps <- lapply(
s,
FUN = extend,
y = r
) |>
rast()
c(
r,
ps
) |>
plot()
```
### Functions for a species distribution modelling workflow
We have some covariate layers: `cov1` and `cov2`
```{r}
library(terra)
cov1 <- example_raster(
seed = -44,
layername = "cov1"
)
cov2 <- example_raster(
seed = 15.3,
layername = "cov2"
)
covs <- c(cov1, cov2)
```
`std_rast` — standardise a `spatRaster` by transforming it to have a range of 0—1
```{r}
cov1_st <- std_rast(cov1)
plot(cov1_st)
```
We have some presences and absences
```{r}
presences <- example_vector(seed = 68) %>%
as.data.frame(geom = "xy")
absences <- example_vector(seed = 9.6) %>%
as.data.frame(geom = "xy")
presences
```
`extract_covariates` — extract covariate values from `spatRaster` or `raster` layers for a given set of points
Pass in either `presences` and `absences` as a `data.frame` or `tibble` of with , or `presences_and_absences` as a single data frame points with a presence or ID column(s)
```{r}
sdm_data <- extract_covariates(
covariates = covs,
presences = presences,
absences = absences
)
```
We can then make a spatial prediction of our model using `predict_sdm` and write and read it out in a single step with `writereadrast`, and write it to a temporary file with `temptif`:
```{r}
# first we make a simple model, using data from above
m <- glm(presence ~ cov1 + cov2, data = sdm_data)
prediction_rast <- predict_sdm(m, covs) |>
writereadrast(filename = temptif())
plot(prediction_rast)
```