R:共识投票的模拟研究

时间:2015-03-07 19:50:48

标签: r for-loop while-loop

在本研究中,我要求nRaters对不同的回应选项进行投票(他们对不同选择做出回应的概率由下面显示的ProbabilityDist定义)。对于每个item.i,投票在两种情况下停止(以先到者为准):

  1. “投票数”超过AgreementThreshold(即同意商品的百分比。
  2. 评分的最大数量已回答问题(由MaxNRaters定义)
  3. 例如,如果Rater1和Rater2响应“A”,则投票停止,但如果Rater1响应“A”但Rater2响应“B”则继续投票(在这种情况下,如果Rater3以“A”回答,则应返回“A”,因为更多达到50%以上。但是,如果Rater3以“C” - “H”响应,则应返回“NoAgreement”,因为未达到AgreementThreshold

    我已经启动了下面的代码,但我仍然坚持如何创建一个循环或具有条件停止的函数。我还希望重复此操作,以便我可以像d.out中的示例输出一样返回输出。

    ResponseChoices= LETTERS[1:8]
    ProbabilityDist= c(0.6, 0.2, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03)
    MaxNRaters=3
    AgreementThreshold = .5 
        #Obtain consensus answer when greater than 50% of raters agree 
        #e.g., if votes for Item.i = "A" "H" "A" return "A"
    
    ### Some kind of conditional loop here ###
    ### Note: the loop should iterate through multiple items using the stop rules described above
    
    VotesForItem.i <- sample(ResponseChoices, nRaters, replace=TRUE, prob=ProbabilityDist)
    
    ##########################################
    
    
    # return(votes)
    
    #Sample of desired output
    items=c(1:5)
    VotedResponses=c("A","A","B","NoAgreement","A")
    d.out=cbind(items,VotedResponses)
    

1 个答案:

答案 0 :(得分:1)

您要求的是什么,尽管MaxNRaters的大值非常慢。

items <- 3    
responses <- c(8, 5, 4) # number of responses for each item
ResponseChoices <- lapply(1:items, function(i) paste0(LETTERS[1:responses[i]], i))
ProbabilityDist <- lapply(1:items, function(i)rmultinom(1, 100, prob = 1:responses[i])/100)

MaxNRaters=3
AgreementThreshold = .5
simResults=NULL
for(j in 1:items){
    i <- 1
    VotedResponses <- c()
    Rater <- c()
    decision <- c()
    while(i<=MaxNRaters){
        Rater <- c(Rater, i)

        resp <- sample(ResponseChoices[[j]], 1 , replace=TRUE, prob=ProbabilityDist[[j]])
        VotedResponses <- c(VotedResponses, resp)
        if(any(table(VotedResponses) > i*AgreementThreshold)&i>1) {
            decision <- c(decision, resp)
            break
        }
        else{
            decision <- c(decision, "No Agreement")
            i <- i+1
        }
    }
    nraters=length(decision)
    decision=decision[nraters]

    simResults.i=cbind(j, nraters,decision)

    simResults=rbind(simResults, simResults.i)
}

或接近您想要的输出的东西

library(dplyr)
data_frame(responses=sample(ResponseChoices, 5, replace=TRUE, prob=ProbabilityDist))%>%
    group_by(responses) %>% mutate(counts=1:n()) %>% ungroup %>% 
    mutate(d= floor(row_number()*AgreementThreshold))%>%  
    mutate(decision=ifelse( cummax(counts) > d, responses, "NoAgreement"))%>% 
    select(-counts, -d)

#   responses    decision
#1         B           B
#2         A NoAgreement
#3         A           A
#4         A           A
#5         A           A

投票在第一次非NoAgreement决定时停止。在上面的示例中,它停在3。