Skip to content
Prev 302008 / 398503 Next

Alternating between "for loops"

On Aug 1, 2012, at 12:02 PM, Mercier Eloi wrote:

            
I had a similar reaction. However, my approach might have been to  
request a more complete verbal description of the data structures  
being operated on and the methods and assumptions being used.  
Generally it is going to be easier to go from a procedural description  
to good R code than it is to go from a SAS Data Proc ... even if it  
were tested and debugged... and yours was not even debugged.
# So is 'y' a matrix of states where the N rows are levels and the J  
columns are discrete times?
# Actually I decided that context suggested you were using letters to  
represent states.
# So we start with N <something>'s in state "A"?
# It seems as though it might be the case that every row is  
independent of the others.
# .. and you are performing ( replicate()-ing in R terminology) this  
test N times
# states:
#("A" "D")
#("a" "d")
#transitions:
    matrix( c( runif(1, 0, 1) <= 0.4, # A -> A
           runif(1,0,1) <= 0.3,  # a -> A
           runif(1,0,1) <= 0.06. # A -> D
           runif(1,0,1) <= 0.04  # A -> d) )

# What is state "a"?
# How do you get from A to a?
# can you get from D or d to A or a?

# Not sure I have gotten the model assumptions down.
# Is D" or "d" an absorbing state such as "dead or "Dead"?
#Presumably the "dummy time periods"
# It would help if the concept of "observable" were explained
# Is this something to do with the capture-recapture observables?
# After allowing for Mercier's astute observation that observable will  
always be 1, I'm wondering if it can be replaced with just this code  
(and not need the loop surrounding it.)

                     observable <- (stay<=PsiAA)

#--------------
# better NOT to use the name "return" since it is a crucial R function  
name.
#------- perhaps:

              randret <-  return=runif(N,0,1)
              observable <- randret <= PsiaA
# -------------------
#---------- perhaps:
           randcap <- runif(N, 0,1)
           y[ randcap <= p, j] <- "A"
#That would replace with "A" (within the j-column) ...
#     only the rows in 'y' for which randcap were less than the  
randcap threshold.

     #------------
# -------Perhaps
      deado <- runif(N, 0,1)
      y[ , j] <- ifelse( deado<=PsiAd, "d", "D")
#------------------------
#    ----------Really?  I thought that condition was assured at this  
point?
David Winsemius, MD
Alameda, CA, USA