@@ -255,25 +255,35 @@ se_median <- function(x) {
255255# ' @param conf confidence interval with default 95%.
256256# ' @param type type for function boot.ci.
257257# ' @param nrepl number of bootstrap replications, defaults to 1000.
258+ # ' @param round logical, applies [roundR] function to results. Output is character.
259+ # ' @param roundDig number of relevant digits for function [roundR].
258260# '
259261# ' @return A tibble with one row and three columns: Median, CIlow, CIhigh.
260262# '
261263# ' @examples
262264# ' # basic usage of median_cl_boot
263265# ' median_cl_boot(x = mtcars$wt)
264266# ' @export
265- median_cl_boot <- function (x , conf = 0.95 , type = " basic" , nrepl = 10 ^ 3 ) {
267+ median_cl_boot <- function (x , conf = 0.95 , type = " basic" , nrepl = 10 ^ 3 , round = FALSE , roundDig = 2 ) {
266268 x <- na.omit(x )
267269 lconf <- (1 - conf ) / 2
268270 uconf <- 1 - lconf
269271 bmedian <- function (x , ind ) median(x [ind ], na.rm = TRUE )
270272 bt <- boot :: boot(x , bmedian , nrepl )
271273 bb <- boot :: boot.ci(bt , type = type )
272- tibble(
273- Median = median(x , na.rm = TRUE ),
274- CIlow = quantile(bt $ t , lconf ),
275- CIhigh = quantile(bt $ t , uconf )
276- )
274+ if (round ) {
275+ return (tibble(
276+ Median = roundR(median(x , na.rm = TRUE ), level = roundDig ),
277+ CIlow = roundR(quantile(bt $ t , lconf ), level = roundDig ),
278+ CIhigh = roundR(quantile(bt $ t , uconf ), level = roundDig )
279+ ))
280+ } else {
281+ return (tibble(
282+ Median = median(x , na.rm = TRUE ),
283+ CIlow = quantile(bt $ t , lconf ),
284+ CIhigh = quantile(bt $ t , uconf )
285+ ))
286+ }
277287}
278288# ' Rename output from \link{median_cl_boot} for use in ggplot.
279289# '
@@ -296,6 +306,47 @@ median_cl_boot_gg <- function(x){
296306 rename(y = " Median" ,ymin = " CIlow" ,ymax = " CIhigh" )
297307 return (out )
298308}
309+
310+ # ' Compute confidence interval of mean by bootstrapping.
311+ # '
312+ # ' \code{mean_cl_boot} computes lower and upper confidence limits for the
313+ # ' estimated mean, based on bootstrapping.
314+ # '
315+ # ' @param x Data for computation.
316+ # ' @param conf confidence interval with default 95%.
317+ # ' @param type type for function boot.ci.
318+ # ' @param nrepl number of bootstrap replications, defaults to 1000.
319+ # ' @param round logical, applies [roundR] function to results. Output is character.
320+ # ' @param roundDig Number of relevant digits for functio [roundR].
321+ # '
322+ # ' @return A tibble with one row and three columns: Mean, CIlow, CIhigh.
323+ # '
324+ # ' @examples
325+ # ' # basic usage of mean_cl_boot
326+ # ' mean_cl_boot(x = mtcars$wt)
327+ # ' @export
328+ mean_cl_boot <- function (x , conf = 0.95 , type = " basic" , nrepl = 10 ^ 3 ,
329+ round = FALSE , roundDig = 2 ) # #
330+ {
331+ x <- na.omit(x )
332+ lconf <- (1 - conf )/ 2
333+ uconf <- 1 - lconf
334+ bmean <- function (x , ind ) mean(x [ind ], na.rm = TRUE )
335+ bt <- boot :: boot(x , bmean , nrepl )
336+ bb <- boot :: boot.ci(bt , type = type )
337+
338+ if (round ){
339+ tibble(Mean = roundR(mean(x , na.rm = TRUE ), level = roundDig ),
340+ CIlow = roundR(quantile(bt $ t , lconf ), level = roundDig ),
341+ CIhigh = roundR(quantile(bt $ t , uconf ), level = roundDig )
342+ )
343+ } else {
344+ tibble(Mean = mean(x , na.rm = TRUE ),
345+ CIlow = quantile(bt $ t , lconf ),
346+ CIhigh = quantile(bt $ t , uconf )
347+ )
348+ }
349+ }
299350# ' Compute absolute and relative frequencies.
300351# '
301352# ' \code{cat_desc_stats} computes absolute and relative frequencies for
0 commit comments