Skip to content

Commit

Permalink
Merge pull request #49 from mattheaphy/dev-cli
Browse files Browse the repository at this point in the history
Development version - control variables and CLI updates
  • Loading branch information
mattheaphy authored Dec 31, 2024
2 parents d518aa9 + b50f449 commit bd84e81
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 114 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: actxps
Title: Create Actuarial Experience Studies: Prepare Data, Summarize Results, and Create Reports
Version: 1.5.0
Version: 1.5.0.9000
Authors@R:
person("Matt", "Heaphy", email = "mattrmattrs@gmail.com", role = c("aut", "cre"))
Maintainer: Matt Heaphy <mattrmattrs@gmail.com>
Expand All @@ -20,7 +20,7 @@ URL: https://github.com/mattheaphy/actxps/,
BugReports: https://github.com/mattheaphy/actxps/issues
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.2
Suggests:
knitr,
RColorBrewer,
Expand All @@ -47,6 +47,7 @@ Imports:
readr,
tidyr,
vctrs,
clock
clock,
cli
LazyData: true
VignetteBuilder: knitr
19 changes: 19 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# actxps (development version)

- New feature - control variables

- `exp_stats()` has two new arguments: `control_vars` and
`control_distinct_max`.
- Control variables are used to estimate the impact of any grouping variables
on observed experience after accounting for the impact of control
variables. The idea here is that experience is first summarized across the
control variables to derive a new expected values basis. This expected
values basis is then used to calculate actual-to-expected ratios.
- The `control_distinct_max` argument places an upper limit on the maximum
number of unique values allowed in any control variable.
- `exp_shiny()` now includes a control variable selection widget.

- Added the "cli" package to replace "rlang" for certain errors, warnings, and
messages.
- Various small typo fixes.

# actxps 1.5.0

- `expose_split()` bug fixes:
Expand Down
3 changes: 2 additions & 1 deletion R/exp_df_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ as_exp_df <- function(x, expected = NULL, wt = NULL,
wt = wt,
credibility = credibility,
conf_level = conf_level, cred_r = cred_r,
conf_int = conf_int)
conf_int = conf_int,
control_vars = NULL)

}

Expand Down
70 changes: 34 additions & 36 deletions R/exp_shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
#' #### Termination studies
#'
#' The expected values checkboxes are used to activate and deactivate expected
#' values passed to the `expected` argument. This impacts the table output
#' directly and the available "y" variables for the plot. If there are no
#' expected values available, this widget will not appear. The "Weight by"
#' widget is used to specify which column, if any, contains weights for
#' summarizing experience.
#' values passed to the `expected` argument. These checkboxes also include a
#' a "control" item for expected values derived using control variables.
#' These boxes impact the table output directly and the available "y" variables
#' for the plot. The "Weight by" widget is used to specify which column, if any,
#' contains weights for summarizing experience. The "Control variables" widget
#' is used to specify which columns, if any, are used as control variables (
#' see [exp_stats()] for more information).
#'
#' #### Transaction studies
#'
Expand Down Expand Up @@ -265,8 +267,8 @@ exp_shiny <- function(dat,
choices <- info$scope[[1]]

if (is.null(dat[[x]])) {
rlang::abort(
glue::glue("Error creating an input widget for {x}. {x} does not exist in the input data.")
cli::cli_abort(
"Error creating an input widget for {x}. {x} does not exist in the input data."
)
}

Expand Down Expand Up @@ -309,8 +311,8 @@ exp_shiny <- function(dat,

} else {

rlang::abort(
glue::glue("Error creating an input widget for {x}. {x} is of class {class(dat[[x]]) |> paste(collapse = ', ')}, which is not supported.")
cli::cli_abort(
"Error creating an input widget for {x}. {x} is of class {class(dat[[x]])}, which is not supported."
)

}
Expand Down Expand Up @@ -355,18 +357,10 @@ exp_shiny <- function(dat,
}

# expected values set up
if (length(expected) > 0) {

has_expected <- TRUE

expected_widget <- checkboxGroupPred("ex_checks", "Expected values:", 6,
choices = expected,
selected = expected)

} else {
has_expected <- FALSE
expected_widget <- NULL
}
expected <- c(expected, "control")
expected_widget <- checkboxGroupPred(
"ex_checks", "Expected values:", 4,
choices = expected, selected = expected[expected != "control"])

# transactions set up
if (has_trx) {
Expand Down Expand Up @@ -506,9 +500,12 @@ exp_shiny <- function(dat,
value = "exp",
shiny::fluidRow(
expected_widget,
selectPred("weightVar", "Weight by:", 6,
selectPred("weightVar", "Weight by:", 4,
choices = c("None",
filter(preds, is_number)$predictors))
filter(preds, is_number)$predictors)),
selectPred("controlVars", "Control variables:", 4,
multiple = TRUE,
choices = preds_small)
)),

trx_tab
Expand All @@ -528,16 +525,16 @@ exp_shiny <- function(dat,
shiny::markdown(
'<div style="text-align: left">
- `y`-axis variable selection
- `Second y-axis` toggle and variable
- `Geometry` for plotting
- `Add smoothing`: add smooth loess curves
- `Confidence intervals`: If available, draw confidence interval
error bars
- `Free y-scales`: enable separate `y` scales in each subplot
- `Log y-axis`: plot y-axes on a log-10 scale
- The grouping variables selected above will determine the
variable on the `x`-axis, the color variable, and faceting
- **y-axis** variable selection
- **Second y-axis** toggle and variable
- **Geometry** for plotting
- **Add smoothing**: add smooth loess curves
- **Confidence intervals**: If available, draw confidence
interval error bars
- **Free y-scales**: enable separate y scales in each subplot
- **Log y-axis**: plot y-axes on a log-10 scale
- The **grouping variables** selected above will determine the
variable on the x-axis, the color variable, and faceting
variables used to create subplots.
</div>'),
Expand Down Expand Up @@ -806,16 +803,17 @@ exp_shiny <- function(dat,
}
}

ex <- if (has_expected) {
input$ex_checks
ex <- input$ex_checks[input$ex_checks != "control"]
ctrl <- if ("control" %in% input$ex_checks) {
input$controlVars %||% ".none"
}

if (input$study_type == "exp") {
rdat() |>
group_by(dplyr::across(dplyr::all_of(.groups))) |>
exp_stats(wt = wt, credibility = credibility, expected = ex,
conf_level = conf_level, cred_r = cred_r,
conf_int = TRUE)
conf_int = TRUE, control_vars = ctrl)
} else {
rdat() |>
group_by(dplyr::across(dplyr::all_of(.groups))) |>
Expand Down
Loading

0 comments on commit bd84e81

Please sign in to comment.