vendredi 26 mai 2017

for loop + 2 else if R

I'm having problems puting two if...else inside my for loop. I have a condition for my if...else. But in any of both cases, I want to do the second part of the code. But for the moment I think I'm only doing the second part if I'm on my first ELSE :

for (i in 2:M){

  #####  1  #####

  theta.prop_1 = theta[i-1,1] + rnorm(1,0,sd.prop)
  theta.prop = c(theta.prop_1, theta[i-1,2:4])
  prob = min(1, exp(lpost(theta.prop) - lpost(theta[i-1,])))
  accept1 = (runif(1) <= prob)
  if (accept1){
    n.accept1 = n.accept1 + 1
    theta[i,] = theta.prop
  }
  else theta[i,] = theta[i-1,]

  #####  2  #####

  theta.prop_2 = theta[i-1,2]+
    rnorm(1,0,sd.prop)
  theta.prop = c(theta[i,1], theta.prop_2, theta[i-1,3:4])
  prob = min(1, exp(lpost(theta.prop) - lpost(theta[i-1,])))
  accept2 = (runif(1) <= prob)
  if (accept2){
    n.accept2 = n.accept2 + 1
    theta[i,] = theta.prop
  }
  else theta[i,] = theta[i-1,]
}

(Not sure if my english was clear enough)

Aucun commentaire:

Enregistrer un commentaire