Skip to content

Commit

Permalink
Update documentation with example data
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Claessens authored and Scott Claessens committed Jul 28, 2024
1 parent 02fc99a commit 8d9c1a5
Show file tree
Hide file tree
Showing 20 changed files with 272 additions and 353 deletions.
32 changes: 14 additions & 18 deletions R/coev_calculate_delta_theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@
#'
#' @examples
#' \dontrun{
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # fit dynamic coevolutionary model
#' m <- coev_fit(
#' data = d,
#' fit <- coev_fit(
#' data = authority$data,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree,
#' id = "language",
#' tree = authority$phylogeny,
#' # additional arguments for cmdstanr::sample()
#' chains = 4,
#' parallel_chains = 4,
#' iter_warmup = 500,
#' seed = 1
#' )
#' # get delta theta
#' coev_calculate_delta_theta(m, response = "y", predictor = "x")
#' )
#'
#' # calculate delta theta
#' coev_calculate_delta_theta(
#' object = fit,
#' response = "political_authority",
#' predictor = "religious_authority"
#' )
#' }
coev_calculate_delta_theta <- function(object, response, predictor) {
# stop if object is not of class coevfit
Expand Down
32 changes: 15 additions & 17 deletions R/coev_calculate_theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,29 @@
#'
#' @examples
#' \dontrun{
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # fit dynamic coevolutionary model
#' m <- coev_fit(
#' data = d,
#' fit <- coev_fit(
#' data = authority$data,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree,
#' id = "language",
#' tree = authority$phylogeny,
#' # additional arguments for cmdstanr::sample()
#' chains = 4,
#' parallel_chains = 4,
#' iter_warmup = 500,
#' seed = 1
#' )
#' )
#'
#' # calculate theta given intervention values
#' coev_calculate_theta(m, list(x = NA, y = 0))
#' coev_calculate_theta(
#' object = fit,
#' intervention_values = list(
#' political_authority = NA,
#' religious_authority = 0
#' )
#' )
#' }
coev_calculate_theta <- function(object, intervention_values) {
# stop if object is not of class coevfit
Expand Down
28 changes: 11 additions & 17 deletions R/coev_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' overdispersion parameters for negative binomial variables (\code{phi}),
#' the degrees of freedom parameters for Student t variables (\code{nu}),
#' the sigma parameters for Gaussian Processes over locations
#' (\code{sigma_dist}), and the rho parameter for Gaussian Processes over
#' (\code{sigma_dist}), and the rho parameters for Gaussian Processes over
#' locations (\code{rho_dist}). These must be entered with valid prior
#' strings, e.g. \code{list(A_offdiag = "normal(0, 2)")}. Invalid prior
#' strings will throw an error when the function internally checks the syntax
Expand All @@ -52,29 +52,23 @@
#'
#' @examples
#' \dontrun{
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # fit dynamic coevolutionary model
#' coev_fit(
#' data = d,
#' fit <- coev_fit(
#' data = authority$data,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree,
#' id = "language",
#' tree = authority$phylogeny,
#' # additional arguments for cmdstanr::sample()
#' chains = 4,
#' parallel_chains = 4,
#' iter_warmup = 500,
#' seed = 1
#' )
#' )
#'
#' # print model summary
#' summary(fit)
#' }
coev_fit <- function(data, variables, id, tree,
effects_mat = NULL, dist_mat = NULL,
Expand Down
31 changes: 13 additions & 18 deletions R/coev_make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' overdispersion parameters for negative binomial variables (\code{phi}),
#' the degrees of freedom parameters for Student t variables (\code{nu}),
#' the sigma parameters for Gaussian Processes over locations
#' (\code{sigma_dist}), and the rho parameter for Gaussian Processes over
#' (\code{sigma_dist}), and the rho parameters for Gaussian Processes over
#' locations (\code{rho_dist}). These must be entered with valid prior
#' strings, e.g. \code{list(A_offdiag = "normal(0, 2)")}. Invalid prior
#' strings will throw an error when the function internally checks the syntax
Expand All @@ -51,24 +51,19 @@
#' @export
#'
#' @examples
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # make stan code
#' coev_make_stancode(
#' data = d,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree
#' )
#' stan_code <- coev_make_stancode(
#' data = authority$data,
#' variables = list(
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "language",
#' tree = authority$phylogeny
#' )
#'
#' # print Stan code
#' cat(stan_code)
coev_make_stancode <- function(data, variables, id, tree,
effects_mat = NULL, dist_mat = NULL,
prior = NULL, prior_only = FALSE) {
Expand Down
28 changes: 10 additions & 18 deletions R/coev_make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' overdispersion parameters for negative binomial variables (\code{phi}),
#' the degrees of freedom parameters for Student t variables (\code{nu}),
#' the sigma parameters for Gaussian Processes over locations
#' (\code{sigma_dist}), and the rho parameter for Gaussian Processes over
#' (\code{sigma_dist}), and the rho parameters for Gaussian Processes over
#' locations (\code{rho_dist}). These must be entered with valid prior
#' strings, e.g. \code{list(A_offdiag = "normal(0, 2)")}. Invalid prior
#' strings will throw an error when the function internally checks the syntax
Expand All @@ -51,24 +51,16 @@
#' @export
#'
#' @examples
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # make stan code
#' # make stan data
#' coev_make_standata(
#' data = d,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree
#' )
#' data = authority$data,
#' variables = list(
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "language",
#' tree = authority$phylogeny
#' )
coev_make_standata <- function(data, variables, id, tree,
effects_mat = NULL, dist_mat = NULL,
prior = NULL, prior_only = FALSE) {
Expand Down
26 changes: 9 additions & 17 deletions R/coev_plot_delta_theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,23 @@
#'
#' @examples
#' \dontrun{
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # fit dynamic coevolutionary model
#' m <- coev_fit(
#' data = d,
#' fit <- coev_fit(
#' data = authority$data,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree,
#' id = "language",
#' tree = authority$phylogeny,
#' # additional arguments for cmdstanr::sample()
#' chains = 4,
#' parallel_chains = 4,
#' iter_warmup = 500,
#' seed = 1
#' )
#' )
#'
#' # plot delta theta values for all effects
#' coev_plot_delta_theta(m)
#' coev_plot_delta_theta(fit)
#' }
coev_plot_delta_theta <- function(object, variables = NULL, ...) {
# stop if object is not of class coevfit
Expand Down
47 changes: 24 additions & 23 deletions R/coev_plot_flowfield.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
#' Plot flowfield of expected evolutionary change from a fitted \code{coevfit} object
#' Plot flowfield of expected evolutionary change from a fitted \code{coevfit}
#' object
#'
#' @param object An object of class \code{coevfit}
#' @param var1 A character string equal to one of the coevolving variables in the model
#' @param var2 A character string equal to one of the coevolving variables in the model
#' @param nullclines Logical (defaults to FALSE); whether to show coloured nullclines
#' to indicate where each variable is at equilibrium, depending on the state of the other
#' @param var1 A character string equal to one of the coevolving variables in
#' the model
#' @param var2 A character string equal to one of the coevolving variables in
#' the model
#' @param nullclines Logical (defaults to FALSE); whether to show coloured
#' nullclines
#' to indicate where each variable is at equilibrium, depending on the state of
#' the other
#'
#' @return A flowfield plot drawn directly to the device
#' @export
#'
#' @examples
#' \dontrun{
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # fit dynamic coevolutionary model
#' m <- coev_fit(
#' data = d,
#' fit <- coev_fit(
#' data = authority$data,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree,
#' id = "language",
#' tree = authority$phylogeny,
#' # additional arguments for cmdstanr::sample()
#' chains = 4,
#' parallel_chains = 4,
#' iter_warmup = 500,
#' seed = 1
#' )
#' # plot flowfield
#' coev_plot_flowfield(m)
#' )
#'
#' # plot flow field
#' coev_plot_flowfield(
#' object = fit,
#' var1 = "political_authority",
#' var2 = "religious_authority"
#' )
#' }
coev_plot_flowfield <- function(object, var1, var2, nullclines = FALSE) {
# stop if object is not of class coevfit
Expand Down
26 changes: 9 additions & 17 deletions R/coev_plot_predictive_check.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@
#'
#' @examples
#' \dontrun{
#' # simulate data
#' n <- 20
#' tree <- ape::rcoal(n)
#' d <- data.frame(
#' id = tree$tip.label,
#' x = rbinom(n, size = 1, prob = 0.5),
#' y = ordered(sample(1:4, size = n, replace = TRUE))
#' )
#' # fit dynamic coevolutionary model
#' m <- coev_fit(
#' data = d,
#' fit <- coev_fit(
#' data = authority$data,
#' variables = list(
#' x = "bernoulli_logit",
#' y = "ordered_logistic"
#' political_authority = "ordered_logistic",
#' religious_authority = "ordered_logistic"
#' ),
#' id = "id",
#' tree = tree,
#' id = "language",
#' tree = authority$phylogeny,
#' # additional arguments for cmdstanr::sample()
#' chains = 4,
#' parallel_chains = 4,
#' iter_warmup = 500,
#' seed = 1
#' )
#' )
#'
#' # plot predictive checks for all variables
#' coev_plot_predictive_check(m)
#' coev_plot_predictive_check(fit)
#' }
coev_plot_predictive_check <- function(object, variables = NULL,
ndraws = NULL) {
Expand Down
Loading

0 comments on commit 8d9c1a5

Please sign in to comment.