Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何使用函数”;“卡拉洛克”;(在R软件包Sekalloc中)将人们随机分为三种情况?_R - Fatal编程技术网

“如何使用函数”;“卡拉洛克”;(在R软件包Sekalloc中)将人们随机分为三种情况?

“如何使用函数”;“卡拉洛克”;(在R软件包Sekalloc中)将人们随机分为三种情况?,r,R,我想使用sequaloc包中的函数“caralloc”将人们随机分成三个条件,而不是默认的两个条件 function (xmat, carwt, p, tol) { if (!is.matrix(xmat)) xmat = as.matrix(xmat) n = nrow(xmat) result = rbinom(n, 1, 0.5) if (n > 1) { for (j in 2:n) { matchx = apply(xmat[1:(j - 1), , d

我想使用sequaloc包中的函数“caralloc”将人们随机分成三个条件,而不是默认的两个条件

function (xmat, carwt, p, tol) 
{
  if (!is.matrix(xmat)) 
  xmat = as.matrix(xmat)
  n = nrow(xmat)
  result = rbinom(n, 1, 0.5)
 if (n > 1) {
for (j in 2:n) {
  matchx = apply(xmat[1:(j - 1), , drop = FALSE], 
    1, function(x, xrow) {
      as.numeric(x == xrow)
    }, xmat[j, ])
  sumsofar = matchx %*% (2 * result[1:(j - 1)] - 1)
  imbalance1 = crossprod(abs(sumsofar + 1), carwt)
  imbalance0 = crossprod(abs(sumsofar - 1), carwt)
  if (imbalance1 < imbalance0 & imbalance0 >= tol) 
    result[j] = rbinom(1, 1, p)
  if (imbalance0 < imbalance1 & imbalance1 >= tol) 
    result[j] = rbinom(1, 1, 1 - p)
 }
}
result
}
函数(xmat、carwt、p、tol)
{
如果(!is.矩阵(xmat))
xmat=as.matrix(xmat)
n=nrow(xmat)
结果=rbinom(n,1,0.5)
如果(n>1){
对于(2:n中的j){
matchx=apply(xmat[1:(j-1),drop=FALSE],
1、功能(x、xrow){
as.numeric(x==xrow)
},xmat[j,]
sumsofar=matchx%*%(2*结果[1:(j-1)]-1)
不平衡1=交叉生产(abs(sumsofar+1),carwt)
不平衡0=交叉生产(abs(sumsofar-1),carwt)
如果(不平衡1<不平衡0&不平衡0>=tol)
结果[j]=rbinom(1,1,p)
如果(不平衡0<不平衡1&不平衡1>=tol)
结果[j]=rbinom(1,1,1-p)
}
}
结果
}
使用包示例:

sampsize <- 200
percent <- c(0.5,0.8,0.2,0.4)
carwt <- c(.4,.3,.2,.1)

set.seed(5798)

xmat <- matrix(rbinom(sampsize*length(percent),1,rep(percent,sampsize)),
          nrow=sampsize,ncol=length(percent),byrow=TRUE)
colnames(xmat) = c("C1","C2","C3","C4")
strat_factor = xmat[,1]*4 + xmat[,2]*2 + xmat[,4] + 1

caralloc(xmat,carwt,1,3)

sampsize这听起来很难让你的团队随机化

您不能只使用
sample()
函数吗

df <- data.frame(
  # some 1000 test patients:  
  patient = paste0(letters, round(runif(1000, 100, 999))), 
  # assigning a group: 0, 1 or 2:
  group = sample(c(0:2), 1000, replace = TRUE))

head(df)
#>   patient group
#> 1    a729     1
#> 2    b633     2
#> 3    c831     2
#> 4    d111     2
#> 5    e406     1
#> 6    f491     0

# evenly distributed?
hist(df$group)

不幸的是,没有。该函数用于协变量自适应随机化,这意味着我们根据一些协变量对人进行随机化。我可以尝试将
result=rbinom(n,1,0.5)
更改为
result=rbinom(n,2,1/3)
?如果是,如何相应调整以下代码?
df <- data.frame(
  # some 1000 test patients:  
  patient = paste0(letters, round(runif(1000, 100, 999))), 
  # assigning a group: 0, 1 or 2:
  group = sample(c(0:2), 1000, replace = TRUE, prob = c(0.1, 0.4, 0.5)))

hist(df$group)