在r

时间:2018-04-28 20:14:39

标签: r loops for-loop

嗨我有一个我想循环的代码,但我不知道如何。您看到我从一些先前的值theta_A和theta_B开始,代码运行并吐出新值,这些值位于我命名的代码底部 新的theta_A和新的theta_B。  在第一次迭代之后,我希望新的theta_A替换theta_A和类似的新theta_B来替换旧的theta_B。然后我希望代码继续运行,直到值收敛到1小数点。

我的代码中已经有一个for循环,我不确定如何将两个循环嵌套在一起,如果它甚至可能。

n=10 #patients undergoing treatment
R=c(9,8,5,7,4)  #patients recovered in each hospital
NR=c(1,2,5,3,6) #patients not recovered in each hospital

theta_A=0.6 #prior probability of recovering using treatment A
theta_B=0.5 #prior probability of recovering using treatment B

#We now need to create empty vectors for our probability functions

f_A=matrix(0, 1, 5)
f_B=matrix(0, 1, 5)

#We need to create empty vectors for our normalisation

fsum=matrix(0, 1, 5)
A=matrix(0, 1, 5)
B=matrix(0, 1, 5)

#We need to create empty vectors for expected numeber of recoveries using each treatment

AR=matrix(0, 1, 5)
ANR=matrix(0, 1, 5)
BR=matrix(0, 1, 5)
BNR=matrix(0, 1, 5)

# Now we employ the expectation algorithm, the for loops run through all 5 hospitals

for (i in 1:5) {
  f_A[i]<-(theta_A)^(R[i])*(1-theta_A)^(n-R[i]) #Chances of exact number of recoveries using treatment A
  f_B[i]<-(theta_B)^(R[i])*(1-theta_B)^(n-R[i]) #Chances of exact numebr of recoveries using treatment B
  #Normalisation
  fsum[i]<-f_A[i]+f_B[i] # Sum of the two recoveries
  #print(totalf[i])
  A[i]<-f_A[i]/fsum[i] #Chances of using treatment A
  B[i]<-f_B[i]/fsum[i] #Chances of using treatment B
  AR[i]<-R[i]*A[i]     #Expected recovered patients using treatment A
  ANR[i]<-NR[i]*A[i]   #Expected non-recovered patients using treatment A
  BR[i]<-R[i]*B[i]     #Expected recovered patients using treatment B
  BNR[i]<-NR[i]*B[i]   #Expected non-recovered patients using treatment B


}

# Now employ maximaisation algorithm
total_recA=sum(AR)
total_nonrecA=sum(ANR)
total_recB=sum(BR)
total_nonrecB=sum(BNR)

# Posterior probability of recovery
new_theta_A=total_recA/(total_recA+total_nonrecA)
new_theta_B=total_recB/(total_recB+total_nonrecB)

1 个答案:

答案 0 :(得分:1)

我使用的功能是repeat

问题在于它似乎并不适合我。它转到theta_A为0.797,theta_B为0.52,并重复相同的结果:

n = 10 #patients undergoing treatment
R = c(9, 8, 5, 7, 4)  #patients recovered in each hospital
NR = c(1, 2, 5, 3, 6) #patients not recovered in each hospital

theta_A = 0.6 #prior probability of recovering using treatment A
theta_B = 0.5 #prior probability of recovering using treatment B

thetaFunction <- function(theta_A, theta_B) {
    #We now need to create empty vectors for our probability functions

    f_A = matrix(0, 1, 5)
    f_B = matrix(0, 1, 5)

    #We need to create empty vectors for our normalisation

    fsum = matrix(0, 1, 5)
    A = matrix(0, 1, 5)
    B = matrix(0, 1, 5)

    #We need to create empty vectors for expected numeber of recoveries     using each treatment

    AR = matrix(0, 1, 5)
    ANR = matrix(0, 1, 5)
    BR = matrix(0, 1, 5)
    BNR = matrix(0, 1, 5)

    # Now we employ the expectation algorithm, the for loops run through all     5 hospitals

    for (i in 1:5) {
        f_A[i] <-
            (theta_A) ^ (R[i]) * (1 - theta_A) ^ (n - R[i]) #Chances of     exact number of recoveries using treatment A
        f_B[i] <-
            (theta_B) ^ (R[i]) * (1 - theta_B) ^ (n - R[i]) #Chances of     exact numebr of recoveries using treatment B
        #Normalisation
        fsum[i] <- f_A[i] + f_B[i] # Sum of the two recoveries
        #print(totalf[i])
        A[i] <- f_A[i] / fsum[i] #Chances of using treatment A
        B[i] <- f_B[i] / fsum[i] #Chances of using treatment B
        AR[i] <-
            R[i] * A[i]     #Expected recovered patients using treatment A
    ANR[i] <-
        NR[i] * A[i]   #Expected non-recovered patients using treatment A
        BR[i] <-
            R[i] * B[i]     #Expected recovered patients using treatment B
        BNR[i] <-
            NR[i] * B[i]   #Expected non-recovered patients using treatment B


    }

    # Now employ maximaisation algorithm
    total_recA = sum(AR)
    total_nonrecA = sum(ANR)
    total_recB = sum(BR)
    total_nonrecB = sum(BNR)

    # Posterior probability of recovery
    new_theta_A = total_recA / (total_recA + total_nonrecA)
    new_theta_B = total_recB / (total_recB + total_nonrecB)

    c(new_theta_A, new_theta_B)
}

result <- c(theta_A, theta_B)

repeat{
    priorResult <- result
    result <- thetaFunction(result[1], result[2])
    if (abs(result[1] - priorResult [1]) <=0.01 & abs(result[2] - priorResult [2]) <=0.01){
        break
    }
}