首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于R中条件的“随机”排序用于心理学研究

基于R中条件的“随机”排序用于心理学研究
EN

Stack Overflow用户
提问于 2015-07-25 15:36:35
回答 2查看 206关注 0票数 1

在我的心理学实验中,我有单词刺激的价类。

1=负,2=中性,3=正

我需要用伪随机的条件来分类数千种刺激。

Val_Category在一行中不能有超过2种相同的价态刺激,即在一列中不超过2倍的负性刺激。

例如- 2,2,2=不可接受

2,2,1= ok

我不能对数据进行排序,即决定整个实验将是1,3,2,3,3,3,2,2,1,因为我不被允许有一个模式。

到目前为止,我尝试过各种各样的包,如湿巾、样品、订单、排序等等,都没有解决这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-25 17:06:35

我认为有上千种方法可以做到这一点,其中没有一种是非常漂亮的。我写了一个小函数来处理订单。有点烦人,但似乎对我所做的努力有效。

为了解释我做了什么,这个函数的工作原理如下:

  1. 取价向量和样品。
  2. 如果发现的序列大于所需的长度,那么(对于每个这样的序列),在“其他地方”取该序列的最后一个值。
  3. 检查问题是否解决了。如果是,则返回重新排序的向量。如果没有,那就回到2。
代码语言:javascript
复制
# some vector of valences
val <- rep(1:3,each=50)

pseudoRandomize <- function(x, n){

  # take an initial sample
  out <- sample(val)
  # check if the sample is "bad" (containing sequences longer than n)
  bad.seq <- any(rle(out)$lengths > n)
  # length of the whole sample
  l0 <- length(out)

  while(bad.seq){
    # get lengths of all subsequences
    l1 <- rle(out)$lengths
    # find the bad ones
    ind <- l1 > n
    # take the last value of each bad sequence, and...
    for(i in cumsum(l1)[ind]){
      # take it out of the original sample
      tmp <- out[-i]
      # pick new position at random
      pos <- sample(2:(l0-2),1)
      # put the value back into the sample at the new position
      out <- c(tmp[1:(pos-1)],out[i],tmp[pos:(l0-1)])
    }
    # check if bad sequences (still) exist
    # if TRUE, then 'while' continues; if FALSE, then it doesn't
    bad.seq <- any(rle(out)$lengths > n)
  }
  # return the reordered sequence
  out

}

示例:

该函数可用于带有或不带名称的向量上。如果该向量被命名,那么这些名称将仍然存在于伪随机向量上。

代码语言:javascript
复制
# simple unnamed vector
val <- rep(1:3,each=5)
pseudoRandomize(val, 2)

# gives:
# [1] 1 3 2 1 2 3 3 2 1 2 1 3 3 1 2

# when names assigned to the vector
names(val) <- 1:length(val)
pseudoRandomize(val, 2)

# gives (first row shows the names):
#  1 13  9  7  3 11 15  8 10  5 12 14  6  4  2 
#  1  3  2  2  1  3  3  2  2  1  3  3  2  1  1 

此属性可用于对整个数据帧进行随机化。为此,将“价态”向量从数据帧中取出,并将名称按行索引(1:nrow(dat))或行名(rownames(dat))分配给它。

代码语言:javascript
复制
# reorder a data.frame using a named vector
dat <- data.frame(val=rep(1:3,each=5), stim=rep(letters[1:5],3))
val <- dat$val
names(val) <- 1:nrow(dat)

new.val <- pseudoRandomize(val, 2)
new.dat <- dat[as.integer(names(new.val)),]

# gives:
#    val stim
# 5    1    e
# 2    1    b
# 9    2    d
# 6    2    a
# 3    1    c
# 15   3    e
# ...
票数 0
EN

Stack Overflow用户

发布于 2015-07-25 17:06:41

我相信这个循环会适当地设置价类别。我叫价类治疗。

代码语言:javascript
复制
#Generate example data
s1 = data.frame(id=c(1:10),treat=NA)

#Setting the first two rows
s1[1,"treat"] <- sample(1:3,1)
s1[2,"treat"] <- sample(1:3,1)

#Looping through the remainder of the rows
for (i in 3:length(s1$id))
{
   s1[i,"treat"] <- sample(1:3,1)

   #Check if the treat value is equal to the previous two values.
   if (s1[i,"treat"]==s1[i-1,"treat"] & s1[i-1,"treat"]==s1[i-2,"treat"])

   #If so draw one of the values not equal to that value
   {
      a = 1:3
      remove <- s1[i,"treat"]
      a=a[!a==remove]
      s1[i,"treat"]  <- sample(a,1)
   }
}

这个解决方案并不特别优雅。可能有一种更快的方法可以通过对几个列或什么进行排序来完成这一任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31628160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档