Skip to content

Commit c295bcd

Browse files
authored
Merge pull request #21 from FEidloth/main
mean_cl_boot; compare2numvars
2 parents 7ac2ecd + 7196adc commit c295bcd

File tree

11 files changed

+291
-86
lines changed

11 files changed

+291
-86
lines changed

.gitignore

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
.Rproj.user
2-
.Rhistory
3-
.RData
4-
.Ruserdata
5-
inst/doc
6-
wrappedtools.Rproj
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
inst/doc
6+
wrappedtools.Rproj
7+
.DS_Store

DESCRIPTION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Authors@R: c(
2222
family = "Asser",
2323
role = c("aut"),
2424
email = "billyasser@hotmail.co.uk",
25+
comment = ""),
26+
person(given = "Franziska",
27+
family = "Eidloth",
28+
role = c("aut"),
29+
email = "franziska.eidloth@gmail.com",
2530
comment = ""))
2631
Maintainer: Andreas Busjahn <andreas@busjahn.net>
2732
License: GPL-3

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export(logrange_12357)
2828
export(logrange_15)
2929
export(logrange_5)
3030
export(markSign)
31+
export(mean_cl_boot)
3132
export(meansd)
3233
export(meanse)
3334
export(median_cl_boot)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#wrappedtools 0.9.7
22
- function identical_cols to find and remove duplicated columns
3+
- function compare2numvars can now additionally calculate confidence intervals
4+
- function compare2numvars now has the additional option for a singleline or stacked display
5+
- new function mean_cl_boot which calculates the mean and confidence intervals
6+
- function median_cl_boot now has an additional round option
37

48
#wrappedtools 0.9.6
59
- function ksnormal now uses Lilliefors test by default

R/descriptives.R

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

R/pkgstart.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ NULL
4040
#' @importFrom rlist list.append
4141
NULL
4242

43-
#' @importFrom forcats fct_lump_n fct_drop
43+
#' @importFrom forcats fct_lump_n fct_drop fct_inorder
4444
NULL
4545

4646
#' @importFrom grDevices boxplot.stats

0 commit comments

Comments
 (0)