# NOT RUN {
# a simple use
## Access by real data
## create a dataframe
df <- data.frame("gender" = sample(c("female", "male"), 1000, TRUE, c(1 / 3, 2 / 3)),
"age" = sample(c("0-30", "30-50", ">50"), 1000, TRUE),
"jobs" = sample(c("stu.", "teac.", "others"), 1000, TRUE),
stringsAsFactors = TRUE)
Res <- evalRand(data = df, method = "HuHuCAR", N = 500,
omega = c(1, 2, rep(1, ncol(df))), p = 0.85)
## view the output
Res
# }
# NOT RUN {
## view all patients' assignments
Res$Assig
# }
# NOT RUN {
## Assess by simulated data
cov_num <- 3
level_num <- c(2, 3, 5)
pr <- c(0.35, 0.65, 0.25, 0.35, 0.4, 0.25, 0.15, 0.2, 0.15, 0.25)
n <- 1000
N <- 50
omega = c(1, 2, 1, 1, 2)
# assess Hu and Hu's procedure with the same group of patients
Res.sim <- evalRand.sim(n = n, N = N, Replace = FALSE, cov_num = cov_num,
level_num = level_num, pr = pr, method = "HuHuCAR",
omega, p = 0.85)
# }
# NOT RUN {
## Compare four procedures
cov_num <- 3
level_num <- c(2, 10, 2)
pr <- c(rep(0.5, times = 2), rep(0.1, times = 10), rep(0.5, times = 2))
n <- 100
N <- 200 # <<adjust according to CPU
bsize <- 4
## set weights for HuHuCAR
omega <- c(1, 2, rep(1, cov_num));
## set weights for PocSimMIN
weight = rep(1, cov_num);
## set biased probability
p = 0.80
# assess Hu and Hu's procedure
RH <- evalRand.sim(n = n, N = N, Replace = FALSE, cov_num = cov_num,
level_num = level_num, pr = pr, method = "HuHuCAR",
omega = omega, p = p)
# assess Pocock and Simon's method
RPS <- evalRand.sim(n = n, N = N, Replace = FALSE, cov_num = cov_num,
level_num = level_num, pr = pr, method = "PocSimMIN",
weight, p = p)
# assess Shao's procedure
RS <- evalRand.sim(n = n, N = N, Replace = FALSE, cov_num = cov_num,
level_num = level_num, pr = pr, method = "StrBCD",
p = p)
# assess stratified randomization
RSR <- evalRand.sim(n = n, N = N, Replace = FALSE, cov_num = cov_num,
level_num = level_num, pr = pr, method = "StrPBR",
bsize)
# create containers
C_M = C_O = C_WS = matrix(NA, nrow = 4, ncol = 4)
colnames(C_M) = colnames(C_O) = colnames(C_WS) =
c("max", "95%quan", "med", "mean")
rownames(C_M) = rownames(C_O) = rownames(C_WS) =
c("HH", "PocSim", "Shao", "StraRand")
# assess the overall imbalance
C_O[1, ] = RH$Imb[1, ]
C_O[2, ] = RPS$Imb[1, ]
C_O[3, ] = RS$Imb[1, ]
C_O[4, ] = RSR$Imb[1, ]
# view the result
C_O
# assess the marginal imbalances
C_M[1, ] = apply(RH$Imb[(1 + RH$strt_num) : (1 + RH$strt_num + sum(level_num)), ], 2, mean)
C_M[2, ] = apply(RPS$Imb[(1 + RPS$strt_num) : (1 + RPS$strt_num + sum(level_num)), ], 2, mean)
C_M[3, ] = apply(RS$Imb[(1 + RS$strt_num) : (1 + RS$strt_num + sum(level_num)), ], 2, mean)
C_M[4, ] = apply(RSR$Imb[(1 + RSR$strt_num) : (1 + RSR$strt_num + sum(level_num)), ], 2, mean)
# view the result
C_M
# assess the within-stratum imbalances
C_WS[1, ] = apply(RH$Imb[2 : (1 + RH$strt_num), ], 2, mean)
C_WS[2, ] = apply(RPS$Imb[2 : (1 + RPS$strt_num), ], 2, mean)
C_WS[3, ] = apply(RS$Imb[2 : (1 + RS$strt_num), ], 2, mean)
C_WS[4, ] = apply(RSR$Imb[2 : (1 + RSR$strt_num), ], 2, mean)
# view the result
C_WS
# Compare the four procedures through plots
meth = rep(c("Hu", "PS", "Shao", "STR"), times = 3)
shape <- rep(1 : 4, times = 3)
crt <- rep(1 : 3, each = 4)
crt_c <- rep(c("O", "M", "WS"), each = 4)
mean <- c(C_O[, 4], C_M[, 4], C_WS[, 4])
df_1 <- data.frame(meth, shape, crt, crt_c, mean,
stringsAsFactors = TRUE)
require(ggplot2)
p1 <- ggplot(df_1, aes(x = meth, y = mean, color = crt_c, group = crt,
linetype = crt_c, shape = crt_c)) +
geom_line(size = 1) +
geom_point(size = 2) +
xlab("method") +
ylab("absolute mean") +
theme(plot.title = element_text(hjust = 0.5))
p1
# }
Run the code above in your browser using DataLab