# install.packages('sf')
# install.packages('RStoolbox')
# install.packages('maptools')
# install.packages("devtools")
# install_github("narayanibarve/ENMGadgets")
library(devtools)
library(ENMGadgets)
require(ENMGadgets)
library(raster)
library(rgdal)
library(sf) # This package allows you to import shapefiles
library(RStoolbox) #This package allows you to perform PCA on rasters or raster stacks
library(ggplot2)
library(reshape2)
library(maptools)
This automatically downloads all 19 of the bioclim variables as a raster stack. You can download it in 3 resolutions: 2.5, 5, and 10. 10 returns grids that are about 18km x 18km. Obviously, we want more detail, but to save on computation time, we will start all models with the coarsest resolution
bioclim10 <- getData("worldclim",var="bio",res=10)
These presence data have already been cleaned
x_trop_pres <- read.csv('data/x_trop_cleaned_pres_df.csv')
Because we are using the native range to generate background data, we need to use the native range to ‘clip’ the bioclim raster. This is easy to do by creating an account in the IUCN redlist, and then just downloading a shapefile of the geographic range. Then I saved the shapefile in the working directory, and this command
x_trop_native_range_shapefile <- shapefile('data/x_trop_sf.shp')
projection(bioclim10)
## [1] "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
projection(x_trop_native_range_shapefile)
## [1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
Here I am cropping the the bioclim raster stack based on the native range shapefile I imported above. Then I am plotting all the bioclim variables to demonstrate they are clipped in the extent of the imported shapefile
bio_10_native_range_crop_xtrop <- crop(bioclim10, extent(x_trop_native_range_shapefile))
plot(bio_10_native_range_crop_xtrop)
Now I am importing the FL shapfile. This I just downloaded from a GIS website
florida_shapefile<-shapefile('data/tl_2016_12_cousub.shp')
Because all three are in different projections, I have to reproject them so that they match
projection(bioclim10)
## [1] "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
florida_shapefile <- spTransform(florida_shapefile,
CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
x_trop_native_range_shapefile <- spTransform(x_trop_native_range_shapefile,
CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
Here I am cropping the the bioclim raster stack based on the native range shapefile I imported above. Then I am plotting all the bioclim variables to demonstrate they are clipped in the extent of the imported shapefile
bio_10_native_range_crop_xtrop <- crop(bioclim10, extent(x_trop_native_range_shapefile))
plot(bio_10_native_range_crop_xtrop)
bio_10_fl_crop <- crop(bioclim10, extent(florida_shapefile))
plot(bio_10_fl_crop)
In order to use Narayani’s code, we have to export each of these rasters as .asci files.
writeRaster(bio_10_fl_crop, filename=names(bio_10_fl_crop), bylayer=TRUE,format="ascii")
# Now, manually put all of the bio1-bio19.asc files into the folder called "Florida_bioclim"
# Do the same thing for this bit of code (I have it as a comment as otherwise it will not run properly)
# writeRaster(bio_10_native_range_crop_xtrop, filename=names(bio_10_fl_crop), bylayer=TRUE,format="ascii")
# Now, manually put all of the bio1-bio19.asc files into the folder called "NR_bioclim"
First you have to run her function.
PCAProjection <- function(BioStackFiles=NA,LoadingFile=NA,
CompImpFile=NA,ProjectonStackFiles=NA,
OutputFolder=NA)
{
if(is.na(BioStackFiles[1])){
stop("Please specify BioStackFiles (bioclimatic ASCII files)")
}
print(BioStackFiles)
BioStack = MakeStack(BioStackFiles)
if(is.na(LoadingFile)){
stop("Please specify LoadingFile (PCA loading)")
}
if(is.na(CompImpFile)){
stop("Please specify CompImpFile (PCA summary)")
}
BioPt1 = rasterToPoints(BioStack)
print("Generating principal component")
pcaSummary = prcomp(na.omit(BioPt1[,3:dim(BioPt1)[2]]), center=TRUE, scale = TRUE)
pcaScores = pcaSummary$x
d1 = dim(pcaScores)
for (i in 1:d1[2])
{
print(paste("Writing principal component file ", i ), sep = " ")
tbl1 = cbind(BioPt1[,1:2], pcaScores[,i])
### Faster but has error sometimes. The problem lies with rasterFromXYZ function.
# r2 = rasterFromXYZ(tbl1,digits=5)
### till here
## Slower but will work irrespective
XYTbl = tbl1[,1:2]
r1 = BioStack[[i]]
r2 = rasterize(XYTbl,r1,field=tbl1[,3])
## Till here
FileName = paste("Comp",i,".asc", sep = "")
writeRaster(r2,FileName)
}
write.table(pcaSummary$rotation, LoadingFile, row.names=T, col.names=T, sep = ",")
### Summary table for PCA
StdDev = pcaSummary$sdev
## Variance explained by each component
VarExp = pcaSummary$sdev^2/sum(pcaSummary$sdev^2)
# cumulative variance explained
CumVar = cumsum(VarExp)
ColNames = paste("PC", seq(1,length(StdDev)), sep = "")
RowNames = c("Standard deviation", "Proportion of Variance",
"Cumulative Proportion")
SumPCAMat = rbind(StdDev, VarExp, CumVar)
write.table(SumPCAMat, CompImpFile, row.names=RowNames, col.names=ColNames, sep = ",")
### variance explained by each component
### pcaSummary$sdev^2 / sum(pcaSummary$sdev^2)
### find out how many components are required to get more than 95%
print(CumVar)
i = which(CumVar >= 0.95)
print(paste("First ", i[1], " components explains >= 95% of variance.", sep = ""))
if(is.na(ProjectonStackFiles[1])){
FutProject = readline("Do you want to project on different data? (Y/N) :")
while (FutProject == "Y")
{
if (FutProject=="Y")
{
F1 = PredictOnNewData(pcaSummary,ProjectonStackFiles=NA,OutputFolder=NA)
FutProject = readline("Do you want to project on different data? (Y/N) :")
}
}
} else {
if(class(ProjectonStackFiles)=="character"){
F1 = PredictOnNewData(pcaSummary,ProjectonStackFiles,OutputFolder)
} else {
for(i in 1:length(ProjectonStackFiles)){
F1 = PredictOnNewData(pcaSummary,ProjectonStackFiles[[i]],OutputFolder[i])
}
}
}
return(pcaSummary)
}
MakeStack <- function(ascfiles)
{
predictors <- stack(ascfiles)
return(predictors)
}
PredictOnNewData <- function(pcaSummary=NA,ProjectonStackFiles=NA,OutputFolder=NA)
{
if(is.na(ProjectonStackFiles[1])){
stop("Please specify ProjectonStackFiles (bioclimatic ASCII files to predict)
or use iPCAProjection for interactive version")
}
FutStack <- MakeStack(ProjectonStackFiles)
if(is.na(OutputFolder)){
stop("Please specify OutputFolder (Output folder for Projection files) or use
iPCAProjection for interactive version")
}
if(is.na(pcaSummary[1])){
print("Please supply summary output of PCARaster function")
return(NULL)
}
fut = rasterToPoints(FutStack)
XYpts = fut[,1:2]
FutData <- fut[,3:dim(fut)[2]]
FutNames = names(FutData)
PresNames <- names(pcaSummary[[4]])
FutData <- data.frame(FutData)
#names(FutData) = PresNames
names(FutData) <- names(pcaSummary[[4]])
#print(names(FutData))
#readline()
##Predict using present data pca object and projection data set as newdata
pcf2=predict(pcaSummary,newdata=FutData)
##Bring in a raster layer with the extent you would like to project to (same extent as the newdata)
for (i in 1:length(FutStack@layers))
{
print(paste("Writing principal component file of projection", i ), sep = " ")
tbl1 = cbind(XYpts[,1:2], pcf2[,i])
### Faster but has error sometimes. The problem lies with rasterFromXYZ function.
# r2 = rasterFromXYZ(tbl1,digits=5)
### till here
## Slower but will work irrespective
XYTbl = tbl1[,1:2]
r1 = FutStack[[1]]
r2 = rasterize(XYTbl,r1,field=tbl1[,3])
## Till here
FileName = paste(OutputFolder, "\\Comp",i,".asc", sep = "")
writeRaster(r2,FileName)
}
}
PCAProjection(BioStackFiles = c("NR_climate/bio1.asc", 'NR_climate/bio2.asc', 'NR_climate/bio3.asc', 'NR_climate/bio4.asc',
'NR_climate/bio5.asc', 'NR_climate/bio6.asc', 'NR_climate/bio7.asc', 'NR_climate/bio8.asc',
'NR_climate/bio9.asc', 'NR_climate/bio10.asc', 'NR_climate/bio11.asc', 'NR_climate/bio12.asc',
'NR_climate/bio13.asc', 'NR_climate/bio14.asc', 'NR_climate/bio15.asc', 'NR_climate/bio16.asc',
'NR_climate/bio17.asc', 'NR_climate/bio18.asc', 'NR_climate/bio19.asc'),
LoadingFile = 'data/PCAloadings.csv',
CompImpFile = 'data/PCAsum.csv',
ProjectonStackFiles = c("FL_climate/bio1.asc", 'FL_climate/bio2.asc', 'FL_climate/bio3.asc', 'FL_climate/bio4.asc',
'FL_climate/bio5.asc', 'FL_climate/bio6.asc', 'FL_climate/bio7.asc', 'FL_climate/bio8.asc',
'FL_climate/bio9.asc', 'FL_climate/bio10.asc', 'FL_climate/bio11.asc', 'FL_climate/bio12.asc',
'FL_climate/bio13.asc', 'FL_climate/bio14.asc', 'FL_climate/bio15.asc', 'FL_climate/bio16.asc',
'FL_climate/bio17.asc', 'FL_climate/bio18.asc', 'FL_climate/bio19.asc'),
OutputFolder = 'data/')
## [1] "NR_climate/bio1.asc" "NR_climate/bio2.asc" "NR_climate/bio3.asc"
## [4] "NR_climate/bio4.asc" "NR_climate/bio5.asc" "NR_climate/bio6.asc"
## [7] "NR_climate/bio7.asc" "NR_climate/bio8.asc" "NR_climate/bio9.asc"
## [10] "NR_climate/bio10.asc" "NR_climate/bio11.asc" "NR_climate/bio12.asc"
## [13] "NR_climate/bio13.asc" "NR_climate/bio14.asc" "NR_climate/bio15.asc"
## [16] "NR_climate/bio16.asc" "NR_climate/bio17.asc" "NR_climate/bio18.asc"
## [19] "NR_climate/bio19.asc"
## [1] "Generating principal component"
## [1] "Writing principal component file 1"
## [1] "Writing principal component file 2"
## [1] "Writing principal component file 3"
## [1] "Writing principal component file 4"
## [1] "Writing principal component file 5"
## [1] "Writing principal component file 6"
## [1] "Writing principal component file 7"
## [1] "Writing principal component file 8"
## [1] "Writing principal component file 9"
## [1] "Writing principal component file 10"
## [1] "Writing principal component file 11"
## [1] "Writing principal component file 12"
## [1] "Writing principal component file 13"
## [1] "Writing principal component file 14"
## [1] "Writing principal component file 15"
## [1] "Writing principal component file 16"
## [1] "Writing principal component file 17"
## [1] "Writing principal component file 18"
## [1] "Writing principal component file 19"
## [1] 0.5358254 0.7420530 0.8512880 0.9178254 0.9421784 0.9617231 0.9743868
## [8] 0.9833070 0.9886646 0.9929693 0.9966211 0.9980176 0.9986679 0.9992406
## [15] 0.9996020 0.9998459 0.9999471 1.0000000 1.0000000
## [1] "First 6 components explains >= 95% of variance."
## [1] "Writing principal component file of projection 1"
## [1] "Writing principal component file of projection 2"
## [1] "Writing principal component file of projection 3"
## [1] "Writing principal component file of projection 4"
## [1] "Writing principal component file of projection 5"
## [1] "Writing principal component file of projection 6"
## [1] "Writing principal component file of projection 7"
## [1] "Writing principal component file of projection 8"
## [1] "Writing principal component file of projection 9"
## [1] "Writing principal component file of projection 10"
## [1] "Writing principal component file of projection 11"
## [1] "Writing principal component file of projection 12"
## [1] "Writing principal component file of projection 13"
## [1] "Writing principal component file of projection 14"
## [1] "Writing principal component file of projection 15"
## [1] "Writing principal component file of projection 16"
## [1] "Writing principal component file of projection 17"
## [1] "Writing principal component file of projection 18"
## [1] "Writing principal component file of projection 19"
## Standard deviations (1, .., p=19):
## [1] 3.190718e+00 1.979476e+00 1.440647e+00 1.124372e+00 6.802252e-01
## [6] 6.093847e-01 4.905193e-01 4.116845e-01 3.190542e-01 2.859881e-01
## [11] 2.634091e-01 1.628863e-01 1.111558e-01 1.043206e-01 8.286596e-02
## [16] 6.806179e-02 4.386943e-02 3.168843e-02 3.012242e-15
##
## Rotation (n x k) = (19 x 19):
## PC1 PC2 PC3 PC4 PC5
## bio1 -0.172222318 -0.41219276 0.07274212 -8.715674e-02 0.037079151
## bio2 -0.262861162 0.16504235 0.10407029 -9.986600e-02 0.522232619
## bio3 0.279844198 -0.04792031 -0.17870851 5.365703e-02 0.103793636
## bio4 -0.290187617 0.03477393 0.05377264 -2.001948e-01 -0.286815304
## bio5 -0.291097914 -0.11660134 0.12480985 -9.320957e-02 0.226836818
## bio6 0.211438111 -0.34422360 -0.10701369 1.320049e-01 -0.225737497
## bio7 -0.286454786 0.13521570 0.13235218 -1.291960e-01 0.258752860
## bio8 -0.161155281 -0.35288973 -0.01774474 -3.087112e-01 -0.177193650
## bio9 0.053356526 -0.46791163 0.01643779 1.509669e-01 0.079077756
## bio10 -0.256545157 -0.26356146 0.08777196 -1.204458e-01 -0.047333950
## bio11 -0.005598865 -0.46612612 0.08882177 9.387329e-02 0.391101888
## bio12 0.268322113 0.01300163 0.33077012 -1.007638e-01 0.106930477
## bio13 0.168863651 0.01661780 0.56438278 -5.927753e-02 -0.107084922
## bio14 0.219244881 -0.04711953 -0.07813003 -5.892009e-01 -0.046026414
## bio15 -0.271167588 0.05432858 0.23020406 -1.001551e-01 -0.374675173
## bio16 0.176268140 0.03360996 0.56439435 1.482881e-05 -0.006996412
## bio17 0.240667964 -0.04682040 -0.09966293 -5.330345e-01 0.002372947
## bio18 0.264908704 0.05395273 -0.06636889 -2.588267e-01 0.321289373
## bio19 0.226443628 -0.08320552 0.26155570 1.824350e-01 -0.044293935
## PC6 PC7 PC8 PC9 PC10
## bio1 -0.03558269 0.0710950926 0.013349053 -0.211632801 0.06239992
## bio2 -0.11325167 0.0230905571 -0.361652018 0.022582082 -0.02411790
## bio3 -0.01409064 0.2371242390 -0.731737677 -0.295317699 0.21312060
## bio4 -0.23919629 0.0760045557 0.070120675 -0.038684690 0.28694175
## bio5 -0.16386020 -0.0219046898 -0.053823778 0.004487260 0.05522742
## bio6 -0.01214034 0.0214861125 -0.014340851 -0.167307575 0.14763405
## bio7 -0.08481139 -0.0248046221 -0.021824924 0.100017120 -0.05506645
## bio8 0.20722647 0.5091417256 -0.145654818 0.188255680 -0.56450416
## bio9 -0.06495375 -0.1916255424 -0.178971330 0.755083021 0.28582591
## bio10 -0.12586774 -0.0009686046 0.122950768 -0.211404008 0.20992659
## bio11 0.18229908 -0.1810451108 0.194068597 -0.391260406 -0.03803954
## bio12 -0.02203485 0.0243700681 0.004407928 -0.019170312 -0.03563314
## bio13 0.25443750 -0.0371896321 -0.102188831 0.018355775 0.01099332
## bio14 -0.13934218 -0.3858944180 -0.015531033 -0.026742778 0.02250640
## bio15 0.03966418 0.0897519247 -0.222869296 -0.074209753 0.36741395
## bio16 0.18651029 -0.0053596690 -0.012435136 0.007307158 0.02348665
## bio17 -0.09956045 -0.2007719509 -0.083903644 0.003077288 -0.08138017
## bio18 0.05537253 0.6021520183 0.385989296 0.127694968 0.43938440
## bio19 -0.81491631 0.2006365602 0.050119938 -0.036258460 -0.23851566
## PC11 PC12 PC13 PC14 PC15
## bio1 0.0545764334 0.033433551 -0.08322574 0.094464135 -0.66204794
## bio2 -0.0382720040 -0.081762688 0.13549422 -0.023097307 -0.02818102
## bio3 0.1267498403 -0.001073304 -0.11741223 0.179509379 0.11118945
## bio4 0.4004341226 -0.084989254 -0.19002636 0.107101045 0.28985068
## bio5 0.1296568743 -0.105917216 0.47201480 -0.353453591 0.05936718
## bio6 0.1255730775 -0.123504898 0.52105554 -0.355009053 0.03732834
## bio7 -0.0004730042 0.012580010 -0.03896512 0.008683653 0.01153716
## bio8 -0.0133695392 0.061585938 0.05499410 0.067719203 0.14417977
## bio9 -0.0135287820 0.044357933 -0.12710196 0.058085657 0.04023027
## bio10 0.2536521929 -0.045351691 -0.15983949 0.157775084 0.11564412
## bio11 -0.2971873385 0.042824565 -0.19807203 0.029892987 0.32760553
## bio12 0.3585994528 0.609714131 -0.10729617 -0.233436175 -0.25880651
## bio13 0.0033741089 -0.633634313 -0.05673564 0.084948006 -0.23888560
## bio14 -0.1462443776 0.117505068 0.37844247 0.480930709 -0.03896920
## bio15 -0.6250607438 0.278892496 -0.01515446 -0.202905045 -0.02620337
## bio16 0.1152797614 0.186549535 0.16640884 0.092444598 0.41922390
## bio17 -0.0439698308 -0.191993122 -0.39290215 -0.560764498 0.11253848
## bio18 -0.1343247202 -0.081249563 0.05514491 0.020923195 -0.00782291
## bio19 -0.2440967154 -0.074803609 -0.04193846 0.066724002 0.04047851
## PC16 PC17 PC18 PC19
## bio1 0.4620925955 -0.090800630 -0.225753879 3.812130e-15
## bio2 0.1040632577 0.652689941 -0.014893551 5.096902e-16
## bio3 -0.0538857714 -0.268241304 0.002537406 -1.515792e-15
## bio4 -0.1121534441 0.145092677 -0.552099549 1.904324e-15
## bio5 -0.1424494323 -0.450247911 -0.015603756 4.360146e-01
## bio6 -0.0148198647 0.265074222 -0.024833538 -4.531232e-01
## bio7 -0.0712435508 -0.406957038 0.005722121 -7.775414e-01
## bio8 -0.0996573415 0.051695986 0.004528747 4.218748e-17
## bio9 -0.0211422166 0.015896511 -0.009095037 1.114018e-16
## bio10 -0.0315259875 0.105494827 0.760402867 -2.319495e-15
## bio11 -0.2362064404 0.036910629 -0.245084985 1.024822e-15
## bio12 -0.3856173171 0.109855104 -0.001515731 3.509963e-17
## bio13 -0.3024892453 0.012463962 -0.004217656 -4.866780e-17
## bio14 -0.1119812503 0.002111602 -0.027686229 7.313055e-17
## bio15 -0.0841822973 0.028688234 0.042804892 1.948537e-16
## bio16 0.5900833143 -0.072903822 -0.006745023 -4.671886e-17
## bio17 0.2436818706 -0.016462458 0.044592977 -1.548517e-16
## bio18 -0.0001406916 -0.007989094 0.005963484 -3.119045e-17
## bio19 0.0001674703 -0.005172020 0.005406452 -9.269172e-17
#Once this is done, the files will be saved as "comp1-19.asc" in the main folder. Manually place them in NR_PCA folder
# Now I am using her function with our data
FL_stack<- MakeStack(c("FL_climate/bio1.asc", 'FL_climate/bio2.asc', 'FL_climate/bio3.asc', 'FL_climate/bio4.asc',
'FL_climate/bio5.asc', 'FL_climate/bio6.asc', 'FL_climate/bio7.asc', 'FL_climate/bio8.asc',
'FL_climate/bio9.asc', 'FL_climate/bio10.asc', 'FL_climate/bio11.asc', 'FL_climate/bio12.asc',
'FL_climate/bio13.asc', 'FL_climate/bio14.asc', 'FL_climate/bio15.asc', 'FL_climate/bio16.asc',
'FL_climate/bio17.asc', 'FL_climate/bio18.asc', 'FL_climate/bio19.asc'))
BioStack_NR = MakeStack(c("NR_climate/bio1.asc", 'NR_climate/bio2.asc', 'NR_climate/bio3.asc', 'NR_climate/bio4.asc',
'NR_climate/bio5.asc', 'NR_climate/bio6.asc', 'NR_climate/bio7.asc', 'NR_climate/bio8.asc',
'NR_climate/bio9.asc', 'NR_climate/bio10.asc', 'NR_climate/bio11.asc', 'NR_climate/bio12.asc',
'NR_climate/bio13.asc', 'NR_climate/bio14.asc', 'NR_climate/bio15.asc', 'NR_climate/bio16.asc',
'NR_climate/bio17.asc', 'NR_climate/bio18.asc', 'NR_climate/bio19.asc'))
BioPt_NR = rasterToPoints(BioStack_NR)
pcaSummary_NR = prcomp(na.omit(BioPt_NR[,3:dim(BioPt_NR)[2]]), center=TRUE, scale = TRUE)
# Now actually running said function with the data
PredictOnNewData(pcaSummary = pcaSummary_NR,
ProjectonStackFiles = c("FL_climate/bio1.asc", 'FL_climate/bio2.asc', 'FL_climate/bio3.asc', 'FL_climate/bio4.asc',
'FL_climate/bio5.asc', 'FL_climate/bio6.asc', 'FL_climate/bio7.asc', 'FL_climate/bio8.asc',
'FL_climate/bio9.asc', 'FL_climate/bio10.asc', 'FL_climate/bio11.asc', 'FL_climate/bio12.asc',
'FL_climate/bio13.asc', 'FL_climate/bio14.asc', 'FL_climate/bio15.asc', 'FL_climate/bio16.asc',
'FL_climate/bio17.asc', 'FL_climate/bio18.asc', 'FL_climate/bio19.asc'),
OutputFolder = 'FL_PCA/')
FL_stack_proj<- MakeStack(c('FL_PCA/Comp1.asc', 'FL_PCA/Comp2.asc', 'FL_PCA/Comp3.asc',
'FL_PCA/Comp4.asc', 'FL_PCA/Comp5.asc', 'FL_PCA/comp6.asc'))
NR_stack <- MakeStack(c('NR_PCA/Comp1.asc', 'NR_PCA/Comp2.asc', 'NR_PCA/Comp3.asc',
'NR_PCA/Comp4.asc', 'NR_PCA/Comp5.asc', 'NR_PCA/comp6.asc'))
plot(FL_stack_proj)
plot(NR_stack)