Cleaning and creating habitat-species DF
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("dplyr")
library(gridExtra)
##
## Attaching package: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
library(ggplot2)
library(ggpubr)
library(multcompView)
library(cowplot)
##
## Attaching package: 'cowplot'
##
## The following object is masked from 'package:ggpubr':
##
## get_legend
##
## The following object is masked from 'package:lubridate':
##
## stamp
library(tidyr)
library(lme4)
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
library(lmerTest)
##
## Attaching package: 'lmerTest'
##
## The following object is masked from 'package:lme4':
##
## lmer
##
## The following object is masked from 'package:stats':
##
## step
library(coin)
## Loading required package: survival
library(FSA)
## Registered S3 methods overwritten by 'FSA':
## method from
## confint.boot car
## hist.boot car
## ## FSA v0.9.5. See citation('FSA') if used in publication.
## ## Run fishR() for related website and fishR('IFAR') for related book.
library(rcompanion)
library(multcomp)
## Loading required package: mvtnorm
## Loading required package: TH.data
## Loading required package: MASS
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
##
##
## Attaching package: 'TH.data'
##
## The following object is masked from 'package:MASS':
##
## geyser
library(emmeans)
library("vegan")
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.6-4
library("rareNMtests")
library(minpack.lm)
library(propagate)
## Loading required package: tmvtnorm
## Loading required package: stats4
## Loading required package: gmm
## Loading required package: sandwich
##
## Attaching package: 'gmm'
##
## The following object is masked from 'package:lme4':
##
## checkConv
##
## Loading required package: Rcpp
## Loading required package: ff
## Loading required package: bit
##
## Attaching package: 'bit'
##
## The following object is masked from 'package:dplyr':
##
## symdiff
##
## The following object is masked from 'package:base':
##
## xor
##
## Attaching package ff
## - getOption("fftempdir")=="C:/Users/atayk/AppData/Local/Temp/Rtmp8uWS4p/ff"
##
## - getOption("ffextension")=="ff"
##
## - getOption("ffdrop")==TRUE
##
## - getOption("fffinonexit")==TRUE
##
## - getOption("ffpagesize")==65536
##
## - getOption("ffcaching")=="mmnoflush" -- consider "ffeachflush" if your system stalls on large writes
##
## - getOption("ffbatchbytes")==16777216 -- consider a different value for tuning your system
##
## - getOption("ffmaxbytes")==536870912 -- consider a different value for tuning your system
##
##
## Attaching package: 'ff'
##
## The following objects are masked from 'package:utils':
##
## write.csv, write.csv2
##
## The following objects are masked from 'package:base':
##
## is.factor, is.ordered
##
##
## Attaching package: 'propagate'
##
## The following object is masked from 'package:lubridate':
##
## interval
setwd("C:/Users/atayk/OneDrive - huji.ac.il/Desktop/Agriculture Faculty/courses/R Niv&Yuval/R project")
#Saving the data as "mydata"
mydata <- read.csv("SAC_data.csv")
#changing my data to be a habitat-species data frame:
widedata <- mydata %>% #Pipe operator
pivot_wider(
names_from = Species, #column names
values_from = Weight, #column values
)
#change the nulls to 0
# the pivot_wider function, as follows:
widedata1 <- mydata %>% #Pipe operator
pivot_wider(
names_from = Species, #column names
values_from = Weight, #column values
values_fill = list(Weight = 0) #:תוספת : Where there isn't data,
#it'll say 0 instead of 'NA'
)
abund <- widedata1[, 5:145] # taking obly the species cols
# making the data to be binary
prab_dor_all <- abund
prab_dor_all[prab_dor_all > 0] <- 1
# subset the data by habitat:
widedata1_O <- subset(widedata1, Habitat == "O")
widedata1_N <- subset(widedata1, Habitat == "N")
abund_o <- widedata1_O[, 5:145] # taking the species cols
abund_N <- widedata1_N[, 5:145] # taking the species cols
# making the data to be binary
prab_dor_O <- abund_o
prab_dor_O[prab_dor_O > 0] <- 1
prab_dor_N <- abund_N
prab_dor_N[prab_dor_N > 0] <- 1
Creating SAC for Open habitats and fitting a model:
set.seed(1)
#We chose Vegan package to create SAC, Using random method:
rarefy_PS_O = specaccum(prab_dor_O , method="random")
#ploting the SAC with CI
plot(rarefy_PS_O, ci.type="poly", col="blue", lwd=2, ci.lty=0, ci.col="lightblue",
xlab="Samplings", ylab="", main="Species Accumulation Curve - Open")
# Add custom y-axis label with rotation
mtext("Species", side=2, line=2.5, las=3)
#checking which model fits best to the SAC:
# Convert to data frames and add a habitat column
rarefy_df_ps_O <- data.frame(Sites=rarefy_PS_O$sites, Richness=rarefy_PS_O$richness, SD=rarefy_PS_O$sd, Habitat="Open")
# Define the Michaelis-Menten function
michaelis <- function(Sites, Vmax, Km) {
Vmax * Sites / (Km + Sites)
}
# Fit a non-linear regression model
fit_nls_O_mic <- nls(Richness ~ michaelis(Sites, Vmax, Km),
data = rarefy_df_ps_O,
start = list(Vmax = 1, Km = 1))
#Taking the AIC out:
AIC(fit_nls_O_mic)
## [1] 471.3631
#Calculating the R^2 - # Calculate residuals from the model
residuals_mic <- residuals(fit_nls_O_mic)
# Calculate the total sum of squares (TSS)
TSS <- sum((rarefy_df_ps_O$Richness - mean(rarefy_df_ps_O$Richness))^2)
# Calculate the residual sum of squares (RSS)
RSS_mic <- sum(residuals_mic^2)
# Calculate R^2
R_squared_mic_O <- 1 - (RSS_mic/TSS)
logarithmic <- function(Sites, a, b) {
a + b * log(Sites)
}
fit_nls_O_log <- nls(Richness ~ logarithmic(Sites, a, b),
data = rarefy_df_ps_O,
start = list(a = 1, b = 1))
#Taking the AIC out:
AIC(fit_nls_O_log)
## [1] 506.7943
power_law <- function(Sites, c, alpha) {
c * Sites^alpha
}
fit_nls_O_power <- nls(Richness ~ power_law(Sites, c, alpha),
data = rarefy_df_ps_O,
start = list(c = 1, alpha = 1))
#Taking the AIC out:
AIC(fit_nls_O_power)
## [1] 219.1771
exponential_model <- function(Sites, b0, b1) {
b0 * (1 - exp(-b1 * Sites))
}
fit_nls_O_exp <- nls(Richness ~ exponential_model(Sites, b0, b1),
data = rarefy_df_ps_O,
start = list(b0 = 1, b1 = 0.01))
# Extracting AIC for the new model
AIC(fit_nls_O_exp)
## [1] 534.7805
#The lowest AIC (the best fit) is power law.
#Calculating the R^2 - # Calculate residuals from the model
residuals <- residuals(fit_nls_O_power)
# Calculate the total sum of squares (TSS)
TSS <- sum((rarefy_df_ps_O$Richness - mean(rarefy_df_ps_O$Richness))^2)
# Calculate the residual sum of squares (RSS)
RSS <- sum(residuals^2)
# Calculate R^2
R_squared_power_O <- 1 - (RSS/TSS)
# Base plot for SAC
plot(rarefy_PS_O, ci.type="poly", col="blue", lwd=4, ci.lty=0, ci.col="lightblue",
xlab="Samplings", ylab="", main="Species Accumulation Curve - Open")
# Add custom y-axis label with rotation
mtext("Species", side=2, line=2.5, las=3)
# Predict and add Michaelis-Menten model line
sites_range <- seq(min(rarefy_df_ps_O$Sites), max(rarefy_df_ps_O$Sites), length.out = 100)
predicted_mic <- predict(fit_nls_O_mic, newdata = data.frame(Sites = sites_range))
lines(sites_range, predicted_mic, col="red")
# Predict and add Power Law model line
predicted_power <- predict(fit_nls_O_power, newdata = data.frame(Sites = sites_range))
lines(sites_range, predicted_power, col="green")
# Predict values for the exponential model
predicted_exp <- predict(fit_nls_O_exp, newdata = data.frame(Sites = sites_range))
# Add the exponential model line to the plot
lines(sites_range, predicted_exp, col="orange")
# Update the legend to include the exponential model
legend("topleft", legend=c("SAC", "Michaelis-Menten", "Power Law", "Exponential"), col=c("blue", "red", "green", "orange"), lty=1, cex=0.8)
#Saving the AICs values to be able to compare
AIC_O <-c(AIC(fit_nls_O_power),AIC(fit_nls_O_mic),AIC(fit_nls_O_log),AIC(fit_nls_O_exp))
#taking the best ones (the lowest ones) and calculate the difference
delta_AIC_O= -AIC(fit_nls_O_power) + AIC(fit_nls_O_mic)
delta_AIC_O
## [1] 252.186
R_squred_O <- c(R_squared_power_O,R_squared_mic_O)
delta_R_O = -R_squared_mic_O + R_squared_power_O
delta_R_O
## [1] 0.02383382
Creating SAC for North habitats and fitting a model:
set.seed(1)
# Using Vegan package to create SAC for North habitat using the random method
rarefy_PS_N = specaccum(prab_dor_N , method="random")
# Plotting the SAC with CI for North habitat
plot(rarefy_PS_N, ci.type="poly", col="blue", lwd=2, ci.lty=0, ci.col="lightblue",
xlab="Samplings", ylab="", main="Species Accumulation Curve - North")
mtext("Species", side=2, line=2.5, las=3)
# Convert to data frames and add a habitat column for North
rarefy_df_ps_N <- data.frame(Sites=rarefy_PS_N$sites, Richness=rarefy_PS_N$richness, SD=rarefy_PS_N$sd, Habitat="North")
# Define models, fit them, take AIC out, and predict for plotting
# Define the Michaelis-Menten function
michaelis <- function(Sites, Vmax, Km) {
Vmax * Sites / (Km + Sites)
}
# Fit a non-linear regression model for Michaelis-Menten
fit_nls_N_mic <- nls(Richness ~ michaelis(Sites, Vmax, Km), data = rarefy_df_ps_N, start = list(Vmax = 1, Km = 1))
AIC(fit_nls_N_mic) # Extract AIC
## [1] 375.0841
# Define and fit logarithmic model
logarithmic <- function(Sites, a, b) { a + b * log(Sites) }
fit_nls_N_log <- nls(Richness ~ logarithmic(Sites, a, b), data = rarefy_df_ps_N, start = list(a = 1, b = 1))
AIC(fit_nls_N_log) # Extract AIC
## [1] 443.0728
# Define and fit power law model
power_law <- function(Sites, c, alpha) { c * Sites^alpha }
fit_nls_N_power <- nls(Richness ~ power_law(Sites, c, alpha), data = rarefy_df_ps_N, start = list(c = 1, alpha = 1))
AIC(fit_nls_N_power) # Extract AIC
## [1] 392.8916
# Define and fit exponential model
exponential_model <- function(Sites, b0, b1) { b0 * (1 - exp(-b1 * Sites)) }
fit_nls_N_exp <- nls(Richness ~ exponential_model(Sites, b0, b1), data = rarefy_df_ps_N, start = list(b0 = 1, b1 = 0.01))
AIC(fit_nls_N_exp) # Extract AIC
## [1] 482.9069
# Base plot for SAC with predictions for North habitat
plot(rarefy_PS_N, ci.type="poly", col="blue", lwd=4, ci.lty=0, ci.col="lightblue", xlab="Samplings", ylab="", main="Species Accumulation Curve - North")
mtext("Species", side=2, line=2.5, las=3)
sites_range_N <- seq(min(rarefy_df_ps_N$Sites), max(rarefy_df_ps_N$Sites), length.out = 100)
predicted_mic_N <- predict(fit_nls_N_mic, newdata = data.frame(Sites = sites_range_N))
lines(sites_range_N, predicted_mic_N, col="red")
predicted_power_N <- predict(fit_nls_N_power, newdata = data.frame(Sites = sites_range_N))
lines(sites_range_N, predicted_power_N, col="green")
# Add a legend
legend("topleft", legend=c("SAC", "Michaelis-Menten", "Power Law"), col=c("blue", "red", "green"), lty=1, cex=0.8)
# Generating a sequence of Sites values for predictions
sites_range <- seq(min(rarefy_df_ps_O$Sites), max(rarefy_df_ps_O$Sites), length.out = 100)
# Predicting with confidence intervals
preds_with_ci <- predict(fit_nls_O_power, newdata = data.frame(Sites = sites_range),
interval = "confidence", level = 0.95)
# Plotting the SAC for north 4 models
plot(rarefy_PS_N, ci.type="poly", col="blue", lwd=3, ci.lty=0, ci.col="lightblue", xlab="Samplings", ylab="Species", main="Species Accumulation Curve - North")
# Base plot for SAC with predictions
# Add Michaelis-Menten, Power Law, and Exponential model lines
sites_range_N <- seq(min(rarefy_df_ps_N$Sites), max(rarefy_df_ps_N$Sites), length.out = 100)
predicted_mic_N <- predict(fit_nls_N_mic, newdata = data.frame(Sites = sites_range_N))
predicted_power_N <- predict(fit_nls_N_power, newdata = data.frame(Sites = sites_range_N))
predicted_exp_N <- predict(fit_nls_N_exp, newdata = data.frame(Sites = sites_range_N))
lines(sites_range_N, predicted_mic_N, col="red")
lines(sites_range_N, predicted_power_N, col="green")
lines(sites_range_N, predicted_exp_N, col="orange")
# Update the legend to include all models
legend("topleft", legend=c("SAC", "Michaelis-Menten", "Power Law", "Exponential"), col=c("blue", "red", "green", "orange"), lty=1, cex=0.8)
#checking the best AIC:
AIC_N <-c(AIC(fit_nls_N_power),AIC(fit_nls_N_mic),AIC(fit_nls_N_log),AIC(fit_nls_N_exp))
#checing the difference between the 2 best models:
delta_AIC_N<- -AIC(fit_nls_N_power) + AIC(fit_nls_N_mic)
delta_AIC_N
## [1] -17.80744
# Calculate R^2 for Michaelis-Menten and Power Law models, and compute delta_R_N
residuals_mic_N <- residuals(fit_nls_N_mic)
RSS_mic_N <- sum(residuals_mic_N^2)
R_squared_mic_N <- 1 - (RSS_mic_N / TSS)
# For Power Law model
residuals_power_N <- residuals(fit_nls_N_power)
RSS_power_N <- sum(residuals_power_N^2)
R_squared_power_N <- 1 - (RSS_power_N / TSS)
# For Logarithmic model
residuals_log_N <- residuals(fit_nls_N_log)
RSS_log_N <- sum(residuals_log_N^2)
R_squared_log_N <- 1 - (RSS_log_N / TSS)
# For Exponential model
residuals_exp_N <- residuals(fit_nls_N_exp)
RSS_exp_N <- sum(residuals_exp_N^2)
R_squared_exp_N <- 1 - (RSS_exp_N / TSS)
R_squred_N <- c(R_squared_power_N,R_squared_mic_N,R_squared_log_N,R_squared_exp_N)
#making a data frame for the sites and richness from the SAC of Open habitat (the output of specacum)
rarefy_PS_O_lm<- data.frame(rarefy_PS_O$sites,rarefy_PS_O$richness)
colnames(rarefy_PS_O_lm) <- c("Sites", "Richness") #changing the col names
# Plot data on regular scale
plot(rarefy_PS_O_lm$Sites, rarefy_PS_O_lm$Richness, log = "", pch = 21, xlab = "samplings", ylab = "Species", main = "Regular scale - Open", col = "black", bg = "darkcyan")
#bulding the linear model
fit_reg_O <- lm(log10(Richness) ~ log10(Sites), data = rarefy_PS_O_lm) # fit power-law model
x_seq_O <- rarefy_PS_O_lm$Sites
pred_reg_O <- predict(fit_reg_O, data.frame(Sites = x_seq_O), interval = "confidence")
# Add polygon for confidence interval
polygon(c(x_seq_O, rev(x_seq_O)), c(10^(pred_reg_O[, "lwr"]), rev(10^(pred_reg_O[, "upr"]))), col = rgb(0.2, 0.2, 0.2, alpha = 0.3), border = NA)
lines(x_seq_O, 10^(pred_reg_O[, "fit"]), col = "blue", lwd = 2) # add trendline
# Add equation for power-law model
coef_reg_O <- coef(fit_reg_O)
a_reg_O <- round(10^coef_reg_O[1],1)
b_reg_O <- round(coef_reg_O[2],1)
eqn_reg_O <- paste0("y = ", a_reg_O, "*x^",b_reg_O)
text(10, 30, eqn_reg_O, pos = 4)
rsq_reg_O <- summary(fit_reg_O)$r.squared # Compute the R-squared
rsq_label_reg_O <- bquote(R^2 == .(format(rsq_reg_O, digits = 2))) # Format R-squared
text(10, 20, rsq_label_reg_O, pos = 4) # Add R-squared label
# Plot data on log-log scale
plot(rarefy_PS_O_lm$Sites, rarefy_PS_O_lm$Richness, log = "xy", pch = 21, xlab = "Samplings (log scale)", ylab = "Species (log scale)", main = "Log-log scale - Open", col = "black", bg = "darkcyan", ylim=c(10,100))
fit_loglog_O <- lm(log10(Richness) ~ log10(Sites), data = rarefy_PS_O_lm)
summary(fit_loglog_O)
##
## Call:
## lm(formula = log10(Richness) ~ log10(Sites), data = rarefy_PS_O_lm)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.094607 -0.006820 -0.000589 0.008821 0.023475
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.189428 0.006226 191.0 <2e-16 ***
## log10(Sites) 0.438292 0.003972 110.3 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01464 on 84 degrees of freedom
## Multiple R-squared: 0.9931, Adjusted R-squared: 0.9931
## F-statistic: 1.217e+04 on 1 and 84 DF, p-value: < 2.2e-16
# fit power-law model
x_seq_O <- rarefy_PS_O_lm$Sites
pred_loglog_O <- predict(fit_loglog_O, data.frame(Sites = x_seq_O), interval = "confidence")
# Add polygon for confidence interval
polygon(c(x_seq_O, rev(x_seq_O)), c(10^(pred_loglog_O[, "lwr"]), rev(10^(pred_loglog_O[, "upr"]))), col = rgb(0.2, 0.2, 0.2, alpha = 0.3), border = NA)
lines(x_seq_O, 10^(pred_loglog_O[, "fit"]), col = "blue", lwd = 2) # add trend line
text(20,30, eqn_reg_O, pos = 4)
text(20, 20, rsq_label_reg_O, pos = 4) # Add R-squared label
rarefy_PS_N_lm<- data.frame(rarefy_PS_N$sites,rarefy_PS_N$richness)
colnames(rarefy_PS_N_lm) <- c("Sites", "Richness") #changing the col names
# for North habitats ####
# Making a data frame for the sites and richness from the SAC of North habitat (the output of specaccum)
rarefy_PS_N_lm <- data.frame(rarefy_PS_N$sites, rarefy_PS_N$richness)
colnames(rarefy_PS_N_lm) <- c("Sites", "Richness") # Changing the column names
# Plot data on regular scale
plot(rarefy_PS_N_lm$Sites, rarefy_PS_N_lm$Richness, log = "", pch = 21, xlab = "Samplings", ylab = "Species", main = "Regular Scale - North", col = "black", bg = "darkcyan")
# Building the linear model
fit_reg_N <- lm(log10(Richness) ~ log10(Sites), data = rarefy_PS_N_lm) # Fit power-law model
x_seq_N <- rarefy_PS_N_lm$Sites
pred_reg_N <- predict(fit_reg_N, data.frame(Sites = x_seq_N), interval = "confidence")
# Add polygon for confidence interval
polygon(c(x_seq_N, rev(x_seq_N)), c(10^(pred_reg_N[, "lwr"]), rev(10^(pred_reg_N[, "upr"]))), col = rgb(0.2, 0.2, 0.2, alpha = 0.3), border = NA)
lines(x_seq_N, 10^(pred_reg_N[, "fit"]), col = "blue", lwd = 2) # Add trendline
# Add equation for power-law model
coef_reg_N <- coef(fit_reg_N)
a_reg_N <- round(10^coef_reg_N[1], 1)
b_reg_N <- round(coef_reg_N[2], 1)
eqn_reg_N <- paste0("y = ", a_reg_N, "*x^", b_reg_N)
text(10, 30, eqn_reg_N, pos = 4)
# Compute the R-squared and add R-squared label
rsq_reg_N <- summary(fit_reg_N)$r.squared
rsq_label_reg_N <- bquote(R^2 == .(format(rsq_reg_N, digits = 2)))
text(10, 20, rsq_label_reg_N, pos = 4)
# Plot data on log-log scale
plot(rarefy_PS_N_lm$Sites, rarefy_PS_N_lm$Richness, log = "xy", pch = 21, xlab = "Samplings (log scale)", ylab = "Species (log scale)", main = "Log-Log Scale - North", col = "black", bg = "darkcyan", ylim = c(10, 100))
fit_loglog_N <- lm(log10(Richness) ~ log10(Sites), data = rarefy_PS_N_lm)
# Summary of fit
summary(fit_loglog_N)
##
## Call:
## lm(formula = log10(Richness) ~ log10(Sites), data = rarefy_PS_N_lm)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.134301 -0.017912 0.003303 0.020979 0.030132
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.153002 0.011158 103.33 <2e-16 ***
## log10(Sites) 0.450549 0.007097 63.48 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02634 on 85 degrees of freedom
## Multiple R-squared: 0.9793, Adjusted R-squared: 0.9791
## F-statistic: 4030 on 1 and 85 DF, p-value: < 2.2e-16
# Predict with confidence intervals on log-log scale
x_seq_N <- rarefy_PS_N_lm$Sites
pred_loglog_N <- predict(fit_loglog_N, data.frame(Sites = x_seq_N), interval = "confidence")
# Add polygon for confidence interval and trend line on log-log scale
polygon(c(x_seq_N, rev(x_seq_N)), c(10^(pred_loglog_N[, "lwr"]), rev(10^(pred_loglog_N[, "upr"]))), col = rgb(0.2, 0.2, 0.2, alpha = 0.3), border = NA)
lines(x_seq_N, 10^(pred_loglog_N[, "fit"]), col = "blue", lwd = 2)
# Add the equation and R-squared label on the log-log scale plot
text(20, 30, eqn_reg_N, pos = 4)
text(20, 20, rsq_label_reg_N, pos = 4)