A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://rishvish.github.io/DImodelsVis/reference/prediction_contributions.html below:

Model term contributions to predicted response — prediction_contributions • DImodelsVis

#' ## Load DImodels package to fit the model
library(DImodels)

## Load data
data(sim2)

## Fit DI model
model1 <- DI(prop = 3:6, DImodel = 'FULL', data = sim2, y = 'response')
#> Fitted model: Separate pairwise interactions 'FULL' DImodel

## Create visualisation
## If no communities are specified 2 communities at
## each level of richness from the original data are used
prediction_contributions(model1)
#>  Finished data preparation.
#>  Created plot.


## Can also manually specify communities of interest
my_comms <- data.frame(p1 = c(1, 0, 0,   0.5, 1/3, 0.25),
                       p2 = c(0, 0, 0.5, 0,   1/3, 0.25),
                       p3 = c(0, 1, 0.5, 0,   1/3, 0.25),
                       p4 = c(0, 0, 0,   0.5, 0,   0.25))

prediction_contributions(model1, data = my_comms)
#>  Finished data preparation.
#>  Created plot.


## Group contributions to show as a single component on the plot
prediction_contributions(model1, data = my_comms,
                         groups = list("Interactions" = c("`p1:p2`", "`p1:p3`",
                                                          "`p1:p4`", "`p2:p3`",
                                                          "`p2:p4`", "`p3:p4`")))
#>  Finished data preparation.
#>  Created plot.


## Add a prediction interval using `se = TRUE` and show bars horizontally
prediction_contributions(model1, data = my_comms, se = TRUE,
                         bar_orientation = "horizontal",
                         groups = list("Interactions" = c("`p1:p2`", "`p1:p3`",
                                                          "`p1:p4`", "`p2:p3`",
                                                          "`p2:p4`", "`p3:p4`")))
#>  Finished data preparation.
#>  Created plot.


## Facet the plot on any variable
my_comms$richness <- c(1, 1, 2, 2, 3, 4)
## Use `facet_var`
prediction_contributions(model1, data = my_comms, facet_var = "richness",
                         bar_orientation = "horizontal",
                         groups = list("Interactions" = c("`p1:p2`", "`p1:p3`",
                                                          "`p1:p4`", "`p2:p3`",
                                                          "`p2:p4`", "`p3:p4`")))
#>  Finished data preparation.
#>  Created plot.


## Can also add additional variables independent of the simplex design
## to get a separate plot for unique combination of the variables
prediction_contributions(model1, data = my_comms,
                         add_var = list("block" = factor(c(1, 2),
                                                         levels = c(1, 2, 3, 4))))
#>  Finished data preparation.
#>  Created all plots.


## Manually specify colours and bar labels
## Model has 10 terms but we grouped 6 of them into 1 term,
## so we need to specify 5 colours (4 ungrouped terms + 1 grouped term)
## Bar labels can be specified using `bar_labs`
## Also, using nrow to arrange plots in rows
prediction_contributions(model1, data = my_comms,
                         colours = c("steelblue1", "steelblue4",
                                     "orange", "orange4",
                                     "grey"),
                         bar_labs = c("p1 Mono", "p3 Mono", "1/2 p2 p3",
                                      "1/2 p1 p4", "1/3 p1 p2 p3", "Centroid"),
                         add_var = list("block" = factor(c(1, 2),
                                                         levels = c(1, 2, 3, 4))),
                         nrow = 2,
                         groups = list("Interactions" = c("`p1:p2`", "`p1:p3`",
                                                          "`p1:p4`", "`p2:p3`",
                                                          "`p2:p4`", "`p3:p4`")))
#>  Finished data preparation.
#>  Created all plots.


## Specify `plot = FALSE` to not create the plot but return the prepared data
head(prediction_contributions(model1, data = my_comms, plot = FALSE,
                              facet_var = "richness",
                              bar_orientation = "horizontal"))
#>  Finished data preparation.
#> # A tibble: 6 × 18
#>   .Community    p1    p2    p3    p4 richness .Richness `p1:p2` `p1:p3` `p1:p4`
#>   <fct>      <dbl> <dbl> <dbl> <dbl>    <dbl>     <dbl>   <dbl>   <dbl>   <dbl>
#> 1 1              1     0     0     0        1         1       0       0       0
#> 2 1              1     0     0     0        1         1       0       0       0
#> 3 1              1     0     0     0        1         1       0       0       0
#> 4 1              1     0     0     0        1         1       0       0       0
#> 5 1              1     0     0     0        1         1       0       0       0
#> 6 1              1     0     0     0        1         1       0       0       0
#> # ℹ 8 more variables: `p2:p3` <dbl>, `p2:p4` <dbl>, `p3:p4` <dbl>, .Pred <dbl>,
#> #   .Lower <dbl>, .Upper <dbl>, .Contributions <chr>, .Value <dbl>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4