Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Third traitstrap imputation trail #21

Open
wants to merge 8 commits into
base: DA.1_traitstrap
Choose a base branch
from
77 changes: 48 additions & 29 deletions scripts/DA1_traitstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ source(here::here(path = "scripts/0_data_import.R"))
### Packages----

# install.packages("remotes")
remotes::install_github("richardjtelford/traitstrap")
# library(traitstrap)
#remotes::install_github("richardjtelford/traitstrap")
library(traitstrap)

species %>%
select(taxon, cover, site, treatment, plot_id) %>%
Expand All @@ -17,15 +17,19 @@ species %>%

### Trait Imputation ----

trait_imputation =
# Bootstrapping of control datasets ----

trait_imputation_control =
trait_impute(
# input data (mandatory)
comm = species %>%
select(taxon, cover, site, treatment, plot_id) %>%
mutate(treatment = as.factor(treatment)),
select(taxon, cover, site, treatment, plot_id, year, season, month) %>%
mutate(treatment = as.factor(treatment)) %>%
filter(treatment == "C"),
traits = traits %>%
select(taxon, trait, value, site, treatment, plot_id) %>%
mutate(treatment = as.factor(treatment)),
select(taxon, trait, value, site, treatment, plot_id, year, season, month) %>%
mutate(treatment = as.factor(treatment)) %>%
filter(treatment == "C"),

# specifies columns in your data (mandatory)
abundance_col = "cover",
Expand All @@ -34,27 +38,26 @@ trait_imputation =
value_col = "value",

# specifies sampling hierarchy
scale_hierarchy = c("site", "plot_id"),

# specifying experimental design
treatment_col = "treatment",
treatment_level = "site",
scale_hierarchy = c("year", "season", "month","site", "plot_id"),


# min number of samples
min_n_in_sample = 3
)

# no experimental design
# Bootstrapping of burnt datasets ----

trait_imputation_2 =
trait_imputation_burnt =
trait_impute(
# input data (mandatory)
comm = species %>%
select(taxon, cover, site, treatment, plot_id) %>%
mutate(treatment = as.factor(treatment)),
select(taxon, cover, site, treatment, plot_id, year, season, month) %>%
mutate(treatment = as.factor(treatment)) %>%
filter(treatment == "NB"),
traits = traits %>%
select(taxon, trait, value, site, treatment, plot_id) %>%
mutate(treatment = as.factor(treatment)),
select(taxon, trait, value, site, treatment, plot_id, year, season, month) %>%
mutate(treatment = as.factor(treatment)) %>%
filter(treatment == "NB"),

# specifies columns in your data (mandatory)
abundance_col = "cover",
Expand All @@ -63,32 +66,48 @@ trait_imputation_2 =
value_col = "value",

# specifies sampling hierarchy
scale_hierarchy = c("site", "treatment", "plot_id"),
scale_hierarchy = c("year", "season", "month","site", "plot_id"),


# min number of samples
min_n_in_sample = 3
)

# Nonparametric Bootstrapping ----

bootstrapped_moments =
bootstrapped_moments_control =
trait_np_bootstrap(
trait_imputation_control,
nrep = 200
)

bootstrapped_moments_burnt =
trait_np_bootstrap(
trait_imputation,
trait_imputation_burnt,
nrep = 200
)


# Summarise Bootstrapping Output ----
# Summarize Bootstrapping Output ----

sum_boot_moment_control <- trait_summarise_boot_moments(bootstrapped_moments_control) %>%
mutate(treatment = "C")

sum_boot_moment_burnt <- trait_summarise_boot_moments(bootstrapped_moments_burnt) %>%
mutate(treatment = "NB")

# Merge bootstrapped data sets ----

sum_boot_moments_full <- sum_boot_moment_control %>%
bind_rows(sum_boot_moment_burnt)

sum_boot_moment <- trait_summarise_boot_moments(
np_bootstrapped_moments
)
sum_boot_moment

# Some 'random' plots ----

autoplot(trait_imputation) +
autoplot(trait_imputation_control) +
theme(axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5))

autoplot(trait_imputation_burnt) +
theme(axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5))

autoplot(trait_imputation_2) +
theme(axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5))
sum_boot_moments_full %>% ggplot(aes(x = treatment, y = mean, fill = treatment)) + geom_boxplot() + facet_grid(trait ~ site, scales = "free")