Skip to content
Prev 302006 / 398503 Next

Alternating between "for loops"

Hello,

Em 01-08-2012 20:02, Mercier Eloi escreveu:
Agree. There's a good way of avoiding headaches, package formatR, 
function tidy.source.

My simplification is different in many places.
A common one is to treat 'observable' as a logical variable, not as a 
numeric one.
I've simplified some tests, the syntax in some places (namely, the 
condition 'if' is a function).
And eliminated some calls to runif(), whenever they could be done only once.

I think my code is the equivalent of Claudia's and I've worked it all. 
here it goes but without comments.



# # # Robust design
# # # with Markovian
# # # emigration and
# # # dummy time
# # # periods
J <- 24
N <- 10
S <- 0.9
PsiAD <- 0.06
PsiAd <- 0.04
PsiAA <- 0.4
PsiaA <- 0.3
p <- 0.5
c <- p
y <- matrix(0, N, J)
y[, 1] <- "A"
dtp <- rep(c(TRUE, TRUE, FALSE, FALSE), 6)[seq_len(J)]
for (j in which(dtp)) {
     for (q in 1:N) {
         (observable <- TRUE)
         if (j %% 2) {
             survive <- runif(1, 0, 1)
             decide <- runif(1, 0, 1)
             if (survive <= S) {
                 if (observable) {
                   observable <- (decide <= PsiAA)
                 } else {
                   observable <- (decide <= PsiaA)
                 }
                 if (observable) {
                   if (runif(1, 0, 1) <= p) y[q, j] <- "A"
                 }
             } else {
                 y[q, j] <- if (decide <= PsiAd) "d" else "D"
                 break
             }
         } else {
             if (observable) {
                 if (runif(1, 0, 1) <= c) y[q, j] <- "A"
             }
         }
     }
}

for (j in which(!dtp)) {
     for (q in 1:N) {
         if (j %% 2) {
             decide <- runif(1, 0, 1)
             if (observable) {
                 observable <- decide <= PsiAA
             } else {
                 observable <- decide <= PsiaA
             }
             if (observable) {
                 if (runif(1, 0, 1) <= p) y[q, j] <- "A"
             }
         } else {
             if (observable) {
                 if (runif(1, 0, 1) <= c) y[q, j] <- "A"
             }
         }
     }
}

# # # Robust design with markovian
# # # emigration
J <- 24
N <- 1000
S <- 0.9
PsiOO <- 0.4
PsiUO <- 0.3
p <- 0.5
c <- p
y <- matrix(0, N, J)
y[, 1] <- 1
for (q in 1:N) {
     (observable <- TRUE)
     for (j in 2:J) {
         if (j %% 2) {
             surviv <- runif(1, 0, 1)
             if (surviv <= S) {
                 decide <- runif(1, 0, 1)
                 if (observable) {
                   observable <- (decide <= PsiOO)
                 } else {
                   observable <- (decide <= PsiUO)
                 }
                 if (observable) {
                   y[q, j] <- if (runif(1, 0, 1) <= p) 1 else 0
                 }
             } else {
                 break
             }
         } else {
             if (observable) {
                 y[q, j] <- if (runif(1, 0, 1) <= c) 1 else 0
             }
         }
     }
}


Hope this helps,

Rui Barradas