Sherwood Forest Regeneration

Author

Josh Shaw

Research

The aim of this study is to understand factors which are affecting regeneration in Sherwood Forest

regen = no. saplings / no. seedlings

Q.1 Is there a difference in the abundance of regen between sites?

1.1 between forest types (natural/plantation) 1.2 between grazing areas (Y/N)

Q.2 what is affecting this difference? Canopy and/or understory factors

  • Factors include

    • Canopy cover and gap diversity

    • Canopy height diversity

    • Understory cover

      • leaf litter

      • deadwood

      • herb vs woody understory

    • Distances

      • to edge of forest

      • to commercial forestry

Q.3 are there connections between variables?

… further q.s about diversity.

Preperation

Load Packages

library(tidyverse)
library(dplyr)
library(knitr)
library(ggplot2)
library(vegan)
library(stringr)
library(rdiversity) # B+G Diversity measurments
library(tibble)
library(janitor)
library(ggpubr)
library(factoextra)
library(patchwork) # Multiplots
library(corrplot) # Cor plots
library(car)
library(lme4)
library(MASS)
library(mgcv) # GAM
library(piecewiseSEM) # SEM
library(glmmTMB) # GLMM method for better fitting
library(performance) # for checking VIFs of GLMMTMB
library(DHARMa) # Checking residuals
library(ggdag) # for piecewise path model
library(broom.mixed) # for extracting model data for visulisation
library(forcats) # for imporving visulisation
library(xfun) # for markdown
library(sjPlot) # for results tables
library(ggrepel)# for plot text
library(ggeffects) # plot predicting 
library(purrr)
library(cowplot) # for plot arranging

Import survey data

# Import Data
trees <- read.csv("trees.csv")

# Quick fix same species with lower vs uppercase
trees <- trees %>%
  mutate(Species = str_to_title(tolower(Species)))

view(trees)

Classify age group

# Calculate DBH from circumference
trees <- trees %>%
  mutate(dbh = if_else(Circumference > 0, Circumference / pi, NA_real_))

# Classify tree, juv, sapling, seedling based on DBH and height
trees <- trees %>%
  mutate(
    class = case_when(
      Height < 50  ~ "seedling",
      Height >= 50 & Height <= 200 ~ "sapling",
      Height > 200 ~ "juvenile",
      TRUE ~ NA_character_)) %>%
  mutate(
    class = case_when(
      is.na(class) & is.na(Height) & dbh >2 & dbh < 10 ~ "juvenile",
      is.na(class) & is.na(Height) & dbh > 10 ~ "tree",
      TRUE ~ class)) 
  
# Convert to dataframe
trees <- as.data.frame(trees)

# Count each class
trees %>%
  count(class)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
class n
juvenile 795
sapling 406
seedling 3175
tree 453
trees <- trees %>%
  rename(transect = Plot)

view(trees)

Summarise per transect

# Summarise data per transect
trees_transect <- trees %>%
  group_by(transect) %>%
  summarise(
            total = n(),
            trees = sum(class == "tree"),
            juveniles = sum(class == "juvenile"),
            saplings = sum(class == "sapling"),
            seedlings = sum(class == "seedling"),
            treedensity = sum(class == "tree")/500,
            juvdensity = sum(class == "juvenile")/500,
            sapdensity = sum(class == "sapling")/500,
            seeddensity = sum(class == "seedling")/500)

Diversity Measurements

# Arrange data
species_matrix <- trees %>%
  group_by(transect, Species) %>%
  summarise(Count = n()) %>%
  pivot_wider(names_from = Species, values_from = Count, values_fill = 0) %>%
  ungroup()
`summarise()` has grouped output by 'transect'. You can override using the
`.groups` argument.
# Remove plot column
species_only <- species_matrix %>% dplyr::select(-transect)

# Organise Oaks - Oak sp. were those which were too young tbi
# Counting as a third species when not the case. When there are all 'three' present, remove oak sp. for diversity measurements

# Check which plots are counting three species
oaks <- species_only %>%
  dplyr::select(1,4,6)

# Set those columns with three species, oak sp. to 0 so it doesnt count
species_filtered <- species_matrix
species_filtered[c(1, 7, 8, 13, 15, 16, 23, 24), "Oak Sp."] <- 0

oak_plots_to_filter <- c(1, 7, 8, 13, 15, 16, 23, 24)
trees_filtered <- trees %>%
  mutate(Species = ifelse(transect %in% oak_plots_to_filter & Species == "Oak Sp.", "Oak Sp. (zeroed)", Species))
trees_filtered %>%
  group_by(transect, class, Species) %>%
  summarise(Count = n(), .groups = "drop") %>%
  mutate(Count = ifelse(Species == "Oak Sp. (zeroed)", 0, Count))
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
transect class Species Count
1 juvenile English Oak 1
1 juvenile Goat Willow 1
1 juvenile Hazel 1
1 juvenile Rowan 9
1 juvenile Sessile Oak 9
1 juvenile Silver Birch 2
1 sapling English Oak 3
1 sapling Rowan 2
1 sapling Sessile Oak 3
1 seedling Oak Sp. (zeroed) 0
1 seedling Rowan 23
1 tree Silver Birch 2
2 juvenile Black Cherry 1
2 juvenile Rowan 10
2 sapling Holly 1
2 sapling Rowan 18
2 sapling Yew 1
2 seedling Holly 6
2 seedling Rowan 15
2 tree English Oak 3
2 tree Silver Birch 10
3 juvenile Elder 2
3 juvenile Hawthorn 2
3 juvenile Sessile Oak 1
3 sapling Elder 1
3 sapling English Oak 2
3 sapling Holly 3
3 seedling Hawthorn 1
3 seedling Holly 2
3 seedling Rowan 8
3 seedling Silver Birch 3
3 seedling Whitebeam 1
3 tree English Oak 3
3 tree Silver Birch 1
4 juvenile Beech 2
4 juvenile English Oak 5
4 juvenile Holly 1
4 juvenile Horse Chestnut 2
4 juvenile Rowan 2
4 juvenile Scots Pine 1
4 juvenile Silver Birch 113
4 sapling English Oak 3
4 sapling Holly 1
4 seedling Beech 1
4 seedling English Oak 2
4 seedling Holly 9
4 seedling Rowan 5
4 tree Horse Chestnut 2
4 tree Silver Birch 22
5 juvenile Beech 1
5 juvenile Rowan 63
5 juvenile Silver Birch 7
5 juvenile Wild Cherry 1
5 sapling Holly 2
5 sapling Rowan 40
5 sapling Wild Cherry 1
5 seedling English Oak 2
5 seedling Holly 5
5 seedling Oak Sp. 1
5 seedling Rowan 44
5 seedling Silver Birch 3
5 seedling Wild Cherry 1
5 tree Beech 2
5 tree English Oak 3
5 tree Silver Birch 66
6 juvenile Sessile Oak 3
6 juvenile Silver Birch 6
6 sapling English Oak 1
6 sapling Holly 1
6 sapling Sessile Oak 1
6 sapling Silver Birch 5
6 seedling Sessile Oak 2
6 tree English Oak 2
6 tree Sessile Oak 5
6 tree Silver Birch 1
7 juvenile Silver Birch 1
7 sapling Holly 2
7 seedling Holly 6
7 seedling Oak Sp. (zeroed) 0
7 seedling Rowan 2
7 tree English Oak 2
7 tree Oak Sp. (zeroed) 0
7 tree Sessile Oak 4
7 tree Silver Birch 5
8 sapling Holly 8
8 sapling Rowan 1
8 seedling Holly 53
8 seedling Oak Sp. (zeroed) 0
8 seedling Rowan 7
8 seedling Silver Birch 4
8 tree English Oak 1
8 tree Sessile Oak 4
8 tree Silver Birch 6
9 sapling Holly 8
9 sapling Oak Sp. 1
9 sapling Sessile Oak 1
9 seedling Black Cherry 1
9 seedling Holly 11
9 seedling Oak Sp. 368
9 seedling Rowan 10
9 seedling Sessile Oak 2
9 seedling Silver Birch 1
9 seedling Yew 1
9 tree Sessile Oak 5
9 tree Silver Birch 4
10 juvenile Silver Birch 63
10 sapling Holly 6
10 seedling Holly 34
10 seedling Oak Sp. 1
10 seedling Rowan 1
10 tree English Oak 1
10 tree Silver Birch 25
11 sapling Holly 2
11 seedling Holly 86
11 seedling Oak Sp. 222
11 seedling Rowan 2
11 seedling Silver Birch 1
11 tree Sessile Oak 7
12 juvenile Holly 1
12 sapling Holly 14
12 seedling Holly 20
12 seedling Oak Sp. 122
12 tree Sessile Oak 8
12 tree Silver Birch 2
13 juvenile Beech 3
13 juvenile English Oak 1
13 juvenile Sessile Oak 1
13 juvenile Silver Birch 3
13 sapling Beech 2
13 sapling Oak Sp. (zeroed) 0
13 sapling Silver Birch 1
13 seedling Oak Sp. (zeroed) 0
13 seedling Silver Birch 2
13 tree Silver Birch 17
14 sapling Beech 3
14 sapling Holly 5
14 sapling Oak Sp. 3
14 sapling Silver Birch 16
14 seedling Beech 5
14 seedling Holly 327
14 seedling Oak Sp. 58
14 seedling Silver Birch 9
14 seedling Yew 2
14 tree Sessile Oak 4
15 juvenile Aspen 3
15 sapling Aspen 9
15 sapling Holly 4
15 sapling Oak Sp. (zeroed) 0
15 sapling Silver Birch 1
15 seedling Aspen 138
15 seedling Beech 1
15 seedling Holly 6
15 seedling Oak Sp. (zeroed) 0
15 seedling Silver Birch 1
15 tree Aspen 1
15 tree English Oak 3
15 tree Sessile Oak 3
16 juvenile Holly 7
16 sapling Holly 31
16 seedling Holly 304
16 seedling Oak Sp. (zeroed) 0
16 tree English Oak 1
16 tree Sessile Oak 3
16 tree Silver Birch 5
17 sapling Holly 7
17 seedling Holly 224
17 seedling Oak Sp. 188
17 seedling Silver Birch 1
17 tree Sessile Oak 7
17 tree Silver Birch 4
19 juvenile Silver Birch 68
19 juvenile Sweet Chestnut 13
19 sapling Holly 3
19 sapling Silver Birch 3
19 sapling Sweet Chestnut 1
19 seedling Holly 52
19 seedling Sweet Chestnut 64
19 tree Silver Birch 32
19 tree Sweet Chestnut 5
20 juvenile American Oak 12
20 juvenile Silver Birch 2
20 juvenile Sweet Chestnut 11
20 sapling American Oak 1
20 sapling Holly 3
20 sapling Oak Sp. 1
20 sapling Scots Pine 3
20 sapling Silver Birch 64
20 sapling Sweet Chestnut 7
20 seedling American Oak 7
20 seedling Holly 30
20 seedling Scots Pine 6
20 seedling Silver Birch 178
20 seedling Sweet Chestnut 44
20 tree American Oak 4
20 tree Scots Pine 1
20 tree Sweet Chestnut 1
22 juvenile Goat Willow 1
22 juvenile Silver Birch 46
22 juvenile Sweet Chestnut 1
22 sapling Beech 1
22 seedling Beech 1
22 seedling Holly 8
22 seedling Silver Birch 3
22 tree Scots Pine 4
22 tree Silver Birch 22
23 juvenile Beech 1
23 juvenile Scots Pine 2
23 juvenile Silver Birch 98
23 juvenile Sweet Chestnut 8
23 sapling Scots Pine 1
23 sapling Silver Birch 5
23 sapling Sweet Chestnut 3
23 seedling Sweet Chestnut 1
23 tree Sessile Oak 2
23 tree Silver Birch 58
23 tree Sweet Chestnut 2
24 juvenile American Oak 13
24 juvenile Beech 1
24 juvenile Silver Birch 29
24 juvenile Sweet Chestnut 3
24 sapling Beech 3
24 sapling Holly 8
24 seedling Beech 3
24 seedling Holly 3
24 tree American Oak 10
24 tree Silver Birch 32
24 tree Sycamore 1
29 juvenile English Oak 4
29 juvenile Holly 10
29 juvenile Rowan 6
29 juvenile Sessile Oak 1
29 juvenile Silver Birch 43
29 sapling Black Cherry 1
29 sapling Hawthorn 1
29 sapling Holly 16
29 sapling Oak Sp. 1
29 sapling Rowan 2
29 sapling Sessile Oak 1
29 sapling Silver Birch 1
29 seedling Hawthorn 6
29 seedling Holly 87
29 seedling Oak Sp. 5
29 seedling Rowan 15
29 tree English Oak 7
29 tree Scots Pine 1
29 tree Sessile Oak 13
29 tree Silver Birch 1
30 juvenile Holly 2
30 juvenile Oak Sp. 4
30 juvenile Scots Pine 18
30 juvenile Sessile Oak 5
30 juvenile Silver Birch 64
30 sapling English Oak 1
30 sapling Holly 9
30 sapling Oak Sp. 18
30 sapling Rowan 11
30 sapling Scots Pine 5
30 sapling Silver Birch 16
30 seedling Holly 179
30 seedling Oak Sp. 6
30 seedling Rowan 34
30 seedling Silver Birch 4
30 tree Rowan 1
30 tree Scots Pine 1
30 tree Sessile Oak 2
30 tree Silver Birch 8
# Plot species accumulation curve
speccurve <- specaccum(comm = species_only, method = "random", permutations = 1000)
plot(speccurve)

species_filtered1 <- species_filtered %>%
  dplyr::select(-transect)

Adult diversity

# Adult richness
adults <- trees_filtered %>% 
  filter(class == "tree")

adult_spec_rich <- table(adults$Species,adults$transect)
adult_spec_rich <- specnumber(adult_spec_rich,MARGIN=2)
adult_spec_rich <- data.frame(transect = names(adult_spec_rich), adult_spec_rich = as.vector(adult_spec_rich))


# Calc adult Shannon Diversity
adult_matrix <- trees_filtered %>%
  filter(class == "tree") %>%
  group_by(transect, Species) %>%
  summarise(Count = n()) %>%
  pivot_wider(names_from = Species, values_from = Count, values_fill = 0) %>%
  ungroup()
`summarise()` has grouped output by 'transect'. You can override using the
`.groups` argument.
adult_matrix <- adult_matrix %>%
  column_to_rownames("transect")

shannon_div <- diversity(x = adult_matrix, index = "shannon")
shannon_div <- data.frame(
  transect = trees_transect$transect,  
  shannon_div = diversity(x = adult_matrix, index = "shannon"))

trees_transect <- merge(trees_transect, adult_spec_rich, by="transect")
trees_transect <- trees_transect %>%
  left_join(shannon_div, by = "transect") 
trees_transect <- trees_transect %>%
  mutate(adult_true_alpha = exp(shannon_div))

Seedling + Sapling diversity

# Seedling Diversity
seedlings <- trees_filtered %>% 
  filter(class == "seedling")

seed_rich <- table(seedlings$Species,seedlings$transect)
seed_rich <- specnumber(seed_rich,MARGIN=2)
seed_rich <- data.frame(transect = names(seed_rich), seed_rich = as.vector(seed_rich))

seedling_matrix <- trees_filtered %>%
  filter(class == "seedling") %>%
  group_by(transect, Species) %>%
  summarise(Count = n()) %>%
  pivot_wider(names_from = Species, values_from = Count, values_fill = 0) %>%
  ungroup()
`summarise()` has grouped output by 'transect'. You can override using the
`.groups` argument.
seedling_matrix <- seedling_matrix %>%
  column_to_rownames("transect")

seedshannon_div <- diversity(x = seedling_matrix, index = "shannon")
seedshannon_div <- data.frame(
  transect = trees_transect$transect,  
  seedshannon_div = diversity(x = seedling_matrix, index = "shannon"))

trees_transect <- merge(trees_transect, seed_rich, by="transect")
trees_transect <- trees_transect %>%
  left_join(seedshannon_div, by = "transect") 
trees_transect <- trees_transect %>%
  mutate(seed_true_alpha = exp(seedshannon_div))

view(trees_transect)

# Sapling Diversity
saplings <- trees_filtered %>% 
  filter(class == "sapling")

sap_rich <- table(saplings$Species,saplings$transect)
sap_rich <- specnumber(sap_rich,MARGIN=2)
sap_rich <- data.frame(transect = names(sap_rich), sap_rich = as.vector(sap_rich))

sapling_matrix <- trees_filtered %>%
  filter(class == "sapling") %>%
  group_by(transect, Species) %>%
  summarise(Count = n()) %>%
  pivot_wider(names_from = Species, values_from = Count, values_fill = 0) %>%
  ungroup()
`summarise()` has grouped output by 'transect'. You can override using the
`.groups` argument.
sapling_matrix <- sapling_matrix %>%
  column_to_rownames("transect")

sapshannon_div <- diversity(x = sapling_matrix, index = "shannon")
sapshannon_div <- data.frame(
  transect = trees_transect$transect,  
  sapshannon_div = diversity(x = sapling_matrix, index = "shannon"))

trees_transect <- merge(trees_transect, sap_rich, by="transect")
trees_transect <- trees_transect %>%
  left_join(sapshannon_div, by = "transect") 
trees_transect <- trees_transect %>%
  mutate(sap_true_alpha = exp(sapshannon_div))


view(trees_transect)

Add GC, LL data

# Leaf litter + ground cover

leaflitter <- read.csv("./ll.csv")
groundcover <- read.csv("./gc.csv")

# Organise ground cover
groundcover <- groundcover %>%
  mutate(perc = X. / 2) %>%
  group_by(plot, category) %>%
  summarise(total_percent = sum(perc, na.rm = TRUE), .groups = "drop") %>%
  rename(transect = plot)

# view ground cover per plot
groundcover %>%
  group_by(category) %>% 
  ggplot(aes(x=transect, y= total_percent, color=category, fill=category)) + 
  geom_col()

# Make wide for analysis
groundcover_wide <- groundcover %>%
  pivot_wider(
    names_from = category,
    values_from = total_percent,
    values_fill = 0)

groundcover_wide <- groundcover_wide %>% clean_names()

leaflitter <- leaflitter %>%
  rename(transect = Plot)

trees_transect <- trees_transect %>%
  left_join(groundcover_wide, by = "transect") %>%
  left_join(leaflitter, by = "transect")

view(trees_transect)

Add Environmental Data

# Add Environmental Data
gapstrans <- read.csv("./csv/canopygaps_transects.csv")
gapsbuff50 <- read.csv("./csv/canopygaps_buffer50.csv")
gapsbuff200 <- read.csv("./csv/canopygaps_buffer200.csv")

heighttrans <- read.csv("./csv/height_transects.csv")
heightbuff50 <- read.csv("./csv/height_buffer50.csv")
heightbuff200 <- read.csv("./csv/height_buffer200.csv")

gapstrans <- gapstrans %>%
  dplyr::select(-1)
gapsbuff50 <- gapsbuff50 %>%
  dplyr::select(-1)
gapsbuff200 <- gapsbuff200 %>%
  dplyr::select(-1)
heighttrans <- heighttrans %>%
  dplyr::select(-1)
heightbuff50 <- heightbuff50 %>%
  dplyr::select(-1)
heightbuff200 <- heightbuff200 %>%
  dplyr::select(-1)


# Create files for transect, buff50, buff200
transectlevel <- merge(trees_transect, gapstrans, by = "transect")
transectlevel <- merge(transectlevel, heighttrans, by = "transect")
transectlevel <- as.data.frame(transectlevel)
view(transectlevel)

buffer50level <- merge(trees_transect, gapsbuff50, by = "transect")
buffer50level <- merge(buffer50level, heightbuff50, by = "transect")
buffer50level >- as.data.frame(buffer50level)
      transect total trees juveniles saplings seedlings treedensity juvdensity
 [1,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [2,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [3,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [4,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [5,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [6,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [7,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
 [8,]     TRUE  TRUE  TRUE     FALSE     TRUE      TRUE        TRUE      FALSE
 [9,]     TRUE  TRUE  TRUE     FALSE     TRUE      TRUE        TRUE      FALSE
[10,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[11,]     TRUE  TRUE  TRUE     FALSE     TRUE      TRUE        TRUE      FALSE
[12,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[13,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[14,]     TRUE  TRUE  TRUE     FALSE     TRUE      TRUE        TRUE      FALSE
[15,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[16,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[17,]     TRUE  TRUE  TRUE     FALSE     TRUE      TRUE        TRUE      FALSE
[18,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[19,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[20,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[21,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[22,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[23,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
[24,]     TRUE  TRUE  TRUE      TRUE     TRUE      TRUE        TRUE       TRUE
      sapdensity seeddensity adult_spec_rich shannon_div adult_true_alpha
 [1,]       TRUE        TRUE            TRUE       FALSE             TRUE
 [2,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [3,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [4,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [5,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [6,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [7,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [8,]       TRUE        TRUE            TRUE        TRUE             TRUE
 [9,]       TRUE        TRUE            TRUE        TRUE             TRUE
[10,]       TRUE        TRUE            TRUE        TRUE             TRUE
[11,]       TRUE        TRUE            TRUE       FALSE             TRUE
[12,]       TRUE        TRUE            TRUE        TRUE             TRUE
[13,]       TRUE        TRUE            TRUE       FALSE             TRUE
[14,]       TRUE        TRUE            TRUE       FALSE             TRUE
[15,]       TRUE        TRUE            TRUE        TRUE             TRUE
[16,]       TRUE        TRUE            TRUE        TRUE             TRUE
[17,]       TRUE        TRUE            TRUE        TRUE             TRUE
[18,]       TRUE        TRUE            TRUE        TRUE             TRUE
[19,]       TRUE        TRUE            TRUE        TRUE             TRUE
[20,]       TRUE        TRUE            TRUE        TRUE             TRUE
[21,]       TRUE        TRUE            TRUE        TRUE             TRUE
[22,]       TRUE        TRUE            TRUE        TRUE             TRUE
[23,]       TRUE        TRUE            TRUE        TRUE             TRUE
[24,]       TRUE        TRUE            TRUE        TRUE             TRUE
      seed_rich seedshannon_div seed_true_alpha sap_rich sapshannon_div
 [1,]      TRUE            TRUE            TRUE     TRUE           TRUE
 [2,]      TRUE            TRUE            TRUE     TRUE           TRUE
 [3,]      TRUE            TRUE            TRUE     TRUE           TRUE
 [4,]      TRUE            TRUE            TRUE     TRUE           TRUE
 [5,]      TRUE            TRUE            TRUE     TRUE           TRUE
 [6,]      TRUE           FALSE            TRUE     TRUE           TRUE
 [7,]      TRUE            TRUE            TRUE     TRUE          FALSE
 [8,]      TRUE            TRUE            TRUE     TRUE           TRUE
 [9,]      TRUE            TRUE            TRUE     TRUE           TRUE
[10,]      TRUE            TRUE            TRUE     TRUE          FALSE
[11,]      TRUE            TRUE            TRUE     TRUE          FALSE
[12,]      TRUE            TRUE            TRUE     TRUE          FALSE
[13,]      TRUE            TRUE            TRUE     TRUE           TRUE
[14,]      TRUE            TRUE            TRUE     TRUE           TRUE
[15,]      TRUE            TRUE            TRUE     TRUE           TRUE
[16,]      TRUE            TRUE            TRUE     TRUE          FALSE
[17,]      TRUE            TRUE            TRUE     TRUE          FALSE
[18,]      TRUE            TRUE            TRUE     TRUE           TRUE
[19,]      TRUE            TRUE            TRUE     TRUE           TRUE
[20,]      TRUE            TRUE            TRUE     TRUE          FALSE
[21,]      TRUE           FALSE            TRUE     TRUE           TRUE
[22,]      TRUE            TRUE            TRUE     TRUE           TRUE
[23,]      TRUE            TRUE            TRUE     TRUE           TRUE
[24,]      TRUE            TRUE            TRUE     TRUE           TRUE
      sap_true_alpha ground herbaceous woody deadwood leaf_litter Litter.depth
 [1,]           TRUE   TRUE       TRUE  TRUE    FALSE       FALSE         TRUE
 [2,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
 [3,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
 [4,]           TRUE   TRUE       TRUE  TRUE     TRUE        TRUE         TRUE
 [5,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
 [6,]           TRUE   TRUE       TRUE  TRUE     TRUE        TRUE         TRUE
 [7,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
 [8,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
 [9,]           TRUE  FALSE       TRUE  TRUE    FALSE        TRUE         TRUE
[10,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
[11,]           TRUE   TRUE       TRUE FALSE     TRUE        TRUE         TRUE
[12,]           TRUE   TRUE       TRUE  TRUE     TRUE        TRUE         TRUE
[13,]           TRUE  FALSE       TRUE  TRUE    FALSE        TRUE         TRUE
[14,]           TRUE   TRUE       TRUE  TRUE     TRUE        TRUE         TRUE
[15,]           TRUE  FALSE       TRUE FALSE     TRUE        TRUE         TRUE
[16,]           TRUE  FALSE       TRUE FALSE     TRUE        TRUE         TRUE
[17,]           TRUE  FALSE       TRUE  TRUE    FALSE        TRUE         TRUE
[18,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
[19,]           TRUE   TRUE       TRUE  TRUE     TRUE        TRUE         TRUE
[20,]           TRUE  FALSE       TRUE FALSE     TRUE        TRUE         TRUE
[21,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
[22,]           TRUE   TRUE       TRUE  TRUE     TRUE        TRUE         TRUE
[23,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
[24,]           TRUE  FALSE       TRUE  TRUE     TRUE        TRUE         TRUE
      Domin num_gaps total_gaparea mean_gaparea min_gaparea max_gaparea
 [1,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [2,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [3,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [4,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [5,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [6,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [7,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [8,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
 [9,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[10,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[11,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[12,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[13,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[14,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[15,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[16,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[17,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[18,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[19,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[20,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[21,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[22,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[23,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
[24,]  TRUE     TRUE          TRUE         TRUE        TRUE        TRUE
      sd_gaparea skew_gaparea cv_area canopy_cover_perc zmean zmax zmin
 [1,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [2,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [3,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [4,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [5,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [6,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [7,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [8,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
 [9,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[10,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[11,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[12,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[13,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[14,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[15,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[16,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[17,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[18,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[19,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[20,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[21,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[22,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[23,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
[24,]       TRUE         TRUE    TRUE              TRUE  TRUE TRUE TRUE
      zentropy  zsd zskew
 [1,]     TRUE TRUE  TRUE
 [2,]     TRUE TRUE FALSE
 [3,]     TRUE TRUE  TRUE
 [4,]     TRUE TRUE  TRUE
 [5,]     TRUE TRUE FALSE
 [6,]     TRUE TRUE FALSE
 [7,]     TRUE TRUE FALSE
 [8,]     TRUE TRUE FALSE
 [9,]     TRUE TRUE FALSE
[10,]     TRUE TRUE  TRUE
[11,]     TRUE TRUE FALSE
[12,]     TRUE TRUE FALSE
[13,]     TRUE TRUE  TRUE
[14,]     TRUE TRUE  TRUE
[15,]     TRUE TRUE  TRUE
[16,]     TRUE TRUE FALSE
[17,]     TRUE TRUE FALSE
[18,]     TRUE TRUE  TRUE
[19,]     TRUE TRUE FALSE
[20,]     TRUE TRUE FALSE
[21,]     TRUE TRUE  TRUE
[22,]     TRUE TRUE FALSE
[23,]     TRUE TRUE  TRUE
[24,]     TRUE TRUE  TRUE
view(buffer50level)

buffer200level <- merge(trees_transect, gapsbuff200, by = "transect")
buffer200level <- merge(buffer200level, heightbuff200, by = "transect")
buffer200level <- as.data.frame(buffer200level)
view(buffer200level)
show(transectlevel)
   transect total trees juveniles saplings seedlings treedensity juvdensity
1         1    60     2        23        8        27       0.004      0.046
2         2    65    13        11       20        21       0.026      0.022
3         3    30     4         5        6        15       0.008      0.010
4         4   171    24       126        4        17       0.048      0.252
5         5   242    71        72       43        56       0.142      0.144
6         6    27     8         9        8         2       0.016      0.018
7         7    26    12         1        2        11       0.024      0.002
8         8   103    11         0        9        83       0.022      0.000
9         9   413     9         0       10       394       0.018      0.000
10       10   131    26        63        6        36       0.052      0.126
11       11   320     7         0        2       311       0.014      0.000
12       12   167    10         1       14       142       0.020      0.002
13       13    47    17         8        4        18       0.034      0.016
14       14   432     4         0       27       401       0.008      0.000
15       15   202     7         3       15       177       0.014      0.006
16       16   362     9         7       31       315       0.018      0.014
17       17   431    11         0        7       413       0.022      0.000
18       19   241    37        81        7       116       0.074      0.162
19       20   375     6        25       79       265       0.012      0.050
20       22    87    26        48        1        12       0.052      0.096
21       23   181    62       109        9         1       0.124      0.218
22       24   106    43        46       11         6       0.086      0.092
23       29   222    22        64       23       113       0.044      0.128
24       30   388    12        93       60       223       0.024      0.186
   sapdensity seeddensity adult_spec_rich shannon_div adult_true_alpha
1       0.016       0.054               1   0.0000000         1.000000
2       0.040       0.042               2   0.5402041         1.716357
3       0.012       0.030               2   0.5623351         1.754765
4       0.008       0.034               2   0.2868360         1.332206
5       0.086       0.112               3   0.3021257         1.352731
6       0.016       0.004               3   0.9002561         2.460233
7       0.004       0.022               4   1.2366849         3.444177
8       0.018       0.166               3   0.9164649         2.500435
9       0.020       0.788               2   0.6869616         1.987667
10      0.012       0.072               2   0.1630236         1.177065
11      0.004       0.622               1   0.0000000         1.000000
12      0.028       0.284               2   0.5004024         1.649385
13      0.008       0.036               1   0.0000000         1.000000
14      0.054       0.802               1   0.0000000         1.000000
15      0.030       0.354               3   1.0042425         2.729839
16      0.062       0.630               3   0.9368883         2.552028
17      0.014       0.826               2   0.6554818         1.926070
18      0.014       0.232               2   0.3960331         1.485918
19      0.158       0.530               3   0.8675632         2.381102
20      0.002       0.024               2   0.4293230         1.536217
21      0.018       0.002               3   0.2839363         1.328348
22      0.022       0.012               3   0.6465629         1.908968
23      0.046       0.226               4   0.9562373         2.601888
24      0.120       0.446               4   0.9830878         2.672696
   seed_rich seedshannon_div seed_true_alpha sap_rich sapshannon_div
1          2       0.4194834        1.521175        3      1.0821955
2          2       0.5982696        1.818969        3      0.3943977
3          5       1.2868726        3.621443        3      1.0114043
4          4       1.1150660        3.049769        2      0.5623351
5          6       0.8247505        2.281312        3      0.2974449
6          1       0.0000000        1.000000        4      1.0735428
7          3       0.9949236        2.704518        1      0.0000000
8          4       0.9786435        2.660844        2      0.3488321
9          7       0.3292354        1.389905        3      0.6390319
10         3       0.2530673        1.287970        1      0.0000000
11         4       0.6470130        1.909828        1      0.0000000
12         2       0.4064946        1.501545        1      0.0000000
13         2       0.3488321        1.417411        3      1.0397208
14         5       0.6123375        1.844739        4      1.1106412
15         5       0.6723917        1.958917        4      1.0200370
16         2       0.1514511        1.163521        1      0.0000000
17         3       0.7046587        2.023156        1      0.0000000
18         2       0.6877868        1.989308        3      1.0042425
19         5       0.9938066        2.701498        6      0.7443648
20         3       0.8239592        2.279507        1      0.0000000
21         1       0.0000000        1.000000        3      0.9368883
22         2       0.6931472        2.000000        2      0.5859526
23         4       0.7632064        2.145143        7      1.1464632
24         4       0.6325796        1.882460        6      1.5845584
   sap_true_alpha ground herbaceous woody deadwood leaf_litter Litter.depth
1        2.951152   10.0      75.00 15.00      0.0         0.0         1.75
2        1.483490    0.0      30.00 45.00      5.0        20.0         6.50
3        2.749459    0.0      30.00  7.50      7.5        55.0         8.50
4        1.754765    5.0      67.50 10.00      2.5        15.0         3.00
5        1.346414    0.0      50.00 30.00     15.0         5.0         6.00
6        2.925727   27.5       3.75  8.75      5.0        55.0         8.50
7        1.000000    0.0      40.00 20.00     12.5        27.5         1.50
8        1.417411    0.0      22.50  2.50      5.0        70.0         5.50
9        1.894646    0.0      30.00 30.00      0.0        40.0         8.00
10       1.000000    0.0      15.00  5.00     10.0        70.0         4.00
11       1.000000    7.5      30.00  0.00      2.5        60.0         6.50
12       1.000000    5.0      17.50  2.50     10.0        65.0         3.50
13       2.828427    0.0      60.00 15.00      0.0        25.0         5.25
14       3.036305    2.5      50.00 20.00      2.5        25.0         5.50
15       2.773297    0.0      30.00  0.00     12.5        57.5         2.50
16       1.000000    0.0      22.50  0.00      7.5        70.0         1.50
17       1.000000    0.0      15.00 10.00      0.0        75.0         1.50
18       2.729839    0.0      30.00  5.00      7.5        57.5        12.00
19       2.105104   15.0      12.50 30.00      7.5        35.0         5.00
20       1.000000    0.0      82.50  0.00      7.5        10.0         3.00
21       2.552028    0.0      62.50  5.00      7.5        25.0         6.00
22       1.796702    2.5       1.25 46.25     10.0        40.0         3.00
23       3.147043    0.0      32.50  2.50      5.0        60.0         5.50
24       4.877137    0.0      75.00  5.00      5.0        15.0         5.50
   Domin num_gaps total_gaparea mean_gaparea min_gaparea max_gaparea sd_gaparea
1    0.5        3        280.00    93.333333       13.50      237.25 124.884030
2    1.5        5        139.75    27.950000        1.75      104.75  43.480384
3    1.0        4        193.25    48.312500        1.50       94.75  53.622280
4    0.5       10        220.00    22.000000        1.00      200.75  62.826591
5    1.0        6         59.75     9.958333        1.00       28.25  12.519402
6    2.0       14         72.00     5.142857        1.00       26.00   7.299048
7    1.5        6        176.25    29.375000        1.00       71.75  29.509638
8    2.0        7         49.75     7.107143        1.00       37.50  13.421731
9    1.5        3         15.00     5.000000        2.25        9.50   3.929058
10   1.0        8         98.25    12.281250        1.00       43.00  16.246669
11   1.5        5          8.25     1.650000        1.00        2.75   0.720243
12   1.0        3         83.25    27.750000        2.50       74.75  40.740797
13   1.0        5        138.00    27.600000        1.25      105.50  44.737289
14   1.0        5        172.25    34.450000        1.50      103.75  40.785797
15   0.5        5        195.25    39.050000        1.00      132.25  56.309524
16   1.0        7         21.00     3.000000        1.00        7.50   2.629956
17   1.0        7         14.25     2.035714        1.00        3.00   0.834523
18   3.0       15        101.25     6.750000        1.50       39.00   9.610262
19   1.0        9        244.50    27.166667        1.00      204.50  66.971076
20   1.0        7        216.50    30.928571        1.50      168.00  60.995170
21   1.5       17        124.75     7.338235        1.00       75.00  17.525192
22   1.0       17         43.00     2.529412        1.00       10.25   2.538581
23   1.0        5         44.75     8.950000        1.00       36.75  15.571809
24   1.0        3        330.50   110.166667       31.00      198.25  83.980777
    skew_gaparea   cv_area canopy_cover_perc     zmean     zmax    zmin
1   1.7011094301 1.3380432             44.00  6.517968 20.27148 0.00125
2   2.0973365122 1.5556488             72.05  8.027988 17.81554 0.01348
3  -0.0001694164 1.1099049             61.35  6.823133 17.93172 0.00009
4   3.1584880803 2.8557541             56.00  3.969087 14.92030 0.01470
5   1.0154286957 1.2571784             88.05  7.863597 16.90434 0.00164
6   2.3539222449 1.4192593             85.60  9.468905 16.46398 0.03270
7   0.4614209996 1.0045834             64.75  7.041515 17.20832 0.00435
8   2.6293982951 1.8884847             90.05  8.281502 16.12211 0.02337
9   1.6066834278 0.7858117             97.00 12.380448 20.86461 0.00888
10  1.3246331643 1.3228840             80.35  5.116158 14.09140 0.01369
11  1.0831350894 0.4365109             98.35 12.742226 20.28054 0.02541
12  1.7176808312 1.4681368             83.35 14.137258 22.65396 0.03605
13  1.9702971321 1.6209163             72.40  7.014578 18.62456 0.03180
14  1.7248950441 1.1839128             65.55  8.306626 19.38063 0.03154
15  1.5743966101 1.4419853             60.95  7.840073 21.03111 0.00665
16  1.2410305986 0.8766519             95.80 12.189206 21.68261 0.02020
17 -0.1735983331 0.4099411             97.15 12.370118 20.65660 0.00655
18  3.1193473327 1.4237426             79.75  5.454497 14.24039 0.00053
19  2.9238810128 2.4651930             51.10 10.921281 26.43260 0.00226
20  2.5507333161 1.9721302             56.70  8.206878 27.10294 0.01100
21  4.0533376912 2.3882025             75.05  4.260843 11.05749 0.01032
22  2.2996278377 1.0036250             91.40 11.046008 27.04146 0.00314
23  2.2139828933 1.7398670             91.05 11.493540 23.58192 0.02611
24  0.4724017385 0.7623066             33.90  2.957561 11.60159 0.02421
   zentropy      zsd        zskew
1  3.170638 6.788242  0.453241023
2  3.533451 6.241905 -0.145928133
3  3.765912 5.168538  0.114354266
4  2.957744 2.741180 -0.023369697
5  3.213326 6.322649 -0.196718864
6  3.826078 4.130972 -0.694645319
7  3.869126 4.873703  0.178624696
8  3.816742 4.774588 -0.381410181
9  3.941720 5.615563 -0.992255965
10 3.556352 3.769621  0.330243056
11 3.925190 4.980110 -1.118858811
12 4.063653 5.638309 -1.061348490
13 3.398937 6.471235  0.199361231
14 3.693705 6.177782 -0.139816799
15 3.854620 5.787904  0.102094832
16 3.891521 5.572761 -1.026732336
17 3.732400 6.720501 -0.865090782
18 3.421122 3.850523 -0.058977411
19 3.628626 9.308783 -0.006909258
20 3.586478 9.099418  0.710013603
21 2.813585 3.531298 -0.032389308
22 4.251236 7.697276  0.056332192
23 4.251540 6.727709 -0.459241864
24 2.866883 2.864289  0.657526016

Explore Data

# Explore classes per plot
trees %>%
  count(class)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
class n
juvenile 795
sapling 406
seedling 3175
tree 453
trees %>%
  group_by(class) %>% 
  ggplot(aes(x=transect, color=class, fill=class)) + 
  geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Further Preparing Data

# Add grazing data
transectlevel <- transectlevel %>%
  mutate(grazing = c("n", "n", "n", "n", "n", "y", "y", "y", "y", "n", "y", "y", "n", "y", "y", "y", "y", "n", "n", "n", "n", "n", "n", "n")) %>%
  mutate(grazing = factor(grazing, levels = c("n", "y")))
  
# Add forest type column
transectlevel$forest_type <- ifelse(row_number(transectlevel) <= 17, "natural", "plantation")

# Reduce number of variables
# Add height CV too
transectlevel$zcv <- transectlevel$zsd / transectlevel$zmean
buffer50level$zcv <- buffer50level$zsd / buffer50level$zmean
buffer200level$zcv <- buffer200level$zsd / buffer200level$zmean


# Check which height and gap variables show similar information
vars <- c("total_gaparea", "mean_gaparea", "min_gaparea", "max_gaparea",
          "sd_gaparea", "skew_gaparea", "cv_area", "canopy_cover_perc",
          "zmean", "zmax", "zmin", "zentropy", "zsd", "zskew", "zcv")
data_subset <- transectlevel[, vars]
data_scaled <- scale(data_subset)  # standardize for PCA
cor_matrix <- cor(data_subset)
corrplot(cor_matrix, method = "color", tl.cex = 0.7, mar = c(0,0,1,0))

pca <- prcomp(data_scaled, center = TRUE, scale. = TRUE)
summary(pca)
Importance of components:
                          PC1    PC2   PC3     PC4     PC5     PC6     PC7
Standard deviation     2.7272 1.6115 1.490 1.12627 0.71060 0.64842 0.54388
Proportion of Variance 0.4958 0.1731 0.148 0.08457 0.03366 0.02803 0.01972
Cumulative Proportion  0.4958 0.6690 0.817 0.90153 0.93519 0.96322 0.98294
                           PC8     PC9    PC10    PC11    PC12    PC13    PC14
Standard deviation     0.30926 0.28966 0.22966 0.11331 0.08281 0.05634 0.02731
Proportion of Variance 0.00638 0.00559 0.00352 0.00086 0.00046 0.00021 0.00005
Cumulative Proportion  0.98932 0.99491 0.99843 0.99928 0.99974 0.99995 1.00000
                            PC15
Standard deviation     1.545e-16
Proportion of Variance 0.000e+00
Cumulative Proportion  1.000e+00
pca$rotation
                          PC1         PC2          PC3         PC4         PC5
total_gaparea     -0.35155103  0.08949471 -0.039134332  0.08365361  0.13716072
mean_gaparea      -0.30793625  0.05613973 -0.325767402  0.14094499  0.03967206
min_gaparea       -0.23202468 -0.10981011 -0.390882850  0.16698931 -0.14913747
max_gaparea       -0.33236910  0.16658555  0.052715528  0.20898239  0.25151300
sd_gaparea        -0.32321928  0.18819913 -0.083018703  0.18927687  0.24754174
skew_gaparea      -0.03894162 -0.02886080  0.591072100  0.23034206 -0.08956301
cv_area           -0.14087301  0.02518436  0.570714035  0.22335400  0.15847844
canopy_cover_perc  0.35155103 -0.08949471  0.039134332 -0.08365361 -0.13716072
zmean              0.28488625  0.31513295 -0.124307423  0.21605463  0.17746417
zmax               0.10951941  0.57854761  0.005199217  0.07594073 -0.03384111
zmin               0.08312262 -0.13709577 -0.074712597  0.74392797 -0.54024907
zentropy           0.26818623  0.28764017 -0.119227947  0.08134865 -0.05676379
zsd                0.03581752  0.58493492  0.056986456 -0.09134004 -0.23532085
zskew             -0.31155238  0.06329475  0.079429828 -0.27424287 -0.44999221
zcv               -0.31229201  0.14973740  0.091859895 -0.24446718 -0.44191236
                           PC6         PC7          PC8         PC9        PC10
total_gaparea     -0.203784135  0.03837590 -0.229546875 -0.38467720 -0.22129403
mean_gaparea       0.030573143  0.11790048  0.344023692  0.21825884 -0.21397555
min_gaparea        0.505922862  0.43387700 -0.382604925  0.23632428 -0.01543152
max_gaparea       -0.007983089 -0.11029970 -0.060244431 -0.08755789  0.26177856
sd_gaparea        -0.028560668 -0.10980027  0.537006794  0.25519460  0.17842687
skew_gaparea       0.338404877  0.48982073  0.348424489 -0.31875348 -0.07833569
cv_area           -0.107979289 -0.05536084 -0.373995584  0.60832896 -0.13807140
canopy_cover_perc  0.203784135 -0.03837590  0.229546875  0.38467720  0.22129403
zmean              0.230379627 -0.05206050 -0.076415269 -0.01130669 -0.15292554
zmax               0.047592101  0.14552833 -0.181255905 -0.08825773  0.62612845
zmin              -0.184789140 -0.27839574 -0.000752603 -0.08289402  0.05219562
zentropy          -0.533182257  0.52510311  0.118248816  0.16733393 -0.23833040
zsd                0.231578663 -0.26154981 -0.021262675  0.01046696 -0.46569991
zskew             -0.294456429  0.21281524 -0.073521016  0.10514367  0.19447947
zcv                0.137445162 -0.19201206  0.134409221  0.04634828 -0.02867721
                          PC11        PC12        PC13         PC14
total_gaparea     -0.145478384  0.02935173 -0.03655812  0.158297371
mean_gaparea      -0.425130283 -0.10100564 -0.16499917 -0.574183950
min_gaparea        0.179454094 -0.06194741  0.02892584  0.218244113
max_gaparea        0.550716322 -0.11272380  0.37375240 -0.452027825
sd_gaparea         0.136229139  0.16224177 -0.14047256  0.541566050
skew_gaparea       0.009080771  0.03603526 -0.03115851 -0.035657255
cv_area           -0.185328575 -0.02226254 -0.01388304  0.006609450
canopy_cover_perc  0.145478384 -0.02935173  0.03655812 -0.158297371
zmean             -0.144394690  0.72277351  0.30068020 -0.084304178
zmax              -0.350822811 -0.20463123 -0.15869829  0.009471625
zmin               0.008536730 -0.00591242 -0.04167924  0.017970970
zentropy           0.160712661 -0.17610033  0.31137961  0.093197086
zsd                0.328024717 -0.16403062 -0.33901831 -0.026213805
zskew              0.175543846  0.55731509 -0.23467109 -0.179314424
zcv               -0.283218644 -0.10288926  0.65144955  0.156785627
                           PC15
total_gaparea     -7.071068e-01
mean_gaparea       1.665335e-16
min_gaparea       -1.873501e-16
max_gaparea        6.661338e-16
sd_gaparea        -1.082467e-15
skew_gaparea      -3.369700e-16
cv_area            1.595946e-16
canopy_cover_perc -7.071068e-01
zmean              4.163336e-17
zmax               1.682682e-16
zmin              -1.734723e-17
zentropy          -9.020562e-17
zsd               -1.449578e-16
zskew             -1.804112e-16
zcv                0.000000e+00
fviz_pca_var(pca, col.var = "contrib")

# Choose Zmean, Zentropy and zcv
# Choose total gaparea and cv gaparea

# Remove unwanted columns
data <- transectlevel %>%
  dplyr::select(-shannon_div, -seedshannon_div, -sapshannon_div, -num_gaps, -total_gaparea, -mean_gaparea, -min_gaparea, -max_gaparea, -sd_gaparea, -skew_gaparea, -zmax, -zmin, -zsd, -zentropy, -zskew)

# Combine altransect# Combine all into one dataset
buffer50_selected <- buffer50level %>%
  dplyr::select(transect, cv_area, canopy_cover_perc, zmean, zcv) %>%
  rename_with(~ paste0(., "_50"), -transect)
data <- data %>%
  left_join(buffer50_selected, by = "transect")

buffer200_selected <- buffer200level %>%
  dplyr::select(transect, cv_area, canopy_cover_perc, zmean, zcv) %>%
  rename_with(~ paste0(., "_200"), -transect)
data <- data %>%
  left_join(buffer200_selected, by = "transect")

rm(buffer200_selected, buffer50_selected)

treedata <- trees %>%
  left_join(data, by = "transect")
  
# Tree data contains all indv tree counts plus transect and wider data

data$transect <- as.factor(data$transect)

Analysis

Differences between sites, forest type + grazing

# Is there a difference in number of seedlings , saplings and species richness between sites and forest type?

# Normality Check
shapiro.test(data$seedlings) # Not normally dist

    Shapiro-Wilk normality test

data:  data$seedlings
W = 0.82032, p-value = 0.0006451
hist(data$seedlings)

shapiro.test(data$saplings) # Not normally dist

    Shapiro-Wilk normality test

data:  data$saplings
W = 0.73311, p-value = 2.851e-05
hist(data$saplings)

shapiro.test(data$seed_rich) # Normally dist

    Shapiro-Wilk normality test

data:  data$seed_rich
W = 0.93508, p-value = 0.1266
hist(data$seed_rich)

shapiro.test(data$seed_true_alpha) # Normally dist

    Shapiro-Wilk normality test

data:  data$seed_true_alpha
W = 0.95584, p-value = 0.3606
hist(data$seed_true_alpha)

shapiro.test(data$sap_rich) # Not normally dist

    Shapiro-Wilk normality test

data:  data$sap_rich
W = 0.86742, p-value = 0.004685
hist(data$sap_rich)

shapiro.test(data$sap_true_alpha) # Not normally dist

    Shapiro-Wilk normality test

data:  data$sap_true_alpha
W = 0.87298, p-value = 0.00602
hist(data$sap_true_alpha)

# Test differences between sites
wilcox.test(data$seedlings, paired = FALSE) # SIG DIFF

    Wilcoxon signed rank exact test

data:  data$seedlings
V = 300, p-value = 1.192e-07
alternative hypothesis: true location is not equal to 0
wilcox.test(data$saplings, paired = FALSE) # SIG DIFF
Warning in wilcox.test.default(data$saplings, paired = FALSE): cannot compute
exact p-value with ties

    Wilcoxon signed rank test with continuity correction

data:  data$saplings
V = 300, p-value = 1.931e-05
alternative hypothesis: true location is not equal to 0
t.test(data$seed_rich, paired = FALSE) # SIG DIFF

    One Sample t-test

data:  data$seed_rich
t = 10.446, df = 23, p-value = 3.337e-10
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 2.706618 4.043382
sample estimates:
mean of x 
    3.375 
t.test(data$seed_true_alpha, paired = FALSE) # SIG DIFF

    One Sample t-test

data:  data$seed_true_alpha
t = 14.759, df = 23, p-value = 3.211e-13
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 1.689327 2.240085
sample estimates:
mean of x 
 1.964706 
wilcox.test(data$sap_rich, paired = FALSE) # SIG DIFF
Warning in wilcox.test.default(data$sap_rich, paired = FALSE): cannot compute
exact p-value with ties

    Wilcoxon signed rank test with continuity correction

data:  data$sap_rich
V = 300, p-value = 1.675e-05
alternative hypothesis: true location is not equal to 0
wilcox.test(data$sap_true_alpha, paired = FALSE) # SIG DIFF
Warning in wilcox.test.default(data$sap_true_alpha, paired = FALSE): cannot
compute exact p-value with ties

    Wilcoxon signed rank test with continuity correction

data:  data$sap_true_alpha
V = 300, p-value = 1.838e-05
alternative hypothesis: true location is not equal to 0
# Test differences between forest types
wilcox.test(seedlings ~ forest_type, data = data) # No sig diff

    Wilcoxon rank sum exact test

data:  seedlings by forest_type
W = 72, p-value = 0.4551
alternative hypothesis: true location shift is not equal to 0
wilcox.test(saplings ~ forest_type, data = data) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  saplings by forest_type
W = 44, p-value = 0.3401
alternative hypothesis: true location shift is not equal to 0
t.test(seed_rich ~ forest_type, data = data) # No sig diff

    Welch Two Sample t-test

data:  seed_rich by forest_type
t = 0.79066, df = 13.174, p-value = 0.4431
alternative hypothesis: true difference in means between group natural and group plantation is not equal to 0
95 percent confidence interval:
 -0.9152008  1.9740243
sample estimates:
   mean in group natural mean in group plantation 
                3.529412                 3.000000 
t.test(seed_true_alpha ~ forest_type, data = data) # No sig diff

    Welch Two Sample t-test

data:  seed_true_alpha by forest_type
t = -0.18914, df = 15.533, p-value = 0.8524
alternative hypothesis: true difference in means between group natural and group plantation is not equal to 0
95 percent confidence interval:
 -0.6045276  0.5057136
sample estimates:
   mean in group natural mean in group plantation 
                1.950295                 1.999702 
wilcox.test(sap_rich ~ forest_type, data = data) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  sap_rich by forest_type
W = 36, p-value = 0.1309
alternative hypothesis: true location shift is not equal to 0
wilcox.test(sap_true_alpha ~ forest_type, data = data) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  sap_true_alpha by forest_type
W = 39, p-value = 0.1985
alternative hypothesis: true location shift is not equal to 0
# Test differences between grazing
wilcox.test(seedlings ~ grazing, data = data) # Sig diff

    Wilcoxon rank sum exact test

data:  seedlings by grazing
W = 33, p-value = 0.03059
alternative hypothesis: true location shift is not equal to 0
wilcox.test(saplings ~ grazing, data = data) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  saplings by grazing
W = 70.5, p-value = 1
alternative hypothesis: true location shift is not equal to 0
t.test(seed_rich ~ grazing, data = data) # No sig diff

    Welch Two Sample t-test

data:  seed_rich by grazing
t = -0.56181, df = 17.182, p-value = 0.5815
alternative hypothesis: true difference in means between group n and group y is not equal to 0
95 percent confidence interval:
 -1.833061  1.061633
sample estimates:
mean in group n mean in group y 
       3.214286        3.600000 
t.test(seed_true_alpha ~ grazing, data = data) # No sig diff

    Welch Two Sample t-test

data:  seed_true_alpha by grazing
t = 0.97804, df = 21.549, p-value = 0.3389
alternative hypothesis: true difference in means between group n and group y is not equal to 0
95 percent confidence interval:
 -0.2868660  0.7977523
sample estimates:
mean in group n mean in group y 
       2.071140        1.815697 
wilcox.test(sap_rich ~ grazing, data = data) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  sap_rich by grazing
W = 91.5, p-value = 0.2035
alternative hypothesis: true location shift is not equal to 0
wilcox.test(sap_true_alpha ~ grazing, data = data) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  sap_true_alpha by grazing
W = 93, p-value = 0.1823
alternative hypothesis: true location shift is not equal to 0
# There are significant differences in number of seedlings, saplings and species richness and diversity between sites
# There are not significant differences between plantation and forestry
# No. saplings but nothing else was affected by grazing

Visualise

Initial relationships

# Have a look at canopy relationships
data %>%
  dplyr::select(seedlings, canopy_cover_perc, cv_area, zmean, zcv) %>%
  pivot_longer(-seedlings, names_to = "variable", values_to = "value") %>%
  ggplot(aes(x = value, y = seedlings)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", color = "darkblue", se = FALSE) +
  facet_wrap(~ variable, scales = "free_x") +
  theme_minimal() +
  labs(x = NULL, y = "Seedling Count", title = "Seedlings vs. Canopy Structure Variables")
`geom_smooth()` using formula = 'y ~ x'

# All having some effect - positive/negative



# Have a look at understory relationships
data %>%
  dplyr::select(seedlings, ground, herbaceous, woody, deadwood, leaf_litter, Litter.depth, Domin) %>%
  pivot_longer(-seedlings, names_to = "variable", values_to = "value") %>%
  ggplot(aes(x = value, y = seedlings)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", color = "darkblue", se = FALSE) +
  facet_wrap(~ variable, scales = "free_x") +
  theme_minimal() +
  labs(x = NULL, y = "Seedling Count", title = "Seedlings vs. Understory Variables")
`geom_smooth()` using formula = 'y ~ x'

Predictor Correlations

# Check corr between predictors
cordata <- data %>%
  mutate(
    forest_type = as.numeric(factor(forest_type)),  # natural = 1, plantation = 2
    grazing     = as.numeric(factor(grazing)))       # n = 1, y = 2

cor_matrix <- cor(cordata %>%
                    dplyr::select(canopy_cover_perc, zmean, cv_area, zcv, treedensity, ground, herbaceous, woody, deadwood, leaf_litter, Litter.depth, Domin, forest_type, grazing),use = "complete.obs")    

custom_labels <- c("Canopy Cover (%)", "Mean Canopy Height", "Canopy Gap CV", "Canopy Height CV", "Tree Density",
                   "Ground Cover (%)", "Herbaceous Cover (%)", "Woody Cover (%)", "Deadwood Cover (%)",
                   "Leaf Litter cover (%)", "Litter Depth", "Deadwood Scale", "Forest Type", "Grazing")
colnames(cor_matrix) <- custom_labels
rownames(cor_matrix) <- custom_labels


corrplot(cor_matrix,
         method = "color",
         type = "upper",
         tl.cex = 0.8,tl.srt = 45,
         addCoef.col = "black")

Canopy -> Understory Relationships

# Is there a relationship between the canopy and understory?

# Understory response matrix
understory_matrix <- data[, c("ground", "herbaceous", "woody", "deadwood", 
                              "leaf_litter", "Domin", "Litter.depth")]


# Run canonical correlation anlaysis
cca_model <- cca(understory_matrix ~ canopy_cover_perc + cv_area + zmean + zcv + treedensity, data = data)

anova(cca_model)               # Overall model
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
Model 5 0.2061921 2.168678 0.014
Residual 18 0.3422784 NA NA
anova(cca_model, by = "term")  # Individual predictors
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
canopy_cover_perc 1 0.1005956 5.290199 0.003
cv_area 1 0.0042634 0.224209 0.915
zmean 1 0.0268231 1.410592 0.245
zcv 1 0.0523998 2.755640 0.032
treedensity 1 0.0221102 1.162750 0.349
Residual 18 0.3422784 NA NA
anova(cca_model, by = "axis")  # Each canonical axis
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
CCA1 1 0.1483059 7.7992230 0.007
CCA2 1 0.0297700 1.5655661 0.934
CCA3 1 0.0211480 1.1121489 0.991
CCA4 1 0.0068202 0.3586637 1.000
CCA5 1 0.0001481 0.0077888 NA
Residual 18 0.3422784 NA NA
# significance from canopy cover and zcv

cca_model <- cca(understory_matrix ~ canopy_cover_perc + zcv , data = data)

anova(cca_model)               # Overall model
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
Model 2 0.1625583 4.422928 0.001
Residual 21 0.3859122 NA NA
anova(cca_model, by = "term")  # Individual predictors
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
canopy_cover_perc 1 0.1005956 5.474062 0.002
zcv 1 0.0619627 3.371793 0.021
Residual 21 0.3859122 NA NA
anova(cca_model, by = "axis")  # Each canonical axis
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
CCA1 1 0.1388612 7.556341 0.001
CCA2 1 0.0236971 1.289514 0.779
Residual 21 0.3859122 NA NA
# Canopy cover and zcv significantly explain understory variability
# CCA1 is significant

plot(cca_model, scaling = 2)

# Look into which understory areas are affected
understory_scores <- scores(cca_model, display = "species", scaling = 2)
cca1_loadings <- understory_scores[, "CCA1"]
sorted_vars <- sort(cca1_loadings, decreasing = TRUE)
sorted_vars
 leaf_litter       ground        Domin Litter.depth     deadwood        woody 
  0.41137189   0.32801079   0.13812672   0.06715687  -0.03598166  -0.13139138 
  herbaceous 
 -0.44299681 
# those on the left of plot increase with open canopy
plot(cca_model, display = "species", scaling = 2, main = "CCA Biplot: Understory Variables")
abline(h = 0, v = 0, lty = 2, col = "grey70")

species_scores <- scores(cca_model, display = "species", scaling = 2)
species_df <- as.data.frame(species_scores)
species_df$Variable <- rownames(species_df)

# Check CCA relation to predictors
env_scores <- scores(cca_model, display = "bp", scaling = 2)
env_scores
                        CCA1        CCA2
canopy_cover_perc  0.8171474 -0.57642873
zcv               -0.9968752 -0.07899226
env_df <- as.data.frame(env_scores)
env_df$Predictor <- rownames(env_df)


# Check cor between predictors
cor(data$canopy_cover_perc, data$zcv)
[1] -0.7691936
fit <- envfit(cca_model, data, scaling = 2)
fit

***VECTORS

                          CCA1     CCA2     r2 Pr(>r)    
total                  0.83816 -0.54542 0.0248  0.775    
trees                 -0.83337 -0.55272 0.1597  0.166    
juveniles             -0.99756  0.06977 0.1845  0.106    
saplings              -0.99998  0.00583 0.0096  0.911    
seedlings              0.97970 -0.20046 0.0799  0.406    
treedensity           -0.83337 -0.55272 0.1597  0.166    
juvdensity            -0.99756  0.06977 0.1845  0.106    
sapdensity            -0.99998  0.00583 0.0096  0.911    
seeddensity            0.97970 -0.20046 0.0799  0.406    
adult_spec_rich        0.91709 -0.39869 0.0243  0.778    
adult_true_alpha       0.99623 -0.08675 0.0965  0.334    
seed_rich             -0.51614 -0.85650 0.0736  0.437    
seed_true_alpha       -0.71416 -0.69998 0.0422  0.614    
sap_rich              -0.81892  0.57391 0.0458  0.607    
sap_true_alpha        -0.87633  0.48171 0.1526  0.179    
ground                 0.42723  0.90414 0.6674  0.001 ***
herbaceous            -0.99336  0.11504 0.8817  0.001 ***
woody                 -0.49899 -0.86661 0.2974  0.024 *  
deadwood               0.64609 -0.76326 0.0688  0.490    
leaf_litter            0.99948 -0.03234 0.8898  0.001 ***
Litter.depth           0.98281  0.18461 0.0205  0.780    
Domin                  0.99989 -0.01489 0.1027  0.312    
cv_area               -0.88571  0.46424 0.1386  0.205    
canopy_cover_perc      0.96538 -0.26083 0.4792  0.001 ***
zmean                  0.99695 -0.07803 0.3246  0.019 *  
zcv                   -0.99997 -0.00712 0.5895  0.001 ***
cv_area_50             0.62570 -0.78006 0.0333  0.686    
canopy_cover_perc_50   0.95795 -0.28692 0.1775  0.124    
zmean_50               0.99832 -0.05790 0.1043  0.330    
zcv_50                -0.99818 -0.06037 0.1744  0.140    
cv_area_200           -0.97410 -0.22610 0.1564  0.161    
canopy_cover_perc_200  0.98143 -0.19182 0.0895  0.363    
zmean_200              0.90304  0.42955 0.0025  0.978    
zcv_200               -0.97065 -0.24051 0.0536  0.566    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Permutation: free
Number of permutations: 999

***FACTORS:

Centroids:
                         CCA1    CCA2
transect1             -2.2347  3.2214
transect2             -0.7009 -3.6295
transect3              0.5780 -0.7228
transect4             -1.6160  1.7753
transect5             -1.6175 -1.9855
transect6              1.9227  6.5740
transect7             -0.6416 -1.4681
transect8              1.2705 -0.5251
transect9             -0.0021 -2.4129
transect10             1.4774 -0.9273
transect11             0.9606  2.1507
transect12             1.3997  0.9007
transect13            -1.2048 -0.4289
transect14            -0.8963 -0.3521
transect15             0.7097 -0.0955
transect16             1.3207 -0.2772
transect17             1.6253 -1.2585
transect19             0.6674 -0.6055
transect20             0.6823  1.6321
transect22            -2.2410  1.3810
transect23            -1.1870  0.3517
transect24             0.7357 -3.8634
transect29             0.6954 -0.2256
transect30            -1.8517  0.7344
grazingn              -0.5448 -0.2523
grazingy               0.7682  0.3557
forest_typenatural     0.1435  0.0349
forest_typeplantation -0.3445 -0.0838

Goodness of fit:
                r2 Pr(>r)
transect    1.0000  1.000
grazing     0.0796  0.165
forest_type 0.0082  0.822
Permutation: free
Number of permutations: 999
rename_map <- c(
  "deadwood" = "Deadwood cover (%)",
  "leaf_litter" = "Leaf Litter Cover (%)",
  "Litter.depth" = "Litter Depth",
  "herbaceous" = "Herbaceous Cover (%)",
  "woody" = "Woody cover (%)",
  "Domin" = "Deadwood Scale",
  "ground" = "Bare Ground cover (%)",
  "zcv" = "Canopy Height CV",
  "canopy_cover_perc" = "Canopy Cover (%)"
)
species_df <- species_df %>%
  mutate(Variable = dplyr::recode(Variable, !!!rename_map))

env_df <- env_df %>%
  mutate(Predictor = dplyr::recode(Predictor, !!!rename_map))

# Filter out deadwood for repel
species_df_nondeadwood <- species_df %>% filter(Variable != "Deadwood cover (%)")
deadwood_df <- species_df %>% filter(Variable == "Deadwood cover (%)")

ggplot() +
  geom_segment(data = species_df, aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
               linewidth = 1, arrow = arrow(length = unit(0.2, "cm")), color = "darkred") +
  geom_text_repel(data = species_df_nondeadwood, aes(x = CCA1, y = CCA2, label = Variable),
                  color = "darkred", size = 4, max.overlaps = Inf, hjust = -0.5, vjust = -2) +
  geom_text_repel(data = deadwood_df, aes(x = CCA1, y = CCA2, label = Variable),
            color = "darkred", size = 4, hjust = 1.2, vjust = 2) +  # Adjust position manually
  geom_segment(data = env_df, aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
               linewidth = 1, arrow = arrow(length = unit(0.25, "cm")), color = "blue") +
  geom_text_repel(data = env_df, aes(x = CCA1, y = CCA2, label = Predictor),
                  color = "blue", size = 4, hjust = 1.5, vjust = 1.5) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey70") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey70") +
  theme_minimal(base_size = 22) +
  labs( x = "CCA1 (85.4%)", y = "CCA2 (14.5%)")

herbaceous requires open canopy, clearly defined on the left of the plot leaf litter more associated with closed canopy

Visualise Pathway

# relationship pathway - canopy to understory + understory to regen

dag_nodes <- tibble(
  name = c(
    "zcv", "canopy cover", "canopy height", "canopy cv", "tree density", 
    "leaf litter", "bare ground", "herbaceous", "woody",  "litter depth", "deadwood", "domin",
    "seedling\nabundance", "sapling\nabundance", "seedling\nrichness", "sapling\nrichness","seedling\ndiversity", "sapling\ndiversity"
  ),
  x = c(2, 3, 4, 5, 6,  1, 2, 3, 4, 5, 6, 7,
        1.5, 2.5, 3.5, 4.5,5.5,6.5),
  y = c(3,3,3,3,3,2,2,2,2,2,2,2,1,1,1,1,1,1),
  box_color = "white"
)

dag_edges <- tibble(
  from = c("canopy cover", "canopy cover", "canopy cover", "canopy cover",
           "zcv",           "zcv"),
  to   = c("leaf litter",   "herbaceous",   "woody",         "bare ground",
           "leaf litter",   "bare ground"),
  effect_size = c(0.41, -0.44, -0.13, 0.33, 0.41, 0.33)
) %>%
  mutate(
    color = ifelse(effect_size > 0, "forestgreen", "firebrick"),
    alpha = 1,
    linetype = "solid",
    label = ifelse(effect_size > 0, "+", "−")
  )

regen_edges <- tibble(
  from = "litter depth",
  to = c("seedling\nabundance", "seedling\nrichness", 
         "sapling\nrichness", "sapling\ndiversity"),
  effect_size = c(0.38, 0.29, 0.25, 0.13)
) %>%
  mutate(
    color = ifelse(effect_size > 0, "forestgreen", "firebrick"),
    alpha = 1,
    linetype = "solid",
    label = ifelse(effect_size > 0, "+", "−")
  )

bind_edges <- bind_rows(dag_edges, regen_edges) %>%
  left_join(dag_nodes, by = c("from" = "name")) %>%
  rename(x_start = x, y_start = y) %>%
  left_join(dag_nodes, by = c("to" = "name")) %>%
  rename(x_end = x, y_end = y)

ggplot() +
  geom_curve(data = bind_edges,
             aes(x = x_start, y = y_start, xend = x_end, yend = y_end,
                 color = color, linetype = linetype, alpha = alpha, linewidth = 2),
             curvature = 0.12,
             arrow = arrow(type = "closed", length = unit(0.25, "cm"))) +
  geom_label(data = dag_nodes,
             aes(x = x, y = y, label = name, fill = box_color),
             fontface = "bold", size = 4,
             label.r = unit(0.25, "lines"), label.size = 0.3, color = "grey40") +
  scale_fill_identity() +
  scale_color_identity() +
  scale_alpha_identity() +
  scale_linetype_identity() +
  coord_equal(xlim = c(0.5, 7.5), ylim = c(0.4, 3.5), expand = FALSE) +
  theme_void()

# Visualise 2 - canopy to regen

dag_nodes <- tibble(
  name = c(
    "canopy cover", "canopy height", "canopy cv", "tree density",
    "seedling\nabundance", "sapling\nabundance", 
    "seedling\nrichness", "sapling\nrichness",
    "seedling\ndiversity", "sapling\ndiversity"
  ),
  x = c(2.5, 3.8, 5, 6,    # canopy layer
        2, 3,      # abundance
        4, 5,      # richness
        6, 7),     # diversity
  y = c(rep(2.6, 4), rep(1.2, 6)),  # vertical layout
  box_color = case_when(
    y == 2.6 ~ "white",  # canopy
    y == 1.2 ~ "white"
  )
)

dag_edges <- tibble(
  from = c("canopy cover", "canopy cover", "canopy cover",
           "canopy cv", "canopy height", "tree density"),
  to = c("seedling\nabundance", "sapling\nabundance", "sapling\ndiversity",
         "seedling\nrichness", "seedling\nrichness", "seedling\nrichness"),
  effect_sign = c("+", "+", "-", "+", "-", "-"),
  curvature = c(0.12, 0.12, -0.06, 0.12, 0.12, 0.12)  # flatter arrow for sapling diversity
  ) %>%
  mutate(
    color = ifelse(effect_sign == "+", "forestgreen", "firebrick"),
    alpha = 1,
    linetype = "solid"
  ) %>%
  left_join(dag_nodes, by = c("from" = "name")) %>%
  rename(x_start = x, y_start = y) %>%
  left_join(dag_nodes, by = c("to" = "name")) %>%
  rename(x_end = x, y_end = y)

edges_regular <- filter(dag_edges, curvature == 0.12)
edges_flat    <- filter(dag_edges, curvature == -0.06)

ggplot() +
  geom_curve(data = edges_regular,
             aes(x = x_start, y = y_start, xend = x_end, yend = y_end,
                 color = color, linetype = linetype),
             curvature = 0.12,
             arrow = arrow(type = "closed", length = unit(0.25, "cm")),
             linewidth = 2) +
  geom_curve(data = edges_flat,
             aes(x = x_start, y = y_start, xend = x_end, yend = y_end,
                 color = color, linetype = linetype),
             curvature = -0.06,  # flatter curve
             arrow = arrow(type = "closed", length = unit(0.25, "cm")),
             linewidth = 2) +
  geom_label(data = dag_nodes,
             aes(x = x, y = y, label = name, fill = box_color),
             fontface = "bold", size = 4,
             label.r = unit(0.25, "lines"), label.size = 0.3, color = "grey40") +
  scale_fill_identity() +
  scale_color_identity() +
  scale_alpha_identity() +
  scale_linetype_identity() +
  coord_equal(xlim = c(0, 8), ylim = c(0.4, 3.5), expand = FALSE) +
  theme_void()

Landscape vs local canopy effects

Are transect level or landscape canopy predictors better?

# Automate

compare_spatial_scales_single <- function(data, predictor_base, responses, model_families) {

  scales <- c("", "_50", "_200")
  
  results <- expand.grid(response = responses, scale = scales, stringsAsFactors = FALSE) %>%
    mutate(
      predictor = paste0(predictor_base, scale),
      formula = paste0(response, " ~ ", predictor),
      family = model_families[response],
      model = pmap(list(formula, family), function(f, fam) {
        tryCatch({
          if (fam == "lm") {
            lm(as.formula(f), data = data)
          } else {
            glmmTMB(as.formula(f), family = fam, data = data)
          }
        }, error = function(e) NA)
      }),
      AIC = map_dbl(model, ~ if (inherits(.x, "lm") || inherits(.x, "glmmTMB")) AIC(.x) else NA)
    ) %>%
    group_by(response) %>%
    arrange(AIC, .by_group = TRUE) %>%
    mutate(best = row_number() == 1) %>%
    ungroup()
  
  return(results)
}


responses <- c("seedlings", "saplings", "seed_rich", "sap_rich", "seed_true_alpha", "sap_true_alpha")
model_families <- c(
  seedlings = "nbinom2",
  saplings = "nbinom2",
  seed_rich = "lm",
  sap_rich = "poisson",
  seed_true_alpha = "lm",
  sap_true_alpha = "gaussian"
)

cc_results <- compare_spatial_scales_single(data, "canopy_cover_perc", responses, model_families)
print(cc_results)
# A tibble: 18 × 8
   response        scale  predictor         formula family model       AIC best 
   <chr>           <chr>  <chr>             <chr>   <chr>  <list>    <dbl> <lgl>
 1 sap_rich        "_200" canopy_cover_per… sap_ri… poiss… <glmmTMB>  88.3 TRUE 
 2 sap_rich        "_50"  canopy_cover_per… sap_ri… poiss… <glmmTMB>  88.6 FALSE
 3 sap_rich        ""     canopy_cover_perc sap_ri… poiss… <glmmTMB>  90.6 FALSE
 4 sap_true_alpha  "_50"  canopy_cover_per… sap_tr… gauss… <glmmTMB>  54.1 TRUE 
 5 sap_true_alpha  "_200" canopy_cover_per… sap_tr… gauss… <glmmTMB>  65.5 FALSE
 6 sap_true_alpha  ""     canopy_cover_perc sap_tr… gauss… <glmmTMB>  65.7 FALSE
 7 saplings        "_200" canopy_cover_per… saplin… nbino… <glmmTMB> 187.  TRUE 
 8 saplings        ""     canopy_cover_perc saplin… nbino… <glmmTMB> 188.  FALSE
 9 saplings        "_50"  canopy_cover_per… saplin… nbino… <glmmTMB> 188.  FALSE
10 seed_rich       "_50"  canopy_cover_per… seed_r… lm     <lm>       94.9 TRUE 
11 seed_rich       ""     canopy_cover_perc seed_r… lm     <lm>       95.1 FALSE
12 seed_rich       "_200" canopy_cover_per… seed_r… lm     <lm>       95.1 FALSE
13 seed_true_alpha ""     canopy_cover_perc seed_t… lm     <lm>       50.2 TRUE 
14 seed_true_alpha "_200" canopy_cover_per… seed_t… lm     <lm>       52.4 FALSE
15 seed_true_alpha "_50"  canopy_cover_per… seed_t… lm     <lm>       52.6 FALSE
16 seedlings       ""     canopy_cover_perc seedli… nbino… <glmmTMB> 284.  TRUE 
17 seedlings       "_200" canopy_cover_per… seedli… nbino… <glmmTMB> 285.  FALSE
18 seedlings       "_50"  canopy_cover_per… seedli… nbino… <glmmTMB> 285.  FALSE
cc_results %>% filter(best)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
response scale predictor formula family model AIC best
sap_rich _200 canopy_cover_perc_200 sap_rich ~ canopy_cover_perc_200 poisson 0, 0, function (x = last.par[lfixed()], …) , {, if (tracepar) {, cat(“par:”), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], ...) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat(“outer mgc:”, max(abs(ans)), “”), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = “ADGrad”, order = 1)), }, else {, stop(“Hessian not yet implemented for models with random effects.”), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, “reportdims”), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat(“Some ‘random’ effect names does not match ‘parameter’ list:”), print(setdiff(random, names(parameters))), cat(“(Note that regular expression match is disabled by default)”), stop(), }, if (any(duplicated(random))) {, cat(“Duplicates in ‘random’ - will be removed”), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat(“Selected random effects did not match any model parameters.”), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop(“Profile parameter vector not unique.”), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if (“ADFun” %in% type) {, if (omp\(autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\)autopar) , openmp(omp\(nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) &#124;&#124; names(I) == "") {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop("Names to be 'integrate'd must be among the random parameters"), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\)which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\(method), ok <- all(duplicated(method)[-1]), if (!ok) , stop("Grouping only allowed for identical methods"), method <- method[1], cfg <- NULL, if (method == "marginal_sr") {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\)activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random_remove, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\(ptr, , "par"))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, "ADGrad"), }, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\(sparse)) , cfg\)sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = “laplace”, config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\)ptr, “par”), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\(autopar && !ADreport) {, TransformADFunObject(ADFun, method = "parallel_accumulate", , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = "reorder_random", , random_order = random, mustWork = 0L), }, if ("Fun" %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if ("ADGrad" %in% type) {, retape_adgrad(lazy = TRUE), }, env\)skipFixedEffects <- !is.null(ADGrad), delayedAssign(“spHess”, sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c329067858>, function (par = last.par) , {, f(par, order = 0, type = “double”), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = “double”, do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 2.47051, -0.01977531, 42.17075, 0, 9, 16, 10, relative convergence (4), 2.47051, -0.01977531, 2.47051, -0.01977531, 0.3548653, -0.004836437, -0.004836437, 6.876518e-05, TRUE, 1.519257e-07, 1.177802e-05, <environment: 0x000001c325a269a8>, glmmTMB(formula = sap_rich ~ canopy_cover_perc_200, data = data, , family = fam, ziformula = ~0, dispformula = ~1), 3, 3, 3, 2, 3, 4, 1, 2, 3, 1, 1, 1, 3, 4, 4, 1, 1, 3, 6, 1, 3, 2, 7, 6, 70.18083, 69.10684, 83.22437, 56.09536, 80.79654, 78.06758, 71.79242, 80.68926, 93.83988, 52.14989, 94.30363, 86.51063, 67.06933, 75.45306, 65.96827, 82.8257, 81.98239, 75.25244, 67.94607, 83.2389, 81.26388, 83.77151, 59.29782, 37.2623, 24, 1, poisson, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (mu) , mu, function (y, mu, wt) , {, r <- mu * wt, p <- which(y > 0), r[p] <- (wt * (y * log(y/mu) - (y - mu)))[p], 2 * r, }, function (y, n, mu, wt, dev) , -2 * sum(dpois(y, mu, log = TRUE) * wt), function (eta) , pmax(exp(eta), .Machine\)double.eps), {, if (any(y < 0)) , stop(“negative values not allowed for the ‘Poisson’ family”), n <- rep.int(1, nobs), mustart <- y + 0.1, }, function (mu) , all(is.finite(mu)) && all(mu > 0), function (eta) , TRUE, function (object, nsim) , {, wts <- object\(prior.weights, if (any(wts != 1)) , warning("ignoring prior weights"), ftd <- fitted(object), rpois(nsim * length(ftd), ftd), }, 1, sap_rich ~ canopy_cover_perc_200, sap_rich ~ canopy_cover_perc_200, sap_rich ~ canopy_cover_perc_200 + 0 + 1, sap_rich ~ canopy_cover_perc_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 88.34150|TRUE | |sap_true_alpha |_50 |canopy_cover_perc_50 |sap_true_alpha ~ canopy_cover_perc_50 |gaussian |1, 1, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c32908a510>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 5.972235, -0.05305152, -0.4160817, 24.06856, 0, 15, 32, 16, relative convergence (4), 5.972235, -0.05305152, -0.4160817, 5.972235, -0.05305152, -0.4160817, 0.5412837, -0.007088823, 6.40989e-09, -0.007088823, 9.605467e-05, -9.777009e-11, 6.40989e-09, -9.777009e-11, 0.02083332, TRUE, -1.066372e-06, -0.0001031268, 1.125735e-06, <environment: 0x000001c325db0858>, glmmTMB(formula = sap_true_alpha ~ canopy_cover_perc_50, data = data, , family = fam, ziformula = ~0, dispformula = ~1), 2.951152, 1.48349, 2.749459, 1.754765, 1.346414, 2.925727, 1, 1.417411, 1.894646, 1, 1, 1, 2.828427, 3.036305, 2.773297, 1, 1, 2.729839, 2.105104, 1, 2.552028, 1.796702, 3.147043, 4.877137, 60.72808, 78.69852, 70.74032, 72.62239, 92.38573, 80.25895, 72.86442, 81.51685, 91.29024, 68.34872, 92.34752, 74.14779, 70.6002, 66.68001, 60.45739, 83.74285, 94.392, 68.82003, 67.96657, 76.28144, 68.51113, 76.96612, 75.29742, 25.53239, 24, 1, gaussian, identity, function (mu) , mu, function (eta) , eta, function (mu) , rep.int(1, length(mu)), function (y, mu, wt) , wt * ((y - mu)^2), function (y, n, mu, wt, dev) , {, nobs <- length(y), nobs * (log(dev/nobs * 2 * pi) + 1) + 2 - sum(log(wt)), }, function (eta) , rep.int(1, length(eta)), {, n <- rep.int(1, nobs), if (is.null(etastart) && is.null(start) && is.null(mustart) && , ((family\)link == “inverse” && any(y == 0)) || (family\(link == , "log" && any(y <= 0)))) , stop("cannot find valid starting values: please specify some"), mustart <- y, }, function (mu) , TRUE, function (eta) , TRUE, NA, sap_true_alpha ~ canopy_cover_perc_50, ~1, sap_true_alpha ~ canopy_cover_perc_50, ~1, sap_true_alpha ~ canopy_cover_perc_50 + 0 + 1, sap_true_alpha ~ canopy_cover_perc_50, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 54.13712|TRUE | |saplings |_200 |canopy_cover_perc_200 |saplings ~ canopy_cover_perc_200 |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328ebbdc8>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 4.695663, -0.02606342, 0.2909236, 90.64947, 0, 17, 30, 18, relative convergence (4), 4.695663, -0.02606342, 0.2909236, 4.695663, -0.02606342, 0.2909236, 1.149036, -0.01510651, -0.001863255, -0.01510651, 0.0002046421, 2.553923e-05, -0.001863255, 2.553923e-05, 0.08519256, TRUE, -4.080531e-06, -0.0002432299, 1.613753e-06, <environment: 0x000001c325db0970>, glmmTMB(formula = saplings ~ canopy_cover_perc_200, data = data, , family = fam, ziformula = ~0, dispformula = ~1), 8, 20, 6, 4, 43, 8, 2, 9, 10, 6, 2, 14, 4, 27, 15, 31, 7, 7, 79, 1, 9, 11, 23, 60, 70.18083, 69.10684, 83.22437, 56.09536, 80.79654, 78.06758, 71.79242, 80.68926, 93.83988, 52.14989, 94.30363, 86.51063, 67.06933, 75.45306, 65.96827, 82.8257, 81.98239, 75.25244, 67.94607, 83.2389, 81.26388, 83.77151, 59.29782, 37.2623, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (eta) , TRUE, log, function (...) , NA_real_, saplings ~ canopy_cover_perc_200, ~1, saplings ~ canopy_cover_perc_200, ~1, saplings ~ canopy_cover_perc_200 + 0 + 1, saplings ~ canopy_cover_perc_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 187.29894|TRUE | |seed_rich |_50 |canopy_cover_perc_50 |seed_rich ~ canopy_cover_perc_50 |lm |2.627506, 0.01012866, -1.2426, -1.424617, 1.655989, 0.6369264, 2.43675, -2.440422, -0.365525, 0.5468374, 3.447846, -0.319787, 0.4371372, -1.378524, -1.342592, 1.697115, 1.760142, -1.475709, -0.5835707, -1.324561, 1.684084, -0.4001349, -2.321432, -1.40707, 0.609832, 1.113885, -16.53406, -0.6816963, 1.852874, 0.8645131, 2.986732, -2.088261, -0.1339902, 0.9195176, 3.979958, -0.1619159, 0.9864959, -1.126054, -1.147992, 1.827765, 1.789283, -1.066716, -0.0008606891, -1.159001, 1.835721, -0.1128588, -2.160911, -1.108625, 0.8810558, 0.5733022, 2, 3.2426, 3.424617, 3.344011, 3.363074, 3.56325, 3.440422, 3.365525, 3.453163, 3.552154, 3.319787, 3.562863, 3.378524, 3.342592, 3.302885, 3.239858, 3.475709, 3.583571, 3.324561, 3.315916, 3.400135, 3.321432, 3.40707, 3.390168, 2.886115, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -361.5441, -67.30367, -0.0125345, 0.01542937, 0.3090737, 0.1288936, 0.0190254, 0.1475835, 0.2927969, -0.04806897, 0.3085059, 0.03809383, -0.01461641, -0.07286266, -0.1653185, 0.1806575, 0.3388829, -0.04106617, -0.05374691, 0.06979569, -0.04565584, 0.07996867, 0.05517498, -0.6842353, 1.204124, 1.105709, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_rich ~ canopy_cover_perc_50, 2, 2, 5, 4, 6, 1, 3, 4, 7, 3, 4, 2, 2, 5, 5, 2, 3, 2, 5, 3, 1, 2, 4, 4, 60.72808, 78.69852, 70.74032, 72.62239, 92.38573, 80.25895, 72.86442, 81.51685, 91.29024, 68.34872, 92.34752, 74.14779, 70.6002, 66.68001, 60.45739, 83.74285, 94.392, 68.82003, 67.96657, 76.28144, 68.51113, 76.96612, 75.29742, 25.53239 | 94.93638|TRUE | |seed_true_alpha | |canopy_cover_perc |seed_true_alpha ~ canopy_cover_perc |lm |2.776657, -0.01087617, -0.7769303, -0.1740606, 1.512039, 0.8821777, 0.4623012, -0.8456571, 0.6320926, 0.8635862, -0.3317637, -0.614787, 0.2028418, -0.3685834, -0.5718113, -0.2189857, -0.1548378, -0.5711986, 0.3031188, 0.08002532, 0.4806134, 0.1195287, -0.9604006, 0.2174247, 0.3587614, -0.5254947, -9.625053, 0.9492296, 1.639924, 1.007541, 0.6027696, -0.7063433, 0.7615798, 1.004997, -0.1870772, -0.4779475, 0.3481646, -0.2303301, -0.4387187, -0.08912153, -0.02714151, -0.4270777, 0.447876, 0.216582, 0.6036674, 0.2452219, -0.8260591, 0.359472, 0.5006437, -0.4105471, 2, 2.298106, 1.993029, 2.109404, 2.167592, 1.81901, 1.845657, 2.072425, 1.797258, 1.721669, 1.902757, 1.706986, 1.870128, 1.989223, 2.063724, 2.113755, 1.73472, 1.720037, 1.909283, 2.220885, 2.159978, 1.960401, 1.782575, 1.786382, 2.407955, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -365.7292, -87.27608, -0.09289654, -0.1541963, 0.2130292, 0.1849574, -0.05393971, 0.235945, 0.3155773, 0.1248034, 0.3310455, 0.1591771, 0.03371317, -0.04477339, -0.0974797, 0.3018279, 0.317296, 0.1179287, -0.2103399, -0.1461757, 0.06407659, 0.2514131, 0.2474029, -0.4074157, 1.204124, 1.029703, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_true_alpha ~ canopy_cover_perc, 1.521175, 1.818969, 3.621443, 3.049769, 2.281312, 1, 2.704518, 2.660844, 1.389905, 1.28797, 1.909828, 1.501545, 1.417411, 1.844739, 1.958917, 1.163521, 2.023156, 1.989308, 2.701498, 2.279507, 1, 2, 2.145143, 1.88246, 44, 72.05, 61.35, 56, 88.05, 85.6, 64.75, 90.05, 97, 80.35, 98.35, 83.35, 72.4, 65.55, 60.95, 95.8, 97.15, 79.75, 51.1, 56.7, 75.05, 91.4, 91.05, 33.9 | 50.24929|TRUE | |seedlings | |canopy_cover_perc |seedlings ~ canopy_cover_perc |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c32521c7e8>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 4.090772, 0.01030543, -0.4145325, 139.2195, 0, 24, 31, 25, relative convergence (4), 4.090772, 0.01030543, -0.4145325, 4.090772, 0.01030543, -0.4145325, 0.7869965, -0.009683597, -0.0005041543, -0.009683597, 0.0001295918, 6.706294e-06, -0.0005041543, 6.706294e-06, 0.06278538, TRUE, 2.337108e-07, -4.176147e-06, 2.947069e-07, <environment: 0x000001c325f13d58>, glmmTMB(formula = seedlings ~ canopy_cover_perc, data = data, , family = fam, ziformula = ~0, dispformula = ~1), 27, 21, 15, 17, 56, 2, 11, 83, 394, 36, 311, 142, 18, 401, 177, 315, 413, 116, 265, 12, 1, 6, 113, 223, 44, 72.05, 61.35, 56, 88.05, 85.6, 64.75, 90.05, 97, 80.35, 98.35, 83.35, 72.4, 65.55, 60.95, 95.8, 97.15, 79.75, 51.1, 56.7, 75.05, 91.4, 91.05, 33.9, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine$double.eps), function (eta) , TRUE, log, function (…) , NA_real_, seedlings ~ canopy_cover_perc, ~1, seedlings ~ canopy_cover_perc, ~1, seedlings ~ canopy_cover_perc + 0 + 1, seedlings ~ canopy_cover_perc, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 284.43904 TRUE
cv_area_results <- compare_spatial_scales_single(data, "cv_area", responses, model_families)
print(cv_area_results)
# A tibble: 18 × 8
   response        scale  predictor   formula       family model       AIC best 
   <chr>           <chr>  <chr>       <chr>         <chr>  <list>    <dbl> <lgl>
 1 sap_rich        "_200" cv_area_200 sap_rich ~ c… poiss… <glmmTMB>  91.7 TRUE 
 2 sap_rich        ""     cv_area     sap_rich ~ c… poiss… <glmmTMB>  92.4 FALSE
 3 sap_rich        "_50"  cv_area_50  sap_rich ~ c… poiss… <glmmTMB>  93.5 FALSE
 4 sap_true_alpha  "_200" cv_area_200 sap_true_alp… gauss… <glmmTMB>  71.9 TRUE 
 5 sap_true_alpha  "_50"  cv_area_50  sap_true_alp… gauss… <glmmTMB>  73.2 FALSE
 6 sap_true_alpha  ""     cv_area     sap_true_alp… gauss… <glmmTMB>  73.2 FALSE
 7 saplings        "_200" cv_area_200 saplings ~ c… nbino… <glmmTMB> 187.  TRUE 
 8 saplings        "_50"  cv_area_50  saplings ~ c… nbino… <glmmTMB> 190.  FALSE
 9 saplings        ""     cv_area     saplings ~ c… nbino… <glmmTMB> 191.  FALSE
10 seed_rich       "_50"  cv_area_50  seed_rich ~ … lm     <lm>       93.5 TRUE 
11 seed_rich       "_200" cv_area_200 seed_rich ~ … lm     <lm>       94.7 FALSE
12 seed_rich       ""     cv_area     seed_rich ~ … lm     <lm>       94.8 FALSE
13 seed_true_alpha ""     cv_area     seed_true_al… lm     <lm>       51.5 TRUE 
14 seed_true_alpha "_200" cv_area_200 seed_true_al… lm     <lm>       52.4 FALSE
15 seed_true_alpha "_50"  cv_area_50  seed_true_al… lm     <lm>       52.5 FALSE
16 seedlings       ""     cv_area     seedlings ~ … nbino… <glmmTMB> 281.  TRUE 
17 seedlings       "_50"  cv_area_50  seedlings ~ … nbino… <glmmTMB> 283.  FALSE
18 seedlings       "_200" cv_area_200 seedlings ~ … nbino… <glmmTMB> 284.  FALSE
cv_area_results %>% filter(best)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
response scale predictor formula family model AIC best
sap_rich _200 cv_area_200 sap_rich ~ cv_area_200 poisson 0, 0, function (x = last.par[lfixed()], …) , {, if (tracepar) {, cat(“par:”), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], ...) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat(“outer mgc:”, max(abs(ans)), “”), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = “ADGrad”, order = 1)), }, else {, stop(“Hessian not yet implemented for models with random effects.”), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, “reportdims”), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat(“Some ‘random’ effect names does not match ‘parameter’ list:”), print(setdiff(random, names(parameters))), cat(“(Note that regular expression match is disabled by default)”), stop(), }, if (any(duplicated(random))) {, cat(“Duplicates in ‘random’ - will be removed”), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat(“Selected random effects did not match any model parameters.”), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop(“Profile parameter vector not unique.”), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if (“ADFun” %in% type) {, if (omp\(autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\)autopar) , openmp(omp\(nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) &#124;&#124; names(I) == "") {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop("Names to be 'integrate'd must be among the random parameters"), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\)which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\(method), ok <- all(duplicated(method)[-1]), if (!ok) , stop("Grouping only allowed for identical methods"), method <- method[1], cfg <- NULL, if (method == "marginal_sr") {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\)activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random_remove, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\(ptr, , "par"))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, "ADGrad"), }, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\(sparse)) , cfg\)sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = “laplace”, config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\)ptr, “par”), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\(autopar && !ADreport) {, TransformADFunObject(ADFun, method = "parallel_accumulate", , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = "reorder_random", , random_order = random, mustWork = 0L), }, if ("Fun" %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if ("ADGrad" %in% type) {, retape_adgrad(lazy = TRUE), }, env\)skipFixedEffects <- !is.null(ADGrad), delayedAssign(“spHess”, sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328c257e0>, function (par = last.par) , {, f(par, order = 0, type = “double”), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = “double”, do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 0.6756855, 0.02985347, 43.86585, 0, 9, 12, 10, relative convergence (4), 0.6756855, 0.02985347, 0.6756855, 0.02985347, 0.08769784, -0.00573894, -0.00573894, 0.00045122, TRUE, -5.443193e-07, -1.387818e-05, <environment: 0x000001c32515bf20>, glmmTMB(formula = sap_rich ~ cv_area_200, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 3, 3, 3, 2, 3, 4, 1, 2, 3, 1, 1, 1, 3, 4, 4, 1, 1, 3, 6, 1, 3, 2, 7, 6, 11.28316, 11.94995, 8.417493, 20.53324, 20.61253, 5.841923, 8.933692, 4.455673, 6.434522, 23.92923, 6.632544, 7.871479, 10.46547, 10.07716, 10.80861, 8.452168, 7.363355, 11.40306, 11.54297, 11.23696, 10.36638, 13.8548, 18.43625, 22.45443, 24, 1, poisson, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (mu) , mu, function (y, mu, wt) , {, r <- mu * wt, p <- which(y > 0), r[p] <- (wt * (y * log(y/mu) - (y - mu)))[p], 2 * r, }, function (y, n, mu, wt, dev) , -2 * sum(dpois(y, mu, log = TRUE) * wt), function (eta) , pmax(exp(eta), .Machine\)double.eps), {, if (any(y < 0)) , stop(“negative values not allowed for the ‘Poisson’ family”), n <- rep.int(1, nobs), mustart <- y + 0.1, }, function (mu) , all(is.finite(mu)) && all(mu > 0), function (eta) , TRUE, function (object, nsim) , {, wts <- object\(prior.weights, if (any(wts != 1)) , warning("ignoring prior weights"), ftd <- fitted(object), rpois(nsim * length(ftd), ftd), }, 1, sap_rich ~ cv_area_200, sap_rich ~ cv_area_200, sap_rich ~ cv_area_200 + 0 + 1, sap_rich ~ cv_area_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 91.73170|TRUE | |sap_true_alpha |_200 |cv_area_200 |sap_true_alpha ~ cv_area_200 |gaussian |1, 1, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328c1e3f8>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 1.545272, 0.04334607, -0.04556866, 32.96088, 0, 14, 19, 15, relative convergence (4), 1.545272, 0.04334607, -0.04556866, 1.545272, 0.04334607, -0.04556866, 0.2245269, -0.01579546, -3.592478e-09, -0.01579546, 0.001337856, 5.174248e-10, -3.592478e-09, 5.174248e-10, 0.02083332, TRUE, 1.587828e-06, 2.802893e-05, 1.83274e-07, <environment: 0x000001c325d0a270>, glmmTMB(formula = sap_true_alpha ~ cv_area_200, data = data, , family = fam, ziformula = ~0, dispformula = ~1), 2.951152, 1.48349, 2.749459, 1.754765, 1.346414, 2.925727, 1, 1.417411, 1.894646, 1, 1, 1, 2.828427, 3.036305, 2.773297, 1, 1, 2.729839, 2.105104, 1, 2.552028, 1.796702, 3.147043, 4.877137, 11.28316, 11.94995, 8.417493, 20.53324, 20.61253, 5.841923, 8.933692, 4.455673, 6.434522, 23.92923, 6.632544, 7.871479, 10.46547, 10.07716, 10.80861, 8.452168, 7.363355, 11.40306, 11.54297, 11.23696, 10.36638, 13.8548, 18.43625, 22.45443, 24, 1, gaussian, identity, function (mu) , mu, function (eta) , eta, function (mu) , rep.int(1, length(mu)), function (y, mu, wt) , wt * ((y - mu)^2), function (y, n, mu, wt, dev) , {, nobs <- length(y), nobs * (log(dev/nobs * 2 * pi) + 1) + 2 - sum(log(wt)), }, function (eta) , rep.int(1, length(eta)), {, n <- rep.int(1, nobs), if (is.null(etastart) && is.null(start) && is.null(mustart) && , ((family\)link == “inverse” && any(y == 0)) || (family\(link == , "log" && any(y <= 0)))) , stop("cannot find valid starting values: please specify some"), mustart <- y, }, function (mu) , TRUE, function (eta) , TRUE, NA, sap_true_alpha ~ cv_area_200, ~1, sap_true_alpha ~ cv_area_200, ~1, sap_true_alpha ~ cv_area_200 + 0 + 1, sap_true_alpha ~ cv_area_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 71.92175|TRUE | |saplings |_200 |cv_area_200 |saplings ~ cv_area_200 |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328ba3310>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 1.943439, 0.06941032, 0.2936215, 90.60021, 0, 17, 25, 18, relative convergence (4), 1.943439, 0.06941032, 0.2936215, 1.943439, 0.06941032, 0.2936215, 0.2269956, -0.01619999, 0.0008193758, -0.01619999, 0.001358527, -6.454072e-05, 0.0008193758, -6.454072e-05, 0.08509427, TRUE, -1.330453e-05, -0.0001016569, 2.831302e-06, <environment: 0x000001c325d49f20>, glmmTMB(formula = saplings ~ cv_area_200, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 8, 20, 6, 4, 43, 8, 2, 9, 10, 6, 2, 14, 4, 27, 15, 31, 7, 7, 79, 1, 9, 11, 23, 60, 11.28316, 11.94995, 8.417493, 20.53324, 20.61253, 5.841923, 8.933692, 4.455673, 6.434522, 23.92923, 6.632544, 7.871479, 10.46547, 10.07716, 10.80861, 8.452168, 7.363355, 11.40306, 11.54297, 11.23696, 10.36638, 13.8548, 18.43625, 22.45443, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (eta) , TRUE, log, function (...) , NA_real_, saplings ~ cv_area_200, ~1, saplings ~ cv_area_200, ~1, saplings ~ cv_area_200 + 0 + 1, saplings ~ cv_area_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 187.20041|TRUE | |seed_rich |_50 |cv_area_50 |seed_rich ~ cv_area_50 |lm |4.448801, -0.2635301, -1.681028, -1.5886, 1.350195, 0.9125105, 2.464712, -2.879753, -0.6112909, 0.7499497, 3.104403, -0.322031, 0.5336418, -1.012258, -1.748934, 1.240668, 1.60091, -1.334013, -0.8841313, -1.289451, 1.979501, -0.3930939, -1.505422, -0.3232001, 1.140363, 0.4963532, -16.53406, -1.972322, 1.771344, 0.9901563, 2.815906, -2.318135, -0.2136688, 0.9268996, 3.6757, -0.1011101, 0.8427284, -0.9805693, -1.267229, 1.728725, 1.868904, -1.105772, -0.3198382, -1.088432, 2.016224, -0.1287625, -1.783346, -0.7124384, 1.07882, 0.8282184, 2, 3.681028, 3.5886, 3.649805, 3.08749, 3.535288, 3.879753, 3.611291, 3.25005, 3.895597, 3.322031, 3.466358, 3.012258, 3.748934, 3.759332, 3.39909, 3.334013, 3.884131, 3.289451, 3.020499, 3.393094, 2.505422, 2.3232, 2.859637, 3.503647, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -19.96177, 7.484239, 0.1130278, -0.1720756, 0.05496562, 0.2296152, 0.09350033, -0.08965464, 0.2376484, -0.05315919, 0.02001705, -0.2102191, 0.1632879, 0.1685595, -0.01408889, -0.04708426, 0.231835, -0.06967785, -0.2060407, -0.01712912, -0.4671936, -0.559583, -0.2876005, 0.03892301, 1.204124, 1.081996, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_rich ~ cv_area_50, 2, 2, 5, 4, 6, 1, 3, 4, 7, 3, 4, 2, 2, 5, 5, 2, 3, 2, 5, 3, 1, 2, 4, 4, 2.913416, 3.264146, 3.031894, 5.165676, 3.466445, 2.159326, 3.178042, 4.548818, 2.099203, 4.275677, 3.728008, 5.451151, 2.655736, 2.616281, 3.983265, 4.230211, 2.142713, 4.399307, 5.419879, 4.006019, 7.37441, 8.065874, 6.030292, 3.586512 | 93.45328|TRUE | |seed_true_alpha | |cv_area |seed_true_alpha ~ cv_area |lm |1.649325, 0.2243996, -0.4284054, -0.179443, 1.723056, 0.7596147, 0.3498767, -0.9678058, 0.8297651, 0.5877446, -0.4357554, -0.6582092, 0.1625503, -0.4772289, -0.5956463, -0.07025559, -0.01398857, -0.6825234, 0.281841, 0.02049615, 0.4989854, 0.1876373, -1.185236, 0.1254624, 0.1053933, 0.0620745, -9.625053, -0.6516219, 1.78578, 0.8831739, 0.417732, -0.8943026, 0.8888185, 0.6775983, -0.3843253, -0.5880643, 0.2018087, -0.4020225, -0.5151162, -0.004953275, 0.06030652, -0.6279279, 0.3201735, 0.09415555, 0.6089352, 0.2804057, -1.077969, 0.1844824, 0.1900683, 0.1126856, 2, 1.949581, 1.998412, 1.898387, 2.290155, 1.931435, 1.967806, 1.874753, 2.0731, 1.82566, 1.946179, 1.747277, 1.978774, 2.013058, 1.914994, 1.972905, 1.846045, 1.741315, 1.968812, 2.202513, 2.09187, 2.185236, 1.874538, 2.03975, 1.820386, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -6.885245, -2.903846, -0.09784056, 0.5033791, -0.04712385, 0.008692095, -0.1341102, 0.1702797, -0.2094488, -0.02449677, -0.3297379, 0.02552409, 0.07813689, -0.07235438, 0.01651825, -0.1781661, -0.3388877, 0.01023601, 0.3688812, 0.1990847, 0.3423679, -0.1344403, 0.1191001, -0.2175433, 1.204124, 1.055661, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_true_alpha ~ cv_area, 1.521175, 1.818969, 3.621443, 3.049769, 2.281312, 1, 2.704518, 2.660844, 1.389905, 1.28797, 1.909828, 1.501545, 1.417411, 1.844739, 1.958917, 1.163521, 2.023156, 1.989308, 2.701498, 2.279507, 1, 2, 2.145143, 1.88246, 1.338043, 1.555649, 1.109905, 2.855754, 1.257178, 1.419259, 1.004583, 1.888485, 0.7858117, 1.322884, 0.4365109, 1.468137, 1.620916, 1.183913, 1.441985, 0.8766519, 0.4099411, 1.423743, 2.465193, 1.97213, 2.388203, 1.003625, 1.739867, 0.7623066 | 51.50346|TRUE | |seedlings | |cv_area |seedlings ~ cv_area |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c321659b30>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 5.776432, -0.7154505, -0.3214083, 137.7482, 0, 16, 18, 17, relative convergence (4), 5.776432, -0.7154505, -0.3214083, 5.776432, -0.7154505, -0.3214083, 0.2937353, -0.1681282, 0.000104374, -0.1681282, 0.1198401, -8.424172e-05, 0.000104374, -8.424172e-05, 0.06443042, TRUE, -5.299393e-06, -5.489108e-06, -1.487094e-06, <environment: 0x000001c325c964d8>, glmmTMB(formula = seedlings ~ cv_area, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 27, 21, 15, 17, 56, 2, 11, 83, 394, 36, 311, 142, 18, 401, 177, 315, 413, 116, 265, 12, 1, 6, 113, 223, 1.338043, 1.555649, 1.109905, 2.855754, 1.257178, 1.419259, 1.004583, 1.888485, 0.7858117, 1.322884, 0.4365109, 1.468137, 1.620916, 1.183913, 1.441985, 0.8766519, 0.4099411, 1.423743, 2.465193, 1.97213, 2.388203, 1.003625, 1.739867, 0.7623066, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine$double.eps), function (eta) , TRUE, log, function (…) , NA_real_, seedlings ~ cv_area, ~1, seedlings ~ cv_area, ~1, seedlings ~ cv_area + 0 + 1, seedlings ~ cv_area, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 281.49649 TRUE
zcv_results <- compare_spatial_scales_single(data, "zcv", responses, model_families)
print(zcv_results)
# A tibble: 18 × 8
   response        scale  predictor formula         family model       AIC best 
   <chr>           <chr>  <chr>     <chr>           <chr>  <list>    <dbl> <lgl>
 1 sap_rich        "_200" zcv_200   sap_rich ~ zcv… poiss… <glmmTMB>  85.3 TRUE 
 2 sap_rich        "_50"  zcv_50    sap_rich ~ zcv… poiss… <glmmTMB>  88.5 FALSE
 3 sap_rich        ""     zcv       sap_rich ~ zcv  poiss… <glmmTMB>  92.2 FALSE
 4 sap_true_alpha  "_50"  zcv_50    sap_true_alpha… gauss… <glmmTMB>  57.8 TRUE 
 5 sap_true_alpha  "_200" zcv_200   sap_true_alpha… gauss… <glmmTMB>  62.8 FALSE
 6 sap_true_alpha  ""     zcv       sap_true_alpha… gauss… <glmmTMB>  69.8 FALSE
 7 saplings        "_200" zcv_200   saplings ~ zcv… nbino… <glmmTMB> 184.  TRUE 
 8 saplings        "_50"  zcv_50    saplings ~ zcv… nbino… <glmmTMB> 188.  FALSE
 9 saplings        ""     zcv       saplings ~ zcv  nbino… <glmmTMB> 189.  FALSE
10 seed_rich       ""     zcv       seed_rich ~ zcv lm     <lm>       95.1 TRUE 
11 seed_rich       "_200" zcv_200   seed_rich ~ zc… lm     <lm>       95.1 FALSE
12 seed_rich       "_50"  zcv_50    seed_rich ~ zc… lm     <lm>       95.1 FALSE
13 seed_true_alpha ""     zcv       seed_true_alph… lm     <lm>       51.8 TRUE 
14 seed_true_alpha "_200" zcv_200   seed_true_alph… lm     <lm>       52.5 FALSE
15 seed_true_alpha "_50"  zcv_50    seed_true_alph… lm     <lm>       52.5 FALSE
16 seedlings       ""     zcv       seedlings ~ zcv nbino… <glmmTMB> 282.  TRUE 
17 seedlings       "_50"  zcv_50    seedlings ~ zc… nbino… <glmmTMB> 285.  FALSE
18 seedlings       "_200" zcv_200   seedlings ~ zc… nbino… <glmmTMB> 285.  FALSE
zcv_results %>% filter(best)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
response scale predictor formula family model AIC best
sap_rich _200 zcv_200 sap_rich ~ zcv_200 poisson 0, 0, function (x = last.par[lfixed()], …) , {, if (tracepar) {, cat(“par:”), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], ...) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat(“outer mgc:”, max(abs(ans)), “”), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = “ADGrad”, order = 1)), }, else {, stop(“Hessian not yet implemented for models with random effects.”), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, “reportdims”), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat(“Some ‘random’ effect names does not match ‘parameter’ list:”), print(setdiff(random, names(parameters))), cat(“(Note that regular expression match is disabled by default)”), stop(), }, if (any(duplicated(random))) {, cat(“Duplicates in ‘random’ - will be removed”), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat(“Selected random effects did not match any model parameters.”), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop(“Profile parameter vector not unique.”), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if (“ADFun” %in% type) {, if (omp\(autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\)autopar) , openmp(omp\(nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) &#124;&#124; names(I) == "") {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop("Names to be 'integrate'd must be among the random parameters"), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\)which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\(method), ok <- all(duplicated(method)[-1]), if (!ok) , stop("Grouping only allowed for identical methods"), method <- method[1], cfg <- NULL, if (method == "marginal_sr") {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\)activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random_remove, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\(ptr, , "par"))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, "ADGrad"), }, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\(sparse)) , cfg\)sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = “laplace”, config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\)ptr, “par”), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\(autopar && !ADreport) {, TransformADFunObject(ADFun, method = "parallel_accumulate", , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = "reorder_random", , random_order = random, mustWork = 0L), }, if ("Fun" %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if ("ADGrad" %in% type) {, retape_adgrad(lazy = TRUE), }, env\)skipFixedEffects <- !is.null(ADGrad), delayedAssign(“spHess”, sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328ba9268>, function (par = last.par) , {, f(par, order = 0, type = “double”), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = “double”, do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, -0.3992688, 1.797758, 40.62684, 0, 8, 9, 9, relative convergence (4), -0.3992688, 1.797758, -0.3992688, 1.797758, 0.2575689, -0.2906317, -0.2906317, 0.3477959, TRUE, 1.682088e-08, 4.777455e-08, <environment: 0x000001c324f71b68>, glmmTMB(formula = sap_rich ~ zcv_200, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 3, 3, 3, 2, 3, 4, 1, 2, 3, 1, 1, 1, 3, 4, 4, 1, 1, 3, 6, 1, 3, 2, 7, 6, 0.7066087, 0.7141342, 0.731103, 0.7166846, 0.703, 0.5711732, 0.6780893, 0.5916551, 0.5955566, 0.9561933, 0.5377227, 0.6141156, 0.8644572, 0.7918702, 0.6870297, 0.6375526, 0.7787891, 0.9341669, 1.021309, 0.7222347, 0.8304642, 0.7632949, 1.070972, 1.282005, 24, 1, poisson, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (mu) , mu, function (y, mu, wt) , {, r <- mu * wt, p <- which(y > 0), r[p] <- (wt * (y * log(y/mu) - (y - mu)))[p], 2 * r, }, function (y, n, mu, wt, dev) , -2 * sum(dpois(y, mu, log = TRUE) * wt), function (eta) , pmax(exp(eta), .Machine\)double.eps), {, if (any(y < 0)) , stop(“negative values not allowed for the ‘Poisson’ family”), n <- rep.int(1, nobs), mustart <- y + 0.1, }, function (mu) , all(is.finite(mu)) && all(mu > 0), function (eta) , TRUE, function (object, nsim) , {, wts <- object\(prior.weights, if (any(wts != 1)) , warning("ignoring prior weights"), ftd <- fitted(object), rpois(nsim * length(ftd), ftd), }, 1, sap_rich ~ zcv_200, sap_rich ~ zcv_200, sap_rich ~ zcv_200 + 0 + 1, sap_rich ~ zcv_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 85.25368|TRUE | |sap_true_alpha |_50 |zcv_50 |sap_true_alpha ~ zcv_50 |gaussian |1, 1, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328b5e968>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, -0.4690088, 3.507023, -0.3400662, 25.89294, 0, 13, 18, 14, relative convergence (4), -0.4690088, 3.507023, -0.3400662, -0.4690088, 3.507023, -0.3400662, 0.3140347, -0.4066853, -5.678544e-09, -0.4066853, 0.5646189, 5.323358e-09, -5.678544e-09, 5.323358e-09, 0.02083332, TRUE, -2.097072e-06, -1.284207e-06, -4.587192e-07, <environment: 0x000001c325c968c8>, glmmTMB(formula = sap_true_alpha ~ zcv_50, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 2.951152, 1.48349, 2.749459, 1.754765, 1.346414, 2.925727, 1, 1.417411, 1.894646, 1, 1, 1, 2.828427, 3.036305, 2.773297, 1, 1, 2.729839, 2.105104, 1, 2.552028, 1.796702, 3.147043, 4.877137, 0.7671353, 0.6445872, 0.7869298, 0.7407641, 0.7378613, 0.518113, 0.567858, 0.5914529, 0.4884729, 0.9676544, 0.4278447, 0.5383332, 0.8023014, 0.7806947, 0.7561543, 0.5298973, 0.5827258, 0.890711, 0.6799489, 0.7234894, 0.8341767, 0.688613, 0.8579411, 1.383126, 24, 1, gaussian, identity, function (mu) , mu, function (eta) , eta, function (mu) , rep.int(1, length(mu)), function (y, mu, wt) , wt * ((y - mu)^2), function (y, n, mu, wt, dev) , {, nobs <- length(y), nobs * (log(dev/nobs * 2 * pi) + 1) + 2 - sum(log(wt)), }, function (eta) , rep.int(1, length(eta)), {, n <- rep.int(1, nobs), if (is.null(etastart) && is.null(start) && is.null(mustart) && , ((family\)link == “inverse” && any(y == 0)) || (family\(link == , "log" && any(y <= 0)))) , stop("cannot find valid starting values: please specify some"), mustart <- y, }, function (mu) , TRUE, function (eta) , TRUE, NA, sap_true_alpha ~ zcv_50, ~1, sap_true_alpha ~ zcv_50, ~1, sap_true_alpha ~ zcv_50 + 0 + 1, sap_true_alpha ~ zcv_50, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 57.78587|TRUE | |saplings |_200 |zcv_200 |saplings ~ zcv_200 |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328b95460>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 0.7930435, 2.483234, 0.4439409, 88.80942, 0, 10, 12, 11, relative convergence (4), 0.7930435, 2.483234, 0.4439409, 0.7930435, 2.483234, 0.4439409, 0.5579862, -0.6796602, -0.0008358369, -0.6796602, 0.8744328, 0.001016489, -0.0008358369, 0.001016489, 0.09187589, TRUE, -2.278842e-05, -1.446949e-05, -9.820199e-06, <environment: 0x000001c325a1dd90>, glmmTMB(formula = saplings ~ zcv_200, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 8, 20, 6, 4, 43, 8, 2, 9, 10, 6, 2, 14, 4, 27, 15, 31, 7, 7, 79, 1, 9, 11, 23, 60, 0.7066087, 0.7141342, 0.731103, 0.7166846, 0.703, 0.5711732, 0.6780893, 0.5916551, 0.5955566, 0.9561933, 0.5377227, 0.6141156, 0.8644572, 0.7918702, 0.6870297, 0.6375526, 0.7787891, 0.9341669, 1.021309, 0.7222347, 0.8304642, 0.7632949, 1.070972, 1.282005, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (eta) , TRUE, log, function (...) , NA_real_, saplings ~ zcv_200, ~1, saplings ~ zcv_200, ~1, saplings ~ zcv_200 + 0 + 1, saplings ~ zcv_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 183.61884|TRUE | |seed_rich | |zcv |seed_rich ~ zcv |lm |3.570197, -0.2770779, -1.28163, -1.354764, 1.63969, 0.6211622, 2.652585, -2.449317, -0.3784205, 0.5895487, 3.555481, -0.3660438, 0.538095, -1.459691, -1.314581, 1.635871, 1.634355, -1.44352, -0.4196645, -1.374598, 1.665971, -0.2629852, -2.34056, -1.377118, 0.5919899, 0.698143, -16.53406, 0.262585, 1.852105, 0.7546397, 2.919937, -2.616109, -0.2431652, 0.5883398, 3.40913, -0.1780585, 0.317672, -1.670681, -0.9073433, 1.832013, 1.824039, -1.585616, -0.4601255, -1.223056, 1.990354, 0.3640721, -2.044005, -1.236316, 0.6011815, 1.15959, 2, 3.28163, 3.354764, 3.36031, 3.378838, 3.347415, 3.449317, 3.378421, 3.410451, 3.444519, 3.366044, 3.461905, 3.459691, 3.314581, 3.364129, 3.365645, 3.44352, 3.419664, 3.374598, 3.334029, 3.262985, 3.34056, 3.377118, 3.40801, 3.301857, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -3.451249, -0.9476939, -0.004333477, -0.07489394, 0.04477332, -0.3432984, -0.07330483, -0.1952872, -0.3250266, -0.02617067, -0.3912379, -0.3828058, 0.1698143, -0.0188791, -0.02465223, -0.3212227, -0.2303738, -0.05874636, 0.09575215, 0.3663063, 0.07087743, -0.06834605, -0.1859904, 0.2182712, 1.204124, 1.016787, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_rich ~ zcv, 2, 2, 5, 4, 6, 1, 3, 4, 7, 3, 4, 2, 2, 5, 5, 2, 3, 2, 5, 3, 1, 2, 4, 4, 1.041466, 0.7775179, 0.7575021, 0.6906324, 0.8040403, 0.4362671, 0.6921384, 0.5765365, 0.4535832, 0.7368071, 0.3908352, 0.3988262, 0.9225409, 0.7437173, 0.7382462, 0.4571882, 0.5432851, 0.7059354, 0.8523527, 1.108755, 0.828779, 0.6968378, 0.585347, 0.9684632 | 95.10198|TRUE | |seed_true_alpha | |zcv |seed_true_alpha ~ zcv |lm |1.554293, 0.5825721, -0.639847, -0.1882852, 1.62585, 1.093133, 0.2586067, -0.8084505, 0.7470038, 0.7706768, -0.4286334, -0.6955667, 0.1278446, -0.2850935, -0.6743289, -0.1428239, -0.02545818, -0.6571171, 0.1523599, 0.02375626, 0.650648, 0.07928379, -1.037117, 0.03974828, 0.249843, -0.2360328, -9.625053, -0.5521, 1.733977, 1.195721, 0.3705888, -0.726932, 0.8497168, 0.8638142, -0.3456805, -0.5891537, 0.2055998, -0.2066763, -0.552531, -0.03583849, 0.08107403, -0.5738656, 0.242743, 0.1276121, 0.766632, 0.2165062, -0.9230857, 0.1428505, 0.3437102, -0.1104311, 2, 2.161023, 2.007254, 1.995593, 1.956637, 2.022705, 1.80845, 1.957514, 1.890168, 1.818538, 1.983537, 1.781983, 1.786638, 2.09174, 1.987562, 1.984375, 1.820639, 1.870796, 1.965552, 2.05085, 2.200223, 2.037117, 1.960252, 1.8953, 2.118493, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -3.451249, -0.9476939, -0.004333477, -0.07489394, 0.04477332, -0.3432984, -0.07330483, -0.1952872, -0.3250266, -0.02617067, -0.3912379, -0.3828058, 0.1698143, -0.0188791, -0.02465223, -0.3212227, -0.2303738, -0.05874636, 0.09575215, 0.3663063, 0.07087743, -0.06834605, -0.1859904, 0.2182712, 1.204124, 1.016787, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_true_alpha ~ zcv, 1.521175, 1.818969, 3.621443, 3.049769, 2.281312, 1, 2.704518, 2.660844, 1.389905, 1.28797, 1.909828, 1.501545, 1.417411, 1.844739, 1.958917, 1.163521, 2.023156, 1.989308, 2.701498, 2.279507, 1, 2, 2.145143, 1.88246, 1.041466, 0.7775179, 0.7575021, 0.6906324, 0.8040403, 0.4362671, 0.6921384, 0.5765365, 0.4535832, 0.7368071, 0.3908352, 0.3988262, 0.9225409, 0.7437173, 0.7382462, 0.4571882, 0.5432851, 0.7059354, 0.8523527, 1.108755, 0.828779, 0.6968378, 0.585347, 0.9684632 | 51.80877|TRUE | |seedlings | |zcv |seedlings ~ zcv |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c321f143c0>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 6.388152, -2.268543, -0.342389, 138.0739, 0, 17, 18, 18, relative convergence (4), 6.388152, -2.268543, -0.342389, 6.388152, -2.268543, -0.342389, 0.844305, -1.115465, -3.610448e-05, -1.115465, 1.584553, 5.322826e-05, -3.610448e-05, 5.322826e-05, 0.06402857, TRUE, 1.457472e-06, 1.026392e-06, 2.691141e-07, <environment: 0x000001c325d4dac0>, glmmTMB(formula = seedlings ~ zcv, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 27, 21, 15, 17, 56, 2, 11, 83, 394, 36, 311, 142, 18, 401, 177, 315, 413, 116, 265, 12, 1, 6, 113, 223, 1.041466, 0.7775179, 0.7575021, 0.6906324, 0.8040403, 0.4362671, 0.6921384, 0.5765365, 0.4535832, 0.7368071, 0.3908352, 0.3988262, 0.9225409, 0.7437173, 0.7382462, 0.4571882, 0.5432851, 0.7059354, 0.8523527, 1.108755, 0.828779, 0.6968378, 0.585347, 0.9684632, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine$double.eps), function (eta) , TRUE, log, function (…) , NA_real_, seedlings ~ zcv, ~1, seedlings ~ zcv, ~1, seedlings ~ zcv + 0 + 1, seedlings ~ zcv, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 282.14784 TRUE
zmean_results <- compare_spatial_scales_single(data, "zmean", responses, model_families)
print(zmean_results)
# A tibble: 18 × 8
   response        scale  predictor formula         family model       AIC best 
   <chr>           <chr>  <chr>     <chr>           <chr>  <list>    <dbl> <lgl>
 1 sap_rich        "_200" zmean_200 sap_rich ~ zme… poiss… <glmmTMB>  86.5 TRUE 
 2 sap_rich        "_50"  zmean_50  sap_rich ~ zme… poiss… <glmmTMB>  91.1 FALSE
 3 sap_rich        ""     zmean     sap_rich ~ zme… poiss… <glmmTMB>  93.0 FALSE
 4 sap_true_alpha  "_50"  zmean_50  sap_true_alpha… gauss… <glmmTMB>  62.0 TRUE 
 5 sap_true_alpha  "_200" zmean_200 sap_true_alpha… gauss… <glmmTMB>  66.4 FALSE
 6 sap_true_alpha  ""     zmean     sap_true_alpha… gauss… <glmmTMB>  67.7 FALSE
 7 saplings        "_200" zmean_200 saplings ~ zme… nbino… <glmmTMB> 182.  TRUE 
 8 saplings        "_50"  zmean_50  saplings ~ zme… nbino… <glmmTMB> 191.  FALSE
 9 saplings        ""     zmean     saplings ~ zme… nbino… <glmmTMB> 191.  FALSE
10 seed_rich       "_200" zmean_200 seed_rich ~ zm… lm     <lm>       94.2 TRUE 
11 seed_rich       "_50"  zmean_50  seed_rich ~ zm… lm     <lm>       94.8 FALSE
12 seed_rich       ""     zmean     seed_rich ~ zm… lm     <lm>       94.9 FALSE
13 seed_true_alpha ""     zmean     seed_true_alph… lm     <lm>       52.0 TRUE 
14 seed_true_alpha "_50"  zmean_50  seed_true_alph… lm     <lm>       52.0 FALSE
15 seed_true_alpha "_200" zmean_200 seed_true_alph… lm     <lm>       52.6 FALSE
16 seedlings       ""     zmean     seedlings ~ zm… nbino… <glmmTMB> 281.  TRUE 
17 seedlings       "_50"  zmean_50  seedlings ~ zm… nbino… <glmmTMB> 284.  FALSE
18 seedlings       "_200" zmean_200 seedlings ~ zm… nbino… <glmmTMB> 285.  FALSE
zmean_results %>% filter(best)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
response scale predictor formula family model AIC best
sap_rich _200 zmean_200 sap_rich ~ zmean_200 poisson 0, 0, function (x = last.par[lfixed()], …) , {, if (tracepar) {, cat(“par:”), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], ...) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat(“outer mgc:”, max(abs(ans)), “”), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = “ADGrad”, order = 1)), }, else {, stop(“Hessian not yet implemented for models with random effects.”), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, “reportdims”), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat(“Some ‘random’ effect names does not match ‘parameter’ list:”), print(setdiff(random, names(parameters))), cat(“(Note that regular expression match is disabled by default)”), stop(), }, if (any(duplicated(random))) {, cat(“Duplicates in ‘random’ - will be removed”), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat(“Selected random effects did not match any model parameters.”), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop(“Profile parameter vector not unique.”), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if (“ADFun” %in% type) {, if (omp\(autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\)autopar) , openmp(omp\(nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) &#124;&#124; names(I) == "") {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop("Names to be 'integrate'd must be among the random parameters"), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\)which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\(method), ok <- all(duplicated(method)[-1]), if (!ok) , stop("Grouping only allowed for identical methods"), method <- method[1], cfg <- NULL, if (method == "marginal_sr") {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\)activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random_remove, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\(ptr, , "par"))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, "ADGrad"), }, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\(sparse)) , cfg\)sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = “laplace”, config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = “remove_random_parameters”, , random_order = random, mustWork = 1L), attr(ADFun\(ptr, "par") <- attr(ADFun\)ptr, “par”)[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\)ptr, “par”), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\(autopar && !ADreport) {, TransformADFunObject(ADFun, method = "parallel_accumulate", , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = "reorder_random", , random_order = random, mustWork = 0L), }, if ("Fun" %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if ("ADGrad" %in% type) {, retape_adgrad(lazy = TRUE), }, env\)skipFixedEffects <- !is.null(ADGrad), delayedAssign(“spHess”, sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328a752e0>, function (par = last.par) , {, f(par, order = 0, type = “double”), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = “double”, do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 2.41158, -0.1636235, 41.2491, 0, 8, 12, 9, relative convergence (4), 2.41158, -0.1636235, 2.41158, -0.1636235, 0.2532097, -0.02961517, -0.02961517, 0.003677334, TRUE, 1.970869e-06, 1.610358e-05, <environment: 0x000001c325951d28>, glmmTMB(formula = sap_rich ~ zmean_200, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 3, 3, 3, 2, 3, 4, 1, 2, 3, 1, 1, 1, 3, 4, 4, 1, 1, 3, 6, 1, 3, 2, 7, 6, 7.976485, 7.527942, 7.863946, 7.833888, 7.929396, 9.484697, 10.07398, 9.909541, 9.891049, 4.851119, 11.25073, 9.769828, 8.931003, 8.14094, 8.194641, 9.408753, 8.519331, 8.439339, 7.772781, 12.93989, 9.80478, 11.82736, 5.766593, 4.383184, 24, 1, poisson, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (mu) , mu, function (y, mu, wt) , {, r <- mu * wt, p <- which(y > 0), r[p] <- (wt * (y * log(y/mu) - (y - mu)))[p], 2 * r, }, function (y, n, mu, wt, dev) , -2 * sum(dpois(y, mu, log = TRUE) * wt), function (eta) , pmax(exp(eta), .Machine\)double.eps), {, if (any(y < 0)) , stop(“negative values not allowed for the ‘Poisson’ family”), n <- rep.int(1, nobs), mustart <- y + 0.1, }, function (mu) , all(is.finite(mu)) && all(mu > 0), function (eta) , TRUE, function (object, nsim) , {, wts <- object\(prior.weights, if (any(wts != 1)) , warning("ignoring prior weights"), ftd <- fitted(object), rpois(nsim * length(ftd), ftd), }, 1, sap_rich ~ zmean_200, sap_rich ~ zmean_200, sap_rich ~ zmean_200 + 0 + 1, sap_rich ~ zmean_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 86.49819|TRUE | |sap_true_alpha |_50 |zmean_50 |sap_true_alpha ~ zmean_50 |gaussian |1, 1, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328acf1c0>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 3.914067, -0.2080092, -0.2518145, 28.01098, 0, 17, 21, 18, relative convergence (4), 3.914067, -0.2080092, -0.2518145, 3.914067, -0.2080092, -0.2518145, 0.2650748, -0.02687102, 1.213089e-09, -0.02687102, 0.003009875, -1.56256e-10, 1.213089e-09, -1.56256e-10, 0.02083332, TRUE, -1.733756e-07, -2.793782e-06, -9.182243e-08, <environment: 0x000001c325d49bd8>, glmmTMB(formula = sap_true_alpha ~ zmean_50, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 2.951152, 1.48349, 2.749459, 1.754765, 1.346414, 2.925727, 1, 1.417411, 1.894646, 1, 1, 1, 2.828427, 3.036305, 2.773297, 1, 1, 2.729839, 2.105104, 1, 2.552028, 1.796702, 3.147043, 4.877137, 7.734654, 8.112863, 8.19836, 6.454479, 8.503838, 9.137033, 9.925839, 9.253438, 11.45561, 5.211221, 12.57806, 11.69396, 7.78207, 7.851172, 7.28093, 10.81797, 12.08129, 5.957389, 13.35016, 13.69688, 3.857767, 12.22543, 8.739816, 2.362655, 24, 1, gaussian, identity, function (mu) , mu, function (eta) , eta, function (mu) , rep.int(1, length(mu)), function (y, mu, wt) , wt * ((y - mu)^2), function (y, n, mu, wt, dev) , {, nobs <- length(y), nobs * (log(dev/nobs * 2 * pi) + 1) + 2 - sum(log(wt)), }, function (eta) , rep.int(1, length(eta)), {, n <- rep.int(1, nobs), if (is.null(etastart) && is.null(start) && is.null(mustart) && , ((family\)link == “inverse” && any(y == 0)) || (family\(link == , "log" && any(y <= 0)))) , stop("cannot find valid starting values: please specify some"), mustart <- y, }, function (mu) , TRUE, function (eta) , TRUE, NA, sap_true_alpha ~ zmean_50, ~1, sap_true_alpha ~ zmean_50, ~1, sap_true_alpha ~ zmean_50 + 0 + 1, sap_true_alpha ~ zmean_50, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 62.02195|TRUE | |saplings |_200 |zmean_200 |saplings ~ zmean_200 |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c328ac0bd8>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 5.307858, -0.3017066, 0.4798479, 88.16816, 0, 13, 22, 14, relative convergence (4), 5.307858, -0.3017066, 0.4798479, 5.307858, -0.3017066, 0.4798479, 0.7627869, -0.08540613, -0.006045195, -0.08540613, 0.009939555, 0.000731196, -0.006045195, 0.000731196, 0.09066947, TRUE, 8.016957e-06, 2.682397e-05, -6.865563e-06, <environment: 0x000001c325d5cc40>, glmmTMB(formula = saplings ~ zmean_200, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 8, 20, 6, 4, 43, 8, 2, 9, 10, 6, 2, 14, 4, 27, 15, 31, 7, 7, 79, 1, 9, 11, 23, 60, 7.976485, 7.527942, 7.863946, 7.833888, 7.929396, 9.484697, 10.07398, 9.909541, 9.891049, 4.851119, 11.25073, 9.769828, 8.931003, 8.14094, 8.194641, 9.408753, 8.519331, 8.439339, 7.772781, 12.93989, 9.80478, 11.82736, 5.766593, 4.383184, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine\(double.eps), function (eta) , TRUE, log, function (...) , NA_real_, saplings ~ zmean_200, ~1, saplings ~ zmean_200, ~1, saplings ~ zmean_200 + 0 + 1, saplings ~ zmean_200, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 | 182.33632|TRUE | |seed_rich |_200 |zmean_200 |seed_rich ~ zmean_200 |lm |4.744591, -0.1576574, -1.487039, -1.557755, 1.495218, 0.4904796, 2.505537, -2.249258, -0.1563529, 0.8177217, 3.814806, -0.9797759, 1.02917, -1.204305, -1.336552, 1.538889, 1.547355, -1.261231, -0.4014552, -1.414067, 1.480846, 0.2954787, -2.198795, -0.8799199, 0.1645553, -0.05354935, -16.53406, -1.496395, 1.834435, 0.8334231, 2.836638, -2.111004, -0.09116665, 0.9032976, 3.902675, -0.2669884, 0.9484471, -1.101406, -1.129644, 1.84376, 1.845567, -1.113561, -0.1435022, -1.146195, 1.831366, 0.005310904, -2.100229, -1.032141, 0.76383, 0.7172591, 2, 3.487039, 3.557755, 3.504782, 3.50952, 3.494463, 3.249258, 3.156353, 3.182278, 3.185194, 3.979776, 2.97083, 3.204305, 3.336552, 3.461111, 3.452645, 3.261231, 3.401455, 3.414067, 3.519154, 2.704521, 3.198795, 2.87992, 3.835445, 4.053549, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -42.55809, 9.491434, 0.07403705, 0.07720386, 0.06714129, -0.09672226, -0.1588084, -0.1414831, -0.1395348, 0.3914629, -0.2827881, -0.1267632, -0.0383861, 0.0448535, 0.03919567, -0.08872099, 0.004986851, 0.01341471, 0.08364198, -0.4607552, -0.1304457, -0.3435411, 0.2950103, 0.4407637, 1.204124, 1.109438, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_rich ~ zmean_200, 2, 2, 5, 4, 6, 1, 3, 4, 7, 3, 4, 2, 2, 5, 5, 2, 3, 2, 5, 3, 1, 2, 4, 4, 7.976485, 7.527942, 7.863946, 7.833888, 7.929396, 9.484697, 10.07398, 9.909541, 9.891049, 4.851119, 11.25073, 9.769828, 8.931003, 8.14094, 8.194641, 9.408753, 8.519331, 8.439339, 7.772781, 12.93989, 9.80478, 11.82736, 5.766593, 4.383184 | 94.17952|TRUE | |seed_true_alpha | |zmean |seed_true_alpha ~ zmean |lm |2.238156, -0.03210284, -0.5077357, -0.1614667, 1.602329, 0.9390319, 0.2955989, -0.9341777, 0.692414, 0.6885476, -0.4508039, -0.7859433, 0.08073291, -0.2827653, -0.5955574, -0.1267516, -0.02755093, -0.6833269, 0.1821156, -0.07374362, 0.813946, 0.3048147, -1.101371, 0.1164518, 0.2759621, -0.26075, -9.625053, -0.4738952, 1.695255, 1.046388, 0.3832649, -0.8546278, 0.7842362, 0.7741008, -0.385974, -0.6843869, 0.1437338, -0.2268174, -0.503599, -0.04132551, 0.06023397, -0.6175301, 0.2469977, 0.02610215, 0.8861531, 0.3907451, -0.9954907, 0.1880283, 0.345276, -0.1482803, 2, 2.028911, 1.980435, 2.019114, 2.110737, 1.985713, 1.934178, 2.012104, 1.972297, 1.840709, 2.073913, 1.829095, 1.78431, 2.012969, 1.97149, 1.986468, 1.846848, 1.841041, 2.063052, 1.887552, 1.974692, 2.101371, 1.883548, 1.869181, 2.14321, 0, 1, -4.898979, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, 0.2041241, -41.7293, 14.76178, 0.09184423, 0.2851844, 0.02136062, -0.08738695, 0.07705051, -0.006949277, -0.2846221, 0.207479, -0.3091299, -0.4036328, 0.07887529, -0.008651252, 0.02295418, -0.2716669, -0.2839224, 0.184559, -0.1857745, -0.001894098, 0.2654202, -0.1942238, -0.2245408, 0.3537077, 1.204124, 1.010224, 1, 2, 1e-07, 2, 22, lm(formula = as.formula(f), data = data), seed_true_alpha ~ zmean, 1.521175, 1.818969, 3.621443, 3.049769, 2.281312, 1, 2.704518, 2.660844, 1.389905, 1.28797, 1.909828, 1.501545, 1.417411, 1.844739, 1.958917, 1.163521, 2.023156, 1.989308, 2.701498, 2.279507, 1, 2, 2.145143, 1.88246, 6.517968, 8.027988, 6.823133, 3.969087, 7.863597, 9.468905, 7.041515, 8.281502, 12.38045, 5.116158, 12.74223, 14.13726, 7.014578, 8.306626, 7.840073, 12.18921, 12.37012, 5.454497, 10.92128, 8.206878, 4.260843, 11.04601, 11.49354, 2.957561 | 52.01111|TRUE | |seedlings | |zmean |seedlings ~ zmean |nbinom2 |0, 0, 0, function (x = last.par[lfixed()], ...) , {, if (tracepar) {, cat("par:\n"), print(x), }, if (!validpar(x)) , return(NaN), if (is.null(random)) {, ans <- f(x, order = 0), if (!ADreport) {, if (is.finite(ans) && ans < value.best) {, last.par.best <<- x, value.best <<- ans, }, }, }, else {, ans <- try({, if (MCcontrol\)doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\(n, seed = MCcontrol\)seed, , order = 0), }, else ff(x, order = 0), }, silent = silent), if (is.character(ans)) , ans <- NaN, }, ans, }, function (x = last.par[lfixed()], …) , {, if (is.null(random)) {, ans <- f(x, order = 1), }, else {, ans <- try({, if (MCcontrol\(doMC) {, ff(x, order = 0), MC(last.par, n = MCcontrol\)n, seed = MCcontrol\(seed, , order = 1), }, else ff(x, order = 1), }, silent = silent), if (is.character(ans)) , ans <- rep(NaN, length(x)), }, if (tracemgc) , cat("outer mgc: ", max(abs(ans)), "\n"), ans, }, function (x = last.par[lfixed()], atomic = usingAtomics()) , {, if (is.null(random)) {, if (!atomic) , return(f(x, order = 2)), if (is.null(ADGrad)) , retape_adgrad(), return(f(x, type = "ADGrad", order = 1)), }, else {, stop("Hessian not yet implemented for models with random effects."), }, }, FALSE, BFGS, function (set.defaults = TRUE) , {, omp <- config(DLL = DLL), random <<- .random, if (atomic) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), out <- EvalDoubleFunObject(Fun, unlist(parameters), get_reportdims = TRUE), ADreportDims <<- attr(out, "reportdims"), }, if (is.character(profile)) {, random <<- c(random, profile), }, if (is.character(random)) {, if (!regexp) {, if (!all(random %in% names(parameters))) {, cat("Some 'random' effect names does not match 'parameter' list:\n"), print(setdiff(random, names(parameters))), cat("(Note that regular expression match is disabled by default)\n"), stop(), }, if (any(duplicated(random))) {, cat("Duplicates in 'random' - will be removed\n"), random <<- unique(random), }, tmp <- lapply(parameters, function(x) x * 0), tmp[random] <- lapply(tmp[random], function(x) x * , 0 + 1), random <<- which(as.logical(unlist(tmp))), if (length(random) == 0) , random <<- NULL, }, if (regexp) {, random <<- grepRandomParameters(parameters, random), if (length(random) == 0) {, cat("Selected random effects did not match any model parameters.\n"), random <<- NULL, }, }, if (is.character(profile)) {, tmp <- lapply(parameters, function(x) x * 0), tmp[profile] <- lapply(tmp[profile], function(x) x * , 0 + 1), profile <<- match(which(as.logical(unlist(tmp))), , random), if (length(profile) == 0) , random <<- NULL, if (any(duplicated(profile))) , stop("Profile parameter vector not unique."), tmp <- rep(0L, length(random)), tmp[profile] <- 1L, profile <<- tmp, }, if (set.defaults) {, par <<- unlist(parameters), }, }, if ("ADFun" %in% type) {, if (omp\)autopar) , openmp(1, DLL = DLL), ADFun <<- MakeADFunObject(data, parameters, reportenv, , ADreport = ADreport, DLL = DLL), if (omp\(autopar) , openmp(omp\)nthreads, DLL = DLL), if (!is.null(integrate)) {, nm <- sapply(parameters, length), nmpar <- rep(names(nm), nm), for (i in seq_along(integrate)) {, I <- integrate[i], if (is.null(names(I)) || names(I) == ““) {, I <- I[[1]], }, ok <- all(names(I) %in% nmpar[random]), if (!ok) , stop(”Names to be ’integrate’d must be among the random parameters”), w <- which(nmpar[random] %in% names(I)), arg_which <- I[[1]]\(which, if (!is.null(arg_which)) , w <- w[arg_which], method <- sapply(I, function(x) x\)method), ok <- all(duplicated(method)[-1]), if (!ok) , stop(“Grouping only allowed for identical methods”), method <- method[1], cfg <- NULL, if (method == “marginal_sr”) {, fac <- factor(nmpar[random[w]], levels = names(I)), cfg <- list(grid = I, random2grid = fac), }, else {, cfg <- I[[1]], }, stopifnot(is.list(cfg)), TransformADFunObject(ADFun, method = method, , random_order = random[w], config = cfg, mustWork = 1L), activeDomain <- as.logical(info(ADFun)\(activeDomain), random_remove <- random[w][!activeDomain[random[w]]], TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random_remove, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random_remove], par_mask <- rep(FALSE, length(attr(ADFun\)ptr, , “par”))), par_mask[random] <- TRUE, par <<- par[-random_remove], nmpar <- nmpar[-random_remove], par_mask <- par_mask[-random_remove], random <<- which(par_mask), if (length(random) == 0) {, random <<- NULL, type <<- setdiff(type, “ADGrad”), }, if (config(DLL = DLL)\(optimize.instantly) {, TransformADFunObject(ADFun, method = "optimize", , mustWork = 1L), }, }, }, if (intern) {, cfg <- inner.control, if (is.null(cfg\)sparse)) , cfg\(sparse <- TRUE, cfg <- lapply(cfg, as.double), TransformADFunObject(ADFun, method = "laplace", config = cfg, , random_order = random, mustWork = 1L), TransformADFunObject(ADFun, method = "remove_random_parameters", , random_order = random, mustWork = 1L), attr(ADFun\)ptr, “par”) <- attr(ADFun\(ptr, "par")[-random], par <<- par[-random], random <<- NULL, if (config(DLL = DLL)\)optimize.instantly) {, TransformADFunObject(ADFun, method = “optimize”, , mustWork = 1L), }, }, if (set.defaults) {, par <<- attr(ADFun\(ptr, "par"), last.par <<- par, last.par1 <<- par, last.par2 <<- par, last.par.best <<- par, value.best <<- Inf, }, }, if (omp\)autopar && !ADreport) {, TransformADFunObject(ADFun, method = “parallel_accumulate”, , num_threads = as.integer(openmp(DLL = DLL)), mustWork = 0L), }, if (length(random) > 0) {, TransformADFunObject(ADFun, method = “reorder_random”, , random_order = random, mustWork = 0L), }, if (“Fun” %in% type) {, Fun <<- MakeDoubleFunObject(data, parameters, reportenv, , DLL = DLL), }, if (“ADGrad” %in% type) {, retape_adgrad(lazy = TRUE), }, env\(skipFixedEffects <- !is.null(ADGrad), delayedAssign("spHess", sparseHessianFun(env, skipFixedEffects = skipFixedEffects), , assign.env = env), }, <environment: 0x000001c321e68a80>, function (par = last.par) , {, f(par, order = 0, type = "double"), as.list(reportenv), }, function (par = last.par, complete = FALSE) , {, f(par, order = 0, type = "double", do_simulate = TRUE), sim <- as.list(reportenv), if (complete) {, ans <- data, ans[names(sim)] <- sim, }, else {, ans <- sim, }, ans, }, 3.506207, 0.1471022, -0.3082785, 137.5448, 0, 19, 20, 20, relative convergence (4), 3.506207, 0.1471022, -0.3082785, 3.506207, 0.1471022, -0.3082785, 0.3901098, -0.03903118, -0.000222097, -0.03903118, 0.004575025, 2.386008e-05, -0.000222097, 2.386008e-05, 0.06468069, TRUE, -2.698117e-05, -0.0003226375, -6.976765e-06, <environment: 0x000001c325d49890>, glmmTMB(formula = seedlings ~ zmean, data = data, family = fam, , ziformula = ~0, dispformula = ~1), 27, 21, 15, 17, 56, 2, 11, 83, 394, 36, 311, 142, 18, 401, 177, 315, 413, 116, 265, 12, 1, 6, 113, 223, 6.517968, 8.027988, 6.823133, 3.969087, 7.863597, 9.468905, 7.041515, 8.281502, 12.38045, 5.116158, 12.74223, 14.13726, 7.014578, 8.306626, 7.840073, 12.18921, 12.37012, 5.454497, 10.92128, 8.206878, 4.260843, 11.04601, 11.49354, 2.957561, 24, 1, nbinom2, function (mu, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(mu * (1 + mu/theta)), }, {, if (any(y < 0)) , stop("negative values not allowed for the negative binomial family"), n <- rep(1, nobs), mustart <- y + (y == 0)/6, }, function (y, mu, wt, theta = NULL) , {, get_nbinom_disp(theta, ".Theta", "theta"), return(2 * wt * (y * log(pmax(1, y)/mu) - (y + theta) * log((y + , theta)/(mu + theta)))), }, log, function (mu) , log(mu), function (eta) , pmax(exp(eta), .Machine\)double.eps), function (eta) , pmax(exp(eta), .Machine$double.eps), function (eta) , TRUE, log, function (…) , NA_real_, seedlings ~ zmean, ~1, seedlings ~ zmean, ~1, seedlings ~ zmean + 0 + 1, seedlings ~ zmean, ~0, ~1, FALSE, FALSE, FALSE, FALSE, 1, FALSE, 1, 1, 11 281.08968 TRUE
all_results <- bind_rows(
  mutate(cc_results, predictor = "canopy_cover_perc"),
  mutate(cv_area_results, predictor = "cv_area"),
  mutate(zcv_results, predictor = "zcv"),
  mutate(zmean_results, predictor = "zmean")
)
all_results <- all_results %>%
  mutate(scale_numeric = case_when(
    scale == "" ~ 0,
    scale == "_50" ~ 50,
    scale == "_200" ~ 200
  ))
all_results$predictor <- dplyr::recode(all_results$predictor,
                                canopy_cover_perc = "Canopy Cover (%)",
                                cv_area = "Canopy Gap CV",
                                zcv = "Canopy Height CV",
                                zmean = "Mean Canopy Height")
all_results$response <- dplyr::recode(all_results$response,
                                       sap_rich = "Sapling Richness ",
                                       sap_true_alpha = "Sapling Alpha Diversity",
                                       seed_rich = "Seedling Richness",
                                       seed_true_alpha = "Seedling Alpha Diversity",
                                       saplings = "Sapling Abundance",
                                       seedlings = "Seedling Abundance")

lowest_points <- all_results %>%
  group_by(response, predictor) %>%
  slice_min(AIC, n = 1, with_ties = FALSE)

ggplot(all_results, aes(x = scale_numeric, y = AIC, color = response)) +
  geom_line(aes(group = response), linewidth = 1.2) +
  geom_point(size = 3.5) +
  geom_point(data = lowest_points,aes(x = scale_numeric, y = AIC, color = response), shape = 22, fill = NA, stroke = 1.2, size = 6) +
  facet_wrap(~predictor, scales = "free_y") +
  scale_color_brewer(palette = "Dark2") +
  labs(
    x = "Spatial Extent (m)",
    y = "AIC",
    color = "Response Variable"
  ) +
  theme_minimal(base_size = 22) +
  theme(
    legend.position = "right",
    legend.box.spacing = unit(1, "cm"),         # Space between legend and plot
    legend.key.size = unit(1.2, "lines"),       # Size of each color box
    legend.text = element_text(margin = margin(t = 6, b = 6))  # Padding above/below text
  )+
  guides(color = guide_legend(override.aes = list(
    shape = 15,     # solid square
    size = 5,       # consistent size
    linewidth = 0   # removes line from legend key
  )))

summary(glmmTMB(saplings ~ canopy_cover_perc, family = nbinom2, data = data)) # neg
 Family: nbinom2  ( log )
Formula:          saplings ~ canopy_cover_perc
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    188.3     191.8     -91.1     182.3        21 


Dispersion parameter for nbinom2 family (): 1.29 

Conditional model:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)        3.897291   0.717045   5.435 5.47e-08 ***
canopy_cover_perc -0.014949   0.009321  -1.604    0.109    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ canopy_cover_perc_50, family = nbinom2, data = data)) # neg
 Family: nbinom2  ( log )
Formula:          saplings ~ canopy_cover_perc_50
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    188.4     192.0     -91.2     182.4        21 


Dispersion parameter for nbinom2 family (): 1.28 

Conditional model:
                     Estimate Std. Error z value Pr(>|z|)    
(Intercept)           4.04128    0.86115   4.693 2.69e-06 ***
canopy_cover_perc_50 -0.01703    0.01143  -1.490    0.136    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ canopy_cover_perc_200, family = nbinom2, data = data)) # neg
 Family: nbinom2  ( log )
Formula:          saplings ~ canopy_cover_perc_200
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    187.3     190.8     -90.6     181.3        21 


Dispersion parameter for nbinom2 family (): 1.34 

Conditional model:
                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)            4.69566    1.07193   4.381 1.18e-05 ***
canopy_cover_perc_200 -0.02606    0.01431  -1.822   0.0685 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ zmean, family = nbinom2, data = data)) # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ zmean
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    190.8     194.4     -92.4     184.8        21 


Dispersion parameter for nbinom2 family (): 1.17 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) 2.753155   0.552197   4.986 6.17e-07 ***
zmean       0.008775   0.060592   0.145    0.885    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ zmean_50, family = nbinom2, data = data)) # neg
 Family: nbinom2  ( log )
Formula:          saplings ~ zmean_50
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    190.8     194.4     -92.4     184.8        21 


Dispersion parameter for nbinom2 family (): 1.17 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  2.92488    0.53491   5.468 4.55e-08 ***
zmean_50    -0.01090    0.05583  -0.195    0.845    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ zmean_200, family = nbinom2, data = data)) # neg
 Family: nbinom2  ( log )
Formula:          saplings ~ zmean_200
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    182.3     185.9     -88.2     176.3        21 


Dispersion parameter for nbinom2 family (): 1.62 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   5.3079     0.8734   6.077 1.22e-09 ***
zmean_200    -0.3017     0.0997  -3.026  0.00248 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ cv_area, family = nbinom2, data = data))  # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ cv_area
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    190.6     194.1     -92.3     184.6        21 


Dispersion parameter for nbinom2 family (): 1.18 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   2.5882     0.4891   5.292 1.21e-07 ***
cv_area       0.1671     0.3184   0.525      0.6    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ cv_area_50, family = nbinom2, data = data)) # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ cv_area_50
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    190.4     193.9     -92.2     184.4        21 


Dispersion parameter for nbinom2 family (): 1.19 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   2.3920     0.6352   3.765 0.000166 ***
cv_area_50    0.1047     0.1481   0.707 0.479426    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ cv_area_200, family = nbinom2, data = data)) # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ cv_area_200
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    187.2     190.7     -90.6     181.2        21 


Dispersion parameter for nbinom2 family (): 1.34 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  1.94344    0.47644   4.079 4.52e-05 ***
cv_area_200  0.06941    0.03686   1.883   0.0597 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ zcv, family = nbinom2, data = data)) # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ zcv
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    189.2     192.7     -91.6     183.2        21 


Dispersion parameter for nbinom2 family (): 1.24 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)  
(Intercept)   1.8323     0.7593   2.413   0.0158 *
zcv           1.3699     1.0385   1.319   0.1871  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ zcv_50, family = nbinom2, data = data)) # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ zcv_50
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    188.3     191.8     -91.1     182.3        21 


Dispersion parameter for nbinom2 family (): 1.29 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)   
(Intercept)   1.7971     0.6849   2.624  0.00869 **
zcv_50        1.3671     0.9104   1.502  0.13318   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(glmmTMB(saplings ~ zcv_200, family = nbinom2, data = data)) # pos
 Family: nbinom2  ( log )
Formula:          saplings ~ zcv_200
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    183.6     187.2     -88.8     177.6        21 


Dispersion parameter for nbinom2 family (): 1.56 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)   
(Intercept)   0.7930     0.7470   1.062  0.28839   
zcv_200       2.4832     0.9351   2.656  0.00792 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Choose lowest AIC variable option for each reponse

LM and GLMs

Seedling Abundance

# Models

glm_seedlings <- glm.nb(seedlings ~ canopy_cover_perc + zmean + cv_area + zcv + treedensity +
                          herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                          forest_type, data = data)
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
Warning in glm.nb(seedlings ~ canopy_cover_perc + zmean + cv_area + zcv + :
alternation limit reached
alias(glm_seedlings)
Model :
seedlings ~ canopy_cover_perc + zmean + cv_area + zcv + treedensity + 
    herbaceous + woody + deadwood + leaf_litter + Litter.depth + 
    Domin + forest_type
vif(glm_seedlings)
canopy_cover_perc             zmean           cv_area               zcv 
        22.930485          7.611031          1.486181          5.318515 
      treedensity        herbaceous             woody          deadwood 
        12.002416         20.914053          9.641357          3.282896 
      leaf_litter      Litter.depth             Domin       forest_type 
        29.018283          2.354011          3.578405          3.384066 
# canopy cover and tree density high collinearity
# remove tree density + leaf_litter

glm_seedlings <- glm.nb(seedlings ~ canopy_cover_perc + zmean + cv_area + zcv + herbaceous + woody + deadwood + Litter.depth + Domin +
                          forest_type, data = data)
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: algorithm did not converge
# Still struggling, try diff method

glm_seedlings <- glmmTMB(seedlings ~ canopy_cover_perc + zmean + cv_area + zcv + herbaceous + woody + deadwood + Litter.depth + Domin +
                          forest_type, family = nbinom2, data = data)

summary(glm_seedlings)
 Family: nbinom2  ( log )
Formula:          
seedlings ~ canopy_cover_perc + zmean + cv_area + zcv + herbaceous +  
    woody + deadwood + Litter.depth + Domin + forest_type
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    288.1     302.3    -132.1     264.1        12 


Dispersion parameter for nbinom2 family (): 1.06 

Conditional model:
                      Estimate Std. Error z value Pr(>|z|)  
(Intercept)            8.65703    3.36330   2.574   0.0101 *
canopy_cover_perc     -0.04585    0.02320  -1.976   0.0481 *
zmean                  0.25420    0.14271   1.781   0.0749 .
cv_area               -0.90334    0.36215  -2.494   0.0126 *
zcv                   -1.65954    2.85800  -0.581   0.5615  
herbaceous            -0.01218    0.01730  -0.704   0.4814  
woody                 -0.02293    0.02482  -0.924   0.3556  
deadwood              -0.03004    0.05499  -0.546   0.5849  
Litter.depth           0.21961    0.17691   1.241   0.2145  
Domin                 -0.47891    0.78197  -0.612   0.5402  
forest_typeplantation -0.15812    0.73514  -0.215   0.8297  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# best model
#vif doesnt work with glmmTMB, try new method

check_collinearity(glm_seedlings)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc 4.976375 3.693369 6.870550 2.230779 0.2009495 0.1455488 0.2707555
zmean 4.467463 3.333709 6.152013 2.113637 0.2238407 0.1625484 0.2999662
cv_area 1.296930 1.117296 1.751667 1.138828 0.7710516 0.5708846 0.8950181
zcv 5.681324 4.191707 7.866166 2.383553 0.1760153 0.1271267 0.2385663
herbaceous 3.069186 2.346447 4.179872 1.751909 0.3258193 0.2392418 0.4261762
woody 2.324674 1.822408 3.133686 1.524688 0.4301679 0.3191130 0.5487245
deadwood 1.444694 1.213198 1.927554 1.201954 0.6921883 0.5187923 0.8242680
Litter.depth 5.318237 3.935020 7.353335 2.306130 0.1880322 0.1359927 0.2541283
Domin 5.539179 4.091214 7.665390 2.353546 0.1805322 0.1304565 0.2444263
forest_type 2.784318 2.145682 3.778948 1.668627 0.3591544 0.2646239 0.4660523
# remove zcv + domin

glm_seedlings <- glmmTMB(seedlings ~ canopy_cover_perc + zmean + cv_area + herbaceous + woody + deadwood + Litter.depth + 
                           forest_type, family = nbinom2, data = data)
check_collinearity(glm_seedlings)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc 3.459894 2.503814 5.023821 1.860079 0.2890262 0.1990517 0.3993907
zmean 3.714435 2.672410 5.405713 1.927287 0.2692199 0.1849895 0.3741941
cv_area 1.271357 1.084848 1.867841 1.127545 0.7865608 0.5353775 0.9217878
herbaceous 2.234883 1.695167 3.193625 1.494952 0.4474508 0.3131238 0.5899124
woody 1.475803 1.205663 2.100774 1.214826 0.6775974 0.4760150 0.8294194
deadwood 1.192289 1.044104 1.838356 1.091920 0.8387228 0.5439643 0.9577588
Litter.depth 2.098097 1.605544 2.991296 1.448481 0.4766224 0.3343033 0.6228420
forest_type 2.119991 1.619870 3.023620 1.456019 0.4717000 0.3307294 0.6173335
stepAIC(glm_seedlings, direction = "both")
Start:  AIC=284.88
seedlings ~ canopy_cover_perc + zmean + cv_area + herbaceous + 
    woody + deadwood + Litter.depth + forest_type

                    Df    AIC
- forest_type        1 283.24
- herbaceous         1 283.29
- deadwood           1 283.29
- woody              1 284.43
- Litter.depth       1 284.67
<none>                 284.88
- cv_area            1 287.78
- canopy_cover_perc  1 287.82
- zmean              1 288.16

Step:  AIC=283.24
seedlings ~ canopy_cover_perc + zmean + cv_area + herbaceous + 
    woody + deadwood + Litter.depth

                    Df    AIC
- herbaceous         1 281.56
- deadwood           1 281.78
- woody              1 282.45
- Litter.depth       1 282.78
<none>                 283.24
+ forest_type        1 284.88
- cv_area            1 286.10
- zmean              1 286.19
- canopy_cover_perc  1 286.91

Step:  AIC=281.55
seedlings ~ canopy_cover_perc + zmean + cv_area + woody + deadwood + 
    Litter.depth

                    Df    AIC
- deadwood           1 279.89
- woody              1 281.01
- Litter.depth       1 281.43
<none>                 281.56
+ herbaceous         1 283.24
+ forest_type        1 283.29
- cv_area            1 284.12
- canopy_cover_perc  1 284.91
- zmean              1 288.74

Step:  AIC=279.89
seedlings ~ canopy_cover_perc + zmean + cv_area + woody + Litter.depth

                    Df    AIC
- woody              1 279.26
<none>                 279.89
- Litter.depth       1 279.93
+ forest_type        1 281.52
+ deadwood           1 281.56
+ herbaceous         1 281.78
- cv_area            1 283.22
- canopy_cover_perc  1 283.32
- zmean              1 287.34

Step:  AIC=279.26
seedlings ~ canopy_cover_perc + zmean + cv_area + Litter.depth

                    Df    AIC
- Litter.depth       1 278.41
<none>                 279.26
+ woody              1 279.89
+ deadwood           1 281.01
+ herbaceous         1 281.02
+ forest_type        1 281.24
- canopy_cover_perc  1 281.37
- cv_area            1 283.06
- zmean              1 285.48

Step:  AIC=278.41
seedlings ~ canopy_cover_perc + zmean + cv_area

                    Df    AIC
<none>                 278.41
+ Litter.depth       1 279.26
- canopy_cover_perc  1 279.57
+ woody              1 279.93
+ herbaceous         1 280.04
+ deadwood           1 280.04
+ forest_type        1 280.13
- cv_area            1 281.74
- zmean              1 283.48
Formula:          seedlings ~ canopy_cover_perc + zmean + cv_area
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
 278.4094  284.2997 -134.2047  268.4094        19 

Number of obs: 24

Dispersion parameter for nbinom2 family (): 0.915 

Fixed Effects:

Conditional model:
      (Intercept)  canopy_cover_perc              zmean            cv_area  
          5.72921           -0.02595            0.24255           -0.90414  
finalmodel <- glmmTMB(seedlings ~ canopy_cover_perc + zmean + cv_area, family = nbinom2, data = data)

check_collinearity(finalmodel)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc 1.669823 1.249140 2.800843 1.292216 0.5988659 0.3570354 0.8005505
zmean 1.536311 1.179556 2.601896 1.239480 0.6509098 0.3843351 0.8477768
cv_area 1.137957 1.010057 2.892435 1.066751 0.8787676 0.3457294 0.9900431
# low collin

summary(finalmodel)
 Family: nbinom2  ( log )
Formula:          seedlings ~ canopy_cover_perc + zmean + cv_area
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    278.4     284.3    -134.2     268.4        19 


Dispersion parameter for nbinom2 family (): 0.915 

Conditional model:
                  Estimate Std. Error z value Pr(>|z|)    
(Intercept)        5.72921    1.19620   4.790 1.67e-06 ***
canopy_cover_perc -0.02595    0.01444  -1.797  0.07231 .  
zmean              0.24255    0.08526   2.845  0.00444 ** 
cv_area           -0.90414    0.36408  -2.483  0.01301 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Check residuals
sim <- simulateResiduals(fittedModel = finalmodel, n = 250, refit = TRUE)
plot(sim)

# Core tests
testUniformity(sim)     # overall fit (KS on uniformity)


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.19236, p-value = 0.2973
alternative hypothesis: two-sided
testDispersion(sim)     # over/underdispersion for NB


    DHARMa nonparametric dispersion test via mean deviance residual fitted
    vs. simulated-refitted

data:  sim
dispersion = 0.76075, p-value = 0.28
alternative hypothesis: two.sided
testZeroInflation(sim)  # excess zeros (if relevant)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = 0, p-value = 1
alternative hypothesis: two.sided
# ALL GOOD

summary(finalmodel)
 Family: nbinom2  ( log )
Formula:          seedlings ~ canopy_cover_perc + zmean + cv_area
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    278.4     284.3    -134.2     268.4        19 


Dispersion parameter for nbinom2 family (): 0.915 

Conditional model:
                  Estimate Std. Error z value Pr(>|z|)    
(Intercept)        5.72921    1.19620   4.790 1.67e-06 ***
canopy_cover_perc -0.02595    0.01444  -1.797  0.07231 .  
zmean              0.24255    0.08526   2.845  0.00444 ** 
cv_area           -0.90414    0.36408  -2.483  0.01301 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#  zmean, cv_area sig affecting seedling abundance

Sapling Abundance

# Saplings with spatial scale
glm_saplings <- glmmTMB(saplings ~ canopy_cover_perc_200 + zmean_200 + cv_area_200 + zcv_200 + treedensity +
                          herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                          forest_type, family = nbinom2, data = data)

check_collinearity(glm_saplings)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_200 12.390332 9.475618 16.307399 3.519990 0.0807081 0.0613219 0.1055340
zmean_200 12.594676 9.629561 16.578603 3.548898 0.0793986 0.0603187 0.1038469
cv_area_200 11.069614 8.480658 14.554572 3.327103 0.0903374 0.0687069 0.1179154
zcv_200 23.041913 17.500334 30.444611 4.800199 0.0433992 0.0328465 0.0571418
treedensity 5.924446 4.604997 7.726822 2.434019 0.1687921 0.1294193 0.2171554
herbaceous 17.392713 13.244274 22.946671 4.170457 0.0574953 0.0435793 0.0755043
woody 9.725029 7.467745 12.770118 3.118498 0.1028275 0.0783078 0.1339092
deadwood 2.732195 2.202638 3.494931 1.652935 0.3660061 0.2861287 0.4540010
leaf_litter 20.948398 15.923090 27.665965 4.576942 0.0477363 0.0361455 0.0628019
Litter.depth 4.562736 3.579640 5.920488 2.136056 0.2191668 0.1689050 0.2793577
Domin 5.012240 3.918070 6.516684 2.238803 0.1995116 0.1534523 0.2552277
forest_type 9.762283 7.495810 12.819559 3.124465 0.1024351 0.0780058 0.1334079
# remove leaf_litter, canopy cover, zcv, cv area
glm_saplings <- glmmTMB(saplings ~ zmean_200 + treedensity +
                          herbaceous + woody + deadwood + Litter.depth + Domin +
                          forest_type, family = nbinom2, data = data)

check_collinearity(glm_saplings)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
zmean_200 1.635640 1.306109 2.319920 1.278922 0.6113813 0.4310494 0.7656331
treedensity 2.812998 2.075953 4.054932 1.677199 0.3554926 0.2466133 0.4817065
herbaceous 2.096923 1.604776 2.989564 1.448076 0.4768892 0.3344969 0.6231399
woody 1.471945 1.203280 2.095692 1.213237 0.6793733 0.4771694 0.8310620
deadwood 2.287829 1.729925 3.272156 1.512557 0.4370956 0.3056089 0.5780597
Litter.depth 3.311170 2.405356 4.800821 1.819662 0.3020080 0.2082977 0.4157388
Domin 3.673296 2.645155 5.343974 1.916584 0.2722351 0.1871267 0.3780497
forest_type 1.187022 1.041630 1.840183 1.089505 0.8424447 0.5434243 0.9600335
stepAIC(glm_saplings, direction = "both")
Start:  AIC=186.01
saplings ~ zmean_200 + treedensity + herbaceous + woody + deadwood + 
    Litter.depth + Domin + forest_type

               Df    AIC
- Litter.depth  1 184.03
- Domin         1 184.18
- treedensity   1 184.27
- herbaceous    1 184.68
- deadwood      1 185.38
- forest_type   1 185.53
<none>            186.01
- woody         1 186.44
- zmean_200     1 191.42

Step:  AIC=184.04
saplings ~ zmean_200 + treedensity + herbaceous + woody + deadwood + 
    Domin + forest_type

               Df    AIC
- treedensity   1 182.31
- Domin         1 182.73
- herbaceous    1 182.79
- deadwood      1 183.45
- forest_type   1 183.56
<none>            184.03
- woody         1 184.67
+ Litter.depth  1 186.01
- zmean_200     1 190.12

Step:  AIC=182.31
saplings ~ zmean_200 + herbaceous + woody + deadwood + Domin + 
    forest_type

               Df    AIC
- Domin         1 181.51
- forest_type   1 181.68
- deadwood      1 181.74
<none>            182.31
- herbaceous    1 182.41
- woody         1 182.70
+ treedensity   1 184.03
+ Litter.depth  1 184.27
- zmean_200     1 189.27

Step:  AIC=181.51
saplings ~ zmean_200 + herbaceous + woody + deadwood + forest_type

               Df    AIC
- forest_type   1 180.37
- herbaceous    1 180.98
- deadwood      1 181.34
<none>            181.51
- woody         1 182.10
+ Domin         1 182.31
+ Litter.depth  1 182.53
+ treedensity   1 182.73
- zmean_200     1 189.71

Step:  AIC=180.37
saplings ~ zmean_200 + herbaceous + woody + deadwood

               Df    AIC
- herbaceous    1 179.68
- deadwood      1 180.34
<none>            180.37
- woody         1 181.24
+ forest_type   1 181.51
+ Domin         1 181.68
+ Litter.depth  1 181.74
+ treedensity   1 181.94
- zmean_200     1 191.58

Step:  AIC=179.68
saplings ~ zmean_200 + woody + deadwood

               Df    AIC
<none>            179.68
- deadwood      1 180.22
+ treedensity   1 180.35
+ herbaceous    1 180.37
+ forest_type   1 180.98
+ Litter.depth  1 181.09
+ Domin         1 181.36
- woody         1 181.55
- zmean_200     1 189.85
Formula:          saplings ~ zmean_200 + woody + deadwood
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
179.68171 185.57198 -84.84086 169.68171        19 

Number of obs: 24

Dispersion parameter for nbinom2 family (): 2.13 

Fixed Effects:

Conditional model:
(Intercept)    zmean_200        woody     deadwood  
    4.68638     -0.31888      0.02175      0.06507  
finalsapmodel <- glmmTMB(saplings ~ zmean_200 + woody + deadwood, family = nbinom2, data = data)

check_collinearity(finalsapmodel)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
zmean_200 1.007867 1 5.925789e+13 1.003926 0.9921946 0 1
woody 1.001782 1 Inf 1.000890 0.9982214 0 1
deadwood 1.007691 1 1.324588e+14 1.003838 0.9923674 0 1
# Not much collinearity

sim <- simulateResiduals(fittedModel = finalsapmodel, n = 100, refit = TRUE)
plot(sim)

# Core tests
testUniformity(sim)     # overall fit (KS on uniformity)


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.22067, p-value = 0.1658
alternative hypothesis: two-sided
testDispersion(sim)     # over/underdispersion for NB


    DHARMa nonparametric dispersion test via mean deviance residual fitted
    vs. simulated-refitted

data:  sim
dispersion = 1.1862, p-value = 0.2
alternative hypothesis: two.sided
testZeroInflation(sim)  # excess zeros (if relevant)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = 0, p-value = 1
alternative hypothesis: two.sided
# ALL GOOD

summary(finalsapmodel)
 Family: nbinom2  ( log )
Formula:          saplings ~ zmean_200 + woody + deadwood
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    179.7     185.6     -84.8     169.7        19 


Dispersion parameter for nbinom2 family (): 2.13 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  4.68638    0.78705   5.954 2.61e-09 ***
zmean_200   -0.31888    0.08672  -3.677 0.000236 ***
woody        0.02175    0.01097   1.982 0.047493 *  
deadwood     0.06507    0.04040   1.611 0.107240    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# zmean200 and woody cover significant

Seedling Richness

# Include richness and alpha data here

corrplot(cor(data[ ,c("canopy_cover_perc", "cv_area", "zmean", "zcv", "treedensity", 
                      "ground", "herbaceous", "woody", "deadwood", "leaf_litter", 
                      "Litter.depth", "Domin", "adult_spec_rich", "adult_true_alpha")], use = "complete.obs"))

# Seedling Richness with spatial scale


lm_seedrich <- lm(seed_rich ~ canopy_cover_perc_50 + zmean + cv_area_50 + zcv + treedensity +
                    herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                    forest_type + adult_true_alpha + adult_spec_rich, data = data)

alias(lm_seedrich)
Model :
seed_rich ~ canopy_cover_perc_50 + zmean + cv_area_50 + zcv + 
    treedensity + herbaceous + woody + deadwood + leaf_litter + 
    Litter.depth + Domin + forest_type + adult_true_alpha + adult_spec_rich
vif(lm_seedrich)
canopy_cover_perc_50                zmean           cv_area_50 
            6.007265             7.499086             4.694452 
                 zcv          treedensity           herbaceous 
            9.430047            15.597795            18.681400 
               woody             deadwood          leaf_litter 
            8.656639             4.352368            17.573882 
        Litter.depth                Domin          forest_type 
            3.870072             4.512028             9.540317 
    adult_true_alpha      adult_spec_rich 
           34.112917            32.324865 
# very high collinearity

lm_seedrich <- lm(seed_rich ~ canopy_cover_perc_50 + zmean + cv_area_50 + zcv + treedensity +
                    woody + deadwood + leaf_litter + Litter.depth + Domin +
                    adult_spec_rich, data = data)
vif(lm_seedrich)
canopy_cover_perc_50                zmean           cv_area_50 
            4.273648             3.473074             2.206033 
                 zcv          treedensity                woody 
            3.673939             3.896870             1.617752 
            deadwood          leaf_litter         Litter.depth 
            1.852051             3.508857             2.626287 
               Domin      adult_spec_rich 
            2.653081             1.873197 
stepAIC(lm_seedrich)
Start:  AIC=30.92
seed_rich ~ canopy_cover_perc_50 + zmean + cv_area_50 + zcv + 
    treedensity + woody + deadwood + leaf_litter + Litter.depth + 
    Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
- zmean                 1    0.0101 32.035 28.931
- zcv                   1    0.0132 32.038 28.933
- woody                 1    0.0297 32.054 28.945
- leaf_litter           1    0.0575 32.082 28.966
- deadwood              1    0.6732 32.698 29.422
- treedensity           1    0.7769 32.802 29.498
- cv_area_50            1    1.3150 33.340 29.889
- canopy_cover_perc_50  1    1.9416 33.966 30.336
<none>                              32.025 30.923
- adult_spec_rich       1    3.4461 35.471 31.376
- Litter.depth          1   13.4527 45.477 37.340
- Domin                 1   15.9582 47.983 38.627

Step:  AIC=28.93
seed_rich ~ canopy_cover_perc_50 + cv_area_50 + zcv + treedensity + 
    woody + deadwood + leaf_litter + Litter.depth + Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
- zcv                   1    0.0194 32.054 26.945
- woody                 1    0.0243 32.059 26.949
- leaf_litter           1    0.0617 32.097 26.977
- deadwood              1    0.6658 32.701 27.424
- treedensity           1    0.8812 32.916 27.582
- cv_area_50            1    1.5633 33.598 28.074
- canopy_cover_perc_50  1    2.5749 34.610 28.786
<none>                              32.035 28.931
- adult_spec_rich       1    3.4386 35.473 29.378
- Litter.depth          1   13.4485 45.483 35.343
- Domin                 1   16.4743 48.509 36.889

Step:  AIC=26.95
seed_rich ~ canopy_cover_perc_50 + cv_area_50 + treedensity + 
    woody + deadwood + leaf_litter + Litter.depth + Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
- woody                 1    0.0236 32.078 24.963
- leaf_litter           1    0.1287 32.183 25.041
- deadwood              1    0.6942 32.748 25.459
- treedensity           1    0.8736 32.928 25.590
- cv_area_50            1    1.6341 33.688 26.138
<none>                              32.054 26.945
- canopy_cover_perc_50  1    3.3347 35.389 27.320
- adult_spec_rich       1    3.5975 35.652 27.498
- Litter.depth          1   13.7258 45.780 33.499
- Domin                 1   16.4559 48.510 34.889

Step:  AIC=24.96
seed_rich ~ canopy_cover_perc_50 + cv_area_50 + treedensity + 
    deadwood + leaf_litter + Litter.depth + Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
- leaf_litter           1    0.2806 32.359 23.172
- deadwood              1    0.7330 32.811 23.505
- treedensity           1    1.0037 33.082 23.702
- cv_area_50            1    1.6268 33.705 24.150
<none>                              32.078 24.963
- adult_spec_rich       1    3.7195 35.797 25.596
- canopy_cover_perc_50  1    4.5332 36.611 26.135
- Litter.depth          1   14.4041 46.482 31.864
- Domin                 1   16.5255 48.603 32.935

Step:  AIC=23.17
seed_rich ~ canopy_cover_perc_50 + cv_area_50 + treedensity + 
    deadwood + Litter.depth + Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
- deadwood              1    0.5754 32.934 21.595
- treedensity           1    0.7281 33.087 21.706
- cv_area_50            1    2.5443 34.903 22.988
<none>                              32.359 23.172
- adult_spec_rich       1    3.5556 35.914 23.674
- canopy_cover_perc_50  1    4.5562 36.915 24.333
- Litter.depth          1   14.2093 46.568 29.909
- Domin                 1   18.5018 50.860 32.025

Step:  AIC=21.59
seed_rich ~ canopy_cover_perc_50 + cv_area_50 + treedensity + 
    Litter.depth + Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
- treedensity           1    0.3924 33.326 19.879
- cv_area_50            1    2.4589 35.393 21.323
<none>                              32.934 21.595
- canopy_cover_perc_50  1    4.5341 37.468 22.690
- adult_spec_rich       1    6.0628 38.997 23.650
- Litter.depth          1   13.8323 46.766 28.011
- Domin                 1   18.5154 51.449 30.301

Step:  AIC=19.88
seed_rich ~ canopy_cover_perc_50 + cv_area_50 + Litter.depth + 
    Domin + adult_spec_rich

                       Df Sum of Sq    RSS    AIC
<none>                              33.326 19.879
- canopy_cover_perc_50  1    4.1440 37.470 20.692
- cv_area_50            1    4.8075 38.134 21.113
- adult_spec_rich       1    5.7340 39.060 21.689
- Litter.depth          1   13.5066 46.833 26.045
- Domin                 1   18.2127 51.539 28.343

Call:
lm(formula = seed_rich ~ canopy_cover_perc_50 + cv_area_50 + 
    Litter.depth + Domin + adult_spec_rich, data = data)

Coefficients:
         (Intercept)  canopy_cover_perc_50            cv_area_50  
             1.57654               0.03287              -0.31646  
        Litter.depth                 Domin       adult_spec_rich  
             0.44097              -2.50157               0.61633  
finalseedrichmodel <- lm(formula = seed_rich ~ canopy_cover_perc_50 + cv_area_50 + Litter.depth + Domin + adult_spec_rich, data = data)

vif(finalseedrichmodel)
canopy_cover_perc_50           cv_area_50         Litter.depth 
            1.180761             1.166826             2.287180 
               Domin      adult_spec_rich 
            2.390876             1.313905 
# Check residuals
plot(finalseedrichmodel)

check_model(finalseedrichmodel)

# ALL GOOD

anova(finalseedrichmodel)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df Sum Sq Mean Sq F value Pr(>F)
canopy_cover_perc_50 1 0.4647099 0.4647099 0.2509961 0.6224458
cv_area_50 1 3.6357899 3.6357899 1.9637393 0.1781205
Litter.depth 1 0.4550209 0.4550209 0.2457630 0.6260728
Domin 1 14.0091887 14.0091887 7.5665522 0.0131517
adult_spec_rich 1 5.7339631 5.7339631 3.0969910 0.0954210
Residuals 18 33.3263274 1.8514626 NA NA
summary(finalseedrichmodel)

Call:
lm(formula = seed_rich ~ canopy_cover_perc_50 + cv_area_50 + 
    Litter.depth + Domin + adult_spec_rich, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-3.12511 -0.61912  0.08265  0.70444  2.07933 

Coefficients:
                     Estimate Std. Error t value Pr(>|t|)   
(Intercept)           1.57654    2.11233   0.746  0.46509   
canopy_cover_perc_50  0.03287    0.02197   1.496  0.15197   
cv_area_50           -0.31646    0.19639  -1.611  0.12449   
Litter.depth          0.44097    0.16327   2.701  0.01462 * 
Domin                -2.50157    0.79760  -3.136  0.00571 **
adult_spec_rich       0.61633    0.35022   1.760  0.09542 . 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.361 on 18 degrees of freedom
Multiple R-squared:  0.4217,    Adjusted R-squared:  0.261 
F-statistic: 2.625 on 5 and 18 DF,  p-value: 0.05954
# litter depth, domin has effect

Sapling Richness

# Sapling Richness

glm_saprich <- glmmTMB(sap_rich ~ canopy_cover_perc_200 + zmean_200 + cv_area_200 + zcv_200 + treedensity +
                         herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                         forest_type + adult_true_alpha + adult_spec_rich, family = poisson, data = data)

check_collinearity(glm_saprich)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_200 15.626421 12.274503 19.974866 3.953027 0.0639942 0.0500629 0.0814697
zmean_200 13.415491 10.555597 17.131322 3.662716 0.0745407 0.0583726 0.0947365
cv_area_200 10.380104 8.195767 13.227513 3.221817 0.0963382 0.0756000 0.1220142
zcv_200 22.199219 17.384670 28.428497 4.711605 0.0450466 0.0351760 0.0575219
treedensity 16.137231 12.671639 20.631840 4.017117 0.0619685 0.0484688 0.0789164
herbaceous 18.514851 14.520164 23.689812 4.302889 0.0540107 0.0422122 0.0688697
woody 10.487248 8.279064 13.365309 3.238402 0.0953539 0.0748206 0.1207866
deadwood 2.728109 2.249228 3.390566 1.651699 0.3665542 0.2949360 0.4445970
leaf_litter 18.976282 14.878915 24.283285 4.356178 0.0526974 0.0411806 0.0672092
Litter.depth 4.234327 3.418702 5.324995 2.057748 0.2361650 0.1877936 0.2925087
Domin 5.292726 4.241140 6.685500 2.300593 0.1889385 0.1495775 0.2357857
forest_type 11.920026 9.392952 15.207987 3.452539 0.0838924 0.0657549 0.1064628
adult_true_alpha 44.448264 34.682974 57.044685 6.666953 0.0224981 0.0175301 0.0288326
adult_spec_rich 50.667175 39.518110 65.043338 7.118088 0.0197366 0.0153744 0.0253049
# canopy cover, herbac, adult spec rich, zcv

glm_saprich <- glmmTMB(sap_rich ~ zmean_200 + treedensity +
                         woody + deadwood + leaf_litter + Litter.depth + Domin +
                         forest_type + adult_true_alpha, family = poisson, data = data)

check_collinearity(glm_saprich)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
zmean_200 1.715161 1.356947 2.432861 1.309641 0.5830357 0.4110386 0.7369487
treedensity 2.311335 1.745366 3.307052 1.520307 0.4326504 0.3023841 0.5729456
woody 1.303853 1.102983 1.896522 1.141864 0.7669576 0.5272810 0.9066322
deadwood 1.780289 1.398860 2.526480 1.334275 0.5617065 0.3958077 0.7148680
leaf_litter 1.576370 1.268533 2.237101 1.255536 0.6343688 0.4470072 0.7883121
Litter.depth 2.832735 2.088988 4.084440 1.683073 0.3530157 0.2448316 0.4787007
Domin 3.197061 2.329844 4.629805 1.788033 0.3127873 0.2159918 0.4292132
forest_type 1.599548 1.283189 2.269319 1.264732 0.6251767 0.4406609 0.7793082
adult_true_alpha 2.173586 1.654971 3.102847 1.474309 0.4600693 0.3222846 0.6042403
stepAIC(glm_saprich)
Start:  AIC=92.63
sap_rich ~ zmean_200 + treedensity + woody + deadwood + leaf_litter + 
    Litter.depth + Domin + forest_type + adult_true_alpha

                   Df    AIC
- treedensity       1 90.627
- leaf_litter       1 90.859
- woody             1 90.882
- deadwood          1 90.941
- zmean_200         1 91.434
- forest_type       1 91.885
- adult_true_alpha  1 92.115
<none>                92.627
- Domin             1 92.873
- Litter.depth      1 94.780

Step:  AIC=90.63
sap_rich ~ zmean_200 + woody + deadwood + leaf_litter + Litter.depth + 
    Domin + forest_type + adult_true_alpha

                   Df    AIC
- leaf_litter       1 88.871
- woody             1 88.882
- deadwood          1 89.142
- zmean_200         1 89.436
- forest_type       1 90.122
<none>                90.627
- adult_true_alpha  1 90.654
- Domin             1 90.997
- Litter.depth      1 92.833

Step:  AIC=88.87
sap_rich ~ zmean_200 + woody + deadwood + Litter.depth + Domin + 
    forest_type + adult_true_alpha

                   Df    AIC
- deadwood          1 87.434
- woody             1 87.476
- zmean_200         1 87.865
- adult_true_alpha  1 88.657
- forest_type       1 88.836
<none>                88.871
- Domin             1 89.596
- Litter.depth      1 91.052

Step:  AIC=87.43
sap_rich ~ zmean_200 + woody + Litter.depth + Domin + forest_type + 
    adult_true_alpha

                   Df    AIC
- woody             1 85.999
- zmean_200         1 86.605
- adult_true_alpha  1 86.953
- forest_type       1 87.313
<none>                87.434
- Domin             1 88.251
- Litter.depth      1 89.785

Step:  AIC=86
sap_rich ~ zmean_200 + Litter.depth + Domin + forest_type + adult_true_alpha

                   Df    AIC
- zmean_200         1 84.887
- adult_true_alpha  1 85.455
- forest_type       1 85.905
<none>                85.999
- Domin             1 87.230
- Litter.depth      1 88.739

Step:  AIC=84.89
sap_rich ~ Litter.depth + Domin + forest_type + adult_true_alpha

                   Df    AIC
<none>                84.887
- adult_true_alpha  1 85.502
- forest_type       1 85.558
- Domin             1 89.848
- Litter.depth      1 91.783
Formula:          
sap_rich ~ Litter.depth + Domin + forest_type + adult_true_alpha
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
 84.88686  90.77713 -37.44343  74.88686        19 

Number of obs: 24

Fixed Effects:

Conditional model:
          (Intercept)           Litter.depth                  Domin  
               0.1480                 0.2147                -0.8530  
forest_typeplantation       adult_true_alpha  
               0.4291                 0.3379  
finalsaprichmodel <- glmmTMB(sap_rich ~ Litter.depth + Domin + forest_type + adult_true_alpha, family = compois, data = data)

check_collinearity(finalsaprichmodel)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
Litter.depth 2.070932 1.488705 3.346803 1.439073 0.4828744 0.2987926 0.6717246
Domin 2.029793 1.465268 3.279277 1.424708 0.4926610 0.3049453 0.6824690
forest_type 1.111618 1.005956 3.091610 1.054333 0.8995898 0.3234561 0.9940788
adult_true_alpha 1.141195 1.012446 2.601809 1.068267 0.8762746 0.3843479 0.9877071
# Low collinearity

summary(finalsaprichmodel)
 Family: compois  ( log )
Formula:          
sap_rich ~ Litter.depth + Domin + forest_type + adult_true_alpha
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
     75.8      82.9     -31.9      63.8        18 


Dispersion parameter for compois family (): 0.287 

Conditional model:
                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)            0.13390    0.31318   0.428  0.66898    
Litter.depth           0.21003    0.04057   5.177 2.26e-07 ***
Domin                 -0.83417    0.18873  -4.420 9.87e-06 ***
forest_typeplantation  0.43371    0.14709   2.949  0.00319 ** 
adult_true_alpha       0.34588    0.12057   2.869  0.00412 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
sim <- simulateResiduals(fittedModel = finalsaprichmodel, n = 250, refit = TRUE)
plot(sim)

# Core tests
testUniformity(sim)     # overall fit (KS on uniformity)


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.15685, p-value = 0.5444
alternative hypothesis: two-sided
testDispersion(sim)     # over/underdispersion for NB


    DHARMa nonparametric dispersion test via mean deviance residual fitted
    vs. simulated-refitted

data:  sim
dispersion = 1.017, p-value = 0.728
alternative hypothesis: two.sided
testZeroInflation(sim)  # excess zeros (if relevant)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = 0, p-value = 1
alternative hypothesis: two.sided
# ALL GOOD

# litter depth, domin, plantation, adult diversity has effect on sapling richness

Seedling diversity

# Seedling diversity

lm_seeddiv <- lm(seed_true_alpha ~ canopy_cover_perc + zmean + cv_area + zcv + treedensity +
                        herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                        forest_type + adult_true_alpha + adult_spec_rich, data = data)

alias(lm_seeddiv)
Model :
seed_true_alpha ~ canopy_cover_perc + zmean + cv_area + zcv + 
    treedensity + herbaceous + woody + deadwood + leaf_litter + 
    Litter.depth + Domin + forest_type + adult_true_alpha + adult_spec_rich
vif(lm_seeddiv)
canopy_cover_perc             zmean           cv_area               zcv 
        24.785205         10.549575          1.514688          6.849725 
      treedensity        herbaceous             woody          deadwood 
        19.706989         21.977453         10.217666          4.780288 
      leaf_litter      Litter.depth             Domin       forest_type 
        30.961014          3.012100          5.838318          6.481990 
 adult_true_alpha   adult_spec_rich 
        28.569113         29.699677 
# very high, remove leaf litter, adult spec rich, canopy cover

lm_seeddiv <- lm(seed_true_alpha ~ zmean + cv_area + zcv + treedensity +
                   herbaceous + woody + deadwood + Litter.depth + Domin +
                   forest_type + adult_true_alpha, data = data)

stepAIC(lm_seeddiv)
Start:  AIC=-3.28
seed_true_alpha ~ zmean + cv_area + zcv + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + forest_type + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- zcv               1   0.00440 7.7057 -5.2661
- zmean             1   0.00513 7.7065 -5.2639
- forest_type       1   0.00645 7.7078 -5.2598
- herbaceous        1   0.03911 7.7404 -5.1583
- woody             1   0.09370 7.7950 -4.9896
- adult_true_alpha  1   0.15081 7.8521 -4.8144
- Domin             1   0.19814 7.8995 -4.6702
- treedensity       1   0.21671 7.9180 -4.6138
- deadwood          1   0.25947 7.9608 -4.4846
- Litter.depth      1   0.30312 8.0045 -4.3533
- cv_area           1   0.32497 8.0263 -4.2879
<none>                          7.7013 -3.2798

Step:  AIC=-5.27
seed_true_alpha ~ zmean + cv_area + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + forest_type + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- forest_type       1   0.00242 7.7082 -7.2586
- zmean             1   0.01878 7.7245 -7.2077
- herbaceous        1   0.07888 7.7846 -7.0217
- adult_true_alpha  1   0.15936 7.8651 -6.7748
- woody             1   0.17508 7.8808 -6.7269
- Domin             1   0.20394 7.9097 -6.6392
- Litter.depth      1   0.30275 8.0085 -6.3412
- cv_area           1   0.32402 8.0298 -6.2776
- treedensity       1   0.32873 8.0345 -6.2635
- deadwood          1   0.35013 8.0559 -6.1997
<none>                          7.7057 -5.2661

Step:  AIC=-7.26
seed_true_alpha ~ zmean + cv_area + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- zmean             1   0.02116 7.7293 -9.1928
- herbaceous        1   0.07646 7.7846 -9.0217
- adult_true_alpha  1   0.16797 7.8761 -8.7412
- woody             1   0.17655 7.8847 -8.7151
- Domin             1   0.20338 7.9115 -8.6336
- Litter.depth      1   0.30307 8.0112 -8.3330
- cv_area           1   0.32924 8.0374 -8.2548
- deadwood          1   0.36266 8.0708 -8.1552
- treedensity       1   0.41025 8.1184 -8.0141
<none>                          7.7082 -7.2586

Step:  AIC=-9.19
seed_true_alpha ~ cv_area + treedensity + herbaceous + woody + 
    deadwood + Litter.depth + Domin + adult_true_alpha

                   Df Sum of Sq    RSS      AIC
- adult_true_alpha  1   0.17222 7.9015 -10.6639
- woody             1   0.17919 7.9085 -10.6428
- Domin             1   0.19442 7.9237 -10.5966
- herbaceous        1   0.20616 7.9355 -10.5611
- Litter.depth      1   0.34175 8.0711 -10.1544
- cv_area           1   0.38815 8.1175 -10.0169
- treedensity       1   0.41088 8.1402  -9.9498
- deadwood          1   0.42145 8.1508  -9.9186
<none>                          7.7293  -9.1928

Step:  AIC=-10.66
seed_true_alpha ~ cv_area + treedensity + herbaceous + woody + 
    deadwood + Litter.depth + Domin

               Df Sum of Sq    RSS      AIC
- Domin         1   0.10022 8.0018 -12.3614
- herbaceous    1   0.19132 8.0928 -12.0898
- woody         1   0.20352 8.1051 -12.0536
- Litter.depth  1   0.22835 8.1299 -11.9802
- cv_area       1   0.41265 8.3142 -11.4422
<none>                      7.9015 -10.6639
- treedensity   1   0.88102 8.7826 -10.1269
- deadwood      1   0.93938 8.8409  -9.9679

Step:  AIC=-12.36
seed_true_alpha ~ cv_area + treedensity + herbaceous + woody + 
    deadwood + Litter.depth

               Df Sum of Sq    RSS     AIC
- Litter.depth  1   0.12846 8.1302 -13.979
- woody         1   0.27789 8.2796 -13.542
- herbaceous    1   0.32066 8.3224 -13.418
- cv_area       1   0.45145 8.4532 -13.044
<none>                      8.0018 -12.361
- deadwood      1   0.97284 8.9746 -11.608
- treedensity   1   1.01919 9.0209 -11.484

Step:  AIC=-13.98
seed_true_alpha ~ cv_area + treedensity + herbaceous + woody + 
    deadwood

              Df Sum of Sq    RSS     AIC
- herbaceous   1   0.24178 8.3720 -15.276
- woody        1   0.25517 8.3854 -15.238
- cv_area      1   0.46409 8.5943 -14.647
<none>                     8.1302 -13.979
- deadwood     1   0.85372 8.9839 -13.583
- treedensity  1   0.89584 9.0261 -13.470

Step:  AIC=-15.28
seed_true_alpha ~ cv_area + treedensity + woody + deadwood

              Df Sum of Sq    RSS     AIC
- woody        1   0.15602 8.5280 -16.833
- deadwood     1   0.64264 9.0146 -15.501
- cv_area      1   0.66232 9.0343 -15.449
- treedensity  1   0.70139 9.0734 -15.345
<none>                     8.3720 -15.276

Step:  AIC=-16.83
seed_true_alpha ~ cv_area + treedensity + deadwood

              Df Sum of Sq    RSS     AIC
- treedensity  1   0.59787 9.1259 -17.206
- deadwood     1   0.60903 9.1370 -17.177
- cv_area      1   0.60999 9.1380 -17.175
<none>                     8.5280 -16.833

Step:  AIC=-17.21
seed_true_alpha ~ cv_area + deadwood

           Df Sum of Sq    RSS     AIC
- deadwood  1   0.23139 9.3573 -18.606
- cv_area   1   0.37169 9.4976 -18.248
<none>                  9.1259 -17.206

Step:  AIC=-18.61
seed_true_alpha ~ cv_area

          Df Sum of Sq    RSS     AIC
- cv_area  1   0.42461 9.7819 -19.541
<none>                 9.3573 -18.606

Step:  AIC=-19.54
seed_true_alpha ~ 1

Call:
lm(formula = seed_true_alpha ~ 1, data = data)

Coefficients:
(Intercept)  
      1.965  
finalseeddivmodel <- lm(formula = seed_true_alpha ~ cv_area + deadwood, 
                        data = data)

alias(finalseeddivmodel)
Model :
seed_true_alpha ~ cv_area + deadwood
vif(finalseeddivmodel)
 cv_area deadwood 
 1.00687  1.00687 
anova(finalseeddivmodel)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df Sum Sq Mean Sq F value Pr(>F)
cv_area 1 0.4246111 0.4246111 0.9770923 0.334171
deadwood 1 0.2313901 0.2313901 0.5324624 0.473635
Residuals 21 9.1258865 0.4345660 NA NA
summary(finalseeddivmodel)

Call:
lm(formula = seed_true_alpha ~ cv_area + deadwood, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.20394 -0.33526  0.02085  0.28051  1.68681 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  1.52252    0.38743   3.930 0.000768 ***
cv_area      0.21067    0.22779   0.925 0.365562    
deadwood     0.02377    0.03258   0.730 0.473635    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.6592 on 21 degrees of freedom
Multiple R-squared:  0.06706,   Adjusted R-squared:  -0.02179 
F-statistic: 0.7548 on 2 and 21 DF,  p-value: 0.4824
# Check residuals
plot(finalseeddivmodel)

check_model(finalseeddivmodel)

# ALL GOOD

# no effects seed div

Sapling Diversity

# Sapling diversity


glm_sapdiv <- glmmTMB(sap_true_alpha ~ canopy_cover_perc_50 + zmean_50 + cv_area_200 + zcv_50 + treedensity +
                        herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                        forest_type + adult_true_alpha + adult_spec_rich, family = gaussian(), data = data)
# Using gaussian family as sapdiv is log transformed

check_collinearity(glm_sapdiv)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_50 29.590860 23.851437 36.771810 5.439748 0.0337942 0.0271947 0.0419262
zmean_50 13.213720 10.711616 16.360466 3.635068 0.0756789 0.0611230 0.0933566
cv_area_200 6.191233 5.077722 7.608812 2.488219 0.1615187 0.1314266 0.1969387
zcv_50 21.281723 17.184747 26.415799 4.613212 0.0469887 0.0378561 0.0581911
treedensity 21.487537 17.349878 26.672313 4.635465 0.0465386 0.0374921 0.0576373
herbaceous 27.929300 22.518310 34.700936 5.284818 0.0358047 0.0288177 0.0444083
woody 10.937822 8.885662 13.524037 3.307238 0.0914259 0.0739424 0.1125409
deadwood 5.006187 4.127184 6.132264 2.237451 0.1997528 0.1630719 0.2422960
leaf_litter 31.909364 25.711655 39.661465 5.648837 0.0313388 0.0252134 0.0388929
Litter.depth 4.246666 3.518067 5.186083 2.060744 0.2354789 0.1928238 0.2842470
Domin 4.310541 3.569289 5.265649 2.076184 0.2319894 0.1899101 0.2801678
forest_type 8.441617 6.883017 10.413141 2.905446 0.1184607 0.0960325 0.1452851
adult_true_alpha 38.152454 30.720715 47.442519 6.176767 0.0262106 0.0210781 0.0325513
adult_spec_rich 37.956045 30.563128 47.197725 6.160848 0.0263463 0.0211875 0.0327192
# remove leaf litter, adult div, canopy cover, zcv

glm_sapdiv <- glmmTMB(sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity +
                        herbaceous + woody + deadwood + Litter.depth + Domin +
                        forest_type + adult_spec_rich, family = gaussian(), data = data)
check_collinearity(glm_sapdiv)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
zmean_50 2.348275 1.838977 3.166742 1.532408 0.4258444 0.3157820 0.5437806
cv_area_200 2.692371 2.080934 3.649670 1.640845 0.3714199 0.2739974 0.4805535
treedensity 2.086081 1.655207 2.800305 1.444327 0.4793677 0.3571040 0.6041540
herbaceous 1.862488 1.499266 2.489957 1.364730 0.5369163 0.4016133 0.6669931
woody 1.247346 1.086936 1.703736 1.116847 0.8017020 0.5869453 0.9200171
deadwood 2.038642 1.622045 2.734243 1.427810 0.4905226 0.3657320 0.6165057
Litter.depth 2.727534 2.105692 3.699102 1.651525 0.3666315 0.2703359 0.4749032
Domin 3.303974 2.512057 4.510644 1.817684 0.3026659 0.2216979 0.3980801
forest_type 1.822816 1.471717 2.435240 1.350117 0.5486017 0.4106371 0.6794787
adult_spec_rich 2.094875 1.661358 2.812560 1.447368 0.4773555 0.3555479 0.6019174
stepAIC(glm_sapdiv)
Start:  AIC=47.14
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + forest_type + adult_spec_rich

                  Df    AIC
- herbaceous       1 45.616
- woody            1 46.654
- deadwood         1 46.773
<none>               47.140
- adult_spec_rich  1 49.275
- cv_area_200      1 50.067
- treedensity      1 52.188
- forest_type      1 57.268
- Domin            1 57.482
- Litter.depth     1 58.410
- zmean_50         1 64.487

Step:  AIC=45.62
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + woody + 
    deadwood + Litter.depth + Domin + forest_type + adult_spec_rich

                  Df    AIC
- woody            1 44.867
<none>               45.616
- deadwood         1 45.723
- adult_spec_rich  1 47.314
- cv_area_200      1 48.699
- treedensity      1 50.188
- Litter.depth     1 56.422
- Domin            1 57.142
- forest_type      1 57.260
- zmean_50         1 66.291

Step:  AIC=44.87
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + deadwood + 
    Litter.depth + Domin + forest_type + adult_spec_rich

                  Df    AIC
<none>               44.867
- deadwood         1 45.278
- adult_spec_rich  1 46.785
- cv_area_200      1 47.190
- treedensity      1 48.383
- forest_type      1 55.382
- Domin            1 56.407
- Litter.depth     1 56.443
- zmean_50         1 64.765
Formula:          
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + deadwood +  
    Litter.depth + Domin + forest_type + adult_spec_rich
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
 44.86717  56.64771 -12.43358  24.86717        14 

Number of obs: 24

Dispersion estimate for gaussian family (sigma^2): 0.165 

Fixed Effects:

Conditional model:
          (Intercept)               zmean_50            cv_area_200  
              4.67521               -0.22666               -0.05472  
          treedensity               deadwood           Litter.depth  
             -7.98579               -0.04395                0.22183  
                Domin  forest_typeplantation        adult_spec_rich  
             -1.15533                0.94791                0.26400  
finalsapdivmodel <- glmmTMB(sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + deadwood + Litter.depth + forest_type + adult_spec_rich, family = gaussian(), data = data)

check_collinearity(finalsapdivmodel)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
zmean_50 1.733613 1.349796 2.538577 1.316668 0.5768299 0.3939214 0.7408526
cv_area_200 1.925325 1.469518 2.823626 1.387561 0.5193930 0.3541545 0.6804952
treedensity 1.763089 1.368094 2.581945 1.327813 0.5671864 0.3873049 0.7309440
deadwood 1.902716 1.455324 2.789706 1.379390 0.5255645 0.3584608 0.6871321
Litter.depth 1.348708 1.119373 2.018631 1.161339 0.7414503 0.4953852 0.8933571
forest_type 1.637973 1.290808 2.399583 1.279833 0.6105107 0.4167392 0.7747088
adult_spec_rich 1.855239 1.425575 2.718698 1.362072 0.5390140 0.3678231 0.7014715
sim <- simulateResiduals(fittedModel = finalsapdivmodel, n = 100, refit = TRUE)
plot(sim)

# Core tests
testUniformity(sim)     # overall fit (KS on uniformity)


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.20539, p-value = 0.2296
alternative hypothesis: two-sided
testDispersion(sim)     # over/underdispersion for NB


    DHARMa nonparametric dispersion test via mean deviance residual fitted
    vs. simulated-refitted

data:  sim
dispersion = 1.4932, p-value = 0.2
alternative hypothesis: two.sided
testZeroInflation(sim)  # excess zeros (if relevant)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = NaN, p-value = 1
alternative hypothesis: two.sided
# ALL GOOD

summary(finalsapdivmodel)
 Family: gaussian  ( identity )
Formula:          
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + deadwood +  
    Litter.depth + forest_type + adult_spec_rich
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
     56.4      67.0     -19.2      38.4        15 


Dispersion estimate for gaussian family (sigma^2): 0.29 

Conditional model:
                        Estimate Std. Error z value Pr(>|z|)    
(Intercept)             3.717101   0.900453   4.128 3.66e-05 ***
zmean_50               -0.212118   0.050047  -4.238 2.25e-05 ***
cv_area_200             0.000309   0.028610   0.011  0.99138    
treedensity           -11.062202   4.143179  -2.670  0.00759 ** 
deadwood               -0.044508   0.036591  -1.216  0.22385    
Litter.depth            0.068351   0.049626   1.377  0.16841    
forest_typeplantation   0.889283   0.309564   2.873  0.00407 ** 
adult_spec_rich         0.133005   0.164728   0.807  0.41942    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# zmean50, tree density, plantation  affects sapling diversity

Grazing

# grazing effects
gdata <- data %>%
  filter(forest_type == "natural")
# just select natural forests 

# Test differences between grazing
wilcox.test(seedlings ~ grazing, data = gdata) # Sig diff

    Wilcoxon rank sum exact test

data:  seedlings by grazing
W = 14, p-value = 0.04309
alternative hypothesis: true location shift is not equal to 0
wilcox.test(saplings ~ grazing, data = gdata) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  saplings by grazing
W = 29.5, p-value = 0.6247
alternative hypothesis: true location shift is not equal to 0
t.test(seed_rich ~ grazing, data = gdata) # No sig diff

    Welch Two Sample t-test

data:  seed_rich by grazing
t = -0.20642, df = 13.83, p-value = 0.8395
alternative hypothesis: true difference in means between group n and group y is not equal to 0
95 percent confidence interval:
 -1.954695  1.611838
sample estimates:
mean in group n mean in group y 
       3.428571        3.600000 
t.test(seed_true_alpha ~ grazing, data = gdata) # No sig diff

    Welch Two Sample t-test

data:  seed_true_alpha by grazing
t = 0.85408, df = 9.4327, p-value = 0.4142
alternative hypothesis: true difference in means between group n and group y is not equal to 0
95 percent confidence interval:
 -0.5328947  1.1866571
sample estimates:
mean in group n mean in group y 
       2.142578        1.815697 
wilcox.test(sap_rich ~ grazing, data = gdata) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  sap_rich by grazing
W = 40.5, p-value = 0.6086
alternative hypothesis: true location shift is not equal to 0
wilcox.test(sap_true_alpha ~ grazing, data = gdata) # No sig diff
Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...): cannot
compute exact p-value with ties

    Wilcoxon rank sum test with continuity correction

data:  sap_true_alpha by grazing
W = 43.5, p-value = 0.4249
alternative hypothesis: true location shift is not equal to 0
# Seedlings
gseedglm <- glmmTMB(seedlings ~ zmean + cv_area + treedensity +
                      herbaceous + woody + deadwood + Litter.depth + Domin +
                      grazing, family = nbinom2, data = gdata)
check_collinearity(gseedglm)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
zmean 3.330408 2.702992 4.188975 1.824940 0.3002636 0.2387219 0.3699603
cv_area 1.921015 1.618335 2.371858 1.386007 0.5205582 0.4216103 0.6179190
treedensity 2.595517 2.136577 3.239774 1.611061 0.3852797 0.3086635 0.4680384
herbaceous 2.933178 2.396716 3.675689 1.712652 0.3409271 0.2720578 0.4172376
woody 1.749461 1.487269 2.152733 1.322672 0.5716047 0.4645258 0.6723732
deadwood 2.935130 2.398220 3.678210 1.713222 0.3407004 0.2718714 0.4169758
Litter.depth 1.919398 1.617098 2.369788 1.385423 0.5209967 0.4219787 0.6183919
Domin 2.950230 2.409859 3.697714 1.717623 0.3389566 0.2704374 0.4149620
grazing 3.297788 2.677835 4.146811 1.815981 0.3032336 0.2411491 0.3734361
#remove cc + ll + zcv

stepAIC(gseedglm, direction = "both")
Start:  AIC=199.33
seedlings ~ zmean + cv_area + treedensity + herbaceous + woody + 
    deadwood + Litter.depth + Domin + grazing

               Df    AIC
- woody         1 197.41
- zmean         1 197.52
- Litter.depth  1 197.96
- herbaceous    1 198.07
- cv_area       1 198.13
<none>            199.33
- deadwood      1 200.28
- treedensity   1 200.94
- Domin         1 202.43
- grazing       1 208.07

Step:  AIC=197.42
seedlings ~ zmean + cv_area + treedensity + herbaceous + deadwood + 
    Litter.depth + Domin + grazing

               Df    AIC
- zmean         1 195.65
- herbaceous    1 196.08
- Litter.depth  1 196.22
- cv_area       1 196.25
<none>            197.41
- deadwood      1 198.44
- treedensity   1 199.16
+ woody         1 199.33
- Domin         1 200.43
- grazing       1 206.26

Step:  AIC=195.65
seedlings ~ cv_area + treedensity + herbaceous + deadwood + Litter.depth + 
    Domin + grazing

               Df    AIC
- Litter.depth  1 194.46
- herbaceous    1 194.55
- cv_area       1 195.28
<none>            195.65
- deadwood      1 197.16
+ zmean         1 197.41
+ woody         1 197.52
- treedensity   1 198.00
- Domin         1 198.60
- grazing       1 208.30

Step:  AIC=194.46
seedlings ~ cv_area + treedensity + herbaceous + deadwood + Domin + 
    grazing

               Df    AIC
- herbaceous    1 193.00
<none>            194.46
- cv_area       1 194.66
- deadwood      1 195.60
+ Litter.depth  1 195.65
+ woody         1 196.12
+ zmean         1 196.22
- treedensity   1 196.52
- Domin         1 197.08
- grazing       1 206.30

Step:  AIC=193
seedlings ~ cv_area + treedensity + deadwood + Domin + grazing

               Df    AIC
<none>            193.00
- deadwood      1 193.92
- cv_area       1 194.30
+ herbaceous    1 194.46
+ Litter.depth  1 194.55
+ zmean         1 194.58
- treedensity   1 194.63
+ woody         1 194.87
- Domin         1 195.49
- grazing       1 204.62
Formula:          
seedlings ~ cv_area + treedensity + deadwood + Domin + grazing
Data: gdata
      AIC       BIC    logLik -2*log(L)  df.resid 
193.00037 198.83287 -89.50019 179.00037        10 

Number of obs: 17

Dispersion parameter for nbinom2 family (): 2.14 

Fixed Effects:

Conditional model:
(Intercept)      cv_area  treedensity     deadwood        Domin     grazingy  
    5.18896     -0.71681     14.79319     -0.08473     -1.03698      2.30894  
finalgseed <- glmmTMB(seedlings ~ cv_area + treedensity + deadwood + Domin + grazing, family = compois, data = gdata)

sim <- simulateResiduals(fittedModel = finalgseed, refit = FALSE)
plot(sim)

testUniformity(sim)    


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.18655, p-value = 0.5345
alternative hypothesis: two-sided
testDispersion(sim)     


    DHARMa nonparametric dispersion test via sd of residuals fitted vs.
    simulated

data:  simulationOutput
dispersion = 0.9665, p-value = 0.856
alternative hypothesis: two.sided
testZeroInflation(sim)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = 0, p-value = 1
alternative hypothesis: two.sided
summary(finalgseed)
 Family: compois  ( log )
Formula:          
seedlings ~ cv_area + treedensity + deadwood + Domin + grazing
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
    188.4     194.3     -87.2     174.4        10 


Dispersion parameter for compois family (): 45.3 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  4.90406    0.71668   6.843 7.77e-12 ***
cv_area     -0.50661    0.32484  -1.560  0.11886    
treedensity 13.54975    7.18161   1.887  0.05920 .  
deadwood    -0.08694    0.03497  -2.487  0.01290 *  
Domin       -1.02567    0.35491  -2.890  0.00385 ** 
grazingy     2.43789    0.47198   5.165 2.40e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Grazing has effect on seeds




# Saplings
gsapglm <- glmmTMB(saplings ~ canopy_cover_perc_200 + zmean_200 + cv_area_200 + zcv_200 + treedensity +
          herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
          grazing, family = nbinom2, data = gdata)
check_collinearity(gsapglm)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_200 6.479086 5.894991 7.132878 2.545405 0.1543428 0.1401959 0.1696355
zmean_200 12.618203 11.431482 13.939929 3.552211 0.0792506 0.0717364 0.0874777
cv_area_200 14.557261 13.180220 16.089983 3.815398 0.0686942 0.0621505 0.0758713
zcv_200 5.409932 4.930820 5.947442 2.325926 0.1848452 0.1681395 0.2028060
treedensity 9.378201 8.509503 10.347389 3.062385 0.1066303 0.0966427 0.1175157
herbaceous 17.366776 15.713987 19.205220 4.167346 0.0575812 0.0520692 0.0636376
woody 8.160701 7.411517 8.997428 2.856694 0.1225385 0.1111429 0.1349252
deadwood 2.944392 2.707597 3.214025 1.715923 0.3396287 0.3111364 0.3693312
leaf_litter 26.358251 23.822986 29.175142 5.134029 0.0379388 0.0342758 0.0419763
Litter.depth 3.004564 2.761845 3.280721 1.733368 0.3328270 0.3048110 0.3620768
Domin 5.629450 5.128781 6.190832 2.372646 0.1776372 0.1615292 0.1949781
grazing 3.573286 3.274620 3.911168 1.890314 0.2798544 0.2556781 0.3053789
#remove cc + ll + zcv + herb
gsapglm <- glmmTMB(saplings ~ canopy_cover_perc_200 + zcv_200 + treedensity +
                     herbaceous + woody + deadwood + Litter.depth + Domin +
                     grazing, family = nbinom2, data = gdata)
check_collinearity(gsapglm)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_200 2.704646 2.220623 3.380603 1.644581 0.3697341 0.2958052 0.4503241
zcv_200 2.656527 2.183560 3.318498 1.629886 0.3764313 0.3013411 0.4579678
treedensity 3.268650 2.655363 4.109150 1.807941 0.3059367 0.2433593 0.3765963
herbaceous 3.214590 2.613674 4.039282 1.792928 0.3110816 0.2475687 0.3826032
woody 1.951958 1.642028 2.411501 1.397125 0.5123062 0.4146795 0.6090029
deadwood 3.137306 2.554080 3.939408 1.771244 0.3187449 0.2538452 0.3915304
Litter.depth 2.354867 1.951373 2.929488 1.534558 0.4246525 0.3413565 0.5124596
Domin 4.090629 3.289532 5.172026 2.022530 0.2444612 0.1933478 0.3039946
grazing 2.896129 2.368161 3.627837 1.701802 0.3452885 0.2756463 0.4222685
stepAIC(gsapglm, direction = "both")
Start:  AIC=130.74
saplings ~ canopy_cover_perc_200 + zcv_200 + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + grazing

                        Df    AIC
- zcv_200                1 128.74
- Litter.depth           1 128.74
- canopy_cover_perc_200  1 129.02
- herbaceous             1 129.04
- treedensity            1 129.15
- deadwood               1 129.32
- Domin                  1 130.15
- grazing                1 130.50
<none>                     130.74
- woody                  1 133.61

Step:  AIC=128.74
saplings ~ canopy_cover_perc_200 + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + grazing

                        Df    AIC
- Litter.depth           1 126.74
- herbaceous             1 127.08
- canopy_cover_perc_200  1 127.17
- treedensity            1 127.19
- deadwood               1 127.39
- grazing                1 128.51
- Domin                  1 128.56
<none>                     128.74
+ zcv_200                1 130.74
- woody                  1 131.72

Step:  AIC=126.74
saplings ~ canopy_cover_perc_200 + treedensity + herbaceous + 
    woody + deadwood + Domin + grazing

                        Df    AIC
- herbaceous             1 125.08
- canopy_cover_perc_200  1 125.19
- treedensity            1 125.20
- deadwood               1 125.39
- grazing                1 126.71
<none>                     126.74
- Domin                  1 127.35
+ Litter.depth           1 128.74
+ zcv_200                1 128.74
- woody                  1 129.94

Step:  AIC=125.08
saplings ~ canopy_cover_perc_200 + treedensity + woody + deadwood + 
    Domin + grazing

                        Df    AIC
- treedensity            1 123.34
- canopy_cover_perc_200  1 123.53
- deadwood               1 124.76
<none>                     125.08
- grazing                1 125.19
- Domin                  1 125.75
+ herbaceous             1 126.74
+ zcv_200                1 127.05
+ Litter.depth           1 127.08
- woody                  1 127.94

Step:  AIC=123.34
saplings ~ canopy_cover_perc_200 + woody + deadwood + Domin + 
    grazing

                        Df    AIC
- canopy_cover_perc_200  1 121.98
- grazing                1 123.23
<none>                     123.34
- Domin                  1 123.95
+ treedensity            1 125.08
+ herbaceous             1 125.20
+ zcv_200                1 125.27
+ Litter.depth           1 125.33
- deadwood               1 126.09
- woody                  1 126.79

Step:  AIC=121.98
saplings ~ woody + deadwood + Domin + grazing

                        Df    AIC
<none>                     121.98
- Domin                  1 122.13
+ canopy_cover_perc_200  1 123.34
+ treedensity            1 123.53
- grazing                1 123.65
+ herbaceous             1 123.90
+ zcv_200                1 123.94
+ Litter.depth           1 123.98
- deadwood               1 125.02
- woody                  1 125.93
Formula:          saplings ~ woody + deadwood + Domin + grazing
Data: gdata
      AIC       BIC    logLik -2*log(L)  df.resid 
121.98088 126.98016 -54.99044 109.98088        11 

Number of obs: 17

Dispersion parameter for nbinom2 family (): 3.24 

Fixed Effects:

Conditional model:
(Intercept)        woody     deadwood        Domin     grazingy  
    1.69424      0.03676      0.07952     -0.61688      0.81257  
finalgssap <- glmmTMB(saplings ~ woody + deadwood + Domin + grazing, family = nbinom2, data = gdata)

sim <- simulateResiduals(fittedModel = finalgssap, refit = FALSE)
plot(sim)

testUniformity(sim)    


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.16417, p-value = 0.6898
alternative hypothesis: two-sided
testDispersion(sim)     


    DHARMa nonparametric dispersion test via sd of residuals fitted vs.
    simulated

data:  simulationOutput
dispersion = 1.1337, p-value = 0.528
alternative hypothesis: two.sided
testZeroInflation(sim)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = 0, p-value = 1
alternative hypothesis: two.sided
summary(finalgssap)
 Family: nbinom2  ( log )
Formula:          saplings ~ woody + deadwood + Domin + grazing
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
      122       127       -55       110        11 


Dispersion parameter for nbinom2 family (): 3.24 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)   
(Intercept)  1.69424    0.55365   3.060  0.00221 **
woody        0.03676    0.01420   2.589  0.00962 **
deadwood     0.07952    0.03392   2.344  0.01908 * 
Domin       -0.61688    0.40540  -1.522  0.12809   
grazingy     0.81257    0.40833   1.990  0.04659 * 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# grazing has effect on saps




# Seed Rich

gseedrich <- lm(seed_rich ~ canopy_cover_perc_50 + zmean + cv_area_50 + zcv + treedensity +
                    herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                    grazing + adult_true_alpha + adult_spec_rich, data = gdata)
vif(gseedrich)
canopy_cover_perc_50                zmean           cv_area_50 
           16.587241             9.202224             6.933326 
                 zcv          treedensity           herbaceous 
           21.145118            26.169855            45.238779 
               woody             deadwood          leaf_litter 
           10.548716             7.928340            45.841474 
        Litter.depth                Domin              grazing 
            3.859984             5.681977             9.597462 
    adult_true_alpha      adult_spec_rich 
          119.438537           147.521083 
gseedrich <- lm(seed_rich ~ zmean + cv_area_50 + treedensity +
                  herbaceous + woody + deadwood + Litter.depth + Domin +
                  grazing + adult_true_alpha, data = gdata)
vif(gseedrich)
           zmean       cv_area_50      treedensity       herbaceous 
        3.226710         1.968080         2.398554         3.130790 
           woody         deadwood     Litter.depth            Domin 
        2.243590         3.570916         2.541962         3.597452 
         grazing adult_true_alpha 
        4.705667         3.648858 
stepAIC(gseedrich, direction = "both")
Start:  AIC=22.53
seed_rich ~ zmean + cv_area_50 + treedensity + herbaceous + woody + 
    deadwood + Litter.depth + Domin + grazing + adult_true_alpha

                   Df Sum of Sq    RSS    AIC
- herbaceous        1    0.2419 17.782 20.764
- cv_area_50        1    0.4486 17.988 20.961
- deadwood          1    0.5349 18.075 21.042
- adult_true_alpha  1    0.7314 18.271 21.226
- woody             1    0.9479 18.488 21.426
- zmean             1    1.2022 18.742 21.658
<none>                          17.540 22.531
- treedensity       1    5.0020 22.542 24.797
- grazing           1    8.8832 26.423 27.497
- Domin             1    9.6716 27.211 27.997
- Litter.depth      1   14.8401 32.380 30.953

Step:  AIC=20.76
seed_rich ~ zmean + cv_area_50 + treedensity + woody + deadwood + 
    Litter.depth + Domin + grazing + adult_true_alpha

                   Df Sum of Sq    RSS    AIC
- cv_area_50        1    0.5808 18.362 19.311
- adult_true_alpha  1    0.6334 18.415 19.359
- deadwood          1    0.9575 18.739 19.656
- woody             1    1.6861 19.468 20.304
<none>                          17.782 20.764
- zmean             1    2.4016 20.183 20.918
+ herbaceous        1    0.2419 17.540 22.531
- treedensity       1    5.6142 23.396 23.429
- grazing           1   10.1194 27.901 26.423
- Domin             1   13.3065 31.088 28.261
- Litter.depth      1   14.6295 32.411 28.970

Step:  AIC=19.31
seed_rich ~ zmean + treedensity + woody + deadwood + Litter.depth + 
    Domin + grazing + adult_true_alpha

                   Df Sum of Sq    RSS    AIC
- adult_true_alpha  1    0.3987 18.761 17.676
- deadwood          1    0.5277 18.890 17.792
- woody             1    1.1563 19.519 18.349
- zmean             1    2.2628 20.625 19.286
<none>                          18.362 19.311
+ cv_area_50        1    0.5808 17.782 20.764
+ herbaceous        1    0.3741 17.988 20.961
- treedensity       1    5.4184 23.781 21.706
- grazing           1    9.5931 27.956 24.456
- Domin             1   12.7271 31.090 26.262
- Litter.depth      1   14.3235 32.686 27.113

Step:  AIC=17.68
seed_rich ~ zmean + treedensity + woody + deadwood + Litter.depth + 
    Domin + grazing

                   Df Sum of Sq    RSS    AIC
- deadwood          1    0.1847 18.946 15.842
- woody             1    1.6481 20.409 17.107
<none>                          18.761 17.676
- zmean             1    3.1744 21.936 18.333
+ adult_true_alpha  1    0.3987 18.362 19.311
+ cv_area_50        1    0.3461 18.415 19.359
+ herbaceous        1    0.2401 18.521 19.457
- treedensity       1    5.0977 23.859 19.762
- grazing           1   13.1629 31.924 24.712
- Domin             1   13.6349 32.396 24.962
- Litter.depth      1   15.0012 33.762 25.664

Step:  AIC=15.84
seed_rich ~ zmean + treedensity + woody + Litter.depth + Domin + 
    grazing

                   Df Sum of Sq    RSS    AIC
- woody             1    1.9166 20.862 15.480
<none>                          18.946 15.842
- zmean             1    2.9898 21.936 16.333
+ herbaceous        1    0.4080 18.538 17.472
+ deadwood          1    0.1847 18.761 17.676
+ cv_area_50        1    0.1582 18.788 17.700
+ adult_true_alpha  1    0.0557 18.890 17.792
- treedensity       1    6.1202 25.066 18.601
- grazing           1   13.3404 32.286 22.904
- Domin             1   13.6069 32.553 23.044
- Litter.depth      1   15.1962 34.142 23.854

Step:  AIC=15.48
seed_rich ~ zmean + treedensity + Litter.depth + Domin + grazing

                   Df Sum of Sq    RSS    AIC
- zmean             1    2.5863 23.449 15.467
<none>                          20.862 15.480
+ woody             1    1.9166 18.946 15.842
+ herbaceous        1    1.2865 19.576 16.398
+ deadwood          1    0.4532 20.409 17.107
+ adult_true_alpha  1    0.1427 20.720 17.364
+ cv_area_50        1    0.1242 20.738 17.379
- treedensity       1    7.6441 28.507 18.788
- grazing           1   11.5283 32.391 20.959
- Domin             1   12.0371 32.900 21.224
- Litter.depth      1   15.9466 36.809 23.133

Step:  AIC=15.47
seed_rich ~ treedensity + Litter.depth + Domin + grazing

                   Df Sum of Sq    RSS    AIC
<none>                          23.449 15.467
+ zmean             1    2.5863 20.862 15.480
+ herbaceous        1    2.3249 21.124 15.692
+ woody             1    1.5131 21.936 16.333
+ adult_true_alpha  1    0.9085 22.540 16.796
+ cv_area_50        1    0.1185 23.330 17.381
+ deadwood          1    0.0576 23.391 17.425
- treedensity       1    6.9002 30.349 17.852
- grazing           1    9.5570 33.006 19.279
- Domin             1   10.8778 34.326 19.946
- Litter.depth      1   13.9386 37.387 21.398

Call:
lm(formula = seed_rich ~ treedensity + Litter.depth + Domin + 
    grazing, data = gdata)

Coefficients:
 (Intercept)   treedensity  Litter.depth         Domin      grazingy  
      2.3183       22.9729        0.4993       -2.6027        2.0389  
finalgseedrich <- lm(seed_rich ~ treedensity + Litter.depth + Domin + grazing, data = gdata)

check_model(finalgseedrich)

summary(finalgseedrich)

Call:
lm(formula = seed_rich ~ treedensity + Litter.depth + Domin + 
    grazing, data = gdata)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.76354 -0.25706  0.02638  0.38239  2.13881 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)  
(Intercept)    2.3183     1.0961   2.115   0.0560 .
treedensity   22.9729    12.2251   1.879   0.0847 .
Litter.depth   0.4993     0.1870   2.671   0.0204 *
Domin         -2.6027     1.1031  -2.359   0.0361 *
grazingy       2.0389     0.9220   2.212   0.0472 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.398 on 12 degrees of freedom
Multiple R-squared:  0.4699,    Adjusted R-squared:  0.2932 
F-statistic: 2.659 on 4 and 12 DF,  p-value: 0.08474
# grazing affects sap rich





# Sap Rich
gsaprich <- glmmTMB(sap_rich ~ canopy_cover_perc_200 + zmean_200 + cv_area_200 + zcv_200 + treedensity +
                      herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                      grazing + adult_true_alpha + adult_spec_rich, family = poisson, data = gdata)
check_collinearity(gsaprich)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_200 10.907054 10.383856 11.459424 3.302583 0.0916838 0.0872644 0.0963033
zmean_200 16.639581 15.827805 17.495800 4.079164 0.0600977 0.0571566 0.0631800
cv_area_200 23.758296 22.588168 24.991847 4.874248 0.0420906 0.0400130 0.0442710
zcv_200 7.307898 6.965894 7.669508 2.703312 0.1368383 0.1303865 0.1435566
treedensity 43.960896 41.773795 46.265312 6.630301 0.0227475 0.0216145 0.0239385
herbaceous 21.174221 20.134174 22.270801 4.601546 0.0472272 0.0449018 0.0496668
woody 22.366455 21.266392 23.526230 4.729319 0.0447098 0.0425057 0.0470226
deadwood 5.800591 5.534478 6.082321 2.408442 0.1723962 0.1644109 0.1806855
leaf_litter 33.063668 31.425118 34.790461 5.750102 0.0302447 0.0287435 0.0318217
Litter.depth 3.720839 3.559468 3.892385 1.928948 0.2687566 0.2569119 0.2809409
Domin 22.311187 21.213906 23.468033 4.723472 0.0448206 0.0426112 0.0471389
grazing 3.192396 3.057653 3.335962 1.786728 0.3132444 0.2997636 0.3270482
adult_true_alpha 263.459946 250.223604 277.399274 16.231449 0.0037956 0.0036049 0.0039964
adult_spec_rich 310.577971 294.969783 327.014870 17.623223 0.0032198 0.0030580 0.0033902
gsaprich <- glmmTMB(sap_rich ~ canopy_cover_perc_200 + zcv_200 + treedensity +
                      herbaceous + woody + deadwood + Litter.depth + Domin +
                      grazing + adult_true_alpha, family = poisson, data = gdata)
check_collinearity(gsaprich)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_200 2.926612 2.391655 3.667208 1.710734 0.3416920 0.2726870 0.4181204
zcv_200 2.690425 2.209669 3.362248 1.640252 0.3716884 0.2974201 0.4525565
treedensity 2.619905 2.155356 3.271240 1.618612 0.3816933 0.3056945 0.4639605
herbaceous 3.733534 3.013975 4.710179 1.932235 0.2678428 0.2123062 0.3317878
woody 1.686617 1.439427 2.072860 1.298698 0.5929028 0.4824253 0.6947211
deadwood 3.507384 2.839501 4.417760 1.872801 0.2851128 0.2263591 0.3521745
Litter.depth 3.058098 2.493011 3.837064 1.748742 0.3270006 0.2606159 0.4011214
Domin 3.874993 3.123126 4.893121 1.968500 0.2580650 0.2043686 0.3201920
grazing 2.768370 2.269714 3.462863 1.663842 0.3612234 0.2887784 0.4405842
adult_true_alpha 3.680659 2.973179 4.641805 1.918504 0.2716905 0.2154335 0.3363403
stepAIC(gsaprich, direction = "both")
Start:  AIC=69.93
sap_rich ~ canopy_cover_perc_200 + zcv_200 + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + grazing + adult_true_alpha

                        Df    AIC
- deadwood               1 67.935
- treedensity            1 67.956
- zcv_200                1 67.976
- adult_true_alpha       1 68.157
- woody                  1 68.271
- herbaceous             1 68.566
- Domin                  1 68.873
- grazing                1 68.928
- canopy_cover_perc_200  1 69.118
<none>                     69.929
- Litter.depth           1 73.034

Step:  AIC=67.94
sap_rich ~ canopy_cover_perc_200 + zcv_200 + treedensity + herbaceous + 
    woody + Litter.depth + Domin + grazing + adult_true_alpha

                        Df    AIC
- treedensity            1 65.959
- zcv_200                1 65.977
- woody                  1 66.271
- adult_true_alpha       1 66.324
- herbaceous             1 66.633
- grazing                1 66.928
- canopy_cover_perc_200  1 67.120
- Domin                  1 67.157
<none>                     67.935
+ deadwood               1 69.929
- Litter.depth           1 71.088

Step:  AIC=65.96
sap_rich ~ canopy_cover_perc_200 + zcv_200 + herbaceous + woody + 
    Litter.depth + Domin + grazing + adult_true_alpha

                        Df    AIC
- zcv_200                1 63.996
- woody                  1 64.271
- adult_true_alpha       1 64.353
- herbaceous             1 64.674
- canopy_cover_perc_200  1 65.176
- grazing                1 65.185
- Domin                  1 65.188
<none>                     65.959
+ treedensity            1 67.935
+ deadwood               1 67.956
- Litter.depth           1 69.255

Step:  AIC=64
sap_rich ~ canopy_cover_perc_200 + herbaceous + woody + Litter.depth + 
    Domin + grazing + adult_true_alpha

                        Df    AIC
- woody                  1 62.276
- adult_true_alpha       1 62.658
- herbaceous             1 62.904
- Domin                  1 63.191
- grazing                1 63.288
- canopy_cover_perc_200  1 63.355
<none>                     63.996
+ zcv_200                1 65.959
+ treedensity            1 65.977
+ deadwood               1 65.992
- Litter.depth           1 67.425

Step:  AIC=62.28
sap_rich ~ canopy_cover_perc_200 + herbaceous + Litter.depth + 
    Domin + grazing + adult_true_alpha

                        Df    AIC
- adult_true_alpha       1 61.025
- Domin                  1 61.232
- grazing                1 61.363
- canopy_cover_perc_200  1 61.542
- herbaceous             1 61.896
<none>                     62.276
+ woody                  1 63.996
+ zcv_200                1 64.271
+ deadwood               1 64.276
+ treedensity            1 64.276
- Litter.depth           1 66.115

Step:  AIC=61.03
sap_rich ~ canopy_cover_perc_200 + herbaceous + Litter.depth + 
    Domin + grazing

                        Df    AIC
- Domin                  1 59.632
- herbaceous             1 60.057
- canopy_cover_perc_200  1 60.485
- grazing                1 60.511
<none>                     61.025
+ adult_true_alpha       1 62.276
+ woody                  1 62.658
+ zcv_200                1 62.837
+ deadwood               1 62.900
+ treedensity            1 63.025
- Litter.depth           1 64.115

Step:  AIC=59.63
sap_rich ~ canopy_cover_perc_200 + herbaceous + Litter.depth + 
    grazing

                        Df    AIC
- grazing                1 58.692
- canopy_cover_perc_200  1 59.049
<none>                     59.632
- herbaceous             1 59.709
+ Domin                  1 61.025
+ adult_true_alpha       1 61.232
+ deadwood               1 61.388
+ woody                  1 61.521
+ zcv_200                1 61.589
+ treedensity            1 61.621
- Litter.depth           1 62.287

Step:  AIC=58.69
sap_rich ~ canopy_cover_perc_200 + herbaceous + Litter.depth

                        Df    AIC
- canopy_cover_perc_200  1 57.213
- herbaceous             1 57.801
<none>                     58.692
+ grazing                1 59.632
+ adult_true_alpha       1 59.923
- Litter.depth           1 60.294
+ zcv_200                1 60.457
+ treedensity            1 60.460
+ Domin                  1 60.511
+ woody                  1 60.660
+ deadwood               1 60.665

Step:  AIC=57.21
sap_rich ~ herbaceous + Litter.depth

                        Df    AIC
- herbaceous             1 56.724
<none>                     57.213
- Litter.depth           1 58.295
+ adult_true_alpha       1 58.557
+ canopy_cover_perc_200  1 58.692
+ Domin                  1 58.915
+ grazing                1 59.049
+ treedensity            1 59.060
+ deadwood               1 59.155
+ woody                  1 59.162
+ zcv_200                1 59.205

Step:  AIC=56.72
sap_rich ~ Litter.depth

                        Df    AIC
<none>                     56.724
- Litter.depth           1 56.889
+ herbaceous             1 57.213
+ Domin                  1 57.402
+ canopy_cover_perc_200  1 57.801
+ woody                  1 58.297
+ zcv_200                1 58.549
+ grazing                1 58.597
+ deadwood               1 58.712
+ treedensity            1 58.714
+ adult_true_alpha       1 58.719
Formula:          sap_rich ~ Litter.depth
Data: gdata
      AIC       BIC    logLik -2*log(L)  df.resid 
 56.72384  58.39026 -26.36192  52.72384        15 

Number of obs: 17

Fixed Effects:

Conditional model:
 (Intercept)  Litter.depth  
     0.37670       0.09662  
finalgsaprich <- glmmTMB(sap_rich ~ Litter.depth, family = poisson, data = gdata)

sim <- simulateResiduals(fittedModel = finalgsaprich, refit = FALSE)
plot(sim)

testUniformity(sim)    


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.21272, p-value = 0.372
alternative hypothesis: two-sided
testDispersion(sim)     


    DHARMa nonparametric dispersion test via sd of residuals fitted vs.
    simulated

data:  simulationOutput
dispersion = 0.44023, p-value = 0.056
alternative hypothesis: two.sided
testZeroInflation(sim)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = 0, p-value = 0.24
alternative hypothesis: two.sided
summary(finalgsaprich)
 Family: poisson  ( log )
Formula:          sap_rich ~ Litter.depth
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
     56.7      58.4     -26.4      52.7        15 


Conditional model:
             Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.37670    0.37990   0.992    0.321
Litter.depth  0.09662    0.06595   1.465    0.143
# no grazing effect on sap rich


# Seed Div
gseeddiv <- lm(seed_true_alpha ~ canopy_cover_perc + zmean + cv_area + zcv + treedensity +
                 herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                 grazing + adult_true_alpha + adult_spec_rich,data = gdata)
vif(gseeddiv)
canopy_cover_perc             zmean           cv_area               zcv 
        40.981436         10.597305          3.107573         10.741935 
      treedensity        herbaceous             woody          deadwood 
        37.423382         23.256017          8.961727          7.658527 
      leaf_litter      Litter.depth             Domin           grazing 
        46.521730          2.957840          7.492340          5.748287 
 adult_true_alpha   adult_spec_rich 
       105.441686        135.616114 
gseeddiv <- lm(seed_true_alpha ~ cv_area + zcv + treedensity +
                 herbaceous + woody + deadwood + Litter.depth + Domin +
                 grazing + adult_true_alpha,data = gdata)
vif(gseeddiv)
         cv_area              zcv      treedensity       herbaceous 
        1.599730         4.608200         2.693770         3.803201 
           woody         deadwood     Litter.depth            Domin 
        1.872804         3.027055         2.386384         3.301266 
         grazing adult_true_alpha 
        4.740098         3.296417 
stepAIC(gseeddiv, direction = "both")
Start:  AIC=2.66
seed_true_alpha ~ cv_area + zcv + treedensity + herbaceous + 
    woody + deadwood + Litter.depth + Domin + grazing + adult_true_alpha

                   Df Sum of Sq    RSS    AIC
- cv_area           1   0.00834 5.4576 0.6846
- treedensity       1   0.00854 5.4578 0.6852
- Domin             1   0.01099 5.4603 0.6928
- deadwood          1   0.06021 5.5095 0.8454
- zcv               1   0.13109 5.5804 1.0627
- woody             1   0.29383 5.7431 1.5514
- grazing           1   0.39468 5.8440 1.8473
- Litter.depth      1   0.42192 5.8712 1.9264
<none>                          5.4493 2.6586
- adult_true_alpha  1   0.82946 6.2787 3.0672
- herbaceous        1   0.88209 6.3314 3.2091

Step:  AIC=0.68
seed_true_alpha ~ zcv + treedensity + herbaceous + woody + deadwood + 
    Litter.depth + Domin + grazing + adult_true_alpha

                   Df Sum of Sq    RSS      AIC
- treedensity       1   0.00844 5.4661 -1.28915
- Domin             1   0.01273 5.4703 -1.27581
- deadwood          1   0.05957 5.5172 -1.13087
- zcv               1   0.12309 5.5807 -0.93626
- woody             1   0.28808 5.7457 -0.44097
- Litter.depth      1   0.41693 5.8745 -0.06395
- grazing           1   0.42356 5.8812 -0.04476
<none>                          5.4576  0.68457
- adult_true_alpha  1   0.84530 6.3029  1.13259
- herbaceous        1   0.96106 6.4187  1.44198
+ cv_area           1   0.00834 5.4493  2.65858

Step:  AIC=-1.29
seed_true_alpha ~ zcv + herbaceous + woody + deadwood + Litter.depth + 
    Domin + grazing + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- Domin             1   0.01943 5.4855 -3.2288
- deadwood          1   0.06296 5.5290 -3.0945
- zcv               1   0.11616 5.5822 -2.9317
- woody             1   0.36534 5.8314 -2.1893
- grazing           1   0.43747 5.9035 -1.9803
- Litter.depth      1   0.46775 5.9338 -1.8933
<none>                          5.4661 -1.2892
- herbaceous        1   0.99401 6.4601 -0.4488
- adult_true_alpha  1   1.05972 6.5258 -0.2767
+ treedensity       1   0.00844 5.4576  0.6846
+ cv_area           1   0.00824 5.4578  0.6852

Step:  AIC=-3.23
seed_true_alpha ~ zcv + herbaceous + woody + deadwood + Litter.depth + 
    grazing + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- deadwood          1   0.08913 5.5746 -4.9548
- zcv               1   0.10952 5.5950 -4.8928
- woody             1   0.44189 5.9274 -3.9117
- grazing           1   0.46863 5.9541 -3.8352
- Litter.depth      1   0.53643 6.0219 -3.6427
<none>                          5.4855 -3.2288
- adult_true_alpha  1   1.12002 6.6055 -2.0703
- herbaceous        1   1.14747 6.6330 -1.9998
+ Domin             1   0.01943 5.4661 -1.2892
+ treedensity       1   0.01514 5.4703 -1.2758
+ cv_area           1   0.01046 5.4750 -1.2613

Step:  AIC=-4.95
seed_true_alpha ~ zcv + herbaceous + woody + Litter.depth + grazing + 
    adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- zcv               1   0.09882 5.6734 -6.6561
- woody             1   0.47871 6.0533 -5.5543
- Litter.depth      1   0.50116 6.0758 -5.4914
- grazing           1   0.63028 6.2049 -5.1339
<none>                          5.5746 -4.9548
- herbaceous        1   1.06387 6.6385 -3.9856
+ deadwood          1   0.08913 5.4855 -3.2288
+ Domin             1   0.04560 5.5290 -3.0945
+ treedensity       1   0.01204 5.5626 -2.9916
+ cv_area           1   0.01096 5.5637 -2.9883
- adult_true_alpha  1   1.69787 7.2725 -2.4350

Step:  AIC=-6.66
seed_true_alpha ~ herbaceous + woody + Litter.depth + grazing + 
    adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- woody             1   0.54563 6.2191 -7.0951
- grazing           1   0.59838 6.2718 -6.9515
- Litter.depth      1   0.62029 6.2937 -6.8922
<none>                          5.6734 -6.6561
- herbaceous        1   1.03127 6.7047 -5.8169
+ zcv               1   0.09882 5.5746 -4.9548
+ deadwood          1   0.07843 5.5950 -4.8928
+ treedensity       1   0.03417 5.6393 -4.7588
+ Domin             1   0.03393 5.6395 -4.7581
+ cv_area           1   0.00107 5.6724 -4.6593
- adult_true_alpha  1   1.59942 7.2729 -4.4341

Step:  AIC=-7.1
seed_true_alpha ~ herbaceous + Litter.depth + grazing + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- Litter.depth      1   0.29072 6.5098 -8.3184
- grazing           1   0.38176 6.6008 -8.0823
- herbaceous        1   0.68200 6.9011 -7.3261
<none>                          6.2191 -7.0951
+ woody             1   0.54563 5.6734 -6.6561
- adult_true_alpha  1   1.24816 7.4672 -5.9857
+ zcv               1   0.16573 6.0533 -5.5543
+ Domin             1   0.13998 6.0791 -5.4821
+ deadwood          1   0.11253 6.1065 -5.4055
+ cv_area           1   0.03236 6.1867 -5.1838
+ treedensity       1   0.00031 6.2188 -5.0959

Step:  AIC=-8.32
seed_true_alpha ~ herbaceous + grazing + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- herbaceous        1   0.44534 6.9551 -9.1935
- grazing           1   0.50433 7.0141 -9.0499
<none>                          6.5098 -8.3184
- adult_true_alpha  1   1.01362 7.5234 -7.8583
+ Litter.depth      1   0.29072 6.2191 -7.0951
+ zcv               1   0.24039 6.2694 -6.9581
+ woody             1   0.21606 6.2937 -6.8922
+ deadwood          1   0.06395 6.4458 -6.4862
+ cv_area           1   0.04346 6.4663 -6.4323
+ Domin             1   0.00481 6.5050 -6.3310
+ treedensity       1   0.00035 6.5094 -6.3193

Step:  AIC=-9.19
seed_true_alpha ~ grazing + adult_true_alpha

                   Df Sum of Sq    RSS     AIC
- adult_true_alpha  1   0.76946 7.7246 -9.4097
<none>                          6.9551 -9.1935
- grazing           1   1.07481 8.0299 -8.7506
+ herbaceous        1   0.44534 6.5098 -8.3184
+ cv_area           1   0.14426 6.8109 -7.5498
+ woody             1   0.12530 6.8298 -7.5025
+ Litter.depth      1   0.05406 6.9011 -7.3261
+ Domin             1   0.04650 6.9086 -7.3075
+ deadwood          1   0.01226 6.9429 -7.2235
+ zcv               1   0.00099 6.9541 -7.1959
+ treedensity       1   0.00011 6.9550 -7.1938

Step:  AIC=-9.41
seed_true_alpha ~ grazing

                   Df Sum of Sq    RSS      AIC
- grazing           1   0.43998 8.1646 -10.4680
<none>                          7.7246  -9.4097
+ adult_true_alpha  1   0.76946 6.9551  -9.1935
+ deadwood          1   0.29214 7.4325  -8.0651
+ cv_area           1   0.29063 7.4340  -8.0616
+ herbaceous        1   0.20118 7.5234  -7.8583
+ woody             1   0.07370 7.6509  -7.5727
+ zcv               1   0.01751 7.7071  -7.4483
+ Litter.depth      1   0.00522 7.7194  -7.4212
+ Domin             1   0.00166 7.7229  -7.4133
+ treedensity       1   0.00146 7.7231  -7.4129

Step:  AIC=-10.47
seed_true_alpha ~ 1

                   Df Sum of Sq    RSS      AIC
<none>                          8.1646 -10.4680
+ cv_area           1   0.60097 7.5636  -9.7677
+ herbaceous        1   0.53529 7.6293  -9.6208
+ grazing           1   0.43998 7.7246  -9.4097
+ zcv               1   0.33495 7.8296  -9.1801
+ deadwood          1   0.28949 7.8751  -9.0817
+ adult_true_alpha  1   0.13462 8.0299  -8.7506
+ treedensity       1   0.10374 8.0608  -8.6853
+ Domin             1   0.09546 8.0691  -8.6679
+ Litter.depth      1   0.02141 8.1432  -8.5126
+ woody             1   0.00036 8.1642  -8.4687

Call:
lm(formula = seed_true_alpha ~ 1, data = gdata)

Coefficients:
(Intercept)  
       1.95  
# no effects




# Sap Div
gsapdiv <- glmmTMB(sap_true_alpha ~ canopy_cover_perc_50 + zmean_50 + cv_area_200 + zcv_50 + treedensity +
                      herbaceous + woody + deadwood + leaf_litter + Litter.depth + Domin +
                      grazing + adult_true_alpha + adult_spec_rich, family = gaussian, data = gdata)
check_collinearity(gsapdiv)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_50 30.415598 30.415598 30.415598 5.515034 0.0328779 0.0328779 0.0328779
zmean_50 32.353494 32.353494 32.353494 5.688013 0.0309086 0.0309086 0.0309086
cv_area_200 14.808861 14.808861 14.808861 3.848228 0.0675271 0.0675271 0.0675271
zcv_50 19.109212 19.109212 19.109212 4.371409 0.0523308 0.0523308 0.0523308
treedensity 25.807788 25.807788 25.807788 5.080137 0.0387480 0.0387480 0.0387480
herbaceous 21.010873 21.010873 21.010873 4.583762 0.0475944 0.0475944 0.0475944
woody 12.592920 12.592920 12.592920 3.548650 0.0794097 0.0794097 0.0794097
deadwood 11.941676 11.941676 11.941676 3.455673 0.0837403 0.0837403 0.0837403
leaf_litter 42.761699 42.761699 42.761699 6.539243 0.0233854 0.0233854 0.0233854
Litter.depth 4.507034 4.507034 4.507034 2.122978 0.2218754 0.2218754 0.2218754
Domin 4.880742 4.880742 4.880742 2.209240 0.2048869 0.2048869 0.2048869
grazing 3.954520 3.954520 3.954520 1.988597 0.2528752 0.2528752 0.2528752
adult_true_alpha 102.296943 102.296943 102.296943 10.114195 0.0097755 0.0097755 0.0097755
adult_spec_rich 120.807113 120.807113 120.807113 10.991229 0.0082777 0.0082777 0.0082777
gsapdiv <- glmmTMB(sap_true_alpha ~ canopy_cover_perc_50 + cv_area_200 + 
                     herbaceous + woody + deadwood + Litter.depth + Domin +
                     grazing + adult_true_alpha, family = gaussian, data = gdata)
check_collinearity(gsapdiv)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_50 1.606524 1.378647 1.971542 1.267487 0.6224619 0.5072173 0.7253488
cv_area_200 3.686307 2.977537 4.649108 1.919976 0.2712742 0.2150950 0.3358481
herbaceous 2.472246 2.041681 3.080779 1.572338 0.4044905 0.3245932 0.4897925
woody 1.676572 1.431791 2.060120 1.294825 0.5964551 0.4854086 0.6984261
deadwood 2.325150 1.928520 2.891206 1.524844 0.4300797 0.3458764 0.5185323
Litter.depth 2.540975 2.094583 3.169413 1.594043 0.3935498 0.3155158 0.4774220
Domin 3.306831 2.684809 4.158500 1.818469 0.3024043 0.2404713 0.3724660
grazing 3.298711 2.678546 4.148004 1.816235 0.3031488 0.2410798 0.3733369
adult_true_alpha 3.219976 2.617827 4.046243 1.794429 0.3105613 0.2471428 0.3819962
stepAIC(gsapdiv, direction = "both")
Start:  AIC=35.72
sap_true_alpha ~ canopy_cover_perc_50 + cv_area_200 + herbaceous + 
    woody + deadwood + Litter.depth + Domin + grazing + adult_true_alpha

                       Df    AIC
- woody                 1 33.729
- cv_area_200           1 34.033
- grazing               1 34.341
- herbaceous            1 35.717
<none>                    35.723
- adult_true_alpha      1 36.077
- deadwood              1 37.115
- Domin                 1 38.719
- canopy_cover_perc_50  1 46.295
- Litter.depth          1 47.768

Step:  AIC=33.73
sap_true_alpha ~ canopy_cover_perc_50 + cv_area_200 + herbaceous + 
    deadwood + Litter.depth + Domin + grazing + adult_true_alpha

                       Df    AIC
- cv_area_200           1 32.033
- grazing               1 32.347
<none>                    33.729
- herbaceous            1 34.182
- adult_true_alpha      1 34.255
- deadwood              1 35.154
+ woody                 1 35.723
- Domin                 1 36.812
- canopy_cover_perc_50  1 44.864
- Litter.depth          1 46.299

Step:  AIC=32.03
sap_true_alpha ~ canopy_cover_perc_50 + herbaceous + deadwood + 
    Litter.depth + Domin + grazing + adult_true_alpha

                       Df    AIC
- grazing               1 31.829
<none>                    32.033
- herbaceous            1 32.546
- adult_true_alpha      1 33.629
+ cv_area_200           1 33.729
+ woody                 1 34.033
- Domin                 1 34.920
- deadwood              1 36.222
- canopy_cover_perc_50  1 43.624
- Litter.depth          1 46.190

Step:  AIC=31.83
sap_true_alpha ~ canopy_cover_perc_50 + herbaceous + deadwood + 
    Litter.depth + Domin + adult_true_alpha

                       Df    AIC
- herbaceous            1 31.143
<none>                    31.829
+ grazing               1 32.033
+ cv_area_200           1 32.347
+ woody                 1 33.644
- Domin                 1 33.856
- adult_true_alpha      1 35.220
- deadwood              1 37.618
- canopy_cover_perc_50  1 41.937
- Litter.depth          1 44.199

Step:  AIC=31.14
sap_true_alpha ~ canopy_cover_perc_50 + deadwood + Litter.depth + 
    Domin + adult_true_alpha

                       Df    AIC
<none>                    31.143
+ herbaceous            1 31.829
+ cv_area_200           1 32.250
+ grazing               1 32.546
+ woody                 1 33.106
- adult_true_alpha      1 33.530
- Domin                 1 34.245
- deadwood              1 37.777
- canopy_cover_perc_50  1 42.066
- Litter.depth          1 42.566
Formula:          
sap_true_alpha ~ canopy_cover_perc_50 + deadwood + Litter.depth +  
    Domin + adult_true_alpha
Data: gdata
      AIC       BIC    logLik -2*log(L)  df.resid 
 31.14279  36.97528  -8.57139  17.14279        10 

Number of obs: 17

Dispersion estimate for gaussian family (sigma^2): 0.16 

Fixed Effects:

Conditional model:
         (Intercept)  canopy_cover_perc_50              deadwood  
             4.93652              -0.04721              -0.07947  
        Litter.depth                 Domin      adult_true_alpha  
             0.26156              -0.90943               0.45325  
finalgsapdiv <- glmmTMB(sap_true_alpha ~ canopy_cover_perc_50 + deadwood + Litter.depth + adult_true_alpha, family = gaussian, data = gdata)
check_collinearity(finalgsapdiv)
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
canopy_cover_perc_50 1.124216 1.009267 2.664938 1.060290 0.8895090 0.3752433 0.9908178
deadwood 1.256833 1.056060 2.176655 1.121086 0.7956504 0.4594205 0.9469158
Litter.depth 1.140593 1.013363 2.479139 1.067985 0.8767372 0.4033659 0.9868129
adult_true_alpha 1.301924 1.076580 2.190361 1.141019 0.7680941 0.4565457 0.9288672
sim <- simulateResiduals(fittedModel = finalgsapdiv, n=100, refit = TRUE)
plot(sim)
qu = 0.25, log(sigma) = -3.209436 : outer Newton did not converge fully.

testUniformity(sim)    


    Exact one-sample Kolmogorov-Smirnov test

data:  simulationOutput$scaledResiduals
D = 0.18909, p-value = 0.5174
alternative hypothesis: two-sided
testDispersion(sim)     


    DHARMa nonparametric dispersion test via mean deviance residual fitted
    vs. simulated-refitted

data:  sim
dispersion = 1.4063, p-value = 0.24
alternative hypothesis: two.sided
testZeroInflation(sim)


    DHARMa zero-inflation test via comparison to expected zeros with
    simulation under H0 = fitted model

data:  simulationOutput
ratioObsSim = NaN, p-value = 1
alternative hypothesis: two.sided
summary(finalgsapdiv)
 Family: gaussian  ( identity )
Formula:          
sap_true_alpha ~ canopy_cover_perc_50 + deadwood + Litter.depth +  
    adult_true_alpha
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
     34.2      39.2     -11.1      22.2        11 


Dispersion estimate for gaussian family (sigma^2): 0.217 

Conditional model:
                     Estimate Std. Error z value Pr(>|z|)    
(Intercept)           5.66048    0.87886   6.441 1.19e-10 ***
canopy_cover_perc_50 -0.05820    0.01132  -5.143 2.70e-07 ***
deadwood             -0.06468    0.02661  -2.431 0.015075 *  
Litter.depth          0.16740    0.05007   3.343 0.000827 ***
adult_true_alpha      0.14001    0.18207   0.769 0.441909    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# effects of grazing



# grazing results
summary(finalgseed)
 Family: compois  ( log )
Formula:          
seedlings ~ cv_area + treedensity + deadwood + Domin + grazing
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
    188.4     194.3     -87.2     174.4        10 


Dispersion parameter for compois family (): 45.3 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  4.90406    0.71668   6.843 7.77e-12 ***
cv_area     -0.50661    0.32484  -1.560  0.11886    
treedensity 13.54975    7.18161   1.887  0.05920 .  
deadwood    -0.08694    0.03497  -2.487  0.01290 *  
Domin       -1.02567    0.35491  -2.890  0.00385 ** 
grazingy     2.43789    0.47198   5.165 2.40e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(finalgssap)
 Family: nbinom2  ( log )
Formula:          saplings ~ woody + deadwood + Domin + grazing
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
      122       127       -55       110        11 


Dispersion parameter for nbinom2 family (): 3.24 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)   
(Intercept)  1.69424    0.55365   3.060  0.00221 **
woody        0.03676    0.01420   2.589  0.00962 **
deadwood     0.07952    0.03392   2.344  0.01908 * 
Domin       -0.61688    0.40540  -1.522  0.12809   
grazingy     0.81257    0.40833   1.990  0.04659 * 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(finalgseedrich)

Call:
lm(formula = seed_rich ~ treedensity + Litter.depth + Domin + 
    grazing, data = gdata)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.76354 -0.25706  0.02638  0.38239  2.13881 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)  
(Intercept)    2.3183     1.0961   2.115   0.0560 .
treedensity   22.9729    12.2251   1.879   0.0847 .
Litter.depth   0.4993     0.1870   2.671   0.0204 *
Domin         -2.6027     1.1031  -2.359   0.0361 *
grazingy       2.0389     0.9220   2.212   0.0472 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.398 on 12 degrees of freedom
Multiple R-squared:  0.4699,    Adjusted R-squared:  0.2932 
F-statistic: 2.659 on 4 and 12 DF,  p-value: 0.08474
summary(finalgsaprich)
 Family: poisson  ( log )
Formula:          sap_rich ~ Litter.depth
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
     56.7      58.4     -26.4      52.7        15 


Conditional model:
             Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.37670    0.37990   0.992    0.321
Litter.depth  0.09662    0.06595   1.465    0.143
summary(finalgsapdiv)
 Family: gaussian  ( identity )
Formula:          
sap_true_alpha ~ canopy_cover_perc_50 + deadwood + Litter.depth +  
    adult_true_alpha
Data: gdata

      AIC       BIC    logLik -2*log(L)  df.resid 
     34.2      39.2     -11.1      22.2        11 


Dispersion estimate for gaussian family (sigma^2): 0.217 

Conditional model:
                     Estimate Std. Error z value Pr(>|z|)    
(Intercept)           5.66048    0.87886   6.441 1.19e-10 ***
canopy_cover_perc_50 -0.05820    0.01132  -5.143 2.70e-07 ***
deadwood             -0.06468    0.02661  -2.431 0.015075 *  
Litter.depth          0.16740    0.05007   3.343 0.000827 ***
adult_true_alpha      0.14001    0.18207   0.769 0.441909    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

First Set of Results

# Results
finalmodel
Formula:          seedlings ~ canopy_cover_perc + zmean + cv_area
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
 278.4094  284.2997 -134.2047  268.4094        19 

Number of obs: 24

Dispersion parameter for nbinom2 family (): 0.915 

Fixed Effects:

Conditional model:
      (Intercept)  canopy_cover_perc              zmean            cv_area  
          5.72921           -0.02595            0.24255           -0.90414  
summary(finalmodel)
 Family: nbinom2  ( log )
Formula:          seedlings ~ canopy_cover_perc + zmean + cv_area
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    278.4     284.3    -134.2     268.4        19 


Dispersion parameter for nbinom2 family (): 0.915 

Conditional model:
                  Estimate Std. Error z value Pr(>|z|)    
(Intercept)        5.72921    1.19620   4.790 1.67e-06 ***
canopy_cover_perc -0.02595    0.01444  -1.797  0.07231 .  
zmean              0.24255    0.08526   2.845  0.00444 ** 
cv_area           -0.90414    0.36408  -2.483  0.01301 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tab_model(finalmodel, show.ci = FALSE, show.aic = TRUE, show.stat = TRUE, p.threshold = 0.05, title = "Seedling Abundance GLMM", show.r2 = TRUE, show.se =  TRUE)
Seedling Abundance GLMM
  seedlings
Predictors Incidence Rate Ratios std. Error Statistic p
(Intercept) 307.73 368.10 4.79 <0.001
canopy cover perc 0.97 0.01 -1.80 0.072
zmean 1.27 0.11 2.84 0.004
cv area 0.40 0.15 -2.48 0.013
Observations 24
AIC 278.409
finalsapmodel
Formula:          saplings ~ zmean_200 + woody + deadwood
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
179.68171 185.57198 -84.84086 169.68171        19 

Number of obs: 24

Dispersion parameter for nbinom2 family (): 2.13 

Fixed Effects:

Conditional model:
(Intercept)    zmean_200        woody     deadwood  
    4.68638     -0.31888      0.02175      0.06507  
summary(finalsapmodel)
 Family: nbinom2  ( log )
Formula:          saplings ~ zmean_200 + woody + deadwood
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
    179.7     185.6     -84.8     169.7        19 


Dispersion parameter for nbinom2 family (): 2.13 

Conditional model:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  4.68638    0.78705   5.954 2.61e-09 ***
zmean_200   -0.31888    0.08672  -3.677 0.000236 ***
woody        0.02175    0.01097   1.982 0.047493 *  
deadwood     0.06507    0.04040   1.611 0.107240    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tab_model(finalsapmodel, show.ci = FALSE, show.aic = TRUE, show.stat = TRUE, p.threshold = 0.05, title = "Sapling Abundance GLMM", show.r2 = TRUE, show.dev = TRUE, show.se =  TRUE)
Sapling Abundance GLMM
  saplings
Predictors Incidence Rate Ratios std. Error Statistic p
(Intercept) 108.46 85.36 5.95 <0.001
zmean 200 0.73 0.06 -3.68 <0.001
woody 1.02 0.01 1.98 0.047
deadwood 1.07 0.04 1.61 0.107
Observations 24
Deviance 24.332
AIC 179.682
finalseedrichmodel

Call:
lm(formula = seed_rich ~ canopy_cover_perc_50 + cv_area_50 + 
    Litter.depth + Domin + adult_spec_rich, data = data)

Coefficients:
         (Intercept)  canopy_cover_perc_50            cv_area_50  
             1.57654               0.03287              -0.31646  
        Litter.depth                 Domin       adult_spec_rich  
             0.44097              -2.50157               0.61633  
summary(finalseedrichmodel)

Call:
lm(formula = seed_rich ~ canopy_cover_perc_50 + cv_area_50 + 
    Litter.depth + Domin + adult_spec_rich, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-3.12511 -0.61912  0.08265  0.70444  2.07933 

Coefficients:
                     Estimate Std. Error t value Pr(>|t|)   
(Intercept)           1.57654    2.11233   0.746  0.46509   
canopy_cover_perc_50  0.03287    0.02197   1.496  0.15197   
cv_area_50           -0.31646    0.19639  -1.611  0.12449   
Litter.depth          0.44097    0.16327   2.701  0.01462 * 
Domin                -2.50157    0.79760  -3.136  0.00571 **
adult_spec_rich       0.61633    0.35022   1.760  0.09542 . 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.361 on 18 degrees of freedom
Multiple R-squared:  0.4217,    Adjusted R-squared:  0.261 
F-statistic: 2.625 on 5 and 18 DF,  p-value: 0.05954
tab_model(finalseedrichmodel, show.ci = FALSE, show.aic = TRUE, show.stat = TRUE, p.threshold = 0.05, title = "Seedling Richness LM", show.r2 = TRUE, show.dev = TRUE, show.se =  TRUE)
Seedling Richness LM
  seed rich
Predictors Estimates std. Error Statistic p
(Intercept) 1.58 2.11 0.75 0.465
canopy cover perc 50 0.03 0.02 1.50 0.152
cv area 50 -0.32 0.20 -1.61 0.124
Litter depth 0.44 0.16 2.70 0.015
Domin -2.50 0.80 -3.14 0.006
adult spec rich 0.62 0.35 1.76 0.095
Observations 24
R2 / R2 adjusted 0.422 / 0.261
Deviance 33.326
AIC 89.988
finalsaprichmodel
Formula:          
sap_rich ~ Litter.depth + Domin + forest_type + adult_true_alpha
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
 75.82770  82.89603 -31.91385  63.82770        18 

Number of obs: 24

Dispersion parameter for compois family (): 0.287 

Fixed Effects:

Conditional model:
          (Intercept)           Litter.depth                  Domin  
               0.1339                 0.2100                -0.8342  
forest_typeplantation       adult_true_alpha  
               0.4337                 0.3459  
summary(finalsaprichmodel)
 Family: compois  ( log )
Formula:          
sap_rich ~ Litter.depth + Domin + forest_type + adult_true_alpha
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
     75.8      82.9     -31.9      63.8        18 


Dispersion parameter for compois family (): 0.287 

Conditional model:
                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)            0.13390    0.31318   0.428  0.66898    
Litter.depth           0.21003    0.04057   5.177 2.26e-07 ***
Domin                 -0.83417    0.18873  -4.420 9.87e-06 ***
forest_typeplantation  0.43371    0.14709   2.949  0.00319 ** 
adult_true_alpha       0.34588    0.12057   2.869  0.00412 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tab_model(finalsaprichmodel, show.ci = FALSE, show.aic = TRUE, show.stat = TRUE, p.threshold = 0.05, title = "Sapling Richness GLMM", show.r2 = TRUE, show.dev = TRUE, show.se =  TRUE)
Warning: deviance residuals not defined for family 'compois': returning NA
Sapling Richness GLMM
  sap rich
Predictors Estimates std. Error Statistic p
(Intercept) 1.14 0.36 0.43 0.669
Litter depth 1.23 0.05 5.18 <0.001
Domin 0.43 0.08 -4.42 <0.001
forest type [plantation] 1.54 0.23 2.95 0.003
adult true alpha 1.41 0.17 2.87 0.004
Observations 24
AIC 75.828
finalseeddivmodel

Call:
lm(formula = seed_true_alpha ~ cv_area + deadwood, data = data)

Coefficients:
(Intercept)      cv_area     deadwood  
    1.52252      0.21067      0.02377  
summary(finalseeddivmodel)

Call:
lm(formula = seed_true_alpha ~ cv_area + deadwood, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.20394 -0.33526  0.02085  0.28051  1.68681 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  1.52252    0.38743   3.930 0.000768 ***
cv_area      0.21067    0.22779   0.925 0.365562    
deadwood     0.02377    0.03258   0.730 0.473635    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.6592 on 21 degrees of freedom
Multiple R-squared:  0.06706,   Adjusted R-squared:  -0.02179 
F-statistic: 0.7548 on 2 and 21 DF,  p-value: 0.4824
tab_model(finalseeddivmodel, show.ci = FALSE, show.aic = TRUE, show.stat = TRUE, p.threshold = 0.05, title = "Seedling True Diversity LM", show.r2 = TRUE, show.dev = TRUE, show.se =  TRUE)
Seedling True Diversity LM
  seed true alpha
Predictors Estimates std. Error Statistic p
(Intercept) 1.52 0.39 3.93 0.001
cv area 0.21 0.23 0.92 0.366
deadwood 0.02 0.03 0.73 0.474
Observations 24
R2 / R2 adjusted 0.067 / -0.022
Deviance 9.126
AIC 52.903
finalsapdivmodel
Formula:          
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + deadwood +  
    Litter.depth + forest_type + adult_spec_rich
Data: data
      AIC       BIC    logLik -2*log(L)  df.resid 
 56.40733  67.00981 -19.20367  38.40733        15 

Number of obs: 24

Dispersion estimate for gaussian family (sigma^2): 0.29 

Fixed Effects:

Conditional model:
          (Intercept)               zmean_50            cv_area_200  
             3.717101              -0.212118               0.000309  
          treedensity               deadwood           Litter.depth  
           -11.062202              -0.044508               0.068351  
forest_typeplantation        adult_spec_rich  
             0.889283               0.133005  
summary(finalsapdivmodel)
 Family: gaussian  ( identity )
Formula:          
sap_true_alpha ~ zmean_50 + cv_area_200 + treedensity + deadwood +  
    Litter.depth + forest_type + adult_spec_rich
Data: data

      AIC       BIC    logLik -2*log(L)  df.resid 
     56.4      67.0     -19.2      38.4        15 


Dispersion estimate for gaussian family (sigma^2): 0.29 

Conditional model:
                        Estimate Std. Error z value Pr(>|z|)    
(Intercept)             3.717101   0.900453   4.128 3.66e-05 ***
zmean_50               -0.212118   0.050047  -4.238 2.25e-05 ***
cv_area_200             0.000309   0.028610   0.011  0.99138    
treedensity           -11.062202   4.143179  -2.670  0.00759 ** 
deadwood               -0.044508   0.036591  -1.216  0.22385    
Litter.depth            0.068351   0.049626   1.377  0.16841    
forest_typeplantation   0.889283   0.309564   2.873  0.00407 ** 
adult_spec_rich         0.133005   0.164728   0.807  0.41942    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tab_model(finalsapdivmodel, show.ci = FALSE, show.aic = TRUE, show.stat = TRUE, p.threshold = 0.05, title = "Sapling True Diversity GLMM", show.r2 = TRUE, show.dev = TRUE, show.se =  TRUE)
Sapling True Diversity GLMM
  sap true alpha
Predictors Estimates std. Error Statistic p
(Intercept) 3.72 0.90 4.13 <0.001
zmean 50 -0.21 0.05 -4.24 <0.001
cv area 200 0.00 0.03 0.01 0.991
treedensity -11.06 4.14 -2.67 0.008
deadwood -0.04 0.04 -1.22 0.224
Litter depth 0.07 0.05 1.38 0.168
forest type [plantation] 0.89 0.31 2.87 0.004
adult spec rich 0.13 0.16 0.81 0.419
Observations 24
R2 / R2 adjusted 0.700 / 0.540
Deviance 6.962
AIC 56.407

SIGNIFICANT FACTORS

Seedling Abundance -

Sapling Abundance -

Seedling Richness -

Sapling Richness -

Seedling Diversity

Sapling Diversity -

Visualisation

Community Composition

Forest Type

# Is there species composition differences between forest types and transect?

# Filter for adults
adult_comp <- treedata %>%
  filter(class %in% c("tree"))

# Community matrix with forest_type in metadata
comm_abun_ft <- adult_comp %>%
  count(transect, forest_type, Species) %>%
  pivot_wider(names_from = Species, values_from = n, values_fill = 0)

# Meta data
meta <- comm_abun_ft %>% 
  dplyr::select(transect, forest_type)

# Community abundance data only
mat <- comm_abun_ft %>%
  dplyr::select(-transect, -forest_type) %>%
  as.data.frame()
rownames(mat) <- comm_abun_ft$transect

# Distances
dist_bray <- vegdist(mat, method = "bray")                      # Species Composition and Abundance

# Transect differences
# Dispersion test not possible due to lack of replication within groups

#PERMANOVA
set.seed(1)
adon_bray <- adonis2(dist_bray ~ transect, data = meta, permutations = 999)

adon_bray
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 0.3433019 0.055228 1.286042 0.267
Residual 22 5.8727804 0.944772 NA NA
Total 23 6.2160823 1.000000 NA NA
# significant differences between transects

# Forest type differences
# Test homogeneity of dispersion
bd_ft <- betadisper(dist_bray, meta$forest_type)
permutest(bd_ft, permutations = 999)

Permutation test for homogeneity of multivariate dispersions
Permutation: free
Number of permutations: 999

Response: Distances
          Df  Sum Sq  Mean Sq      F N.Perm Pr(>F)
Groups     1 0.00126 0.001262 0.0252    999  0.867
Residuals 22 1.10323 0.050147                     
plot(bd_ft)

plot(bd_ft, ellipse = TRUE, hull = FALSE)

# How variable are the two forest types?
bd_ft$group
 [1] natural    natural    natural    natural    natural    natural   
 [7] natural    natural    natural    natural    natural    natural   
[13] natural    natural    natural    natural    natural    plantation
[19] plantation plantation plantation plantation plantation plantation
Levels: natural plantation
tapply(bd_ft$distances, meta$forest_type, mean)
   natural plantation 
 0.4334977  0.4494484 
# natural composition is more variable with greater distance from center

# PERMANOVA
set.seed(1)
adon_ft_bray <- adonis2(dist_bray ~ forest_type, data = meta, permutations = 999)

adon_ft_bray
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 0.791591 0.1273456 3.21044 0.033
Residual 22 5.424491 0.8726544 NA NA
Total 23 6.216082 1.0000000 NA NA
# Significant differences between forest types




# Regen species composition

# Filter for regen only
regen_comp <- treedata %>%
  filter(class %in% c("seedling", "sapling"))

comm_abun_regen <- regen_comp %>%
  count(transect, forest_type, Species) %>%
  pivot_wider(names_from = Species, values_from = n, values_fill = 0)

meta_regen <- comm_abun_regen %>% 
  dplyr::select(transect, forest_type)
mat_regen <- comm_abun_regen %>%
  dplyr::select(-transect, -forest_type) %>%
  as.data.frame()
rownames(mat_regen) <- comm_abun_regen$transect

dist_bray_regen <- vegdist(mat_regen, method = "bray")

# Transect differences
adon_bray_regen <- adonis2(dist_bray_regen ~ transect, data = meta_regen, permutations = 999)

adon_bray_regen
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 0.6746019 0.084894 2.04093 0.029
Residual 22 7.2718053 0.915106 NA NA
Total 23 7.9464072 1.000000 NA NA
# Significant differences in regen comp between transects

# Forest type differences
bd_ft_regen <- betadisper(dist_bray_regen, meta_regen$forest_type)
permutest(bd_ft_regen, permutations = 999)

Permutation test for homogeneity of multivariate dispersions
Permutation: free
Number of permutations: 999

Response: Distances
          Df   Sum Sq   Mean Sq      F N.Perm Pr(>F)
Groups     1 0.003622 0.0036218 0.5563    999  0.462
Residuals 22 0.143233 0.0065106                     
adon_bray_ftregen <- adonis2(dist_bray_regen ~ forest_type, data = meta_regen, permutations = 999)

adon_bray_ftregen
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 0.4133496 0.0520172 1.207171 0.276
Residual 22 7.5330576 0.9479828 NA NA
Total 23 7.9464072 1.0000000 NA NA
# No significant differences in regen comp between forest types



# Visualise per forest type

pcoa_out <- cmdscale(dist_bray, eig = TRUE, k = 2)
scores_df <- as.data.frame(pcoa_out$points)
colnames(scores_df) <- c("PCoA1", "PCoA2")

# Add metadata
scores_df$forest_type <- meta$forest_type

# Calculate % variance explained
eig_vals <- pcoa_out$eig
prop_var <- round(100 * eig_vals[1:2] / sum(eig_vals), 1)

# Define colors and shapes
forest_colors <- c("natural" = "black", "plantation" = "red")
forest_shapes <- c("natural" = 16, "plantation" = 17)  # circle, triangle

# Plot
adultcomp <- ggplot(scores_df, aes(x = PCoA1, y = PCoA2, color = forest_type, shape = forest_type)) +
  geom_point(size = 3, alpha = 0.8) +
  stat_ellipse(type = "norm", level = 0.95, size = 0.8) +
  scale_color_manual(values = forest_colors) +
  scale_shape_manual(values = forest_shapes) +
  labs(
    x = paste0("PCoA 1 (", prop_var[1], "%)"),
    y = paste0("PCoA 2 (", prop_var[2], "%)"),
    color = "Forest Type",
    shape = "Forest Type",
    title = "PCoA of Adult Composition (Bray–Curtis)"
  ) +
  theme_minimal(base_size = 12) +
  theme(legend.position = "right",
        plot.title = element_text(hjust = 0.5, face = "bold"),
        panel.grid = element_blank())
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# regen
pcoa_outregen <- cmdscale(dist_bray_regen, eig = TRUE, k = 2)
scores_dfregen <- as.data.frame(pcoa_outregen$points)
colnames(scores_dfregen) <- c("PCoA1", "PCoA2")

# Add metadata
scores_dfregen$forest_type <- meta$forest_type

# Calculate % variance explained
eig_valsregen <- pcoa_outregen$eig
prop_varregen <- round(100 * eig_valsregen[1:2] / sum(eig_valsregen), 1)

# Define colors and shapes
forest_colors <- c("natural" = "black", "plantation" = "red")
forest_shapes <- c("natural" = 16, "plantation" = 17)  # circle, triangle

# Plot
regencomp <- ggplot(scores_dfregen, aes(x = PCoA1, y = PCoA2, color = forest_type, shape = forest_type)) +
  geom_point(size = 3, alpha = 0.8) +
  stat_ellipse(type = "norm", level = 0.95, size = 0.8) +
  scale_color_manual(values = forest_colors) +
  scale_shape_manual(values = forest_shapes) +
  labs(
    x = paste0("PCoA 1 (", prop_var[1], "%)"),
    y = paste0("PCoA 2 (", prop_var[2], "%)"),
    color = "Forest Type",
    shape = "Forest Type",
    title = "PCoA of Regeneration Composition (Bray–Curtis)"
  ) +
  theme_minimal(base_size = 12) +
  theme(legend.position = "right",
        plot.title = element_text(hjust = 0.5, face = "bold"),
        panel.grid = element_blank())




adultcomp + regencomp

Grazing

# GRazing comm comp

# Filter for adults in natural forest transects 1–17
adult_comp_nat <- treedata %>%
  filter(class == "tree", transect %in% 1:17)

# Community matrix with grazing in metadata
comm_abun_grazing <- adult_comp_nat %>%
  count(transect, grazing, Species) %>%
  pivot_wider(names_from = Species, values_from = n, values_fill = 0)

# Metadata
meta_grazing <- comm_abun_grazing %>%
  dplyr::select(transect, grazing)

# Community abundance matrix
mat_grazing <- comm_abun_grazing %>%
  dplyr::select(-transect, -grazing) %>%
  as.data.frame()
rownames(mat_grazing) <- comm_abun_grazing$transect

# Bray-Curtis distances
dist_bray_grazing <- vegdist(mat_grazing, method = "bray")

# PERMANOVA: transect differences
adon_grazing_transect <- adonis2(dist_bray_grazing ~ transect, data = meta_grazing, permutations = 999)
adon_grazing_transect
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 0.6138221 0.1639972 2.942525 0.039
Residual 15 3.1290582 0.8360028 NA NA
Total 16 3.7428803 1.0000000 NA NA
# PERMANOVA: grazing differences
adon_grazing <- adonis2(dist_bray_grazing ~ grazing, data = meta_grazing, permutations = 999)
adon_grazing
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 1.654338 0.4419959 11.88152 0.001
Residual 15 2.088542 0.5580041 NA NA
Total 16 3.742880 1.0000000 NA NA
# sig diff between grazed and non grazed

# Dispersion test
bd_grazing <- betadisper(dist_bray_grazing, meta_grazing$grazing)
permutest(bd_grazing, permutations = 999)

Permutation test for homogeneity of multivariate dispersions
Permutation: free
Number of permutations: 999

Response: Distances
          Df  Sum Sq  Mean Sq      F N.Perm Pr(>F)
Groups     1 0.08097 0.080972 2.3767    999  0.127
Residuals 15 0.51103 0.034069                     
# PCoA
pcoa_adult_grazing <- cmdscale(dist_bray_grazing, eig = TRUE, k = 2)
scores_adult_grazing <- as.data.frame(pcoa_adult_grazing$points)
colnames(scores_adult_grazing) <- c("PCoA1", "PCoA2")
scores_adult_grazing$grazing <- meta_grazing$grazing

# Variance explained
eig_vals_adult <- pcoa_adult_grazing$eig
prop_var_adult <- round(100 * eig_vals_adult[1:2] / sum(eig_vals_adult), 1)

scores_adult_grazing$grazing <- dplyr::recode(scores_adult_grazing$grazing,
                                       "y" = "grazed",
                                       "n" = "ungrazed")
scores_adult_grazing$grazing <- factor(scores_adult_grazing$grazing,
                                       levels = c("grazed", "ungrazed"))

# Plot
adult_grazing_plot <- ggplot(scores_adult_grazing, aes(x = PCoA1, y = PCoA2, color = grazing, shape = grazing)) +
  geom_point(size = 3, alpha = 0.8) +
  stat_ellipse(type = "norm", level = 0.95, size = 0.8) +
  scale_color_manual(values = c("grazed" = "darkgreen", "ungrazed" = "orange")) +
  scale_shape_manual(values = c("grazed" = 16, "ungrazed" = 17)) +
  labs(
    x = paste0("PCoA 1 (", prop_var_adult[1], "%)"),
    y = paste0("PCoA 2 (", prop_var_adult[2], "%)"),
    title = "PCoA of Adult Composition by Grazing"
  ) +
  theme_minimal(base_size = 12) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"))

adult_grazing_plot

# Regen grazing comp

# Filter for regeneration in natural forest transects 1–17
regen_comp_nat <- treedata %>%
  filter(class %in% c("seedling", "sapling"), transect %in% 1:17)

# Community matrix with grazing in metadata
comm_abun_regen_grazing <- regen_comp_nat %>%
  count(transect, grazing, Species) %>%
  pivot_wider(names_from = Species, values_from = n, values_fill = 0)

# Metadata
meta_regen_grazing <- comm_abun_regen_grazing %>%
  dplyr::select(transect, grazing)

# Community abundance matrix
mat_regen_grazing <- comm_abun_regen_grazing %>%
  dplyr::select(-transect, -grazing) %>%
  as.data.frame()
rownames(mat_regen_grazing) <- comm_abun_regen_grazing$transect

# Bray-Curtis distances
dist_bray_regen_grazing <- vegdist(mat_regen_grazing, method = "bray")

# PERMANOVA: transect differences
adon_regen_transect <- adonis2(dist_bray_regen_grazing ~ transect, data = meta_regen_grazing, permutations = 999)
adon_regen_transect
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 1.544973 0.2821949 5.897038 0.001
Residual 15 3.929870 0.7178051 NA NA
Total 16 5.474844 1.0000000 NA NA
# PERMANOVA: grazing differences
adon_regen_grazing <- adonis2(dist_bray_regen_grazing ~ grazing, data = meta_regen_grazing, permutations = 999)
adon_regen_grazing
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df SumOfSqs R2 F Pr(>F)
Model 1 1.016729 0.1857092 3.420937 0.007
Residual 15 4.458115 0.8142908 NA NA
Total 16 5.474844 1.0000000 NA NA
# sig dif in regen comp

# Dispersion test
bd_regen_grazing <- betadisper(dist_bray_regen_grazing, meta_regen_grazing$grazing)
permutest(bd_regen_grazing, permutations = 999)

Permutation test for homogeneity of multivariate dispersions
Permutation: free
Number of permutations: 999

Response: Distances
          Df  Sum Sq  Mean Sq      F N.Perm Pr(>F)
Groups     1 0.00131 0.001305 0.0393    999  0.863
Residuals 15 0.49840 0.033227                     
# PCoA
pcoa_regen_grazing <- cmdscale(dist_bray_regen_grazing, eig = TRUE, k = 2)
scores_regen_grazing <- as.data.frame(pcoa_regen_grazing$points)
colnames(scores_regen_grazing) <- c("PCoA1", "PCoA2")
scores_regen_grazing$grazing <- meta_regen_grazing$grazing

# Variance explained
eig_vals_regen <- pcoa_regen_grazing$eig
prop_var_regen <- round(100 * eig_vals_regen[1:2] / sum(eig_vals_regen), 1)

# Recode grazing levels
scores_regen_grazing$grazing <- dplyr::recode(scores_regen_grazing$grazing,
                                              "y" = "grazed",
                                              "n" = "ungrazed")
scores_regen_grazing$grazing <- factor(scores_regen_grazing$grazing,
                                       levels = c("grazed", "ungrazed"))

# Plot
regen_grazing_plot <- ggplot(scores_regen_grazing, aes(x = PCoA1, y = PCoA2, color = grazing, shape = grazing)) +
  geom_point(size = 3, alpha = 0.8) +
  stat_ellipse(type = "norm", level = 0.95, size = 0.8) +
  scale_color_manual(values = c("grazed" = "darkgreen", "ungrazed" = "orange")) +
  scale_shape_manual(values = c("grazed" = 16, "ungrazed" = 17)) +
  labs(
    x = paste0("PCoA 1 (", prop_var_regen[1], "%)"),
    y = paste0("PCoA 2 (", prop_var_regen[2], "%)"),
    title = "PCoA of Regeneration Composition by Grazing"
  ) +
  theme_minimal(base_size = 12) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"))


regen_grazing_plot

adult_grazing_plot + regen_grazing_plot

Comp CCA

# CCA comp


adult_comp_nat <- treedata %>%
  filter(class == "tree", transect %in% 1:17)

regen_comp_nat <- treedata %>%
  filter(class %in% c("seedling", "sapling"), transect %in% 1:17)

adult_mat <- adult_comp_nat %>%
  count(transect, Species) %>%
  pivot_wider(names_from = Species, values_from = n, values_fill = 0)
regen_mat <- regen_comp_nat %>%
  count(transect, Species) %>%
  pivot_wider(names_from = Species, values_from = n, values_fill = 0)

meta_grazing <- regen_comp_nat %>%
  distinct(transect, grazing) %>%
  mutate(grazing = dplyr::recode(grazing, "y" = "grazed", "n" = "ungrazed"))
meta_grazing <- meta_grazing %>%
  mutate(transect = as.character(transect))

regen_comm <- regen_mat %>%
  column_to_rownames("transect") %>%
  as.data.frame()
adult_comm <- adult_mat %>%
  column_to_rownames("transect") %>%
  as.data.frame()

predictors <- adult_comm %>%
  rownames_to_column("transect") %>%
  left_join(meta_grazing, by = "transect") %>%
  column_to_rownames("transect")
predictors$grazing <- factor(predictors$grazing)

cca_out <- cca(regen_comm ~ ., data = predictors)

summary(cca_out)

Call:
cca(formula = regen_comm ~ `Silver Birch` + `English Oak` + `Horse Chestnut` +      Beech + `Sessile Oak` + `Oak Sp.` + Aspen + grazing, data = predictors) 

Partitioning of scaled Chi-square:
              Inertia Proportion
Total          2.4019     1.0000
Constrained    1.7565     0.7313
Unconstrained  0.6455     0.2687

Eigenvalues, and their contribution to the scaled Chi-square 

Importance of components:
                        CCA1   CCA2    CCA3    CCA4    CCA5     CCA6     CCA7
Eigenvalue            0.7580 0.6575 0.18313 0.09449 0.04794 0.012459 0.002949
Proportion Explained  0.3156 0.2737 0.07624 0.03934 0.01996 0.005187 0.001228
Cumulative Proportion 0.3156 0.5893 0.66555 0.70489 0.72484 0.730031 0.731259
                           CCA8    CA1     CA2     CA3     CA4      CA5
Eigenvalue            2.085e-05 0.2668 0.16753 0.13850 0.04882 0.018894
Proportion Explained  8.680e-06 0.1111 0.06975 0.05766 0.02033 0.007866
Cumulative Proportion 7.313e-01 0.8424 0.91210 0.96976 0.99009 0.997956
                           CA6       CA7       CA8
Eigenvalue            0.003740 0.0011329 3.678e-05
Proportion Explained  0.001557 0.0004716 1.531e-05
Cumulative Proportion 0.999513 0.9999847 1.000e+00

Accumulated constrained eigenvalues
Importance of components:
                        CCA1   CCA2   CCA3    CCA4    CCA5     CCA6     CCA7
Eigenvalue            0.7580 0.6575 0.1831 0.09449 0.04794 0.012459 0.002949
Proportion Explained  0.4315 0.3743 0.1043 0.05379 0.02729 0.007093 0.001679
Cumulative Proportion 0.4315 0.8059 0.9101 0.96392 0.99122 0.998309 0.999988
                           CCA8
Eigenvalue            2.085e-05
Proportion Explained  1.187e-05
Cumulative Proportion 1.000e+00
anova(cca_out, permutations = 999)              # Overall model
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
Model 8 1.7564625 2.721178 0.036
Residual 8 0.6454788 NA NA
anova(cca_out, by = "axis", permutations = 999) # Axis significance
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
CCA1 1 0.7579723 9.3942336 0.029
CCA2 1 0.6575075 8.1490820 0.352
CCA3 1 0.1831267 2.2696536 0.992
CCA4 1 0.0944863 1.1710538 1.000
CCA5 1 0.0479406 0.5941706 NA
CCA6 1 0.0124590 0.1544154 NA
CCA7 1 0.0029493 0.0365534 NA
CCA8 1 0.0000208 0.0002584 NA
Residual 8 0.6454788 NA NA
anova(cca_out, by = "term", permutations = 999) # Individual predictors
Warning in attr(x, "align"): 'xfun::attr()' is deprecated.
Use 'xfun::attr2()' instead.
See help("Deprecated")
Df ChiSquare F Pr(>F)
Silver Birch 1 0.4153543 5.1478600 0.055
English Oak 1 0.5475716 6.7865474 0.004
Horse Chestnut 1 0.0992368 1.2299307 0.339
Beech 1 0.0378915 0.4696238 0.787
Sessile Oak 1 0.1354391 1.6786189 0.310
Oak Sp. 1 0.0101541 0.1258488 0.900
Aspen 1 0.3153147 3.9079795 0.060
grazing 1 0.1955004 2.4230119 0.126
Residual 8 0.6454788 NA NA