knitr::opts_chunk$set(echo = FALSE)
library(DiagrammeR)
library(knitr)
library(kableExtra)
library(FD)
library(vegan)
library(ltm)
library(edcpR)
library(tidyverse)

Feedback assignment 3

Session 4: Relationships

Format

Exercises for today

Case studies:

Question 1 (20 min)

Common beech (Fagus sylvatica) is increasing in ForĂȘt de Compiegne (N. France) and we want to know if it has an effect on the forest understory species. We have sampled the understory in 26 forest plots. Some plots with, other without Fagus in the overstory.

Research question: Does Fagus sylvatica (common beech) have an effect on the understory cover in temperate forests?

Use the Compiegne datasets. The Understory cover is the sum of all species individuals:

library(edcpR)
data(Compiegne_env, Compiegne_sp, package = "edcpR")

Solution Question 1

Understory_cover <- rowSums(Compiegne_sp[2:dim(Compiegne_sp)[2]])
Compiegne <- data.frame(plot = Compiegne_env$plot,
                        Fagus = Compiegne_env$Fagus,
                        Understory_cover = Understory_cover)
head(Compiegne)

Solution Question 1 (2)

Point-biserial correlation coefficient (rp)

Solution Question 1 (3)

Solution Question 1 (4)

X = Compiegne$Understory_cover
Y = Compiegne$Fagus

n <- length(Y)

a = sqrt((sum(Y)*(n-sum(Y)))/(n^2))
sn <- sqrt((1/(n))*sum((X- mean(X))^2))

rpb = a*(mean(X[Y==1])-mean(X[Y==0]))/sn
rpb # very weak correlation

t <-  rpb*sqrt((n-2)/(1-rpb^2))
pt(t, n-2) # Not significant

Solution Question 1 (5)

cor.test(Compiegne$Fagus, Compiegne$Understory_cover)
biserial.cor(Compiegne$Understory_cover, Compiegne$Fagus, level = 2)

Question 2 (20 min)

We want to study the relationship between different soil variables to gain more insight in soil processes in Finnish marshes.

Research question: Is there a relationship between soil Ca content and soil pH?

Use the varechem dataset:

library(vegan)
data(varechem, package = "vegan")

Solution Question 2 (1)

par(mfrow = c(2,2))

hist(varechem$Ca, prob=TRUE, xlab="Ca",main="Histogram of Calcium")
m<-mean(varechem$Ca)
std<-sqrt(var(varechem$Ca))
curve(dnorm(x, mean=m, sd=std),col="darkblue", lwd=2, add=TRUE, yaxt="n")

hist(varechem$pH , prob=TRUE, xlab="pH",main="Histogram of pH")
m<-mean(varechem$pH)
std<-sqrt(var(varechem$pH))
curve(dnorm(x, mean=m, sd=std),col="darkblue", lwd=2, add=TRUE, yaxt="n")

qqnorm(varechem$Ca, main="Normal Q-Q of Calcium")
qqnorm(log(varechem$pH), main="Normal Q-Q of pH")

Solution Question 2 (2)

lmMod <- lm(Ca ~ pH, data=varechem)
par(mfrow = c(2,2))
plot(lmMod)

Solution Question 2 (3)

n <- length(varechem$Ca)
di <- rank(varechem$Ca, ties.method = "average") - rank(varechem$pH, ties.method = "average")

rs <-  1 - (6*sum(di^2))/(n*(n^2-1)) # r-squared
rs # Weak correlation

t <- rs*sqrt((n-2)/(1-rs^2)) # Test statistic
pt(t, n-2) # not significant

Solution Question 2 (4)

cor.test(varechem$Ca, varechem$pH, method = "spearman")

Solution Question 2 (5)

You can use the pearson correlation if:

Question 3 (15 min)

Using the dune_spec dataset:

Research question: Is the species composition of a haypasture dune grassland closer to a hayfield dune grassland or to pasture dune grassland?

data(dune_spec, package = "edcpR")

Solution Question 3

vegdist(dune_spec[,-1],method = "bray")

Question 4 (15 min)

Using the varechem dataset:

Research question: How do the soil conditions compare between the plots?

rm("varechem")
data(varechem, package = "vegan")

Solution Question 4 (1)

# Baresoil, is not a soil condition, so it can just be deleted from the data frame
varechem <- varechem %>% select(-Baresoil)
head(varechem)

# Standardize variables
varechem_scale <- as.data.frame(scale(varechem))

Solution Question 4 (1)

# Euclidian distance
euclidean <- vegdist(varechem_scale, method = "euclidean")
euclidean <- scores(euclidean)

# Some possible conclusions...
which(euclidean == min(euclidean[euclidean > 0]), arr.ind=TRUE)
which(euclidean == max(euclidean), arr.ind=TRUE)

Assignment

NO Assignment this week.

Next session assignment on this and the next exercise session.



wardfont/edcpR documentation built on Dec. 23, 2021, 5:07 p.m.