Title: | Fitting and Assessing Neighborhood Models of the Effect of Interspecific Competition on the Growth of Trees |
---|---|
Description: | Code for fitting and assessing models for the growth of trees. In particular for the Bayesian neighborhood competition linear regression model of Allen (2020): methods for model fitting and generating fitted/predicted values, evaluating the effect of competitor species identity using permutation tests, and evaluating model performance using spatial cross-validation. |
Authors: | Albert Y. Kim [aut, cre] , David Allen [aut] , Simon Couch [aut] |
Maintainer: | Albert Y. Kim <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.0.9000 |
Built: | 2024-11-03 03:36:17 UTC |
Source: | https://github.com/rudeboybert/forestecology |
Identify trees in the buffer region
add_buffer_variable(growth_df, direction = "in", size, region)
add_buffer_variable(growth_df, direction = "in", size, region)
growth_df |
|
direction |
"in" for buffers that are contained within |
size |
Distance to determine which neighboring trees to a focal tree are
competitors. The units are assumed to be the same as the |
region |
An |
The same growth_df
data frame but with a new boolean
variable buffer
indicating if a tree is in the study region buffer
area. This uses compute_buffer_region()
to define the boundary
of the buffer region.
Other spatial functions:
compute_buffer_region()
,
focal_vs_comp_distance()
library(tibble) library(sfheaders) library(ggplot2) # Example square region to be buffered region <- tibble( x = c(0, 0, 1, 1), y = c(0, 1, 1, 0) ) %>% sf_polygon() # Example points study_points <- tibble( x = runif(50), y = runif(50) ) %>% sf_point() # Size of buffer size <- 0.05 # Identify whether points are within size of boundary study_points <- study_points %>% add_buffer_variable(direction = "in", size = size, region = region) # Plot study points coded by whether they are within size of boundary p <- ggplot() + geom_sf(data = region, fill = "transparent") + geom_sf(data = study_points, aes(col = buffer)) p # Additionally, show buffer boundary in red buffer_boundary <- region %>% compute_buffer_region(direction = "in", size = size) p + geom_sf(data = buffer_boundary, col = "red", fill = "transparent")
library(tibble) library(sfheaders) library(ggplot2) # Example square region to be buffered region <- tibble( x = c(0, 0, 1, 1), y = c(0, 1, 1, 0) ) %>% sf_polygon() # Example points study_points <- tibble( x = runif(50), y = runif(50) ) %>% sf_point() # Size of buffer size <- 0.05 # Identify whether points are within size of boundary study_points <- study_points %>% add_buffer_variable(direction = "in", size = size, region = region) # Plot study points coded by whether they are within size of boundary p <- ggplot() + geom_sf(data = region, fill = "transparent") + geom_sf(data = study_points, aes(col = buffer)) p # Additionally, show buffer boundary in red buffer_boundary <- region %>% compute_buffer_region(direction = "in", size = size) p + geom_sf(data = buffer_boundary, col = "red", fill = "transparent")
Plot Bayesian model parameters
## S3 method for class 'comp_bayes_lm' autoplot(object, type = "intercepts", sp_to_plot = NULL, ...)
## S3 method for class 'comp_bayes_lm' autoplot(object, type = "intercepts", sp_to_plot = NULL, ...)
object |
Output of |
type |
A single character string for plot type with possible values "intercepts", "dbh_slopes", or "competition". |
sp_to_plot |
Vector of subset of species to plot |
... |
Currently ignored—only included for consistency with generic. |
focal_vs_comp
with new column of predicted growth_hat
library(ggplot2) library(ggridges) # Load in posterior parameter example data(comp_bayes_lm_ex) # Plot beta_0, growth intercepts autoplot(comp_bayes_lm_ex, type = "intercepts") # Plot beta_dbh, growth-dbh slope autoplot(comp_bayes_lm_ex, type = "dbh_slopes") # Plot lambdas, competition coefficients autoplot(comp_bayes_lm_ex, type = "competition")
library(ggplot2) library(ggridges) # Load in posterior parameter example data(comp_bayes_lm_ex) # Plot beta_0, growth intercepts autoplot(comp_bayes_lm_ex, type = "intercepts") # Plot beta_dbh, growth-dbh slope autoplot(comp_bayes_lm_ex, type = "dbh_slopes") # Plot lambdas, competition coefficients autoplot(comp_bayes_lm_ex, type = "competition")
This is an example cross validation grid. This is needed to create the focal versus comp data frame and run cross-validated models
blocks_ex
blocks_ex
A sf
polygons
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Point location of the individual
Other example data objects:
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(ggplot2) library(sf) library(dplyr) comp_dist <- 1 ggplot(blocks_ex) + geom_sf() + geom_sf(data = growth_spatial_ex) focal_vs_comp_ex <- growth_spatial_ex %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist, blocks = blocks_ex, id = "ID", comp_x_var = "basal_area")
library(ggplot2) library(sf) library(dplyr) comp_dist <- 1 ggplot(blocks_ex) + geom_sf() + geom_sf(data = growth_spatial_ex) focal_vs_comp_ex <- growth_spatial_ex %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist, blocks = blocks_ex, id = "ID", comp_x_var = "basal_area")
This is example forest census data to be analyzed with this package.
census_1_ex
census_1_ex
A tibble
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Species of the individual
x-coordinate meters from reference point
y-coordinate meters from reference point
Date the stem was measured
Code for additional information on the stem: M means the main stem of the individual tree and R means the stem was lost, but the tag was moved to another stem greater than DBH cutoff, this stands for resprout.
Diameter at breast height of the tree in cm
Other example data objects:
blocks_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(dplyr) library(stringr) growth_ex <- compute_growth( census_1 = census_1_ex %>% mutate(sp = to_any_case(sp) %>% factor()), census_2 = census_2_ex %>% filter(!str_detect(codes, "R")) %>% mutate(sp = to_any_case(sp) %>% factor()), id = "ID" )
library(dplyr) library(stringr) growth_ex <- compute_growth( census_1 = census_1_ex %>% mutate(sp = to_any_case(sp) %>% factor()), census_2 = census_2_ex %>% filter(!str_detect(codes, "R")) %>% mutate(sp = to_any_case(sp) %>% factor()), id = "ID" )
This is an example second census to be analyzed with the package.
census_2_ex
census_2_ex
A tibble
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Species of the individual
x-coordinate meters from reference point
y-coordinate meters from reference point
Date the stem was measured
Code for additional information on the stem: M means the main stem of the individual tree and R means the stem was lost, but the tag was moved to another stem greater than DBH cutoff, this stands for resprout.
Diameter at breast height of the tree in cm
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(dplyr) library(stringr) growth_ex <- compute_growth( census_1 = census_1_ex %>% mutate(sp = to_any_case(sp) %>% factor()), census_2 = census_2_ex %>% filter(!str_detect(codes, "R")) %>% mutate(sp = to_any_case(sp) %>% factor()), id = "ID" )
library(dplyr) library(stringr) growth_ex <- compute_growth( census_1 = census_1_ex %>% mutate(sp = to_any_case(sp) %>% factor()), census_2 = census_2_ex %>% filter(!str_detect(codes, "R")) %>% mutate(sp = to_any_case(sp) %>% factor()), id = "ID" )
The Big Woods data come from three censuses of a 23 ha forest research plots. All free-standing vegetation greater than 1 cm diameter at 1.3 m height (diameter at breast height; DBH) were tagged, identified, spatially mapped and had their DBH measured. The original census took place in 2003 and covered only 12 ha. A second census took place from 2008-2010 and expanded the plot to its current 23 ha. In the first and second censuses trees larger than 3.1 cm DBH were included. Finally a third census took place in 2014. In this census trees larger than 1 cm DBH were included. In the second and third censuses the original trees were found, recorded for survival, remeasured, and new individuals were tagged. This data frame has data from the second census (2008-2010).
census_2008_bw
census_2008_bw
A data frame with 27193 rows and 8 variables:
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Stem number for a multi-stemmed individual. For all trees this starts
at 1 and continues up from there. To uniquely identify a stem across the plot this
value must be combined with treeID
.
Diameter at breast
Code for the species. See species_bw
for scientific name.
x-coordinate meters from reference point
y-coordinate meters from reference point
Approximate date the stem was measured
Code for additional information on the stem: M means the main stem of the individual tree; AL means the stem is alive but leaning or completely fallen over; B means the stem is broken and over half the canopy is assumed to be missing; and R means the stem was lost, but the tag was moved to another stem greater than DBH cutoff, this stands for resprout.
This plot is part of the Smithsonian Institution's Forest Global Earth Observatory (ForestGEO) global network of forest research sites. For complete details on this dataset see its Deep Blue Data repository page.
Other Big Woods data:
census_2014_bw
,
species_bw
,
study_region_bw
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(ggplot2) library(sf) # Convert all 2008 plot stems to sf object census_2008_bw_sf <- census_2008_bw %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = census_2008_bw_sf, size = 0.25)
library(ggplot2) library(sf) # Convert all 2008 plot stems to sf object census_2008_bw_sf <- census_2008_bw %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = census_2008_bw_sf, size = 0.25)
The Big Woods data come from three censuses of a 23 ha forest research plots. All free-standing vegetation greater than 1 cm diameter at 1.3 m height (diameter at breast height; DBH) were tagged, identified, spatially mapped and had their DBH measured. The original census took place in 2003 and covered only 12 ha. A second census took place from 2008-2010 and expanded the plot to its current 23 ha. In the first and second censuses trees larger than 3.1 cm DBH were included. Finally a third census took place in 2014. In this census trees larger than 1 cm DBH were included. In the second and third censuses the original trees were found, recorded for survival, remeasured, and new individuals were tagged. This data frame has data from the third census (2014).
census_2014_bw
census_2014_bw
A data frame with 48371 rows and 8 variables:
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Stem number for a multi-stemmed individual. For all trees this starts
at 1 and continues up from there. To uniquely identify a stem across the plot this
value must be combined with treeID
.
Code for the species. See species_bw
for scientific name.
Diameter at breast
x-coordinate meters from reference point
y-coordinate meters from reference point
Approximate date the stem was measured
Code for additional information on the stem: M means the main stem of the individual tree; AL means the stem is alive but leaning or completely fallen over; B means the stem is broken and over half the canopy is assumed to be missing; and R means the stem was lost, but the tag was moved to another stem greater than DBH cutoff, this stands for resprout.
This plot is part of the Smithsonian Institution's Forest Global Earth Observatory (ForestGEO) global network of forest research sites. For complete details on this dataset see its Deep Blue Data repository page.
Other Big Woods data:
census_2008_bw
,
species_bw
,
study_region_bw
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(ggplot2) library(sf) library(dplyr) # Convert all 2008 plot stems to sf object census_2008_bw_sf <- census_2008_bw %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = census_2008_bw_sf, size = 0.25) # Species-specific mortality between 2008 and 2014 censuses census_2008_bw %>% left_join(census_2014_bw, by = c("treeID", "stemID"), suffix = c("_2008", "_2014")) %>% mutate(mortality = ifelse(is.na(dbh_2014), 1, 0)) %>% group_by(sp_2008) %>% summarize(mortality = mean(mortality), n = n()) %>% arrange(desc(n))
library(ggplot2) library(sf) library(dplyr) # Convert all 2008 plot stems to sf object census_2008_bw_sf <- census_2008_bw %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = census_2008_bw_sf, size = 0.25) # Species-specific mortality between 2008 and 2014 censuses census_2008_bw %>% left_join(census_2014_bw, by = c("treeID", "stemID"), suffix = c("_2008", "_2014")) %>% mutate(mortality = ifelse(is.na(dbh_2014), 1, 0)) %>% group_by(sp_2008) %>% summarize(mortality = mean(mortality), n = n()) %>% arrange(desc(n))
Fit a Bayesian linear regression model with interactions terms where
|
mean hyperparameter vector for of length |
|
covariance hyperparameter matrix for of dimension |
|
shape hyperparameter for |
|
scale hyperparameter for
|
comp_bayes_lm(focal_vs_comp, prior_param = NULL, run_shuffle = FALSE)
comp_bayes_lm(focal_vs_comp, prior_param = NULL, run_shuffle = FALSE)
focal_vs_comp |
data frame from |
prior_param |
A list of |
run_shuffle |
boolean as to whether to run permutation test shuffle of competitor tree species within a particular focal_ID |
A list of {a_star, b_star, mu_star, V_star}
posterior hyperparameters
Closed-form solutions of Bayesian linear regression doi:10.1371/journal.pone.0229930.s004
Other modeling functions:
create_bayes_lm_data()
,
predict.comp_bayes_lm()
,
run_cv()
library(dplyr) # Load in focal versus comp data(focal_vs_comp_ex) comp_bayes_lm_ex <- focal_vs_comp_ex %>% comp_bayes_lm(prior_param = NULL, run_shuffle = FALSE)
library(dplyr) # Load in focal versus comp data(focal_vs_comp_ex) comp_bayes_lm_ex <- focal_vs_comp_ex %>% comp_bayes_lm(prior_param = NULL, run_shuffle = FALSE)
This object contains an example fitted Bayesian competition
model outputted by comp_bayes_lm()
.
comp_bayes_lm_ex
comp_bayes_lm_ex
A list subclass containing the following elements:
Prior parameters supplied to comp_bayes_lm()
Posterior parameters outputted by comp_bayes_lm()
The formula object used in model fitting
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(dplyr) library(yardstick) # Compare model predictions to observation predictions <- focal_vs_comp_ex %>% mutate(growth_hat = predict(comp_bayes_lm_ex, focal_vs_comp_ex)) predictions %>% rmse(truth = growth, estimate = growth_hat) %>% pull(.estimate) # Plot posterior parameters comp_bayes_lm_ex %>% autoplot()
library(dplyr) library(yardstick) # Compare model predictions to observation predictions <- focal_vs_comp_ex %>% mutate(growth_hat = predict(comp_bayes_lm_ex, focal_vs_comp_ex)) predictions %>% rmse(truth = growth, estimate = growth_hat) %>% pull(.estimate) # Plot posterior parameters comp_bayes_lm_ex %>% autoplot()
Compute buffer to a region.
compute_buffer_region(region, direction = "in", size)
compute_buffer_region(region, direction = "in", size)
region |
An |
direction |
"in" for buffers that are contained within |
size |
Distance to determine which neighboring trees to a focal tree are
competitors. The units are assumed to be the same as the |
An sf
polygon object of buffer
Other spatial functions:
add_buffer_variable()
,
focal_vs_comp_distance()
library(tibble) library(sfheaders) library(ggplot2) # Example square region to be buffered (as sf object) region <- tibble( x = c(0, 0, 1, 1), y = c(0, 1, 1, 0) ) %>% sf_polygon() # Size of buffer size <- 0.05 # Compute "inwards" buffer inwards_buffer_region <- region %>% compute_buffer_region(direction = "in", size = size) # Compute "outwards" buffer outwards_buffer_region <- region %>% compute_buffer_region(direction = "out", size = size) # Plot all three regions: ggplot() + geom_sf(data = outwards_buffer_region, col = "blue", fill = "transparent") + geom_sf(data = region, fill = "transparent") + geom_sf(data = inwards_buffer_region, col = "orange", fill = "transparent") + labs(title = "Regions: original (black), inwards buffer (orange), and outwards buffer (blue)")
library(tibble) library(sfheaders) library(ggplot2) # Example square region to be buffered (as sf object) region <- tibble( x = c(0, 0, 1, 1), y = c(0, 1, 1, 0) ) %>% sf_polygon() # Size of buffer size <- 0.05 # Compute "inwards" buffer inwards_buffer_region <- region %>% compute_buffer_region(direction = "in", size = size) # Compute "outwards" buffer outwards_buffer_region <- region %>% compute_buffer_region(direction = "out", size = size) # Plot all three regions: ggplot() + geom_sf(data = outwards_buffer_region, col = "blue", fill = "transparent") + geom_sf(data = region, fill = "transparent") + geom_sf(data = inwards_buffer_region, col = "orange", fill = "transparent") + labs(title = "Regions: original (black), inwards buffer (orange), and outwards buffer (blue)")
Based on two tree censuses, compute the average annual growth in dbh
for all
trees.
compute_growth(census_1, census_2, id)
compute_growth(census_1, census_2, id)
census_1 |
A data frame of the first census. |
census_2 |
A data frame of the second (later) census |
id |
Name of variable that uniquely identifies each tree common
to |
An sf
data frame with column growth
giving the average
annual growth in dbh
.
Other data processing functions:
create_bayes_lm_data()
,
create_focal_vs_comp()
library(dplyr) library(stringr) growth_ex <- compute_growth( census_1 = census_1_ex %>% mutate(sp = to_any_case(sp) %>% factor()), census_2 = census_2_ex %>% filter(!str_detect(codes, "R")) %>% mutate(sp = to_any_case(sp) %>% factor()), id = "ID" )
library(dplyr) library(stringr) growth_ex <- compute_growth( census_1 = census_1_ex %>% mutate(sp = to_any_case(sp) %>% factor()), census_2 = census_2_ex %>% filter(!str_detect(codes, "R")) %>% mutate(sp = to_any_case(sp) %>% factor()), id = "ID" )
This function "widens" focal-competitor data frames for use inside of
package modeling functions, where each comp_sp
inside of the comp
list-column receives its own column with its associated total basal area.
This function is used internally by comp_bayes_lm()
and
predict.comp_bayes_lm()
exported as a convenience for
applications extending this package's functionality.
create_bayes_lm_data(focal_vs_comp, run_shuffle = FALSE)
create_bayes_lm_data(focal_vs_comp, run_shuffle = FALSE)
focal_vs_comp |
data frame from |
run_shuffle |
boolean as to whether to run permutation test shuffle of competitor tree species within a particular focal_ID |
Data frame for internal package use.
Other modeling functions:
comp_bayes_lm()
,
predict.comp_bayes_lm()
,
run_cv()
Other data processing functions:
compute_growth()
,
create_focal_vs_comp()
create_bayes_lm_data(focal_vs_comp_ex)
create_bayes_lm_data(focal_vs_comp_ex)
"Focal versus competitor trees" data frames are the main data frame used for analysis. "Focal trees" are all trees that satisfy the following criteria
Were alive at both censuses
Were not part of the study region's buffer as computed by add_buffer_variable()
Were not a resprout at the second census. Such trees should be coded as
"R"
in the codes2
variable (OK if a resprout at first census)
For each focal tree, "competitor trees" are all trees that (1) were alive at
the first census and (2) within comp_dist
distance of the focal tree.
create_focal_vs_comp(growth_df, comp_dist, blocks, id, comp_x_var)
create_focal_vs_comp(growth_df, comp_dist, blocks, id, comp_x_var)
growth_df |
A |
comp_dist |
Distance to determine which neighboring trees to a focal tree are competitors. |
blocks |
An sf object of a |
id |
A character string of the variable name in |
comp_x_var |
A character string indicating which numerical variable to use as competitor explanatory variable |
focal_vs_comp
data frame of all focal trees and for
each focal tree all possible competitor trees. In particular, for
each competitor tree we return comp_x_var
. Potential examples of
comp_x_var
include
basal area or
estimate above ground biomass.
In order to speed computation, in particular of distances between all
focal/competitor tree pairs, we use the cross-validation blockCV
object to divide the study region into smaller subsets.
Other data processing functions:
compute_growth()
,
create_bayes_lm_data()
library(ggplot2) library(dplyr) library(stringr) library(sf) library(sfheaders) library(tibble) # Create fold information sf object. SpatialBlock_ex <- tibble( # Study region boundary x = c(0, 0, 5, 5), y = c(0, 5, 5, 0) ) %>% # Convert to sf object sf_polygon() %>% mutate(folds = "1") # Plot example data. Observe for comp_dist = 1.5, there are 6 focal vs comp pairs: # 1. focal 1 vs comp 2 # 2. focal 2 vs comp 1 # 3. focal 2 vs comp 3 # 4. focal 3 vs comp 2 # 5. focal 4 vs comp 5 # 6. focal 5 vs comp 4 ggplot() + geom_sf(data = SpatialBlock_ex, fill = "transparent") + geom_sf_label(data = growth_toy, aes(label = ID)) # Return corresponding data frame growth_toy %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist = 1.5, blocks = SpatialBlock_ex, id = "ID", comp_x_var = "basal_area") # Load in growth_df with spatial data # See ?growth_ex for attaching spatial data to growth_df data(growth_spatial_ex) # Load in blocks data(blocks_ex) focal_vs_comp_ex <- growth_spatial_ex %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist = 1, blocks = blocks_ex, id = "ID", comp_x_var = "basal_area")
library(ggplot2) library(dplyr) library(stringr) library(sf) library(sfheaders) library(tibble) # Create fold information sf object. SpatialBlock_ex <- tibble( # Study region boundary x = c(0, 0, 5, 5), y = c(0, 5, 5, 0) ) %>% # Convert to sf object sf_polygon() %>% mutate(folds = "1") # Plot example data. Observe for comp_dist = 1.5, there are 6 focal vs comp pairs: # 1. focal 1 vs comp 2 # 2. focal 2 vs comp 1 # 3. focal 2 vs comp 3 # 4. focal 3 vs comp 2 # 5. focal 4 vs comp 5 # 6. focal 5 vs comp 4 ggplot() + geom_sf(data = SpatialBlock_ex, fill = "transparent") + geom_sf_label(data = growth_toy, aes(label = ID)) # Return corresponding data frame growth_toy %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist = 1.5, blocks = SpatialBlock_ex, id = "ID", comp_x_var = "basal_area") # Load in growth_df with spatial data # See ?growth_ex for attaching spatial data to growth_df data(growth_spatial_ex) # Load in blocks data(blocks_ex) focal_vs_comp_ex <- growth_spatial_ex %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist = 1, blocks = blocks_ex, id = "ID", comp_x_var = "basal_area")
Return all pairwise distances between two data frames of trees
focal_vs_comp_distance(focal_trees, comp_trees)
focal_vs_comp_distance(focal_trees, comp_trees)
focal_trees |
An |
comp_trees |
An |
A data frame with three columns: focal_ID
of focal tree,
comp_dist
of competitor tree, and dist
of distance between
them.
Other spatial functions:
add_buffer_variable()
,
compute_buffer_region()
library(tibble) library(ggplot2) library(sf) # Create toy example focal and competitor trees focal_trees <- tibble( focal_ID = c(1, 2, 3), x = c(0.3, 0.6, 0.7), y = c(0.1, 0.5, 0.7) ) %>% st_as_sf(coords = c("x", "y")) comp_trees <- tibble( comp_ID = c(4, 5, 6, 7), x = c(0, 0.2, 0.4, 0.6), y = c(0.6, 0.7, 1, 0.2) ) %>% st_as_sf(coords = c("x", "y")) # Plot both sets of trees ggplot() + geom_sf_label(data = focal_trees, aes(label = focal_ID), col = "black") + geom_sf_label(data = comp_trees, aes(label = comp_ID), col = "orange") + labs(title = "Focal trees in black, competitor trees in orange") # Compute corresponding distances between the 3 focal trees and 4 competitor trees focal_vs_comp_distance(focal_trees, comp_trees)
library(tibble) library(ggplot2) library(sf) # Create toy example focal and competitor trees focal_trees <- tibble( focal_ID = c(1, 2, 3), x = c(0.3, 0.6, 0.7), y = c(0.1, 0.5, 0.7) ) %>% st_as_sf(coords = c("x", "y")) comp_trees <- tibble( comp_ID = c(4, 5, 6, 7), x = c(0, 0.2, 0.4, 0.6), y = c(0.6, 0.7, 1, 0.2) ) %>% st_as_sf(coords = c("x", "y")) # Plot both sets of trees ggplot() + geom_sf_label(data = focal_trees, aes(label = focal_ID), col = "black") + geom_sf_label(data = comp_trees, aes(label = comp_ID), col = "orange") + labs(title = "Focal trees in black, competitor trees in orange") # Compute corresponding distances between the 3 focal trees and 4 competitor trees focal_vs_comp_distance(focal_trees, comp_trees)
This is an example focal versus comp data frame. The rows are focal trees which are repeated for all competitor trees within a specified distance from them. In this case that distance is 1. This is the focal versus comp for growth_spatial_ex.
focal_vs_comp_ex
focal_vs_comp_ex
Tree identification number for the focal tree
Species of the focal tree
Diameter at breast height of the focal tree at the first census
The CV-fold that the focal tree is in
The point location of the focal tree
The average annual growth of the focal tree between censuses
A list-column: characteristics of the relevant competitor trees
The comp
list-column contains tibble::tbl_dfs with columns:
Tree identification number for the competitor tree
The distance between the focal and comp tree, this will be less than the max distance specified.
Species of the comp tree
Numerical variable associated with comp tree
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
comp_bayes_lm_ex <- focal_vs_comp_ex %>% comp_bayes_lm(prior_param = NULL, run_shuffle = FALSE)
comp_bayes_lm_ex <- focal_vs_comp_ex %>% comp_bayes_lm(prior_param = NULL, run_shuffle = FALSE)
forestecology
packageMethods and data for forest ecology model selection and assessment
See the README on GitHub
This is an example growth data frame formed from two census data frames. In this case it is made by combining census_1_ex and census_2_ex. The individuals alive in both censuses were linked by their tree ID.
growth_ex
growth_ex
A sf
spatial tibble
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Species of the individual
Code for additional information on the stem during the first census: M means the main stem of the individual tree and R means the stem was lost, but the tag was moved to another stem greater than DBH cutoff, this stands for resprout.
Diameter at breast height of the tree in cm at the first census
Diameter at breast height of the tree in cm at the second census
Average annual growth between the two censuses in cm per year
Codes at the second census
Point location of the individual
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(ggplot2) library(dplyr) library(sf) library(sfheaders) library(blockCV) growth_ex %>% ggplot() + geom_sf() growth_ex %>% group_by(sp) %>% summarize(mean(growth)) # Add buffer growth_spatial_ex <- growth_ex %>% add_buffer_variable(direction = "in", size = 1, region = study_region_ex) # Add cross-validation folds fold1 <- rbind(c(0, 0), c(5, 0), c(5, 5), c(0, 5), c(0, 0)) fold2 <- rbind(c(5, 0), c(10, 0), c(10, 5), c(5, 5), c(5, 0)) blocks <- bind_rows( sf_polygon(fold1), sf_polygon(fold2) ) %>% mutate(foldID = c(1, 2)) SpatialBlock_ex <- spatialBlock( speciesData = growth_ex, verbose = FALSE, k = 2, selection = "systematic", blocks = blocks ) # Add foldID to data growth_spatial_ex <- growth_spatial_ex %>% mutate(foldID = SpatialBlock_ex$foldID %>% as.factor())
library(ggplot2) library(dplyr) library(sf) library(sfheaders) library(blockCV) growth_ex %>% ggplot() + geom_sf() growth_ex %>% group_by(sp) %>% summarize(mean(growth)) # Add buffer growth_spatial_ex <- growth_ex %>% add_buffer_variable(direction = "in", size = 1, region = study_region_ex) # Add cross-validation folds fold1 <- rbind(c(0, 0), c(5, 0), c(5, 5), c(0, 5), c(0, 0)) fold2 <- rbind(c(5, 0), c(10, 0), c(10, 5), c(5, 5), c(5, 0)) blocks <- bind_rows( sf_polygon(fold1), sf_polygon(fold2) ) %>% mutate(foldID = c(1, 2)) SpatialBlock_ex <- spatialBlock( speciesData = growth_ex, verbose = FALSE, k = 2, selection = "systematic", blocks = blocks ) # Add foldID to data growth_spatial_ex <- growth_spatial_ex %>% mutate(foldID = SpatialBlock_ex$foldID %>% as.factor())
This is an example growth data frame formed from two census data frames which has been updated with spatial data. It starts from growth_ex.
growth_spatial_ex
growth_spatial_ex
A sf
spatial tibble
Tree identification number. This identifies an individual tree and can be used to connect trees between the two censuses.
Species of the individual
Code for additional information on the stem during the first census: M means the main stem of the individual tree and R means the stem was lost, but the tag was moved to another stem greater than DBH cutoff, this stands for resprout.
Diameter at breast height of the tree in cm at the first census
Diameter at breast height of the tree in cm at the second census
Average annual growth between the two censuses in cm per year
Codes at the second census
Point location of the individual
A boolean variable for whether the individual is in the buffer region or not
Which cross-validation fold the individual is in
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_toy
,
species_bw
,
study_region_bw
,
study_region_ex
library(ggplot2) library(dplyr) library(sf) comp_dist <- 1 ggplot() + geom_sf(data = growth_spatial_ex, aes(col = buffer), size = 2) ggplot() + geom_sf(data = growth_spatial_ex, aes(col = foldID), size = 2) # Create the focal versus comp data frame focal_vs_comp_ex <- growth_spatial_ex %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist, blocks = blocks_ex, id = "ID", comp_x_var = "basal_area")
library(ggplot2) library(dplyr) library(sf) comp_dist <- 1 ggplot() + geom_sf(data = growth_spatial_ex, aes(col = buffer), size = 2) ggplot() + geom_sf(data = growth_spatial_ex, aes(col = foldID), size = 2) # Create the focal versus comp data frame focal_vs_comp_ex <- growth_spatial_ex %>% mutate(basal_area = 0.0001 * pi * (dbh1 / 2)^2) %>% create_focal_vs_comp(comp_dist, blocks = blocks_ex, id = "ID", comp_x_var = "basal_area")
create_focal_vs_comp()
An example sf
of type generated by compute_growth()
growth_toy
growth_toy
A sf
spatial features polygon
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
species_bw
,
study_region_bw
,
study_region_ex
Applies fitted model from comp_bayes_lm()
and
returns posterior predicted values.
## S3 method for class 'comp_bayes_lm' predict(object, newdata, ...)
## S3 method for class 'comp_bayes_lm' predict(object, newdata, ...)
object |
Output of |
newdata |
A data frame of type |
... |
Currently ignored—only included for consistency with generic. |
A vector of predictions with length equal to the input data.
Closed-form solutions of Bayesian linear regression doi:10.1371/journal.pone.0229930.s004
Other modeling functions:
comp_bayes_lm()
,
create_bayes_lm_data()
,
run_cv()
library(dplyr) library(sf) library(ggplot2) # Load in posterior parameter example # and growth data to compare to data(comp_bayes_lm_ex, growth_ex) predictions <- focal_vs_comp_ex %>% mutate(growth_hat = predict(comp_bayes_lm_ex, focal_vs_comp_ex)) predictions %>% ggplot(aes(growth, growth_hat)) + geom_point() + geom_abline(slope = 1, intercept = 0)
library(dplyr) library(sf) library(ggplot2) # Load in posterior parameter example # and growth data to compare to data(comp_bayes_lm_ex, growth_ex) predictions <- focal_vs_comp_ex %>% mutate(growth_hat = predict(comp_bayes_lm_ex, focal_vs_comp_ex)) predictions %>% ggplot(aes(growth, growth_hat)) + geom_point() + geom_abline(slope = 1, intercept = 0)
This function carries out the bayesian modeling process with spatial cross-validation as described in Allen and Kim (2020). Given a focal-competitor data frame, it appends a column with predicted growth values.
run_cv( focal_vs_comp, comp_dist, blocks, prior_param = NULL, run_shuffle = FALSE )
run_cv( focal_vs_comp, comp_dist, blocks, prior_param = NULL, run_shuffle = FALSE )
focal_vs_comp |
data frame from |
comp_dist |
Distance to determine which neighboring trees to a focal tree are competitors. |
blocks |
An sf object of a |
prior_param |
A list of |
run_shuffle |
boolean as to whether to run permutation test shuffle of competitor tree species within a particular focal_ID |
focal_vs_comp
with new column of predicted growth_hat
Other modeling functions:
comp_bayes_lm()
,
create_bayes_lm_data()
,
predict.comp_bayes_lm()
run_cv( focal_vs_comp_ex, comp_dist = 1, blocks = blocks_ex )
run_cv( focal_vs_comp_ex, comp_dist = 1, blocks = blocks_ex )
A date frame mapping the species codes to their common names, scientific names, and families. This also includes a trait-based clustering of the species.
species_bw
species_bw
A data frame with 46 rows and 6 variables:
The code for the species. Link to census_2008_bw
and census_2014_bw
with sp
variable.
Genus
Species epithet
Scientific name
Family
Clustering of species based on three traits rather than their evolutionary relationships. The traits are specific leaf area, maximum height, and wood density
For more information on trait clustering see Allen and Kim 2020 "A permutation test and spatial cross-validation approach to assess models of interspecific competition between trees." doi:10.1371/journal.pone.0229930PLOS One 15: e0229930.
Other Big Woods data:
census_2008_bw
,
census_2014_bw
,
study_region_bw
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
study_region_bw
,
study_region_ex
library(dplyr) # Original 2008 census data census_2008_bw # 2008 census data with additional species information census_2008_bw %>% left_join(species_bw, by = "sp")
library(dplyr) # Original 2008 census data census_2008_bw # 2008 census data with additional species information census_2008_bw %>% left_join(species_bw, by = "sp")
Boundary region for Bigwoods defined in terms of (x,y) vertices of a polygon.
study_region_bw
study_region_bw
A sf
spatial features polygon
Other Big Woods data:
census_2008_bw
,
census_2014_bw
,
species_bw
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_ex
library(ggplot2) library(sf) # Convert all 2008 plot stems to sf object census_2008_bw_sf <- census_2008_bw %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = census_2008_bw_sf, size = 0.25) + geom_sf(data = study_region_bw, color = "red", fill = "transparent")
library(ggplot2) library(sf) # Convert all 2008 plot stems to sf object census_2008_bw_sf <- census_2008_bw %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = census_2008_bw_sf, size = 0.25) + geom_sf(data = study_region_bw, color = "red", fill = "transparent")
Boundary region for small example data set defined in terms of (x,y) vertices of a polygon.
study_region_ex
study_region_ex
A sf
spatial features polygon
Other example data objects:
blocks_ex
,
census_1_ex
,
census_2008_bw
,
census_2014_bw
,
census_2_ex
,
comp_bayes_lm_ex
,
focal_vs_comp_ex
,
growth_ex
,
growth_spatial_ex
,
growth_toy
,
species_bw
,
study_region_bw
library(ggplot2) library(sf) # Convert stems to sf object census_1_ex_sf <- census_1_ex %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = study_region_ex) + geom_sf(data = study_region_bw, color = "red", fill = "transparent")
library(ggplot2) library(sf) # Convert stems to sf object census_1_ex_sf <- census_1_ex %>% st_as_sf(coords = c("gx", "gy")) # Plot stems with plot boundary ggplot() + geom_sf(data = study_region_ex) + geom_sf(data = study_region_bw, color = "red", fill = "transparent")