Oefening 1-2-3

Packages laden

output = FALSE
echo = FALSE
library("dplyr")
library("vegan")
library("tidyverse")
library("readr")
library("conflicted")
library("ggpubr")

conflicts_prefer(dplyr::filter)

Vegdata <- read_csv('Vegetatiedata.csv')
omgevingdata <- read_csv('Omgevingsdata.csv')
Ellenberg <- read.csv('Ellenbergwaarden.csv')

Data inladen en vegetatiematrix maken

Door select(-c(1,2)) te gebruiken gaan we de eerste 2 kolommen weghalen - zo hebben we enkel soorten en bedekkingen in de matrix.

Door pivot_wider te gebruiken kunnen we ons tabel omdraaien van assen

Vegdata_wide <- Vegdata %>%
  pivot_wider(names_from = "Soort", values_from = "Bedekking")

  
M <- Vegdata_wide %>%
  select(-c(1, 2))

Anemoon, filteren op de soort.

Anemone <- Vegdata %>% 
  filter(Soort == "Bosanemoon")

Proefvlakken selecteren waar de bedekking groter is dan 0 - en de soort dus aanwezig is

Anemone %>%                                     
  summarise(sum(Bedekking > 0))
# A tibble: 1 × 1
  `sum(Bedekking > 0)`
                 <int>
1                   23

Groeperen per type - alluviaal en plateau - en dan de gemiddelde bedekking tonen en afronden naar 1 cijfer op de komma

Anemone %>%
  group_by(Type) %>%
  summarize(mean_cover = round( mean(Bedekking), 1))
# A tibble: 2 × 2
   Type mean_cover
  <dbl>      <dbl>
1     1       14.5
2     2        2.9

Histogram maken met de bedekking van anemoon , en in hoeveel proefvlakken deze voorkom, waar we een bedekking-% hanteren van 5

ggplot(Anemone, aes(Bedekking)) +
  geom_histogram(binwidth = 5) + 
  theme_bw()

Standaardafwijking en gemiddelde berekenen

Anemone_stats <- Anemone %>%
  group_by(Type) %>%
  summarize(n = n(),
            mn = mean(Bedekking),
            SE = sd(Bedekking)/sqrt(n))

Barplot maken waarbij de standaardafwijking en de 0.05 limieten worden toegevoegd.

ggplot(Anemone_stats, aes(Type, mn)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = mn, ymax = mn + 1.96*SE), width = .2) +
  scale_y_continuous("Gemiddelde bedekking") +
  theme_bw()

Soortendiversiteit berekenen waarbij de bedekking >0 is

S_df <- Vegdata %>%
  group_by(PlotID) %>%
  summarize(S = sum(Bedekking > 0))
head(S_df)
# A tibble: 6 × 2
  PlotID     S
   <dbl> <int>
1      2    19
2      3    22
3      4    20
4      5    15
5      6     4
6      8    22

Soortenfrequentie berekenen waarbij de bedekking >0 is

freq_df <- Vegdata %>%
  group_by(Soort) %>%
  summarize(freq = sum(Bedekking > 0))
head(freq_df)
# A tibble: 6 × 2
  Soort         freq
  <chr>        <int>
1 Bloedzuring      5
2 Bosandoorn       8
3 Bosanemoon      23
4 Boszegge        12
5 Dalkruid         5
6 Drienerfmuur    11

Echte S berekenen - Waarbij MARGI = 1 over de kolommen telt, dus per proefvlak

S <- specnumber(M, MARGIN=1) 
echo = TRUE
S
 [1] 19 22 20 15  4 22 14 18 17 24 25 26  6 12  8  6 21 17 19  6  3 23  7 18 19
[26] 21

Echte F berekenen - Waarbij MARGIN = 2 over de rijen telt, dus per soort

freq <- specnumber(M, MARGIN = 2)
echo = TRUE
freq
            Dalkruid          Bloedzuring               Eenbes 
                   5                    5                    6 
  Kruipend zenegroen            Hondsdraf   Kleine maagdenpalm 
                   7                    7                    7 
          Bosandoorn        Moeraswalstro           Ijle zegge 
                   8                   10                   11 
         Kale jonker         Drienerfmuur          Muskuskruid 
                  11                   11                   12 
            Boszegge           Kleefkruid                Viool 
                  12                   12                   13 
    Grote brandnetel      Echte valeriaan        Pinksterbloem 
                  14                   14                   15 
         Hennepnetel        Ruw beemdgras      Geel nagelkruid 
                  15                   15                   16 
         Fluitekruid         Moerasspirea  Slanke sleutelbloem 
                  17                   17                   19 
          Speenkruid           Ruwe smele       Gele dovenetel 
                  19                   21                   22 
Gewone salomonszegel           Bosanemoon         Zwarte braam 
                  22                   23                   26 

Shannon en Jaccard index berekenen

H <- diversity(M, index="shannon")
echo = TRUE
H
 [1] 2.547375 2.620406 2.316648 1.498037 1.071018 2.209921 1.952380 2.187076
 [9] 1.610882 2.339864 2.208831 2.288592 1.439948 1.634410 1.698783 1.246830
[17] 1.942611 1.912282 1.831681 1.367738 1.098612 2.430457 1.406899 1.629628
[25] 2.195728 1.804483
J <- H/log(S)
echo = TRUE
J
 [1] 0.8651478 0.8477417 0.7733160 0.5531791 0.7725761 0.7149435 0.7398021
 [8] 0.7566764 0.5685706 0.7362568 0.6862118 0.7024322 0.8036505 0.6577348
[15] 0.8169419 0.6958689 0.6380676 0.6749515 0.6220815 0.7633490 1.0000000
[22] 0.7751431 0.7230029 0.5638125 0.7457204 0.5926983

Diversiteit data-frame maken per bostype met de diversiteitsindices

diversiteit <- data.frame(Type = Vegdata_wide$Type, S = S, J = J, H = H)
echo = TRUE
head(diversiteit)
  Type  S         J        H
1    1 19 0.8651478 2.547375
2    1 22 0.8477417 2.620406
3    1 20 0.7733160 2.316648
4    1 15 0.5531791 1.498037
5    2  4 0.7725761 1.071018
6    1 22 0.7149435 2.209921

Gemiddelde en standaardfout berekenen voor de Shannon index (H)

diversiteit_stats <- diversiteit %>%
  group_by(Type) %>%
  summarize(n = n(),
            mn = mean(H),
            SE = sd(H)/sqrt(n))

Barplot maken met de Gemiddelde Shannon index met de berekende standaardfout en gemiddeldes per bostype

echo = TRUE
ggplot(diversiteit_stats, aes(Type, mn)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = mn, ymax = mn + 1.96*SE), width = .2) +
  scale_y_continuous("Gemiddelde Shannon index") +
  theme_bw()

Hill numbers, hier gaan we de hill-number van 1e orde berekenen door de exponent van Shannon te nemen

diversiteit <- diversiteit %>%
  mutate(D_1 = exp(H))

Vervolgens gaan we kijken wat de gemiddeldes en standaardfouten zijn

diversiteit_stats2 <- diversiteit %>%
  pivot_longer(2:5, names_to = "Index", values_to = "Waarde") %>%
  filter(Index == "S" | Index == "D_1") %>%
  group_by(Type, Index) %>%
  summarize(n = n(),
            mn = mean(Waarde),
            SE = sd(Waarde)/sqrt(n))

Barplot maken waarbij de standaardfouten inzitten en de gemiddelde diversiteit is opgenomen volgens S en 1e orde Hill

echo = TRUE

ggplot(diversiteit_stats2, aes(Type, mn, fill = Index)) +
  geom_bar(stat = "Identity", position = "dodge") +
  geom_errorbar(aes(ymin = mn - 1.96 * SE, ymax = mn + 1.96 * SE), width = .2, position = position_dodge(width = .9)) +
  scale_y_continuous("Gemiddelde diversiteit")

Voor proefvlak 1-5 berekenen wat de afstand is tussen hun volgens de Jaccard methode – Kan ook met absentie/aanwezigheid data

M1_5 <- M[1:5,]
d_j <- vegdist(M1_5, method="jaccard", upper=T, diag=T, binary=T)

Omzetten van de Jaccard-distance naar een matrix

str(d_j)
 'dist' num [1:10] 0.1364 0.1429 0.381 0.85 0.0909 ...
 - attr(*, "maxdist")= num 1
 - attr(*, "Size")= int 5
 - attr(*, "Diag")= logi TRUE
 - attr(*, "Upper")= logi TRUE
 - attr(*, "method")= chr "binary jaccard"
 - attr(*, "call")= language vegdist(x = M1_5, method = "jaccard", binary = T, diag = T, upper = T)
D_j <- as.matrix(d_j)   
echo = TRUE
D_j
          1          2          3         4         5
1 0.0000000 0.13636364 0.14285714 0.3809524 0.8500000
2 0.1363636 0.00000000 0.09090909 0.3913043 0.8695652
3 0.1428571 0.09090909 0.00000000 0.4090909 0.8571429
4 0.3809524 0.39130435 0.40909091 0.0000000 0.8125000
5 0.8500000 0.86956522 0.85714286 0.8125000 0.0000000

Berekenen wat het grootste verschil is tussen de 5 proefvlakken door rowSums te gebruiken Dit werkt door

echo = TRUE

rowSums(D_j)
       1        2        3        4        5 
1.510173 1.488142 1.500000 1.993848 3.389208 
data <- rowSums(D_j)
data
       1        2        3        4        5 
1.510173 1.488142 1.500000 1.993848 3.389208 
plot(data)

min(data)
[1] 1.488142
max(data)
[1] 3.389208

Dus koppel 2-5 is het meest verschillende van elkaar


Oefening deel 2

Soortenfrequentie ophalen

specfreq <- specnumber(M)

De Ellenberg waarde voor L berekenen uit de omgevingsdata

L_plot<-Vegdata %>%
  mutate(Presentie = Bedekking>0) %>%
  left_join(Ellenberg) %>%
  mutate(L_pres = Presentie * L) %>%
  group_by(PlotID, Type) %>%
  summarize(L_plot = round(mean(L_pres[L_pres>0], na.rm = T), 2))

De Ellenberg waarde voor F berekenen uit de omgevingsdata

F_plot<-
  Vegdata %>%
  mutate(Presentie = Bedekking>0) %>%
  left_join(Ellenberg) %>%
  mutate(F_pres = Presentie * F) %>%
  group_by(PlotID, Type) %>%
  summarize(F_plot = round(mean(F_pres[F_pres>0], na.rm = T), 2))

De Ellenberg waarde voor N berekenen uit de omgevingsdata

N_plot<-
  Vegdata %>%
  mutate(Presentie = Bedekking>0) %>%
  left_join(Ellenberg) %>%
  mutate(N_pres = Presentie * N) %>%
  group_by(PlotID, Type) %>%
  summarize(N_plot = round(mean(N_pres[N_pres>0], na.rm = T), 2))

De Ellenberg waarde voor N berekenen uit de omgevingsdata

R_plot<-
  Vegdata %>%
  mutate(Presentie = Bedekking>0) %>%
  left_join(Ellenberg) %>%
  mutate(R_pres = Presentie * R) %>%
  group_by(PlotID, Type) %>%
  summarize(R_plot = round(mean(R_pres[R_pres>0], na.rm = T), 2))

Dataframe make met deze waardes

OmgevingsEllenberg <- data.frame(omgevingdata, N_plot$N_plot, R_plot$R_plot, L_plot$L_plot, F_plot$F_plot)

Correlatie tussen N en R berekenen

echo = TRUE
cor.test(OmgevingsEllenberg$N_plot, OmgevingsEllenberg$R_plot)

    Pearson's product-moment correlation

data:  OmgevingsEllenberg$N_plot and OmgevingsEllenberg$R_plot
t = 11.429, df = 24, p-value = 3.405e-11
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.8257614 0.9634598
sample estimates:
      cor 
0.9191225 

Relatie tussen gemiddelde R en N visualiseren in een scatterplot. Waarhene we een linear model regression trekken

echo = TRUE
ggplot(OmgevingsEllenberg, aes(N_plot$N_plot, R_plot$R_plot)) +
  geom_point() +
  stat_smooth(method = "lm") +
  theme_bw() +
  scale_x_continuous("Gemiddelde Ellenberg N") +
  scale_y_continuous("Gemiddelde Ellenberg R")

Dataframe maken met alle gemiddelde ellenbergwaardes

df <- (OmgevingsEllenberg$N_plot.N_plot + OmgevingsEllenberg$R_plot.R_plot + OmgevingsEllenberg$L_plot.L_plot + OmgevingsEllenberg$F_plot.F_plot)/4

Nieuw dataframe met de 4 Ellenbergwaardegemiddeldes EN de extra gemiddeldes van ze allemaal

OmgevingsEllenberg1 <- data.frame(omgevingdata, N_plot$N_plot, R_plot$R_plot, L_plot$L_plot, F_plot$F_plot, df)

Correlatietest trekken die de gemiddelde Ellenbergwaardes correleerd met de Calciumconcentraties (Random voorbeeld)

echo = TRUE
cor.test(OmgevingsEllenberg1$df, OmgevingsEllenberg1$Ca)

    Pearson's product-moment correlation

data:  OmgevingsEllenberg1$df and OmgevingsEllenberg1$Ca
t = 5.9623, df = 24, p-value = 3.738e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.5498509 0.8927941
sample estimates:
      cor 
0.7726412 

Visualiseren van deze correlatie met een linear model tussen Gemiddelde Ellenberg en Calcium concentratie

library("ggpubr")
ggplot(OmgevingsEllenberg1, aes(df, Ca)) +
  geom_point() +
  stat_smooth(method = "lm") +
  theme_bw() +
  scale_x_continuous("Gemiddelde Ellenbergwaardes") +
  scale_y_continuous("Calcium concentratie") +
  stat_cor()

Dataframe van de soortendiversiteit berekenen

echo = TRUE
S_df <- Vegdata %>%
  group_by(PlotID) %>%
  summarize(S = sum(Bedekking > 0))
head(S_df)
# A tibble: 6 × 2
  PlotID     S
   <dbl> <int>
1      2    19
2      3    22
3      4    20
4      5    15
5      6     4
6      8    22

Dataframe van de frequentie berekenen

echo = TRUE
freq_df <- Vegdata %>%
  group_by(Soort) %>%
  summarize(freq = sum(Bedekking > 0))
head(freq_df)
# A tibble: 6 × 2
  Soort         freq
  <chr>        <int>
1 Bloedzuring      5
2 Bosandoorn       8
3 Bosanemoon      23
4 Boszegge        12
5 Dalkruid         5
6 Drienerfmuur    11

Nieuwe dataframe maken van alles wat we tot nu toe hadden PLUSSSS de soortendiversiteit

OmgevingsEllenberg2 <- data.frame(omgevingdata, N_plot$N_plot, R_plot$R_plot, L_plot$L_plot, F_plot$F_plot, df, S_df$S)

Correlatietest trekken tussen gemiddelde Ellenbergwaarde en Soortendiversiteit

echo= TRUE
cor.test(OmgevingsEllenberg2$df, OmgevingsEllenberg2$S_df.S)

    Pearson's product-moment correlation

data:  OmgevingsEllenberg2$df and OmgevingsEllenberg2$S_df.S
t = 8.2659, df = 24, p-value = 1.761e-08
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.7092458 0.9357863
sample estimates:
      cor 
0.8602604 

Visualiseren van deze correlatie met een linear model tussen Gemiddelde Ellenberg en Soortendiversiteit

ggplot(OmgevingsEllenberg2, aes(df, S_df.S)) +
  geom_point() +
  stat_smooth(method = "lm") +
  theme_bw() +
  scale_x_continuous("Gemiddelde Ellenbergwaardes") +
  scale_y_continuous("Soortendiversiteit") +
  stat_cor()

Berekenen van de gemiddelde Ellenbergwaardes voor de R factor. En ook de standaardafwijking

Ellenberg_stats <- OmgevingsEllenberg2 %>%
  group_by(Type) %>%
  summarize(n = n(),
            mn_R = mean(R_plot.R_plot),
            SE_R = sd(R_plot$R_plot)/sqrt(n))

Barplot maken met de gemiddelde Ellenbergwaarde R

ggplot(Ellenberg_stats, aes(Type, mn_R)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = mn_R - 1.96*SE_R, ymax = mn_R + 1.96*SE_R), width = .2) +
  scale_y_continuous("Gemiddelde Ellenberg R") +
  theme_bw()

T-test uitvoeren tussen het bostype en de R waardes

echo = TRUE
t.test(OmgevingsEllenberg2$R_plot ~ OmgevingsEllenberg2$Type)

    Welch Two Sample t-test

data:  OmgevingsEllenberg2$R_plot by OmgevingsEllenberg2$Type
t = 3.4641, df = 6.1351, p-value = 0.01293
alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
95 percent confidence interval:
 0.3066761 1.7557299
sample estimates:
mean in group 1 mean in group 2 
       6.542632        5.511429 

Omzsetten van de plotid en S waardes voor de dataset

plotid <- as.numeric(OmgevingsEllenberg2$PlotID)
S <- as.numeric(OmgevingsEllenberg2$S_df.S)

Proefvlakmatrix maken door plotid en soortendiversiteit samen te zetten

proefvlakmatrix <- data.frame(OmgevingsEllenberg2$PlotID, OmgevingsEllenberg2$S_df.S)

Pca maken met deze matrix

echo = TRUE
pca <- rda(proefvlakmatrix, scale=T)
summary(pca)

Call:
rda(X = proefvlakmatrix, scale = T) 

Partitioning of correlations:
              Inertia Proportion
Total               2          1
Unconstrained       2          1

Eigenvalues, and their contribution to the correlations 

Importance of components:
                         PC1    PC2
Eigenvalue            1.1556 0.8444
Proportion Explained  0.5778 0.4222
Cumulative Proportion 0.5778 1.0000
biplot(pca)

freq <- specnumber(proefvlakmatrix$OmgevingsEllenberg2.S_df.S, MARGIN=2)  
pca.plot <- scores(pca, display = "sites")
pca.spp <- scores(pca, display = "species") 

Proefvlakmatrix maken waarbij bostype ook is toegevoegd

proefvlakmatrix1 <- data.frame(proefvlakmatrix,OmgevingsEllenberg2$Type)

‘Freq’ maken met enkel meest voorkomende soorten (Zie margin = 2 !!)

freq <- specnumber(proefvlakmatrix1, MARGIN=2)   

Assenscores van de plots berekeken

pca.plot <- scores(pca, display = "sites")[freq>10,]  

Een lege biplot maken zonder plots of soorten

Soorten erop weergeven als vectoren vanuit de oorsprong naar de soortscore pca.spp

De soortnamen erbij zetten (coordinaten op basis van soortscore ‘pca.spp’)

Plot scores erbij zetten met verschillende kleuren voor alluviaal vs. plateau

biplot(pca, type="none")

arrows(x0=0,y0=0,x1=2*pca.spp[,1],y1=2*pca.spp[,2], col="red",length=0.05)

text(x=2*pca.spp[,1], y=2*pca.spp[,2],labels=rownames(pca.spp),cex=0.8) 

points(pca.plot, pch=20, col=c("black","red")[ifelse(proefvlakmatrix1$OmgevingsEllenberg2.Type=="1", 1, 2)])        

Nieuwe omgevingvariabele definieren

omgeving <- OmgevingsEllenberg2

Nieuwe pca maken met de omgevingsvariabelen. Waarbij we plotid weghalen en een standaardplot genereren

OmgevingsEllenberg3 <- OmgevingsEllenberg2 %>%
  select(-c(1))


pca1 <- data.frame(OmgevingsEllenberg3)
pca1 <- rda(pca1, scale = T)

freq <- specnumber(proefvlakmatrix1, MARGIN=2) 
pca.plot <- scores(pca1, display = "sites")  
pca.spp <- scores(pca1, display = "species") 


biplot(pca1, type="none")

arrows(x0=0,y0=0,x1=2*pca.spp[,1],y1=2*pca.spp[,2], col="red",length=0.05)

text(x=2*pca.spp[,1], y=2*pca.spp[,2],labels=rownames(pca.spp),cex=0.8) 

points(pca.plot, pch=20, col=c("black","red")[ifelse(proefvlakmatrix1$OmgevingsEllenberg2.Type=="1", 1, 2)])   

Eerst een nieuwe vegetatiedatamatrix creeeren zonder type en plotid - numerical values

nieuweveg <- select(Vegdata_wide, -c(1,2))

Euclidische afstand berekenen op 50 x proberen via de log-berekening

nmds.Eucl.log <- metaMDS(log((nieuweveg)+1), distance="euclidean", k=2, trymax=50)     
Run 0 stress 0.1435 
Run 1 stress 0.1497169 
Run 2 stress 0.1428497 
... New best solution
... Procrustes: rmse 0.02583588  max resid 0.1022301 
Run 3 stress 0.15429 
Run 4 stress 0.1571116 
Run 5 stress 0.1647942 
Run 6 stress 0.1428497 
... Procrustes: rmse 3.405603e-05  max resid 0.0001083573 
... Similar to previous best
Run 7 stress 0.1523905 
Run 8 stress 0.15429 
Run 9 stress 0.1502731 
Run 10 stress 0.1428497 
... New best solution
... Procrustes: rmse 1.486733e-05  max resid 5.108914e-05 
... Similar to previous best
Run 11 stress 0.1523905 
Run 12 stress 0.1580933 
Run 13 stress 0.149769 
Run 14 stress 0.1497689 
Run 15 stress 0.1580933 
Run 16 stress 0.1438554 
Run 17 stress 0.1497691 
Run 18 stress 0.1438562 
Run 19 stress 0.1600062 
Run 20 stress 0.1600062 
*** Best solution repeated 1 times

Stressplot tonen van wat er daar gebeurd

stressplot(nmds.Eucl.log)
title(sub = "Log")

Dit kunnen we ook lineair doen - maar deze heeft hier een slechtere fit

nmds.Eucl <- metaMDS(nieuweveg, distance="euclidean", k=2, trymax=50)                           
Square root transformation
Wisconsin double standardization
Run 0 stress 0.05175759 
Run 1 stress 0.06157319 
Run 2 stress 0.05208168 
... Procrustes: rmse 0.004791603  max resid 0.01801017 
Run 3 stress 0.05175744 
... New best solution
... Procrustes: rmse 0.000261629  max resid 0.0005877253 
... Similar to previous best
Run 4 stress 0.08431729 
Run 5 stress 0.05175743 
... New best solution
... Procrustes: rmse 6.506819e-06  max resid 2.174768e-05 
... Similar to previous best
Run 6 stress 0.0520816 
... Procrustes: rmse 0.004806223  max resid 0.0181578 
Run 7 stress 0.05175755 
... Procrustes: rmse 9.557078e-05  max resid 0.0002112283 
... Similar to previous best
Run 8 stress 0.05208154 
... Procrustes: rmse 0.004797816  max resid 0.01806591 
Run 9 stress 0.0517574 
... New best solution
... Procrustes: rmse 8.480679e-05  max resid 0.0001889883 
... Similar to previous best
Run 10 stress 0.06170932 
Run 11 stress 0.1389083 
Run 12 stress 0.05175759 
... Procrustes: rmse 0.000200664  max resid 0.0004401752 
... Similar to previous best
Run 13 stress 0.05175748 
... Procrustes: rmse 0.0001327725  max resid 0.000295787 
... Similar to previous best
Run 14 stress 0.05877988 
Run 15 stress 0.136651 
Run 16 stress 0.05208162 
... Procrustes: rmse 0.004822109  max resid 0.01832268 
Run 17 stress 0.05208154 
... Procrustes: rmse 0.004811498  max resid 0.01821738 
Run 18 stress 0.05175754 
... Procrustes: rmse 0.0001449419  max resid 0.0003168611 
... Similar to previous best
Run 19 stress 0.05175744 
... Procrustes: rmse 9.608798e-05  max resid 0.0002101552 
... Similar to previous best
Run 20 stress 0.05208169 
... Procrustes: rmse 0.004826878  max resid 0.01836752 
*** Best solution repeated 5 times
stressplot(nmds.Eucl)
title(sub = "Normaal")

NMDS plot genereren

plot(nmds.Eucl.log)

bostype <- OmgevingsEllenberg2$Type
# Een afgewerkte figuur in 3 stappen
## Een 'lege' biplot, zonder plots of soorten
plot(nmds.Eucl.log, type="none")

## Plot scores erbij zetten met verschillende kleuren voor Alluvial en Plateau

points(nmds.Eucl.log, display="sites", pch=20, col=c("blue","red")[bostype])  
orditorp(nmds.Eucl.log, display="species", priority = specfreq, col="green")

Hierboven deden we het met een Euclidische afstand, nu met een Jaccard berekening. Om het verschil tussen de 2 te bekijken.

nmds.Eucl.jac <- metaMDS(omgeving, distance="jaccard", k=2, trymax=50)                           
Square root transformation
Wisconsin double standardization
Run 0 stress 0.1035982 
Run 1 stress 0.1099157 
Run 2 stress 0.1037696 
... Procrustes: rmse 0.004578452  max resid 0.01798382 
Run 3 stress 0.1187311 
Run 4 stress 0.1187311 
Run 5 stress 0.1037697 
... Procrustes: rmse 0.00459793  max resid 0.01801336 
Run 6 stress 0.1123727 
Run 7 stress 0.1075487 
Run 8 stress 0.1075487 
Run 9 stress 0.1080033 
Run 10 stress 0.1123727 
Run 11 stress 0.1187312 
Run 12 stress 0.1075486 
Run 13 stress 0.1187311 
Run 14 stress 0.1037696 
... Procrustes: rmse 0.004569442  max resid 0.0179655 
Run 15 stress 0.1048711 
Run 16 stress 0.1261673 
Run 17 stress 0.1037697 
... Procrustes: rmse 0.004603373  max resid 0.0180226 
Run 18 stress 0.1086016 
Run 19 stress 0.1086016 
Run 20 stress 0.1086017 
Run 21 stress 0.1099157 
Run 22 stress 0.1035982 
... Procrustes: rmse 4.307155e-05  max resid 0.000123233 
... Similar to previous best
*** Best solution repeated 1 times
stressplot(nmds.Eucl.jac)
title(sub = "Jaccard")

Hier gaan we de proefvlakscores van de NMDS gaan extraheren, en meer specifiek de scores voor de eerste as

proefvlakscores <- scores(nmds.Eucl)
as1 <- proefvlakscores$sites[,1]

En dan visualiseren met abiotische variabelen

plot(as1, omgeving$pH.KCl,)

plot(as1, omgeving$N)

plot(as1, omgeving$Ca)

En een correlatietest doen

cor.test(as1, omgeving$pH.KCl)

    Pearson's product-moment correlation

data:  as1 and omgeving$pH.KCl
t = -5.4097, df = 24, p-value = 1.478e-05
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.8768291 -0.4964031
sample estimates:
       cor 
-0.7412294 

De resultaten visualiseren

omgeving2 <- data.frame(omgeving, as1)
omgeving2 <- omgeving2 %>%
  select(-1)
res <- cor(omgeving2)
round(res, 2)
               Type pH.KCl     N    Ca     K    Mg N_plot.N_plot R_plot.R_plot
Type           1.00  -0.75  0.68 -0.71 -0.47 -0.47         -0.83         -0.76
pH.KCl        -0.75   1.00 -0.63  0.93  0.67  0.63          0.77          0.65
N              0.68  -0.63  1.00 -0.49 -0.30 -0.17         -0.60         -0.45
Ca            -0.71   0.93 -0.49  1.00  0.68  0.76          0.76          0.71
K             -0.47   0.67 -0.30  0.68  1.00  0.61          0.58          0.52
Mg            -0.47   0.63 -0.17  0.76  0.61  1.00          0.53          0.54
N_plot.N_plot -0.83   0.77 -0.60  0.76  0.58  0.53          1.00          0.92
R_plot.R_plot -0.76   0.65 -0.45  0.71  0.52  0.54          0.92          1.00
L_plot.L_plot -0.87   0.81 -0.63  0.77  0.60  0.52          0.95          0.89
F_plot.F_plot -0.83   0.79 -0.69  0.75  0.51  0.51          0.90          0.81
df            -0.86   0.78 -0.62  0.77  0.58  0.54          0.98          0.94
S_df.S        -0.80   0.76 -0.70  0.71  0.47  0.43          0.84          0.72
as1            0.86  -0.74  0.59 -0.77 -0.58 -0.55         -0.94         -0.96
              L_plot.L_plot F_plot.F_plot    df S_df.S   as1
Type                  -0.87         -0.83 -0.86  -0.80  0.86
pH.KCl                 0.81          0.79  0.78   0.76 -0.74
N                     -0.63         -0.69 -0.62  -0.70  0.59
Ca                     0.77          0.75  0.77   0.71 -0.77
K                      0.60          0.51  0.58   0.47 -0.58
Mg                     0.52          0.51  0.54   0.43 -0.55
N_plot.N_plot          0.95          0.90  0.98   0.84 -0.94
R_plot.R_plot          0.89          0.81  0.94   0.72 -0.96
L_plot.L_plot          1.00          0.95  0.99   0.86 -0.95
F_plot.F_plot          0.95          1.00  0.94   0.90 -0.88
df                     0.99          0.94  1.00   0.86 -0.97
S_df.S                 0.86          0.90  0.86   1.00 -0.81
as1                   -0.95         -0.88 -0.97  -0.81  1.00
library(corrplot) 
corrplot(res, type = "upper", tl.col = "black", tl.srt = 90)

dis <- vegdist(log(M+1), method = "euclidean")
clus1 <- hclust(dis, method = "single")
plot(clus1)
rect.hclust(clus1, k=2)

dis2 <- vegdist(log(M+1), method = "jaccard")
clus2 <- hclust(dis2, method = "single")
plot(clus2)
rect.hclust(clus2, k=2)

grp1 <- cutree(clus1, k=2)
grp1
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
diseuc <- vegdist(log(M+1), method = "euclidean")
clusavg <- hclust(diseuc, method = "average")
grp2 <- cutree(clus1, k=2)
grp2
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
diseuc <- vegdist(log(M+1), method = "euclidean")
cluscom <- hclust(diseuc, method = "complete")

plot(cluscom)
rect.hclust(cluscom, k=3)

plot(clusavg)
grp2 <- cutree(clusavg, k=2)
grp2
 [1] 1 1 1 1 2 1 1 1 1 1 1 1 2 1 2 2 1 1 1 2 2 1 2 1 1 1
rect.hclust(clusavg, k=2)

Omgevingsdata_clus <- omgevingdata %>%
  mutate(grp2) %>%
  group_by(grp2) %>%
  summarise(n = n(), mn_pH = mean(omgeving$pH.KCl), SE_pH = sd(omgeving$pH.KCl)/sqrt(n))
ggplot(Omgevingsdata_clus, aes(grp2, mn_pH)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = mn_pH, ymax = mn_pH + 1.96*SE_pH), width = .2) +
  scale_x_discrete("Clustergroep", labels = c("1"="groep 1","2"="groep 2")) +
  scale_y_continuous("Gemiddelde pH") + 
  theme_bw()

t.test(Omgevingsdata_clus)

    One Sample t-test

data:  Omgevingsdata_clus
t = 2.1349, df = 7, p-value = 0.07016
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.5058992  9.9108677
sample estimates:
mean of x 
 4.702484 
nmds.Eucl.euc <- metaMDS(log((diseuc)+1), distance="euclidean", k=2, trymax=50)
Run 0 stress 0.1428786 
Run 1 stress 0.1502731 
Run 2 stress 0.1497689 
Run 3 stress 0.1497691 
Run 4 stress 0.15429 
Run 5 stress 0.1647942 
Run 6 stress 0.1435001 
Run 7 stress 0.1477201 
Run 8 stress 0.1643187 
Run 9 stress 0.1603684 
Run 10 stress 0.1643338 
Run 11 stress 0.1580933 
Run 12 stress 0.1683292 
Run 13 stress 0.1497169 
Run 14 stress 0.1563048 
Run 15 stress 0.1523905 
Run 16 stress 0.1643338 
Run 17 stress 0.1502731 
Run 18 stress 0.1439438 
Run 19 stress 0.1497169 
Run 20 stress 0.157543 
Run 21 stress 0.1502731 
Run 22 stress 0.1428497 
... New best solution
... Procrustes: rmse 0.004337419  max resid 0.01625352 
Run 23 stress 0.1498685 
Run 24 stress 0.1502731 
Run 25 stress 0.1502731 
Run 26 stress 0.1544705 
Run 27 stress 0.1497169 
Run 28 stress 0.1497169 
Run 29 stress 0.15429 
Run 30 stress 0.15429 
Run 31 stress 0.1502731 
Run 32 stress 0.1526639 
Run 33 stress 0.1435 
Run 34 stress 0.1497689 
Run 35 stress 0.1435 
Run 36 stress 0.15429 
Run 37 stress 0.1435 
Run 38 stress 0.1502731 
Run 39 stress 0.1428497 
... New best solution
... Procrustes: rmse 1.334463e-05  max resid 4.176609e-05 
... Similar to previous best
*** Best solution repeated 1 times
stressplot(nmds.Eucl.euc) 
title(sub = "Euc")

grp2 <- cutree(cluscom, k=2)


plot(nmds.Eucl.euc, type="none") 
points(nmds.Eucl.euc, display="sites", pch=20, col=c("blue","red", "black", "pink")[grp2])

plot(nmds.Eucl.euc, type="none") 
points(nmds.Eucl.euc, display="sites", pch=20, col=c("yellow","green", "cyan", "violet")[bostype])

# Install and load the hillR package
library(hillR)

# Example vegetation dataset
data <- matrix(
  c(10, 20, 0, 5,   # Site 1
    0,  5, 15, 10,  # Site 2
    5,  0, 10, 5),  # Site 3
  nrow = 3, byrow = TRUE
)
colnames(data) <- c("Species1", "Species2", "Species3", "Species4")
rownames(data) <- c("Site1", "Site2", "Site3")

# Calculate Hill numbers for different q values
q0 <- hill_taxa(data, q = 0) # Species richness
q1 <- hill_taxa(data, q = 1) # Shannon diversity
q2 <- hill_taxa(data, q = 2) # Simpson diversity

# Results
print("Hill numbers (q = 0):")
[1] "Hill numbers (q = 0):"
print(q0)
Site1 Site2 Site3 
    3     3     3 
print("Hill numbers (q = 1):")
[1] "Hill numbers (q = 1):"
print(q1)
   Site1    Site2    Site3 
2.600490 2.749459 2.828427 
print("Hill numbers (q = 2):")
[1] "Hill numbers (q = 2):"
print(q2)
   Site1    Site2    Site3 
2.333333 2.571429 2.666667