-
Notifications
You must be signed in to change notification settings - Fork 0
/
preparing_env_data.R
65 lines (46 loc) · 1.91 KB
/
preparing_env_data.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
##### Adjusting environmental data #####
#Unlink(".RData") if necessary
#remotes::install_github("rspatial/terra")
#Input filenames
inf <- list.files("data/rasters", pattern = "tif$", full.names = TRUE)
#Create output filenames and folder
outf <- gsub("data/rasters", "output", inf)
dir.create("output", FALSE, FALSE)
#Shapefile and extent to crop
sc <- vect("data/polygons/sc.shp")
new_crs <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
sc <- project(sc, new_crs)
#sc <- buffer(sc, 10000)
#Aggregate to 5 km, crop, and mask
for (i in 1:length(inf)) {
r <- terra::rast(inf[i])
r <- terra::aggregate(r, 5, na.rm = TRUE)
r <- terra::crop(r, sc, mask = TRUE, filename = outf[i], overwrite = TRUE)
}
##### Principal component analysis of environmental data #####
vars <- list.files("output/", pattern = "tif$", full.name = TRUE)
vars <- rast(vars)
#Selecting only the variables
vars <- as.data.frame(vars, xy = TRUE)
vars <- na.omit(vars)
#Standardize variables
vars_scaled <- data.frame(apply(vars[, 3:25], 2, scale))
#PCA object: use return = TRUE to return the rotated values
pca_out <- prcomp(x = vars_scaled, retx = TRUE)
#Selecting components that represent ~95% of bioclimatic information
n_axes <- length(summary(pca_out)$importance[3, ])
cum_vars <- summary(pca_out)$importance[3, ]
vars_95 <- cum_vars <= 0.95 #five first axes
#Load scores
axes <- as.data.frame(pca_out$x) #rotated values
axes_95 <- axes[, vars_95] == T
axes_vars_95 <- axes[, 1:ncol(axes_95)]
axes_xy <- cbind(vars[, 1:2], axes_vars_95) #getting coordinates back
saveRDS(axes_xy, 'rds/predictors_df.rds') #Whole study region axes to be used
#As raster now
axes_xy_raster <- rast(axes_xy)
plot(axes_xy_raster)
#Adding a coordinate system
crs(axes_xy_raster) <- "epsg:4326"
saveRDS(axes_xy_raster, 'rds/predictors_raster.rds')
rm(list=ls())