library(DImodels)
library(dplyr)
## Load data
data(sim0)
## Fit model
mod <- lm(response ~ 0 + (p1 + p2 + p3)^2, data = sim0)
## Prepare data for creating a contour map of predicted response over
## the ternary surface
## Remember to specify prop with the same character values as the names
## of the variables in the model containing the prop.
plot_data <- ternary_data(resolution = 1, model = mod,
prop = c("p1", "p2", "p3"))
## Show plot
ternary_plot(data = plot_data)
## Can also add any additional variables independent of the simplex using
## the `add_var` argument
sim0$treatment <- rep(c("A", "B", "C", "D"), each = 16)
new_mod <- update(mod, ~. + treatment, data = sim0)
plot_data <- ternary_data(prop = c("p1", "p2", "p3"),
add_var = list("treatment" = c("A", "B")),
resolution = 1, model = new_mod)
## Plot to compare between additional variables
# \donttest{
ternary_plot(plot_data)
# }
## 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
contour_data <- ternary_data(prop = c("p1", "p2", "p3"),
model = mod,
prediction = FALSE,
resolution = 1)
head(contour_data)
## Manually add the treatment variable
contour_data$treatment <- "A"
## Make predictions
head(add_prediction(data = contour_data, model = new_mod))
## Manually add the interaction terms
contour_data <- contour_data %>%
mutate(`p1:p2` = p1*p2,
`p2:p3` = p2*p3,
`p1:p3` = p1*p3)
## Add predictions using model coefficients
contour_data <- add_prediction(data = contour_data,
coefficient = mod$coefficient)
head(contour_data)
## Note: Add predictions via coefficients would not give confidence intervals
## to get CIs using coefficients we need to specify the variance-covariance
## matrix using `vcov`
contour_data <- add_prediction(data = contour_data,
coefficient = mod$coefficient,
vcov = vcov(mod),
interval = "confidence")
head(contour_data)
## Show plot
# \donttest{
ternary_plot(contour_data)
# }
## See `?ternary_plot` for options to customise the ternary_plot
Run the code above in your browser using DataLab