-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_generates_model.R
85 lines (64 loc) · 2.73 KB
/
01_generates_model.R
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
######################################################
#### Generates tif file to make hexSticker figure ####
######################################################
# By Sara Mortara & Andrea Sanchez-Tapia
# loading packages
library(dplyr)
library(modleR)
library(raster)
# download data of Tremarctos ornatus
data <- rgbif::occ_search(scientificName = "Tremarctos ornatus")
df <- data$data %>%
filter(., !is.na(decimalLatitude) & !is.na(decimalLongitude)) %>%
.[.$decimalLatitude!=max(.$decimalLatitude),] %>%
as.data.frame(.[,c(3,4)])
pca_files <- list.files(path="data/raster/pca_env",
full.names = TRUE)
pca <- stack(pca_files)
#write.table(df, "data/ursal.csv", col.names = TRUE, row.names = FALSE)
pca
dim(df)
head(df)
# latin america countries
# la <- c("Brazil", "Mexico", "Colombia", "Argentina", "Peru",
# "Venezuela", 'Chile', 'Guatemala', 'Ecuador', 'Cuba',
# 'Bolivia', 'Haiti', 'Dominican Republic', 'Honduras',
# 'Paraguay', 'El Salvador', 'Nicaragua', 'Costa Rica',
# 'Panama', 'Puerto Rico', 'Uruguay', 'Guadeloupe', 'Martinique',
# 'French Guiana', 'Saint Martin', 'Saint Barthelemy')
cores <- c(rgb(0,0,0, maxColorValue = 255),
rgb(28,28,25, maxColorValue = 255),
rgb(252,234,151, maxColorValue = 255),
rgb(166,2,2, maxColorValue = 255),
rgb(255,192,3, maxColorValue = 255)
)
red <- rgb(166,2,2, maxColorValue = 255, alpha=50)
dim(df)
df2 <- df[sample(1:nrow(df), 100),]
plot(pca[[1]], legend=FALSE, las=1, col=cores[5])
points(decimalLatitude ~ decimalLongitude, df2, col=red, pch=19, cex=1.5)
# run model for ursal
ursal_setup <- setup_sdmdata(species_name = "ursal",
occurrences = df,
predictors = pca,
models_dir = "data/model",
lon = 'decimalLongitude',
lat = 'decimalLatitude',
boot_n = 1,
boot_proportion = 0.7,
buffer_type = "mean",
clean_dupl = TRUE,
clean_nas = TRUE,
clean_uni = TRUE,
select_variables = FALSE
)
ursal_rf <- do_any(species_name= 'ursal',
predictors = pca,
sdmdata = ursal_setup,
models_dir = "data/model",
algo = "rf",
write_png = TRUE)
ursal_tif <- raster('data/model/ursal/present/partitions/rf_cont_ursal_1_1.tif') %>%
aggregate(., c(5, 5))
plot(ursal_tif)
writeRaster(ursal_tif,filename = 'data/modleR.tif')