Skip to content

Commit 03cfa99

Browse files
committed
Add error checks and code comments to dset(); update version nr to 0.2.0 since this should be an initial stable release
1 parent eb9b091 commit 03cfa99

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: CIPerm
22
Type: Package
33
Title: Computationally-Efficient CIs for Mean Shift from Permutation Methods
4-
Version: 0.1.0.9005
4+
Version: 0.2.0
55
Date: 2022-03-22
66
Authors@R: c(
77
person("Emily", "Tupaj", role = "aut"),

R/cint.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
cint <- function(dset, conf.level = .95, tail = c("Two", "Left", "Right")){
3838

39-
sig = 1 - conf.level
39+
sig <- 1 - conf.level
4040
stopifnot(sig > 0 & sig < 1)
4141
tail <- match.arg(tail)
4242

@@ -84,8 +84,8 @@ cint <- function(dset, conf.level = .95, tail = c("Two", "Left", "Right")){
8484
siglevel <- sig
8585
index <- roundOrCeiling(siglevel*num) - 1
8686
UB <- w.i[(num-index)]
87-
LT = c(-Inf, UB)
88-
conf.achieved = 1-((index+1)/num)
87+
LT <- c(-Inf, UB)
88+
conf.achieved <- 1-((index+1)/num)
8989
message(paste0("Achieved conf. level: 1-(", index+1, "/", num, ")"))
9090
return(list(conf.int = LT,
9191
conf.level.achieved = conf.achieved))
@@ -94,8 +94,8 @@ cint <- function(dset, conf.level = .95, tail = c("Two", "Left", "Right")){
9494
index <- roundOrCeiling(siglevel*num) - 1
9595
LB <- w.i[1+nk0+index] # starts counting from the (1+nk0)'th element of w.i
9696
# (not the first (original) which will always be 'NaN')
97-
RT = c(LB, Inf)
98-
conf.achieved = 1-((index+1)/num)
97+
RT <- c(LB, Inf)
98+
conf.achieved <- 1-((index+1)/num)
9999
message(paste0("Achieved conf. level: 1-(", index+1, "/", num, ")"))
100100
return(list(conf.int = RT,
101101
conf.level.achieved = conf.achieved))
@@ -107,8 +107,8 @@ cint <- function(dset, conf.level = .95, tail = c("Two", "Left", "Right")){
107107
# (not the first (original) which will always be 'NaN')
108108
Upper <- if(is.na(UB)) Inf else UB
109109
Lower <- if(is.na(LB)) -Inf else LB
110-
CI = c(Lower, Upper)
111-
conf.achieved = 1-(2*(index+1)/num)
110+
CI <- c(Lower, Upper)
111+
conf.achieved <- 1-(2*(index+1)/num)
112112
message(paste0("Achieved conf. level: 1-2*(", index+1, "/", num, ")"))
113113
return(list(conf.int = CI,
114114
conf.level.achieved = conf.achieved))

R/dset.R

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,50 @@
111111

112112
dset <- function(group1, group2, nmc = 10000, returnData = FALSE){
113113
stopifnot(nmc >= 0)
114-
# TODO: add more error checks:
115-
# nmc must be a non-neg integer, not 1, & not too small relative to n,m(?);
116-
# group1 and group2 must be numeric, vectors, and non-empty
114+
stopifnot(nmc != 1)
115+
stopifnot(is.numeric(group1) & is.numeric(group2))
116+
stopifnot(length(group1) >= 1 & length(group2) >= 1)
117+
stopifnot(!any(is.na(c(group1, group2))))
117118

118-
# creates the dataset referenced in pval and cint
119119
combined <- c(group1, group2)
120120

121121
n <- length(group1)
122122
m <- length(group2)
123123
den <- (1/n + 1/m)
124124

125125
N <- n + m
126-
num <- choose(N, n)
127-
if(nmc == 0 | num <= nmc) {
128-
dcombn <- utils::combn(1:N, n)
126+
num <- choose(N, n) # number of possible combinations
127+
128+
# Form a matrix where each column contains indices in new "group1" for that comb or perm
129+
if(nmc == 0 | num <= nmc) { # take all possible combinations
130+
dcombn1 <- utils::combn(1:N, n)
129131
} else { # use Monte Carlo sample of permutations, not all possible combinations
130-
dcombn <- replicate(nmc, sample(N, n))
131-
dcombn[,1] <- 1:n # force the 1st "combination" to be original data order
132+
dcombn1 <- replicate(nmc, sample(N, n))
133+
dcombn1[,1] <- 1:n # force the 1st "combination" to be original data order
132134
num <- nmc
133135
}
134136

135-
dcombn2 <- apply(dcombn, 2, function(x) setdiff(1:N, x))
136-
group1_perm <- matrix(combined[dcombn], nrow = n)
137+
# Form the equivalent matrix for indices in new "group2"
138+
dcombn2 <- apply(dcombn1, 2, function(x) setdiff(1:N, x))
139+
140+
# Form the corresponding matrices of data values, not data indices
141+
group1_perm <- matrix(combined[dcombn1], nrow = n)
137142
group2_perm <- matrix(combined[dcombn2], nrow = m)
138143

139-
k <- colSums(matrix(dcombn %in% ((n+1):N), nrow=n))
144+
# For each comb or perm, compute:
145+
# difference in group means; sum in group1; difference in group medians;
146+
# and sum of *ranks* in group1 (the statistic for the Wilcoxon rank sum test)
140147
diffmean <- colMeans(group1_perm) - colMeans(group2_perm)
141148
sum1 <- colSums(group1_perm)
142149
diffmedian <- matrixStats::colMedians(group1_perm) - matrixStats::colMedians(group2_perm)
143-
144150
r <- rank(combined, ties.method = "first")
145-
wilsum <- colSums(matrix(r[dcombn], nrow = n))
146-
wkd = (diffmean[1] - diffmean) / (k * den)
151+
wilsum <- colSums(matrix(r[dcombn1], nrow = n))
152+
153+
# For each comb or perm, compute:
154+
# k = how many values swapped from group1 to group2?
155+
# wkd = Nguyen (2009) statistic whose quantiles are used for CI endpoints
156+
k <- colSums(matrix(dcombn1 %in% ((n+1):N), nrow=n))
157+
wkd <- (diffmean[1] - diffmean) / (k * den)
147158

148159
dataframe <- data.frame(diffmean = diffmean,
149160
sum1 = sum1,

R/pval.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ pval <- function(dset, tail = c("Two", "Left", "Right"),
4545
num <- nrow(dset)
4646

4747
if (tail == "Left"){
48-
pvalmean = sum(dset$diffmean <= dset$diffmean[1])/num
49-
pvalsum = sum(dset$sum1 <= dset$sum1[1])/num
50-
pvalmedian = sum(dset$diffmedian <= dset$diffmedian[1])/num
51-
pvalwilsum = sum(dset$wilsum <= dset$wilsum[1])/num
48+
pvalmean <- sum(dset$diffmean <= dset$diffmean[1])/num
49+
pvalsum <- sum(dset$sum1 <= dset$sum1[1])/num
50+
pvalmedian <- sum(dset$diffmedian <= dset$diffmedian[1])/num
51+
pvalwilsum <- sum(dset$wilsum <= dset$wilsum[1])/num
5252
} else if (tail == "Right") {
53-
pvalmean = sum(dset$diffmean >= dset$diffmean[1])/num
54-
pvalsum = sum(dset$sum1 >= dset$sum1[1])/num
55-
pvalmedian = sum(dset$diffmedian >= dset$diffmedian[1])/num
56-
pvalwilsum = sum(dset$wilsum >= dset$wilsum[1])/num
53+
pvalmean <- sum(dset$diffmean >= dset$diffmean[1])/num
54+
pvalsum <- sum(dset$sum1 >= dset$sum1[1])/num
55+
pvalmedian <- sum(dset$diffmedian >= dset$diffmedian[1])/num
56+
pvalwilsum <- sum(dset$wilsum >= dset$wilsum[1])/num
5757
} else { # tail == "Two"
5858
pvalmean <- sum(abs(dset$diffmean - mean(dset$diffmean)) >= abs(dset$diffmean[1] - mean(dset$diffmean)))/num
5959
pvalsum <- sum(abs(dset$sum1 - mean(dset$sum1)) >= abs(dset$sum1[1] - mean(dset$sum1)))/num

0 commit comments

Comments
 (0)