Skip to content

Commit

Permalink
Confidence interval update.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewenharrison committed Sep 19, 2023
1 parent 688da62 commit 436b6de
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# finalfit 1.0.61
* bug fixes
* fit2df.lmerMod() set default `confint_sep = " to "`.
* boot.predict() confidence interval limits and methods options added.
* boot_predict() and boot_compare() confidence interval limits and methods options added.

# finalfit 1.0.6
* new plots example vignette https://finalfit.org/articles/all_plots_examples.html
Expand Down
15 changes: 11 additions & 4 deletions R/boot_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#' (see examples).
#'
#' @param bs.out Output from \code{boot::boot},
#' @param confint_level The confidence level to use for the confidence interval.
#' Must be strictly greater than 0 and less than 1. Defaults to 0.95,
#' which corresponds to a 95 percent confidence interval.
#' @param confint_sep String separating lower and upper confidence interval
#' @param condense Logical. FALSE gives numeric values, usually for plotting.
#' TRUE gives table for final output.
Expand All @@ -23,7 +26,8 @@
#' @examples
#' # See boot_predict.

boot_compare = function(bs.out, confint_sep = " to ", comparison = "difference", condense=TRUE,
boot_compare = function(bs.out, confint_level = 0.95, confint_sep = " to ",
comparison = "difference", condense=TRUE,
compare_name = NULL, digits = c(2, 3), ref_symbol = 1){

if(is.null(compare_name)){
Expand All @@ -40,12 +44,15 @@ boot_compare = function(bs.out, confint_sep = " to ", comparison = "difference",
estimate = bs_c / bs_1
null_ref = 1
}


confint_level_low = (1 - confint_level) / 2
confint_level_high = 1 - confint_level_low

if(is.null(dim(estimate))) estimate = matrix(estimate, ncol=1) #allow single vector to pass to apply

estimate_centre = apply(estimate, 2, median)
estimate_conf.low = apply(estimate, 2, quantile, probs = c(0.025))
estimate_conf.high = apply(estimate, 2, quantile, probs = c(0.975))
estimate_conf.low = apply(estimate, 2, quantile, probs = confint_level_low)
estimate_conf.high = apply(estimate, 2, quantile, probs = confint_level_high)
estimate_p1 = apply(estimate, 2, function(x) mean(x < null_ref ))
estimate_p2 = apply(estimate, 2, function(x) mean(x > null_ref ))
estimate_p3 = apply(estimate, 2, function(x) mean(x == null_ref ))
Expand Down
4 changes: 2 additions & 2 deletions R/boot_predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' @param estimate_name Name to be given to prediction variable y-hat.
#' @param confint_level The confidence level to use for the confidence interval.
#' Must be strictly greater than 0 and less than 1. Defaults to 0.95,
#' which corresponds to a 95 percent confidence interval
#' which corresponds to a 95 percent confidence interval.
#' @param conf.method Passed to the type argument of boot::boot.ci().
#' Defaults to "perc". The allowed types are "perc", "basic", "bca", and "norm".
#' Does not support "stud" or "all"
Expand Down Expand Up @@ -146,7 +146,7 @@ boot_predict = function (fit, newdata, type = "response", R = 100,
bs.out = boot::boot(data = .data, statistic = statistic, R = R,
formula = formula, family = family)

bs.tidy = broom::tidy(bs.out, conf.int = TRUE, conf.level = conf.level, conf.method = conf.method)
bs.tidy = broom::tidy(bs.out, conf.int = TRUE, confint.level = confint.level, conf.method = conf.method)
bs.tidy = data.frame(bs.tidy)


Expand Down
2 changes: 1 addition & 1 deletion R/ff_merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ff_merge = function(factorlist, fit2df_df, ref_symbol = "-", estimate_name=NULL,
if(is.null(factorlist$fit_id)) stop("Include fit_id=TRUE in summary_factorlist()")
if(inherits(factorlist, "data.frame.ff") & "(Intercept)" %in% fit2df_df$explanatory){
factorlist = dplyr::bind_rows(factorlist, dplyr::tibble(label = "Intercept", fit_id = "(Intercept)")) %>%
mutate(index = ifelse(is.na(index), 0, index)) %>%
dplyr::mutate(index = ifelse(is.na(index), 0, index)) %>%
replace(is.na(.), "-")
}
explanatory_name = names(fit2df_df)[1]
Expand Down
2 changes: 1 addition & 1 deletion R/finalfit_internal_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ globalVariables(c("L95", "U95", "fit_id", "Total", "dependent",
"w", "Freq", "g", "total_prop", "Prop", "index_total", "vname", "Combined",
"2.5 %", "97.5 %", "p.value", "estimate", "index", "n", "missing_n", "var_type",
"missing_percent", "var1", "var2", "keep", "label", "rowid", "term",
"confint_L", "confint_U", "explanatory", "p", "where"))
"confint_L", "confint_U", "explanatory", "p", "where", "confint.level"))


# Workaround ::: as summary.formula not (yet) exported from Hmisc
Expand Down
5 changes: 5 additions & 0 deletions man/boot_compare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions man/boot_predict.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 436b6de

Please sign in to comment.