美文网首页随机化
R语言基于最小化差异实现动态随机化

R语言基于最小化差异实现动态随机化

作者: 灵活胖子的进步之路 | 来源:发表于2020-10-11 23:38 被阅读0次
library(Minirand)


ntrt <- 2#分组数
nsample <- 60#样本数
trtseq <- c(1, 2)#分组序列
ratio <- c(1, 1)#分组比例
c1 <- sample(seq(1, 0), nsample, replace = TRUE, prob = c(0.4, 0.6)) 
c2 <- sample(seq(1, 0), nsample, replace = TRUE, prob = c(0.3, 0.7))
c3 <- sample(c(2, 1, 0), nsample, replace = TRUE, prob = c(0.33, 0.2, 0.5)) 
c4 <- sample(seq(1, 0), nsample, replace = TRUE, prob = c(0.33, 0.67)) 
covmat <- cbind(c1, c2, c3, c4) # generate the matrix of covariate factors for the subjects
# label of the covariates 
colnames(covmat) = c("Gender", "Age", "Hypertension", "Use of Antibiotics") 
covwt <- c(1/4, 1/4, 1/4, 1/4) #equal weights,四个协变量权重一样

res <- rep(10, nsample) # result is the treatment needed from minimization method


#gernerate treatment assignment for the 1st subject
res[1] = sample(trtseq, 1, replace = TRUE, prob = ratio/sum(ratio)) 

for (j in 2:nsample)
{
  # get treatment assignment sequentiall for all subjects
  res[j] <- Minirand(covmat=covmat, j, covwt=covwt, ratio=ratio, 
                     ntrt=ntrt, trtseq=trtseq, method="SD", result=res, p = 0.9)
}
trt1 <- res[3]
#Display the number of randomized subjects at covariate factors
balance1 <- randbalance(trt1, covmat, ntrt, trtseq) 
balance1
totimbal(trt = trt1, covmat = covmat, covwt = covwt, 
         ratio = ratio, ntrt = ntrt, trtseq = trtseq, method = "SD")

相关文章

网友评论

    本文标题:R语言基于最小化差异实现动态随机化

    本文链接:https://www.haomeiwen.com/subject/imiypktx.html