-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbsenceBGData.Rmd
118 lines (94 loc) · 2.74 KB
/
AbsenceBGData.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
---
title: "Absence_presence_data"
author: "Hoang Le Xuan"
date: "2024-04-04"
output: html_document
---
Reference:https://rspatial.org/
## Absence and background points
first get the list of filenames with the predictor raster data
```{r}
library(dismo)
library(maptools)
data(wrld_simpl)
```
```{r}
files <- list.files(path="data/bioclim", pattern='asc', full.names=TRUE )
# we use the first file to create a RasterLayer
mask <- raster(files[1])
# select 500 random points
# set seed to assure that the examples will always
# have the same random sample.
set.seed(1963)
bg <- randomPoints(mask, 500 )
```
And inspect the results by plotting
```{r}
# set up the plotting area for two maps
par(mfrow=c(1,2))
plot(!is.na(mask), legend=FALSE)
points(bg, cex=0.5)
# now we repeat the sampling, but limit
# the area of sampling using a spatial extent
e <- extent(-180, 180, -90, 90)
bg2 <- randomPoints(mask, 50, ext=e)
plot(!is.na(mask), legend=FALSE)
plot(e, add=TRUE, col='red')
points(bg2, cex=0.5)
```
## Creating SpatialPointsDataFrame
```{r}
ct <- read.csv("data/ctsel.csv")
coordinates(ct) <- ~lon+lat
projection(ct) <- CRS('+proj=longlat +datum=WGS84')
```
## Environmental data
### Raster data
create a RasterStack of predictor variables.
```{r}
predictors <- stack(files)
predictors
#class : RasterStack
#dimensions : 3600, 8640, 31104000, 19 (nrow, ncol, ncell, nlayers)
#resolution : 0.04166667, 0.04166667 (x, y)
#extent : -180, 180, -60, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#names : bclim1, bclim10, bclim11, bclim12, bclim13, bclim14, bclim15, bclim16, bclim17, bclim18, bclim19, bclim2, bclim3, bclim4, bclim5,
names(predictors)
#[1] "bclim1" "bclim10" "bclim11" "bclim12" "bclim13" "bclim14" "bclim15" "bclim16" "bclim17" "bclim18"
#[11] "bclim19" "bclim2" "bclim3" "bclim4" "bclim5" "bclim6" "bclim7" "bclim8" "bclim9"
```
Overlay the data point to the bioclim1 map:
```{r}
# first layer of the RasterStack
plot(predictors, 1)
# note the "add=TRUE" argument with plot
plot(wrld_simpl, add=TRUE)
# with the points function, "add" is implicit
points(ct, col='blue')
```
## Extracting values from rasters
```{r}
presvals <- extract(predictors, ct)
# Predictor values
# setting random seed to always create the same
# random set of points for this example
set.seed(0)
backgr <- randomPoints(predictors, 500)
absvals <- extract(predictors, backgr)
#Presence Absence point
pb <- c(rep(1, nrow(presvals)), rep(0, nrow(absvals)))
sdmdata <- data.frame(cbind(pb, rbind(presvals, absvals)))
sdmdata
```
```{r}
summary(sdmdata)
pairs(sdmdata[,2:5], cex=0.1)
```
```{r}
saveRDS(sdmdata, "sdm.Rds")
saveRDS(presvals, "pvals.Rds")
#load again;
sdmdata <- readRDS("sdm.Rds")
presvals <- readRDS("pvals.Rds")
```