conditional_ternary_data {DImodelsVis} | R Documentation |
Conditional ternary diagrams
Description
The helper function for preparing the underlying data for creating conditional
ternary diagrams, where we fix variables to have a constant value
such that
and
and vary the proportion of the remaining three variables
between
and
to visualise the change in the predicted response as a
contour map within a ternary diagram. The output of this function can be passed to the
conditional_ternary_plot
function to plot the results. Viewing multiple
2-d slices across multiple variables should allow to create an approximation of
how the response varies across the n-dimensional simplex.
Usage
conditional_ternary_data(
prop,
FG = NULL,
values = NULL,
tern_vars = NULL,
conditional = NULL,
add_var = list(),
resolution = 3,
prediction = TRUE,
...
)
Arguments
prop |
A character vector indicating the model coefficients corresponding to variable proportions. These variables should be compositional in nature (i.e., proportions should sum to 1). |
FG |
A character vector specifying the grouping of the variables
specified in 'prop'. Specifying this parameter would call the
grouped_ternary_data function internally. See |
values |
A numeric vector specifying the proportional split of the variables within a group. The default is to split the group proportion equally between each variable in the group. |
tern_vars |
A character vector giving the names of the three variables to be shown in the ternary diagram. |
conditional |
A data-frame describing the names of the compositional variables
and their respective values at which to slice the
simplex space. The format should be, for example, as follows: |
add_var |
A list or data-frame specifying values for additional variables in the model other than the proportions (i.e. not part of the simplex design). This could be useful for comparing the predictions across different values for a non-compositional variable. If specified as a list, it will be expanded to show a plot for each unique combination of values specified, while if specified as a data-frame, one plot would be generated for each row in the data. |
resolution |
A number between 1 and 10 describing the resolution of the resultant graph. A high value would result in a higher definition figure but at the cost of being computationally expensive. |
prediction |
A logical value indicating whether to pass the final data
to the 'add_prediction' function and append the
predictions to the data. Default value is |
... |
Arguments passed on to
|
Value
A data-frame containing compositional columns with names specified in 'prop' parameter along with any additional columns specified in 'add_var' parameter. The first five columns of the data contain the three variables (specified in 'tern_vars') shown in the ternary along with their 2-d projection and should not be modified. The following additional columns could also be present in the data.
- .x
The x-projection of the points within the ternary.
- .y
The y-projection of the points within the ternary.
- .add_str_ID
An identifier column for grouping the cartesian product of all additional columns specified in 'add_var' parameter (if 'add_var' is specified).
- .Sp
An identifier column specifying the variable(s) along which the high dimensional simplex is sliced.
- .Value
The value(s) (between 0 and 1) along the direction of variable(s) in '.Sp' at which the high dimensional simplex is sliced.
- .Facet
An identifier column formed by combining '.Sp' and '.value' to group observations within a specific slice of the high dimensional simplex.
- .Pred
The predicted response for each observation (if 'prediction' is
TRUE
).- .Lower
The lower limit of the prediction/confidence interval for each observation.
- .Upper
The upper limit of the prediction/confidence interval for each observation.
Examples
library(DImodels)
## Load data
data(sim4)
## Fit model
mod <- glm(response ~ 0 + (p1 + p2 + p3 + p4 + p5 + p6)^2, data = sim4)
## Create data
## Any species not specified in `tern_vars` or conditional will be assumed
## to be 0, for example p5 and p6 here.
head(conditional_ternary_data(prop = c("p1", "p2", "p3", "p4", "p5", "p6"),
tern_vars = c("p1", "p2", "p3"),
conditional = data.frame("p4" = c(0, 0.2, 0.5)),
model = mod,
resolution = 1))
## Can also condition on multiple species
cond <- data.frame(p4 = c(0, 0.2), p5 = c(0.5, 0.1), p6 = c(0, 0.3))
cond
head(conditional_ternary_data(prop = c("p1", "p2", "p3", "p4", "p5", "p6"),
tern_vars = c("p1", "p2", "p3"),
conditional = cond,
model = mod,
resolution = 1))
## Fit model
mod <- glm(response ~ 0 + (p1 + p2 + p3 + p4 + p5 + p6)^2 + treatment,
data = sim4)
## Can also add any additional variables independent of the simplex
## Notice the additional `.add_str_ID` column
head(conditional_ternary_data(prop = c("p1", "p2", "p3", "p4", "p5", "p6"),
tern_vars = c("p1", "p2", "p3"),
conditional = data.frame("p4" = c(0, 0.2, 0.5)),
add_var = list("treatment" = c(50, 150)),
model = mod,
resolution = 1))
## It could be desirable to take the output of this function and add
## additional variables to the data before making predictions
## Use `prediction = FALSE` to get data without any predictions
cond_data <- conditional_ternary_data(prop = c("p1", "p2", "p3", "p4", "p5", "p6"),
tern_vars = c("p1", "p2", "p3"),
conditional = data.frame("p4" = c(0, 0.2, 0.5)),
prediction = FALSE,
resolution = 1)
## The data can then be modified and the `add_prediction` function can be
## called manually using either the model object or model coefficients
cond_data$treatment <- 50
head(add_prediction(data = cond_data, model = mod))