# Computer Lab 5. Estimating Population Density and Biodiversity Figure 5: Sample figures from the estimating population density and biodiversity lab

Background
In lecture this week we learned about population- and community- level interactions, and how they can influence individual and species-specific behavior. Differences in population density can lead to shifts in the proportion of different behavioral phenotypes in a population, and the presence of competitors of other species can shape the behavior of animals on shorter (ecological) and longer (evolutionary) time scales. In this lab you will become familiar with the ways that scientists estimate population density and biodiversity of terrestrial vertebrates.

Goals of the exercises
The main goal(s) of today’s lab are to:
1) Estimate population density of focal vertebrates from the field lab.
2) Learn the difference between alpha, beta and gamma diversity.
3) Compare population density and biodiversity across your sampling sites.

Getting started
First we need to load the relevant packages for our data analysis. Packages contain all the functions that are needed for data analysis.

``library(behaviouR)``

Then we read in our data

``CensusData <- read.csv('FieldLab5CensusData.csv')``

## Part 1. Population density estimation.

First let’s focus on your data.
NOTE: you need to change ‘A’ to the name you used!

``MyCensusData <- subset(CensusData, Partner=='A')``

Now we will subset the data to focus on your focal species for density estimation. NOTE: in this example we are subsetting the data so that we only have the squirrels; you will need to change the code to subset based on your focal species!

``MyCensusDataFocal <- subset(MyCensusData, Species=='squirrel')``

Here we will determine the width of our sampling area by creating a histogram of perpendicular detection distances. We will identify the point where our detections start to dropoff. We will assume that after this point our ability to detect animals past that distance drops substantially, and including observations past this distance could potentially bias our results.

``````hist(MyCensusDataFocal\$PerpendicularDistance,xlab='Perpendicular distance (D)',
ylab='Number of observations',main='')`````` In this example there is a clear break between 15 and 20 meters, so we will only use observations that were within 15 meters.
NOTE: Your data will look different than this!

Change the value (in this example 15) to the cutoff point indicated in your data!

``CutOffPoint <- 15``
``````# Here we subset our data so that it only includes detections that were within 15 meters of the census route

Now we need to subset by each site.
NOTE: You will need to change to the site names you used

``````MyCensusDataFocalSiteA <- subset(MyCensusDataFocalAdjusted,Site=='SiteA')

Now we will calculate the population density based on our two surveys. Change the following to indicate the distance (in meters) of your survey for site A. This will be the actual straight-line distance you walked.

``SiteACensusDistance <- 500``

Now we will calculate the area of our census. The sample area (a) is equal to the length of the transect multiplied by twice the width or a= 2wl. We divide by 1000 to convert our answer to square kilometers.

``````SiteACensusArea <- 2*SiteACensusDistance*CutOffPoint/1000
SiteACensusArea``````
``##  15``

Now we need to calculate the number of focal animals observed using the following code.

``NumberFocalAnimalsSiteA <- nrow(MyCensusDataFocalSiteA)``

Then we can calculate the density by dividing the total number of animals we observed by the area we censused.

``````PopulationDensitySiteA <-NumberFocalAnimalsSiteA/SiteACensusArea
PopulationDensitySiteA``````
``##  0.4666667``

Now we will calculate the population density based on our second survey. Change the following to indicate the distance (in meters) of your survey for site B.This will be the actual straight-line distance you walked.

``SiteBCensusDistance <- 500``

Now we will calculate the area of our census. The sample area (a) is equal to the length of the transect multiplied by twice the width or a= 2wl.We divide by 1000 to convert our answer to square kilometers.

``````SiteBCensusArea <- 2*SiteBCensusDistance*CutOffPoint/1000
SiteBCensusArea``````
``##  15``

Now we need to calculate the number of animals using the following code.

``NumberFocalAnimalsSiteB <- nrow(MyCensusDataFocalSiteB)``

Then we can calculate the density by dividing the total number of animals we observed by the area we censused.

``````PopulationDensitySiteB <-NumberFocalAnimalsSiteB/SiteBCensusArea
PopulationDensitySiteB``````
``##  1.133333``

Now we can compare population density using the following code. The code below asks if the population density of site A was higher than site B?

``PopulationDensitySiteA > PopulationDensitySiteB``
``##  FALSE``

The code below asks if the population density of site B was higher than site A?

``PopulationDensitySiteB > PopulationDensitySiteA``
``##  TRUE``

Question 1. What were the population density estimates (reported as number of individuals per square kilometer) for your two sites? Do your results of the population density estimates match your predictions? Why or why not?

## Part 2. Comparing biodiversity.

### Alpha diversity.

First, we will estimate the alpha diversity, or the diversity within a particular area or ecosystem. The alpha diversity is simply the number of different species present at each site.

Here we subset by partner and site A (you may need to change these!)

``````MyCensusDataSiteA <- subset(MyCensusData,Partner=='A' & Site=='SiteA')

# What were the unique species present?
unique(MyCensusDataSiteA\$Species)``````
``````##  cardinal  blackird1 rabbit    deer      squirrel  crow
## Levels: blackird1 cardinal crow deer rabbit squirrel``````
``````# How many unique species were there?
SiteANumberSpecies <- length(unique(MyCensusDataSiteA\$Species))
SiteANumberSpecies``````
``##  6``

Here we subset by partner and site B (you may need to change these!)

``````MyCensusDataSiteB <- subset(MyCensusData,Partner=='A' & Site=='SiteB')

# What were the unique species present?
unique(MyCensusDataSiteB\$Species)``````
``````##  squirrel  blackird1 rabbit    deer      crow
## Levels: blackird1 cardinal crow deer rabbit squirrel``````
``````# How many unique species were there?
SiteBNumberSpecies <- length(unique(MyCensusDataSiteB\$Species))
SiteBNumberSpecies``````
``##  5``

Question 2. Which of your sites had higher species richness (i.e. number of species)?

### Beta diversity.

Now we will estimate beta diversity, which estimates changes in species diversity between ecosystems or along environmental gradients.

``````# This code tells us which species both sites have in common
intersect(unique(MyCensusDataSiteA\$Species),unique(MyCensusDataSiteB\$Species))``````
``##  "blackird1" "rabbit"    "deer"      "squirrel"  "crow"``
``````# Now we calculate the number of species in common
SpeciesInCommonBothSites <- length(intersect(unique(MyCensusDataSiteA\$Species),unique(MyCensusDataSiteB\$Species))
)
SpeciesInCommonBothSites``````
``##  5``

To investigate community similarity we will calculate Sørenson’s index; a value of 1 means exactly the same number of species a value of 0 means no overlap.

Beta diversity = 2c / S1 + S2
Where c is the number of species the sites have in common, S1 is the number of species at the first site and S2 is the number of species at the second site

``2*SpeciesInCommonBothSites/ (SiteANumberSpecies+SiteBNumberSpecies)``
``##  0.9090909``

Now we will use your partner’s data to estimate alpha diversity

``````MyPartnersCensusDataSiteA <- subset(CensusData,Partner=='B' & Site=='SiteC')
unique(MyPartnersCensusDataSiteA\$Species)``````
``````##  squirrel crow
## Levels: blackird1 cardinal crow deer rabbit squirrel``````
``length(unique(MyPartnersCensusDataSiteA\$Species))``
``##  2``
``````SiteANumberSpecies <- length(unique(MyPartnersCensusDataSiteA\$Species))
SiteANumberSpecies``````
``##  2``
``````MyPartnersCensusDataSiteB <- subset(CensusData,Partner=='B' & Site=='SiteD')
unique(MyPartnersCensusDataSiteB\$Species)``````
``````##  squirrel
## Levels: blackird1 cardinal crow deer rabbit squirrel``````
``length(unique(MyPartnersCensusDataSiteB\$Species))``
``##  1``
``````SiteBNumberSpecies <- length(unique(MyPartnersCensusDataSiteB\$Species))
SiteBNumberSpecies``````
``##  1``

Question 3. How did the alpha diversity of each of your sites compare with that of your partner?

### Gamma diversity

Gamma diversity is the total number of species over a large area or region; there are many different ways that this can be measured. The way we will do it is a bit of an oversimplification by simply comparing the number of species seen during the census at both locations.

First we subset by the first partner

``````MyCensusData <- subset(CensusData, Partner=='A')
unique(MyCensusData\$Species)``````
``````##  cardinal  blackird1 rabbit    deer      squirrel  crow
## Levels: blackird1 cardinal crow deer rabbit squirrel``````

Then we plot the results

``gghistogram(data=MyCensusData, x='Species',stat="count")`` Now we subset by the second partner

``````MyPartnersCensusData <- subset(CensusData, Partner=='B')
unique(MyPartnersCensusData\$Species)``````
``````##  squirrel crow
## Levels: blackird1 cardinal crow deer rabbit squirrel``````

Then we plot the results

``gghistogram(data=MyPartnersCensusData, x='Species',stat="count")`` Now we can plot all the data together, separated by partner

``````gghistogram(data=CensusData, x='Species',stat="count",
facet.by = 'Partner',x.text.angle =90,
fill='Partner')+xlab('Species')+ylab('Number of individuals')`````` Question 4. How did the gamma diversity of your site compare with that of your partners?

## Part 3. Biodiversity indices in the real world.

There are special packages in R that can measure different diversity indices, as biodiversity indices are tools many ecologists use. The package we will use is called ‘vegan’.

``library(vegan)``

First we need to convert our data into a table that can be used to calculate the indices.

``BiodiversityTable <- table(CensusData\$Site,CensusData\$Species)``

Simpson’s Index (D) measures the probability that two individuals randomly selected from a sample will belong to the same species (or some category other than species). With this index, 1 represents infinite diversity and 0 means no diversity.

``````H <- diversity(BiodiversityTable,index="simpson")
H``````
``````##     SiteA     SiteB     SiteC     SiteD
## 0.7962963 0.6728395 0.4444444 0.0000000``````

Species evenness refers to how close in numbers each species is in an environment. We can calculate evenness using the following code. The value is constrained between 0 and 1, with communities that have a more even representation of species having values closer to 1.

``````J <- H/log(specnumber(BiodiversityTable))
J``````
``````##     SiteA     SiteB     SiteC     SiteD
## 0.4444214 0.4180587 0.6411978       NaN``````

Question 5. Which of the four sites you analyzed was most diverse? Which was the most even? Why is it important to consider diversity and evenness when studying biodiversity?