Search code examples
rmatrixmarkov

Creating a transition matrix in R using sea turtle data


This has been rattling around my head for a while.

From Broderick et al. 2006 (http://www.ascension-island.gov.ac/wp-content/uploads/2012/12/are-green-turtles-globally-endangered.pdf), they created a transition matrix (Table 1) for the probability of a turtle surviving and growing to the next age-class and the probability of a turtle surviving and growing to the ith age-class.

Could anyone show me how to create this sort of matrix in R?

Table 1.

[Age-class                  S1 S2 S3 S4 S5 S6
Structure S1 (egg-neonate) P1 F2 F3 F4 F5 F6
S2 (pelagic individual)    G1 P2 0 0 0 0
S3 (benthic individual)    0 G2 P3 0 0 0
S4 (sub-adult)             0 0 G3 P4 0 0
S5 (maturing adult)        0 0 0 G4 P5 0
S6 (adult)                 0 0 0 0 G5 P6
Values S1   0 0 0 F4 F5 F6
S2          0.4394 0.5704 0 0 0 0
S3          0 0.0741 0.8413 0 0 0
S4          0 0 0.0391 0.8405 0 0
S5          0 0 0 0.0069 0.7782 0
S6          0 0 0 0.1700 0.9482]

Also, using this matrix you can calculate the proportion of hatchlings that survive to adulthood.


Solution

  • You have a couple of potential problems or barriers to surmount. Since R uses column major indexing, you need to use the byrow parameter for the matrix call. You are also missing a 0 (as was the original article as well) in the last row of the values. I don't see any point in creating the upper matrix using character values, so will instead demonstrate a couple of data input methods. Using scan to bring data in allows one to import text from the command line without retyping everything. The default mode for input with scan is "numeric" so you do not need to include a what argument:

    valsRMI3 = c(0.3299, 53.4639, + 90.6716)
    valsRMI4 <-c(0.2474, 40.0980, 68.0037)  # copied from the PDF file
    
    mvals <- scan(text="0.4394 0.5704 0 0 0 0
     0 0.0741 0.8413 0 0 0
     0 0 0.0391 0.8405 0 0
     0 0 0 0.0069 0.7782 0
     0 0 0 0 0.1700 0.9482")  # added the extra 0 after noting incorrect number of input values
    

    To make a matrix with row and column labels (useful for indexing) for the case of RMI=3:

    matrix( c( 0,0,0,valsRMI3,  # the first row
               mvals),          # rest of matrix values
            nrow=6, 
            byrow=TRUE, 
            dimnames=list( paste0("S", 1:6), paste0("S", 1:6))  )
    
    #--------------
           S1     S2     S3     S4      S5      S6
    S1 0.0000 0.0000 0.0000 0.3299 53.4639 90.6716
    S2 0.4394 0.5704 0.0000 0.0000  0.0000  0.0000
    S3 0.0000 0.0741 0.8413 0.0000  0.0000  0.0000
    S4 0.0000 0.0000 0.0391 0.8405  0.0000  0.0000
    S5 0.0000 0.0000 0.0000 0.0069  0.7782  0.0000
    S6 0.0000 0.0000 0.0000 0.0000  0.1700  0.9482
    

    There is matrix-exponential function that is available within the Matrix and expm packages and matrix-power in expm which may be needed for assessment of Markov modeling predictions.