forked from ProjectMOSAIC/mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffmean.R
42 lines (41 loc) · 1.4 KB
/
diffmean.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#' Difference in means and proportions
#'
#' Wrappers around `diff(mean(...))` and `diff(prop(...))` that
#' facilitate better naming of the result
#'
#' @param x,data,... as in [mosaic::mean()] or [mosaic::prop()]
#' @param only.2 a logical indicating whether differences should only be computed
#' between two groups.
#' @examples
#' if (require(mosaicData)) {
#' diffprop( homeless ~ sex , data=HELPrct)
#' do(3) * diffprop( homeless ~ shuffle(sex) , data=HELPrct)
#' diffmean( age ~ substance, data=HELPrct, only.2=FALSE)
#' do(3) * diffmean(age ~ shuffle(substance), data=HELPrct, only.2=FALSE)
#' diffmean( age ~ sex, data=HELPrct)
#' do(3) * diffmean(age ~ shuffle(sex), data=HELPrct)
#' }
#' @export
diffmean <- function( x, ..., data=parent.frame(), only.2=TRUE ) {
m <- mean_(x, ..., data=data)
nms <- names(m)
res <- diff(m)
names(res) <-
if (length(nms) < 3) "diffmean" else paste(tail(nms,-1), head(nms, -1), sep="-")
if (length(nms) > 2 && only.2) {
stop("To compare more than two means, set only.2=FALSE")
}
res
}
#' @rdname diffmean
#' @export
diffprop<- function( x, ..., data=parent.frame(), only.2 = TRUE ) {
p <- prop(x, ..., data=data)
nms <- names(p)
res <- diff(p)
names(res) <- if (length(nms) < 3) "diffprop" else paste(tail(nms,-1), head(nms, -1), sep="-")
if (length(nms) > 2 && only.2) {
stop("To compare more than two proportions, set only.2=FALSE")
}
res
}