######################################################################################################### ## ## PVA SIMULATION LAB - instructor script for multiple years of data ## written for Rare Species Conservation, ## Noah Charney ## Hampshire College 2011, updated 2013 ## ## See: Charney, N. D. and S. Record. ## Performing a population viability analysis from data students collect on a local plant ## in review at Teaching Issues and Experiments in Ecology ## ## Required libraries: popbio, gplots ## ## These scripts should be contained in a zipped folder with source code and data. ## See the file "README.txt" for explanation of associated files ## ######################################################################################################### # - A - SET UP THE WORKSPACE library(gplots) library(popbio) #NOTE! Do the following if you get a warning along the lines of "there is no package called 'popbio'" # 1. Go to "Packages" --> "Install package(s)..." # 2. Select your closest mirror (e.g. "USA (MA)") # 3. Scroll down to select "popbio" then hit "OK" #Now we must tell R where to find the files you downloaded for this lab #If you are using a PC or a Mac with R from prior to 2013, use the following line: setwd(choose.dir()) #navigate to the directory you created with the lab files in it #If you are using a newer Mac, the code above will not work. #Instead, you must type in between the quotes below the exact name of the full # path directory to where the lab files are located on your hard drive # and then run the line. # For instance, on computers where we downloaded the files at our College we type: # setwd('/users/nsuser/downloads') setwd('') #On some machines, if you are clicked on the "R Console" window, # you may be able to go to the "File" menu and click on "Source" then # locate the file 'PVA_source.R' # Or you could open the 'PVA_source.R' file with R, highlight # every line of code in the file, and run it all. #However, with this method you will not have told R the directory, # and you will need to do a couple extra things below when you # want to read in the student data. #Now we read in the source file source('PVA_source.R') ############################################################################# # - B - SET BASIC MODEL PARAMETERS num.stages <- 3 #Number of life stages in our model num.iter <- 50 #Number of iterations in the simulation num.years <- 50 #Number of years into the future to run the simulations carrying_capacity <- 1000 quasi_extinction_threshold <- 10 supplement_matrix <- matrix(0,nrow=num.stages,,ncol=1,dimnames=list(paste('stage',1:num.stages),'Number of extra plants to plant each year')) ############################################################################## # # - C - # DATA ENTRY # # #Read in a matrix with this year's observed data. # All of your data should be in the file 'multiyear_stages_longform.csv' # containing the colums: # 'Survey,' 'ID,' and 'Stage' # Stage "-1" means it was not detected in a survey # Stage "0" means it was detected, but dead # #The default file contains student data on striped pipsissewa multiyear.stages <- read.csv('multiyear_stages_longform.csv') #Note, if your directory is not set right, and R cannot locate the file above, # then replace the line with: # this_years_stages <- read.csv(file.choose()) # and navigate to your file manually ####### # If you do not already have stages assigned for your plants, # you can follow a process similar to what we did for ours: # multiyear.rawdata <- read.csv('multiyear_data_nostages.csv') #get unique stem id's multiyear.rawdata[multiyear.rawdata[,'Stem_ID']=='','Stem_ID'] <- 1 ID <- paste(multiyear.rawdata[,'Plant_ID'],'_',multiyear.rawdata[,'Stem_ID'],sep='') #set stages LL <- multiyear.rawdata[,'Largest_Leaf_Length.cm'] NL <- multiyear.rawdata[,'Num_Leaves'] NF <- multiyear.rawdata[,'Num_fruits'] Stage <- vector('numeric', length = dim(multiyear.rawdata)[1]) Stage[] <- -1 Stage[NL < 3 | LL < 3] <- 1 Stage[NL >= 3 & LL >= 3] <- 2 Stage[NF > 0 ] <- 3 Stage[NL ==0 | LL == 0] <- 0 Survey <- multiyear.rawdata[,'Survey'] multiyear.stages <- data.frame(Survey,ID,Stage) write.csv(multiyear.stages,'multiyear_stages_longform.csv',row.names=FALSE) # # ###### # - D - # FORMAT TO GIVE TO STUDENTS # Here, we want a matrix with one row per unique stem multiyear.stages.shortform <- get_multiyear_stages_shortform(multiyear.stages) #below is the file you can give to the students for manually examining transitions write.csv(multiyear.stages.shortform,'multiyear_data_shortform.csv', row.names=FALSE) # # - E - # GET DATA THAT STUDENTS SHOULD BE GETTING MANUALLY IN STEP 2 # And test model # # #get actual Transitions: this is what students should get transitions <- get_multiyear_transitions(multiyear.stages.shortform) transitions transition_matrix <- transitions[[1]][-1,-c(1,2),dim(transitions[[1]])[3]] #note, this is an extremely crude estimate of fertility fertility <- matrix(0,nrow=num.stages,,ncol=1) fertility[num.stages,1] <- transitions[[2]] number_of_plants_in_year_1 <- apply(transitions[[1]][,,dim(transitions[[1]])[3]-2],2,sum)[-c(1,2)] #Test that the data the students will enter will be relatively stable (mean of simulations is constant population size) run.simulation(num.iter,num.years,number_of_plants_in_year_1,transition_matrix[-1,],fertility,carrying_capacity,supplement_matrix,quasi_extinction_threshold)