Guest User

Untitled

a guest
Apr 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. library(raster); #For raster-based loading, calculations, and mapping
  2. library(rgdal); #For reading the M polygon
  3.  
  4. setwd("~/Dropbox/ENMSeminar/Labs:Homeworks/Lab5/Lab5Data/WorldClimTerrestrial/");
  5. #Navigate to folder containing environmental data
  6.  
  7. #Loading a raster stack
  8. envtList <- list.files(pattern = ".asc"); #Gets a list of .asc files
  9. envtStack <- stack(envtList); #Reads in .asc files as a raster stack
  10. crs(envtStack) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" #Defines projection of layers
  11. plot(envtStack); #Plots all the layers of the raster stack object
  12.  
  13. #Get M shapefile
  14. setwd("~/Dropbox/ENMSeminar/Labs:Homeworks/Lab5/Lab5Data/SyrmaticusSoemmerringii/");#Changing working directory to the folder where the shapefile of your M from created in Part One of the lab.
  15. copperPheasant <- readOGR("./Syrmaticus soemmerringii.shp"); #Reads in shapefile; You will change this file to the shapefile you created in part 1 of the lab.
  16. crs(copperPheasant) <- crs(envtStack); #Defines Copper Pheasant M projection as identical to envtStack
  17.  
  18. #Masking your environmental variables to M
  19. copperPheasantTraining <- crop(envtStack, copperPheasant) #Cropping the raster stack
  20. copperPheasantTraining <- mask(copperPheasantTraining, copperPheasant) #Masking the raster stack
  21. writeRaster(copperPheasantTraining, filename = "SyrmaticusSoemmerringii",
  22. format = "ascii", bylayer = T, suffix=names(envtStack), NAFlag = "-9999", overwrite = T);
  23. #Saves all the layers in the stack, with "SyrmaticusSoemmerringii" as a prefix
  24.  
  25. #Removing correlated variables
  26. pairs(copperPheasantTraining);
  27. #Shows correlations among variables. Inspect visually. Which layers are highly correlated?
  28. reducedCopperPheasantTraining <- copperPheasantTraining[[-3:-6]];
  29. #Removes Bio 10 through Bio 13.
  30. pairs(reducedCopperPheasantTraining);
  31. reducedCopperPheasantTraining <- reducedCopperPheasantTraining[[-6:-8]];
  32. #Removes Bio 17 through Bio 19
  33. pairs(reducedCopperPheasantTraining);
  34. reducedCopperPheasantTraining <- reducedCopperPheasantTraining[[-9:-11]];
  35. #Removes Bio 5 through Bio 7
  36. pairs(reducedCopperPheasantTraining);
  37. #Ok, now none of the variables have a correlation coefficient of 0.90. This is a somewhat arbitrary threshold. Use your best judgement to find a logical breakpoint for removing correlated variables from your environmental dataset.
  38. writeRaster(reducedCopperPheasantTraining, filename = "reducedSyrmaticusSoemmerringii",
  39. format = "ascii", bylayer = T, suffix=names(reducedCopperPheasantTraining), NAFlag = "-9999", overwrite = T);
  40. #Saved reduced set if you don't want to hunt for the variables you want to get rid of.
Add Comment
Please, Sign In to add comment