Chapter 14 Further Issues III: Editing Matrices in MPMs

You’ll soon be hearing the chime Close to midnight If I could turn back the time I’d make it alright

— Ghost, Dance Macabre (2018)

In the last two chapters, we saw how to import MPMs and discretized IPMs as lefkoMat objects. Let’s now explore how we can edit these structures. Editing functions included the ability to add and delete matrices, to subset MPMs into smaller MPMs, and to edit matrices within MPMs.

Let’s start by importing a lefkoMat object to work with. We will use the anthyllis MPM that is already packaged within lefko3 for now, although later on we will use other MPMs to illustrate other points.

data(anthyllis)
summary(anthyllis)
> 
> This ahistorical lefkoMat object contains 27 matrices.
> 
> Each matrix is square with 4 rows and columns, and a total of 16 elements.
> A total of 167 survival transitions were estimated, with 6.185 per matrix.
> A total of 48 fecundity transitions were estimated, with 1.778 per matrix.
> This lefkoMat object covers 1 population, 9 patches, and 3 time steps.
> 
> This lefkoMat object appears to have been imported. Number of unique individuals and transitions not known.
> 
> Survival probability sum check (each matrix represented by column in order):
>           [,1]  [,2]   [,3]   [,4]   [,5]  [,6]   [,7]  [,8]  [,9] [,10]  [,11]
> Min.    0.0667 0.500 0.0625 0.0769 0.0909 0.000 0.0000 0.333 0.171 0.000 0.0000
> 1st Qu. 0.0810 0.575 0.1708 0.1192 0.1334 0.075 0.0375 0.405 0.268 0.170 0.0000
> Median  0.1198 0.657 0.2106 0.3077 0.2276 0.100 0.1706 0.453 0.350 0.239 0.0000
> Mean    0.1599 0.637 0.2209 0.4231 0.2303 0.175 0.1895 0.469 0.320 0.203 0.0556
> 3rd Qu. 0.1987 0.720 0.2607 0.6116 0.3245 0.200 0.3225 0.517 0.402 0.271 0.0556
> Max.    0.3333 0.736 0.4000 1.0000 0.3750 0.500 0.4167 0.636 0.409 0.333 0.2222
>         [,12]  [,13] [,14]  [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
> Min.    0.000 0.0000 0.000 0.0370 0.333 0.000 0.238  0.00 0.000 0.000 0.000
> 1st Qu. 0.375 0.0000 0.193 0.0408 0.394 0.381 0.310  0.00 0.500 0.000 0.125
> Median  0.500 0.0163 0.313 0.0657 0.564 0.518 0.410  0.00 0.833 0.250 0.271
> Mean    0.409 0.0510 0.281 0.0705 0.565 0.420 0.388  0.25 0.667 0.304 0.260
> 3rd Qu. 0.534 0.0673 0.401 0.0954 0.736 0.557 0.488  0.25 1.000 0.554 0.406
> Max.    0.636 0.1714 0.500 0.1136 0.800 0.645 0.493  1.00 1.000 0.714 0.500
>         [,23] [,24]  [,25] [,26]  [,27]
> Min.    0.000 0.000 0.0000 0.000 0.0000
> 1st Qu. 0.500 0.250 0.0000 0.375 0.0833
> Median  0.762 0.333 0.0833 0.583 0.2556
> Mean    0.631 0.323 0.0833 0.542 0.3153
> 3rd Qu. 0.893 0.406 0.1667 0.750 0.4875
> Max.    1.000 0.625 0.1667 1.000 0.7500

Quickly looking over the structure, we see that we have an object that is composed of 9 populations (organized as patches in the lefkoMat object), with four years of data yielding three time steps for each population. The lefkoMat object holds an imported ahistorical MPM. Let’s move on to some editing.

14.1 Subsetting a lefkoMat object

Situations may arise in which a user will wish to create a new lefkoMat object that is itself a subset of another. Such situations can arise, for example, when a lefkoMat object covers several patches or populations and the user wishes to create a single object to hold a single patch or population. In these situations, we can use the subset_lM() function.

Let’s see an example of this function at work. Here, we will create a subset of the anthyllis object focused only on patch S.

anth_S <- subset_lM(anthyllis, patch = "S")
summary(anth_S)
> 
> This ahistorical lefkoMat object contains 3 matrices.
> 
> Each matrix is square with 4 rows and columns, and a total of 16 elements.
> A total of 10 survival transitions were estimated, with 3.333 per matrix.
> A total of 4 fecundity transitions were estimated, with 1.333 per matrix.
> This lefkoMat object covers 1 population, 1 patch, and 3 time steps.
> 
> This lefkoMat object appears to have been imported. Number of unique individuals and transitions not known.
> 
> Survival probability sum check (each matrix represented by column in order):
>           [,1]  [,2]   [,3]
> Min.    0.0000 0.000 0.0000
> 1st Qu. 0.0000 0.375 0.0833
> Median  0.0833 0.583 0.2556
> Mean    0.0833 0.542 0.3153
> 3rd Qu. 0.1667 0.750 0.4875
> Max.    0.1667 1.000 0.7500

And so we see here that we have created a much smaller MPM that includes only one of the original populations. The result includes three matrices only. We can see this within the labels object.

anth_S$labels
>   pop patch year2
> 1   1     S  2003
> 2   1     S  2004
> 3   1     S  2005

Now let’s try another subset, but this time focused on matrices in which the year in time t is either 2004 or 2005.

anth_20042005 <- subset_lM(anthyllis, year = c(2004, 2005))
summary(anth_20042005)
> 
> This ahistorical lefkoMat object contains 18 matrices.
> 
> Each matrix is square with 4 rows and columns, and a total of 16 elements.
> A total of 117 survival transitions were estimated, with 6.5 per matrix.
> A total of 30 fecundity transitions were estimated, with 1.667 per matrix.
> This lefkoMat object covers 1 population, 9 patches, and 2 time steps.
> 
> This lefkoMat object appears to have been imported. Number of unique individuals and transitions not known.
> 
> Survival probability sum check (each matrix represented by column in order):
>          [,1]   [,2]   [,3]  [,4]  [,5]  [,6]   [,7]  [,8]  [,9]  [,10] [,11]
> Min.    0.500 0.0625 0.0909 0.000 0.333 0.171 0.0000 0.000 0.000 0.0370 0.000
> 1st Qu. 0.575 0.1708 0.1334 0.075 0.405 0.268 0.0000 0.375 0.193 0.0408 0.381
> Median  0.657 0.2106 0.2276 0.100 0.453 0.350 0.0000 0.500 0.313 0.0657 0.518
> Mean    0.637 0.2209 0.2303 0.175 0.469 0.320 0.0556 0.409 0.281 0.0705 0.420
> 3rd Qu. 0.720 0.2607 0.3245 0.200 0.517 0.402 0.0556 0.534 0.401 0.0954 0.557
> Max.    0.736 0.4000 0.3750 0.500 0.636 0.409 0.2222 0.636 0.500 0.1136 0.645
>         [,12] [,13] [,14] [,15] [,16] [,17]  [,18]
> Min.    0.238 0.000 0.000 0.000 0.000 0.000 0.0000
> 1st Qu. 0.310 0.500 0.000 0.500 0.250 0.375 0.0833
> Median  0.410 0.833 0.250 0.762 0.333 0.583 0.2556
> Mean    0.388 0.667 0.304 0.631 0.323 0.542 0.3153
> 3rd Qu. 0.488 1.000 0.554 0.893 0.406 0.750 0.4875
> Max.    0.493 1.000 0.714 1.000 0.625 1.000 0.7500

Now we have a subset that includes 18 matrices, since nine of the original 27 matrices were for year 2003 and so are not included in the new object.

We can make even more complicated subsets by using the matrix number from the A list, or the row number from the labels data frame, as below.

anth_complex <- subset_lM(anthyllis, mat_num = c(1, 5, 20, 21))
summary(anth_complex)
> 
> This ahistorical lefkoMat object contains 4 matrices.
> 
> Each matrix is square with 4 rows and columns, and a total of 16 elements.
> A total of 24 survival transitions were estimated, with 6 per matrix.
> A total of 6 fecundity transitions were estimated, with 1.5 per matrix.
> This lefkoMat object covers 1 population, 3 patches, and 3 time steps.
> 
> This lefkoMat object appears to have been imported. Number of unique individuals and transitions not known.
> 
> Survival probability sum check (each matrix represented by column in order):
>           [,1]   [,2]  [,3]  [,4]
> Min.    0.0667 0.0909 0.000 0.000
> 1st Qu. 0.0810 0.1334 0.500 0.000
> Median  0.1198 0.2276 0.833 0.250
> Mean    0.1599 0.2303 0.667 0.304
> 3rd Qu. 0.1987 0.3245 1.000 0.554
> Max.    0.3333 0.3750 1.000 0.714

Here we made a new lefkoMat object composed of four matrices, and chose those directly with the mat_num option. Let’s see the metadata associated with these matrices.

anth_complex$labels
>   pop patch year2
> 1   1     C  2003
> 2   1     E  2004
> 3   1     Q  2004
> 4   1     Q  2005

14.2 Adding and deleting matrices

All lefkoMat objects may be edited. One primary way in which we might wish to edit these objects is through the addition or deletion of matrices. Let’s take a look at deleting matrices first.

The delete_lM() function is the primary means of removing matrices from a lefkoMat object. It takes the same inputs as subset_lM() and uses them in similar ways, but to remove matrices rather than subset them. It removes the matrix and edits the associated labels and quality control objects to reflect the change. Let’s try one such situation, in which we create a new lefkoMat object by creating a copy of anthyllis in which we have removed patch S.

anth_noS <- delete_lM(anthyllis, patch = "S")
summary(anth_noS)
> 
> This ahistorical lefkoMat object contains 24 matrices.
> 
> Each matrix is square with 4 rows and columns, and a total of 16 elements.
> A total of 157 survival transitions were estimated, with 6.542 per matrix.
> A total of 44 fecundity transitions were estimated, with 1.833 per matrix.
> This lefkoMat object covers 1 population, 8 patches, and 3 time steps.
> 
> This lefkoMat object appears to have been imported. Number of unique individuals and transitions not known.
> 
> Survival probability sum check (each matrix represented by column in order):
>           [,1]  [,2]   [,3]   [,4]   [,5]  [,6]   [,7]  [,8]  [,9] [,10]  [,11]
> Min.    0.0667 0.500 0.0625 0.0769 0.0909 0.000 0.0000 0.333 0.171 0.000 0.0000
> 1st Qu. 0.0810 0.575 0.1708 0.1192 0.1334 0.075 0.0375 0.405 0.268 0.170 0.0000
> Median  0.1198 0.657 0.2106 0.3077 0.2276 0.100 0.1706 0.453 0.350 0.239 0.0000
> Mean    0.1599 0.637 0.2209 0.4231 0.2303 0.175 0.1895 0.469 0.320 0.203 0.0556
> 3rd Qu. 0.1987 0.720 0.2607 0.6116 0.3245 0.200 0.3225 0.517 0.402 0.271 0.0556
> Max.    0.3333 0.736 0.4000 1.0000 0.3750 0.500 0.4167 0.636 0.409 0.333 0.2222
>         [,12]  [,13] [,14]  [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
> Min.    0.000 0.0000 0.000 0.0370 0.333 0.000 0.238  0.00 0.000 0.000 0.000
> 1st Qu. 0.375 0.0000 0.193 0.0408 0.394 0.381 0.310  0.00 0.500 0.000 0.125
> Median  0.500 0.0163 0.313 0.0657 0.564 0.518 0.410  0.00 0.833 0.250 0.271
> Mean    0.409 0.0510 0.281 0.0705 0.565 0.420 0.388  0.25 0.667 0.304 0.260
> 3rd Qu. 0.534 0.0673 0.401 0.0954 0.736 0.557 0.488  0.25 1.000 0.554 0.406
> Max.    0.636 0.1714 0.500 0.1136 0.800 0.645 0.493  1.00 1.000 0.714 0.500
>         [,23] [,24]
> Min.    0.000 0.000
> 1st Qu. 0.500 0.250
> Median  0.762 0.333
> Mean    0.631 0.323
> 3rd Qu. 0.893 0.406
> Max.    1.000 0.625

We can see that we have a new object with three fewer matrices, and one fewer patch. Let’s see the labels object to make sure.

anth_noS$labels
>    pop patch year2
> 1    1     C  2003
> 2    1     C  2004
> 3    1     C  2005
> 4    1     E  2003
> 5    1     E  2004
> 6    1     E  2005
> 7    1     F  2003
> 8    1     F  2004
> 9    1     F  2005
> 10   1     G  2003
> 11   1     G  2004
> 12   1     G  2005
> 13   1     L  2003
> 14   1     L  2004
> 15   1     L  2005
> 16   1     O  2003
> 17   1     O  2004
> 18   1     O  2005
> 19   1     Q  2003
> 20   1     Q  2004
> 21   1     Q  2005
> 22   1     R  2003
> 23   1     R  2004
> 24   1     R  2005

The last patch is clearly patch R. So, patch S does not exist in the new edited lefkoMat object.

We may also add matrices, using the add_lM() function. In this case, we usually need to add a little information to make sure that the labels object is updated properly. We provide the new matrices as single matrices or as a list of matrices, via the Amats option with a logical setting for UFdecomp, or via the Umats and Fmats option if we have survival-transition matrices separated from fecundity matrices. We provide the extra information identifying these matrices via the entrystage, pop, patch, and year options.

Let’s see how this function works by adding the S patches back to our new lefkoMat object.

# POPN S 2003-2004
XS3 <- matrix(c(0, 0, 2.1, 0.816666667,
  0.166666667, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0.166666667), 4, 4, byrow = TRUE)

# POPN S 2004-2005
XS4 <- matrix(c(0, 0, 0, 7,
  0.333333333, 0.5, 0, 0,
  0, 0, 0, 0,
  0.333333333, 0, 0, 1), 4, 4, byrow = TRUE)

# POPN S 2005-2006
XS5 <- matrix(c(0, 0, 0, 1.4,
  0, 0, 0, 0,
  0, 0, 0, 0.2,
  0.111111111, 0.75, 0, 0.2), 4, 4, byrow = TRUE)

mats_list_S <- list(XS3, XS4, XS5)

anth_noS_addedS <- add_lM(anth_noS, Amats = mats_list_S, UFdecomp = TRUE,
  patch = c("S", "S", "S"), year = c(2003, 2004, 2005))
summary(anth_noS_addedS)
> 
> This ahistorical lefkoMat object contains 27 matrices.
> 
> Each matrix is square with 4 rows and columns, and a total of 16 elements.
> A total of 167 survival transitions were estimated, with 6.185 per matrix.
> A total of 48 fecundity transitions were estimated, with 1.778 per matrix.
> This lefkoMat object covers 1 population, 9 patches, and 3 time steps.
> 
> This lefkoMat object appears to have been imported. Number of unique individuals and transitions not known.
> 
> Survival probability sum check (each matrix represented by column in order):
>           [,1]  [,2]   [,3]   [,4]   [,5]  [,6]   [,7]  [,8]  [,9] [,10]  [,11]
> Min.    0.0667 0.500 0.0625 0.0769 0.0909 0.000 0.0000 0.333 0.171 0.000 0.0000
> 1st Qu. 0.0810 0.575 0.1708 0.1192 0.1334 0.075 0.0375 0.405 0.268 0.170 0.0000
> Median  0.1198 0.657 0.2106 0.3077 0.2276 0.100 0.1706 0.453 0.350 0.239 0.0000
> Mean    0.1599 0.637 0.2209 0.4231 0.2303 0.175 0.1895 0.469 0.320 0.203 0.0556
> 3rd Qu. 0.1987 0.720 0.2607 0.6116 0.3245 0.200 0.3225 0.517 0.402 0.271 0.0556
> Max.    0.3333 0.736 0.4000 1.0000 0.3750 0.500 0.4167 0.636 0.409 0.333 0.2222
>         [,12]  [,13] [,14]  [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
> Min.    0.000 0.0000 0.000 0.0370 0.333 0.000 0.238  0.00 0.000 0.000 0.000
> 1st Qu. 0.375 0.0000 0.193 0.0408 0.394 0.381 0.310  0.00 0.500 0.000 0.125
> Median  0.500 0.0163 0.313 0.0657 0.564 0.518 0.410  0.00 0.833 0.250 0.271
> Mean    0.409 0.0510 0.281 0.0705 0.565 0.420 0.388  0.25 0.667 0.304 0.260
> 3rd Qu. 0.534 0.0673 0.401 0.0954 0.736 0.557 0.488  0.25 1.000 0.554 0.406
> Max.    0.636 0.1714 0.500 0.1136 0.800 0.645 0.493  1.00 1.000 0.714 0.500
>         [,23] [,24]  [,25] [,26]  [,27]
> Min.    0.000 0.000 0.0000 0.000 0.0000
> 1st Qu. 0.500 0.250 0.0000 0.375 0.0833
> Median  0.762 0.333 0.0833 0.583 0.2556
> Mean    0.631 0.323 0.0833 0.542 0.3153
> 3rd Qu. 0.893 0.406 0.1667 0.750 0.4875
> Max.    1.000 0.625 0.1667 1.000 0.7500

Users can compare this summary to that of anthyllis to see that they are the same.

In the options above, we set UFdecomp = TRUE. This is an important setting, because it allows R to use the stageframe in the existing lefkoMat object to infer which elements correspond to fecundity transitions. The function create_lM() also has this setting, but the default for that function is TRUE. In add_lM(), the default is set to FALSE, meaning that fecundity terms would not be set to the F matrices instead of the U matrices without the user explicitly setting it to TRUE. Conversely, if matrix elements holding fecundity rates also mix those rates with survival probabilities, then the user may wish to set this option to FALSE across the board and edit the U and F matrices by hand to set the matrices properly.

14.3 Editing elements within matrices

Users may occasionally need to edit matrices within lefkoMat objects. If the user merely wishes to change a single element in a single matrix and knows the element’s position, then it is easy to manipulate that element manually. However, the issue becomes more complicated if the matrix is particularly large, or if the elements that need adjusting occur in several matrices. To deal with these issues, lefko3 contains the function edit_lM(), which can change elements associated with specific transitions or groups of transitions, and in not just a single matrix but even in groups of matrices.

Function edit_lM() takes inputs similar to function supplemental(), with the addition of specific input options for population (pop), patch (patch), and time (year2). It uses stage names in the associated stageframe for the input lefkoMat object, and can utilize the same stage name shorthand that supplemental() uses.

As an example, let’s say that we wish to create a lefkoMat object based on anthyllis, but in which the transition from seedling to small flowering adult is equal to 0.10 in population S. Let’s take a look at the stageframe to see the location of the element in question.

anthyllis$ahstages[,c(1:3)]
>   stage_id stage_id.1 stage
> 1        1          1   Sdl
> 2        2          2   Veg
> 3        3          3 SmFlo
> 4        4          4  LFlo

The survival transition element in question would be in column 1 and row 3. Let’s now investigate the value of this transition in the current MPM. Instead of targeting just the element, let’s see all of the survival transition matrices (we would not likely do this if the matrices were large).

anthyllis$U[which(anthyllis$labels$patch == "S")]
> [[1]]
>           [,1] [,2] [,3]      [,4]
> [1,] 0.0000000    0    0 0.0000000
> [2,] 0.1666667    0    0 0.0000000
> [3,] 0.0000000    0    0 0.0000000
> [4,] 0.0000000    0    0 0.1666667
> 
> [[2]]
>           [,1] [,2] [,3] [,4]
> [1,] 0.0000000  0.0    0    0
> [2,] 0.3333333  0.5    0    0
> [3,] 0.0000000  0.0    0    0
> [4,] 0.3333333  0.0    0    1
> 
> [[3]]
>           [,1] [,2] [,3] [,4]
> [1,] 0.0000000 0.00    0  0.0
> [2,] 0.0000000 0.00    0  0.0
> [3,] 0.0000000 0.00    0  0.2
> [4,] 0.1111111 0.75    0  0.2

We see that in all cases, this transition equals 0. This may be due to a biologically impossible transition, or to no individuals taking that particular transition within a small dataset.

Let’s create our new lefkoMat object, using anthyllis as a template and the edit_lM() function to hold the editing instructions. In addition to changing the survival transitions noted above, let’s also double fecundity across the board. Note the similarity to how such information would be structured in function supplemental(), even to the point of using the "rep" shorthand to code for reproductive stages.

anth_editexample <- edit_lM(anthyllis, patch = "S",
  stage3 = c("SmFlo", "Sdl"), stage2 = c("Sdl", "rep"), givenrate = c(0.10, NA),
  multiplier = c(1.0, 2.0), type =c(1, 2))

Now let’s compare the survival transitions in the old vs. new MPMs.

anthyllis$U[which(anthyllis$labels$patch == "S")]
> [[1]]
>           [,1] [,2] [,3]      [,4]
> [1,] 0.0000000    0    0 0.0000000
> [2,] 0.1666667    0    0 0.0000000
> [3,] 0.0000000    0    0 0.0000000
> [4,] 0.0000000    0    0 0.1666667
> 
> [[2]]
>           [,1] [,2] [,3] [,4]
> [1,] 0.0000000  0.0    0    0
> [2,] 0.3333333  0.5    0    0
> [3,] 0.0000000  0.0    0    0
> [4,] 0.3333333  0.0    0    1
> 
> [[3]]
>           [,1] [,2] [,3] [,4]
> [1,] 0.0000000 0.00    0  0.0
> [2,] 0.0000000 0.00    0  0.0
> [3,] 0.0000000 0.00    0  0.2
> [4,] 0.1111111 0.75    0  0.2
anth_editexample$U[which(anth_editexample$labels$patch == "S")]
> [[1]]
>           [,1] [,2] [,3]      [,4]
> [1,] 0.0000000    0    0 0.0000000
> [2,] 0.1666667    0    0 0.0000000
> [3,] 0.1000000    0    0 0.0000000
> [4,] 0.0000000    0    0 0.1666667
> 
> [[2]]
>           [,1] [,2] [,3] [,4]
> [1,] 0.0000000  0.0    0    0
> [2,] 0.3333333  0.5    0    0
> [3,] 0.1000000  0.0    0    0
> [4,] 0.3333333  0.0    0    1
> 
> [[3]]
>           [,1] [,2] [,3] [,4]
> [1,] 0.0000000 0.00    0  0.0
> [2,] 0.0000000 0.00    0  0.0
> [3,] 0.1000000 0.00    0  0.2
> [4,] 0.1111111 0.75    0  0.2

The survival transition from seedling to small flowering adult has been altered. Now let’s see the fecundity matrices in the old vs. new MPMs.

anthyllis$F[which(anthyllis$labels$patch == "S")]
> [[1]]
>      [,1] [,2] [,3]      [,4]
> [1,]    0    0  2.1 0.8166667
> [2,]    0    0  0.0 0.0000000
> [3,]    0    0  0.0 0.0000000
> [4,]    0    0  0.0 0.0000000
> 
> [[2]]
>      [,1] [,2] [,3] [,4]
> [1,]    0    0    0    7
> [2,]    0    0    0    0
> [3,]    0    0    0    0
> [4,]    0    0    0    0
> 
> [[3]]
>      [,1] [,2] [,3] [,4]
> [1,]    0    0    0  1.4
> [2,]    0    0    0  0.0
> [3,]    0    0    0  0.0
> [4,]    0    0    0  0.0
anth_editexample$F[which(anth_editexample$labels$patch == "S")]
> [[1]]
>      [,1] [,2] [,3]     [,4]
> [1,]    0    0  4.2 1.633333
> [2,]    0    0  0.0 0.000000
> [3,]    0    0  0.0 0.000000
> [4,]    0    0  0.0 0.000000
> 
> [[2]]
>      [,1] [,2] [,3] [,4]
> [1,]    0    0    0   14
> [2,]    0    0    0    0
> [3,]    0    0    0    0
> [4,]    0    0    0    0
> 
> [[3]]
>      [,1] [,2] [,3] [,4]
> [1,]    0    0    0  2.8
> [2,]    0    0    0  0.0
> [3,]    0    0    0  0.0
> [4,]    0    0    0  0.0

Clearly the fecundity has been doubled.

14.4 Dealing with age-specific issues

The function edit_lM() can be used to edit Leslie and age-by-stage MPMs, as well as stage-based ahistorical and historical MPMs. Let’s load an age-by-stage MPM, this time focused on the lathyrus dastaset, and look at a summary.

data(lathyrus)

lathyrus$indiv_id <- paste(lathyrus$SUBPLOT, lathyrus$GENET)

sizevector <- c(0, 100, 13, 127, 3730, 3800, 0)
stagevector <- c("Sd", "Sdl", "VSm", "Sm", "VLa", "Flo", "Dorm")
repvector <- c(0, 0, 0, 0, 0, 1, 0)
obsvector <- c(0, 1, 1, 1, 1, 1, 0)
matvector <- c(0, 0, 1, 1, 1, 1, 1)
immvector <- c(1, 1, 0, 0, 0, 0, 0)
propvector <- c(1, 0, 0, 0, 0, 0, 0)
indataset <- c(0, 1, 1, 1, 1, 1, 1)
binvec <- c(0, 100, 11, 103, 3500, 3800, 0.5)

lathframe_raw <- sf_create(sizes = sizevector, stagenames = stagevector, 
  repstatus = repvector, obsstatus = obsvector, matstatus = matvector, 
  immstatus = immvector, indataset = indataset, binhalfwidth = binvec, 
  propstatus = propvector)

lathvert_raw <- verticalize3(lathyrus, noyears = 4, firstyear = 1988, 
  patchidcol = "SUBPLOT", individcol = "indiv_id", blocksize = 9, 
  juvcol = "Seedling1988", sizeacol = "Volume88", repstracol = "FCODE88", 
  fecacol = "Intactseed88", deadacol = "Dead1988", nonobsacol = "Dormant1988", 
  stageassign = lathframe_raw, stagesize = "sizea", censorcol = "Missing1988", 
  censorkeep = NA, censorRepeat = TRUE, censor = TRUE)

lathvert_raw_small <- subset(lathvert_raw, firstseen > 1988)

lathsupp2 <- supplemental(stage3 = c("Sd", "Sdl", "Sd", "Sdl"), 
  stage2 = c("Sd", "Sd", "rep", "rep"), givenrate = c(0.345, 0.054, NA, NA),
  multiplier = c(NA, NA, 0.345, 0.054), type = c(1, 1, 3, 3),
  stageframe = lathframe_raw, historical = FALSE, agebased = TRUE)

lathmat2p_raw <- arlefko2(data = lathvert_raw_small, stageframe = lathframe_raw,
  supplement = lathsupp2, stages = c("stage3", "stage2", "stage1"),
  patch = "all", patchcol = "patchid", yearcol = "year2", agecol = "obsage",
  indivcol = "individ")
> Warning: Supplement table contains multiple entries for the same transition(s).
summary(lathmat2p_raw)
> 
> This ahistorical lefkoMat object contains 12 matrices.
> 
> Each matrix is square with 21 rows and columns, and a total of 441 elements.
> A total of 209 survival transitions were estimated, with 17.417 per matrix.
> A total of 12 fecundity transitions were estimated, with 1 per matrix.
> This lefkoMat object covers 1 population, 6 patches, and 2 time steps.
> 
> The dataset contains a total of 345 unique individuals and 531 unique transitions.
> 
> Survival probability sum check (each matrix represented by column in order):
>          [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
> Min.    0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
> 1st Qu. 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
> Median  0.000 0.000 0.000 0.399 0.000 0.399 0.000 0.399 0.000 0.399 0.000 0.000
> Mean    0.281 0.349 0.261 0.385 0.274 0.463 0.268 0.423 0.223 0.388 0.234 0.279
> 3rd Qu. 0.399 0.778 0.399 0.750 0.399 1.000 0.399 0.778 0.399 0.818 0.399 0.399
> Max.    1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000

These are not huge matrices. They contain seven stages, and 3+ ages (meaning that we have three ages recognized, in which the final age incorporates ages greater than the final age). Further, we have already used the supplemental() function to make general edits to all matrices. Let’s take a look at the first two A matrices, corresponding to patch 1 .

lathmat2p_raw$A[c(1:2)]
> [[1]]
>        [,1]      [,2]      [,3]   [,4]  [,5] [,6]  [,7]  [,8] [,9] [,10] [,11]
>  [1,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 1.035 0.000    0     0     0
>  [2,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.162 0.000    0     0     0
>  [3,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [4,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [5,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [6,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [7,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [8,] 0.345 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [9,] 0.054 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [10,] 0.000 0.9047619 0.7142857 0.1875 0.250    0 0.000 0.000    0     0     0
> [11,] 0.000 0.0000000 0.1428571 0.6875 0.500    0 1.000 0.000    0     0     0
> [12,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [13,] 0.000 0.0000000 0.0000000 0.0625 0.125    0 0.000 0.000    0     0     0
> [14,] 0.000 0.0000000 0.0000000 0.0000 0.125    0 0.000 0.000    0     0     0
> [15,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.345    0     0     0
> [16,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.054    0     0     0
> [17,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [18,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [19,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [20,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [21,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>       [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
>  [1,]     0     0     0 0.000     0     0     0     0     0     0
>  [2,]     0     0     0 0.000     0     0     0     0     0     0
>  [3,]     0     0     0 0.000     0     0     0     0     0     0
>  [4,]     0     0     0 0.000     0     0     0     0     0     0
>  [5,]     0     0     0 0.000     0     0     0     0     0     0
>  [6,]     0     0     0 0.000     0     0     0     0     0     0
>  [7,]     0     0     0 0.000     0     0     0     0     0     0
>  [8,]     0     0     0 0.000     0     0     0     0     0     0
>  [9,]     0     0     0 0.000     0     0     0     0     0     0
> [10,]     0     0     0 0.000     0     0     0     0     0     0
> [11,]     0     0     0 0.000     0     0     0     0     0     0
> [12,]     0     0     0 0.000     0     0     0     0     0     0
> [13,]     0     0     0 0.000     0     0     0     0     0     0
> [14,]     0     0     0 0.000     0     0     0     0     0     0
> [15,]     0     0     0 0.345     0     0     0     0     0     0
> [16,]     0     0     0 0.054     0     0     0     0     0     0
> [17,]     0     0     0 0.000     0     0     0     0     0     0
> [18,]     0     0     0 0.000     0     0     0     0     0     0
> [19,]     0     0     0 0.000     0     0     0     0     0     0
> [20,]     0     0     0 0.000     0     0     0     0     0     0
> [21,]     0     0     0 0.000     0     0     0     0     0     0
> 
> [[2]]
>        [,1]      [,2]      [,3] [,4] [,5] [,6] [,7]  [,8] [,9]     [,10]
>  [1,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [2,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [3,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [4,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [5,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [6,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [7,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [8,] 0.345 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [9,] 0.054 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [10,] 0.000 0.6666667 0.5714286  0.0    0    0    0 0.000    0 0.0000000
> [11,] 0.000 0.0000000 0.4285714  0.5    0    0    0 0.000    0 0.0000000
> [12,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [13,] 0.000 0.1111111 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [14,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [15,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.345    0 0.0000000
> [16,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.054    0 0.0000000
> [17,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.7241379
> [18,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.1379310
> [19,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [20,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [21,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>            [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
>  [1,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [2,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [3,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [4,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [5,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [6,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [7,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [8,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
>  [9,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
> [10,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
> [11,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
> [12,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
> [13,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
> [14,] 0.00000000     0     0     0 0.000     0     0     0     0     0     0
> [15,] 0.00000000     0     0     0 0.345     0     0     0     0     0     0
> [16,] 0.00000000     0     0     0 0.054     0     0     0     0     0     0
> [17,] 0.05555556     0     0     0 0.000     0     0     0     0     0     0
> [18,] 0.55555556     0     1     0 0.000     0     0     0     0     0     0
> [19,] 0.11111111     0     0     0 0.000     0     0     0     0     0     0
> [20,] 0.05555556     0     0     1 0.000     0     0     0     0     0     0
> [21,] 0.22222222     0     0     0 0.000     0     0     0     0     0     0

The matrix structure is as a supermatrix in which each age is composed of seven rows and seven columns, corresponding to the stages in the stageframe. One interesting observation about these matrices is that the third age is generally empty. This is of course due to the fact that the dataset itself only contains four years, and we subset this to reduce it by one year. So, one edit that we might wish to make is to copy the transitions from age 2 over to age 3. Let’s try to do that here.

lathmat2p_raw_ageedit <- edit_lM(lathmat2p_raw, stage3 = c("all", "all"),
  stage2 = c("all", "all"), eststage3 = c("all", "all"),
  eststage2 = c("all", "all"), age2 = c(3, 3), estage2 = c(2, 2),
  multiplier = c(1, 1), type = c(1, 2))
summary(lathmat2p_raw_ageedit)
> 
> This ahistorical lefkoMat object contains 12 matrices.
> 
> Each matrix is square with 21 rows and columns, and a total of 441 elements.
> A total of 209 survival transitions were estimated, with 17.417 per matrix.
> A total of 12 fecundity transitions were estimated, with 1 per matrix.
> This lefkoMat object covers 1 population, 6 patches, and 2 time steps.
> 
> The dataset contains a total of 345 unique individuals and 531 unique transitions.
> 
> Survival probability sum check (each matrix represented by column in order):
>          [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
> Min.    0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
> 1st Qu. 0.000 0.000 0.000 0.143 0.000 0.399 0.000 0.399 0.000 0.399 0.000 0.000
> Median  0.000 0.500 0.000 0.667 0.000 0.846 0.000 0.700 0.000 0.625 0.000 0.399
> Mean    0.281 0.533 0.261 0.564 0.274 0.693 0.268 0.624 0.223 0.608 0.234 0.417
> 3rd Qu. 0.399 1.000 0.399 1.000 0.399 1.000 0.399 1.000 0.399 1.000 0.399 0.889
> Max.    1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000

Perusing the summary output, we might spot a few differences in the survival probability sum check. For example, the mean value given for matrix 2 is higher than previously. Let’s now take a look at the first two matrices.

lathmat2p_raw_ageedit$A[c(1:2)]
> [[1]]
>        [,1]      [,2]      [,3]   [,4]  [,5] [,6]  [,7]  [,8] [,9] [,10] [,11]
>  [1,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 1.035 0.000    0     0     0
>  [2,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.162 0.000    0     0     0
>  [3,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [4,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [5,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [6,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [7,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [8,] 0.345 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>  [9,] 0.054 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [10,] 0.000 0.9047619 0.7142857 0.1875 0.250    0 0.000 0.000    0     0     0
> [11,] 0.000 0.0000000 0.1428571 0.6875 0.500    0 1.000 0.000    0     0     0
> [12,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [13,] 0.000 0.0000000 0.0000000 0.0625 0.125    0 0.000 0.000    0     0     0
> [14,] 0.000 0.0000000 0.0000000 0.0000 0.125    0 0.000 0.000    0     0     0
> [15,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.345    0     0     0
> [16,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.054    0     0     0
> [17,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [18,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [19,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [20,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
> [21,] 0.000 0.0000000 0.0000000 0.0000 0.000    0 0.000 0.000    0     0     0
>       [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
>  [1,]     0     0     0 0.000     0     0     0     0     0     0
>  [2,]     0     0     0 0.000     0     0     0     0     0     0
>  [3,]     0     0     0 0.000     0     0     0     0     0     0
>  [4,]     0     0     0 0.000     0     0     0     0     0     0
>  [5,]     0     0     0 0.000     0     0     0     0     0     0
>  [6,]     0     0     0 0.000     0     0     0     0     0     0
>  [7,]     0     0     0 0.000     0     0     0     0     0     0
>  [8,]     0     0     0 0.000     0     0     0     0     0     0
>  [9,]     0     0     0 0.000     0     0     0     0     0     0
> [10,]     0     0     0 0.000     0     0     0     0     0     0
> [11,]     0     0     0 0.000     0     0     0     0     0     0
> [12,]     0     0     0 0.000     0     0     0     0     0     0
> [13,]     0     0     0 0.000     0     0     0     0     0     0
> [14,]     0     0     0 0.000     0     0     0     0     0     0
> [15,]     0     0     0 0.345     0     0     0     0     0     0
> [16,]     0     0     0 0.054     0     0     0     0     0     0
> [17,]     0     0     0 0.000     0     0     0     0     0     0
> [18,]     0     0     0 0.000     0     0     0     0     0     0
> [19,]     0     0     0 0.000     0     0     0     0     0     0
> [20,]     0     0     0 0.000     0     0     0     0     0     0
> [21,]     0     0     0 0.000     0     0     0     0     0     0
> 
> [[2]]
>        [,1]      [,2]      [,3] [,4] [,5] [,6] [,7]  [,8] [,9]     [,10]
>  [1,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [2,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [3,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [4,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [5,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [6,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [7,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [8,] 0.345 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>  [9,] 0.054 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [10,] 0.000 0.6666667 0.5714286  0.0    0    0    0 0.000    0 0.0000000
> [11,] 0.000 0.0000000 0.4285714  0.5    0    0    0 0.000    0 0.0000000
> [12,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [13,] 0.000 0.1111111 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [14,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [15,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.345    0 0.0000000
> [16,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.054    0 0.0000000
> [17,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.7241379
> [18,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.1379310
> [19,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [20,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
> [21,] 0.000 0.0000000 0.0000000  0.0    0    0    0 0.000    0 0.0000000
>            [,11] [,12] [,13] [,14] [,15] [,16]     [,17]      [,18] [,19] [,20]
>  [1,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [2,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [3,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [4,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [5,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [6,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [7,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [8,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
>  [9,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
> [10,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
> [11,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
> [12,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
> [13,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
> [14,] 0.00000000     0     0     0 0.000     0 0.0000000 0.00000000     0     0
> [15,] 0.00000000     0     0     0 0.345     0 0.0000000 0.00000000     0     0
> [16,] 0.00000000     0     0     0 0.054     0 0.0000000 0.00000000     0     0
> [17,] 0.05555556     0     0     0 0.000     0 0.7241379 0.05555556     0     0
> [18,] 0.55555556     0     1     0 0.000     0 0.1379310 0.55555556     0     1
> [19,] 0.11111111     0     0     0 0.000     0 0.0000000 0.11111111     0     0
> [20,] 0.05555556     0     0     1 0.000     0 0.0000000 0.05555556     0     0
> [21,] 0.22222222     0     0     0 0.000     0 0.0000000 0.22222222     0     0
>       [,21]
>  [1,]     0
>  [2,]     0
>  [3,]     0
>  [4,]     0
>  [5,]     0
>  [6,]     0
>  [7,]     0
>  [8,]     0
>  [9,]     0
> [10,]     0
> [11,]     0
> [12,]     0
> [13,]     0
> [14,]     0
> [15,]     0
> [16,]     0
> [17,]     0
> [18,]     0
> [19,]     0
> [20,]     1
> [21,]     0

We see that the first matrix appears the same, but this is because there are essentially no mature transitions for age 2 individuals. The second matrix shows age 2 transitions have been copied over to age 3.

Similar strategies can be used with Leslie MPMs, and also to change specific transitions in single matrices, or groups of matrices.

14.5 Large matrices

Let’s try one more situation, this time dealing with large, sparse matrices. We will deal with a historical version of the Cypripedium MPM. First, let’s create the MPM itself.

data(cypdata)

sizevector <- c(0, 0, 0, 0, 0, 0, 1, 3, 6, 11, 19.5)
stagevector <- c("SD", "P1", "P2", "P3", "SL", "D", "XSm", "Sm", "Md", "Lg",
  "XLg")
repvector <- c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1)
obsvector <- c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1)
matvector <- c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1)
immvector <- c(0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0)
propvector <- c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
indataset <- c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1)
binvec <- c(0, 0, 0, 0, 0, 0.5, 0.5, 1.5, 1.5, 3.5, 5)
comments <- c("Dormant seed", "1st yr protocorm", "2nd yr protocorm",
  "3rd yr protocorm", "Seedling", "Dormant adult",
  "Extra small adult (1 shoot)", "Small adult (2-4 shoots)",
  "Medium adult (5-7 shoots)", "Large adult (8-14 shoots)",
  "Extra large adult (>14 shoots)")
cypframe_raw <- sf_create(sizes = sizevector, stagenames = stagevector, 
  repstatus = repvector, obsstatus = obsvector, matstatus = matvector,
  propstatus = propvector, immstatus = immvector, indataset = indataset, 
  binhalfwidth = binvec, comments = comments)

cypraw_v1 <- verticalize3(data = cypdata, noyears = 6, firstyear = 2004, 
  patchidcol = "patch", individcol = "plantid", blocksize = 4,
  sizeacol = "Inf2.04", sizebcol = "Inf.04", sizeccol = "Veg.04",
  repstracol = "Inf.04", repstrbcol = "Inf2.04", fecacol = "Pod.04",
  stageassign = cypframe_raw, stagesize = "sizeadded", NAas0 = TRUE,
  NRasRep = TRUE, age_offset = 4)

seeds_per_fruit <- 5000
sl_mult <- 0.7

cypsupp3_raw <- supplemental(stage3 = c("SD", "SD", "P1", "P1", "P2", "P2",
    "P3", "SL", "SL", "SL", "D", "XSm", "Sm", "D", "XSm", "Sm", "mat", "mat",
    "mat", "SD", "P1"),
  stage2 = c("SD", "SD", "SD", "SD", "P1", "P1", "P2", "P3", "SL", "SL", "SL",
    "SL", "SL", "SL", "SL", "SL", "D", "XSm", "Sm", "rep", "rep"),
  stage1 = c("SD", "rep", "SD", "rep", "SD", "rep", "P1", "P2", "P3", "SL",
    "SL", "SL", "SL", "P3", "P3", "P3", "SL", "SL", "SL", "mat", "mat"),
  eststage3 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "D", "XSm", "Sm", "D",
    "XSm", "Sm", "mat", "mat", "mat", NA, NA), 
  eststage2 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "XSm", "XSm", "XSm",
    "XSm", "XSm", "XSm", "D", "XSm", "Sm", NA, NA),
  eststage1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "XSm", "XSm", "XSm",
    "XSm", "XSm", "XSm", "XSm", "XSm", "XSm", NA, NA),
  givenrate = c(0.08, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.05, 0.05, 0.05, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
  multiplier = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, sl_mult, sl_mult,
    sl_mult, sl_mult, sl_mult, sl_mult, 1, 1, 1, 0.5 * seeds_per_fruit,
    0.5 * seeds_per_fruit),
  type =c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3),
  stageframe = cypframe_raw, historical = TRUE)

cypmatrix3rp <- rlefko3(data = cypraw_v1, stageframe = cypframe_raw,
  year = "all", patch = "all", stages = c("stage3", "stage2", "stage1"), 
  size = c("size3added", "size2added", "size1added"), supplement = cypsupp3_raw, 
  yearcol = "year2", patchcol = "patchid", indivcol = "individ")
> Warning: Supplement table contains multiple entries for the same transition(s).
summary(cypmatrix3rp)
> 
> This historical lefkoMat object contains 12 matrices.
> 
> Each matrix is square with 121 rows and columns, and a total of 14641 elements.
> A total of 516 survival transitions were estimated, with 43 per matrix.
> A total of 70 fecundity transitions were estimated, with 5.833 per matrix.
> This lefkoMat object covers 1 population, 3 patches, and 4 time steps.
> 
> The dataset contains a total of 74 unique individuals and 320 unique transitions.
> 
> Survival probability sum check (each matrix represented by column in order):
>          [,1]   [,2]   [,3]  [,4]  [,5]  [,6] [,7]  [,8]  [,9]  [,10] [,11]
> Min.    0.000 0.0000 0.0000 0.000 0.000 0.000 0.00 0.000 0.000 0.0000 0.000
> 1st Qu. 0.000 0.0000 0.0000 0.000 0.000 0.000 0.00 0.000 0.000 0.0000 0.000
> Median  0.000 0.0000 0.0000 0.000 0.000 0.000 0.00 0.000 0.000 0.0000 0.000
> Mean    0.107 0.0945 0.0851 0.101 0.158 0.158 0.14 0.169 0.119 0.0851 0.119
> 3rd Qu. 0.000 0.0000 0.0000 0.000 0.100 0.100 0.05 0.100 0.000 0.0000 0.000
> Max.    1.000 1.0000 1.0000 1.000 1.000 1.000 1.00 1.000 1.000 1.0000 1.000
>         [,12]
> Min.    0.000
> 1st Qu. 0.000
> Median  0.000
> Mean    0.144
> 3rd Qu. 0.000
> Max.    1.000

We have 12 matrices, and each matrix has 121 rows and columns. On average, approximately 49 elements in each matrix are non-zero, making these matrices extremely sparse. Let’s also take a look at the labels element to see the order of the matrices.

cypmatrix3rp$labels
>    pop patch year2
> 1    1     A  2005
> 2    1     A  2006
> 3    1     A  2007
> 4    1     A  2008
> 5    1     B  2005
> 6    1     B  2006
> 7    1     B  2007
> 8    1     B  2008
> 9    1     C  2005
> 10   1     C  2006
> 11   1     C  2007
> 12   1     C  2008

Let’s suppose that we have some extra information that germination is higher in patch B than the other patches. We assumed that germination occurred at a rate of 0.10 typically, but let’s say that we have data suggesting that in patch B, it is twice that value. In this case, we can use function edit_lM() to make this change, as below.

cypmatrix3rp_boostB <- edit_lM(cypmatrix3rp, patch = "B", stage3 = c("P1", "P1"),
  stage2 = c("rep", "SD"), stage1 = c("mat", "rep"), givenrate = c(NA, 0.2),
  multiplier = c(2, NA), type = c(2, 1))
summary(cypmatrix3rp_boostB)
> 
> This historical lefkoMat object contains 12 matrices.
> 
> Each matrix is square with 121 rows and columns, and a total of 14641 elements.
> A total of 516 survival transitions were estimated, with 43 per matrix.
> A total of 70 fecundity transitions were estimated, with 5.833 per matrix.
> This lefkoMat object covers 1 population, 3 patches, and 4 time steps.
> 
> The dataset contains a total of 74 unique individuals and 320 unique transitions.
> 
> Survival probability sum check (each matrix represented by column in order):
>          [,1]   [,2]   [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10] [,11]
> Min.    0.000 0.0000 0.0000 0.000 0.000 0.000 0.000 0.000 0.000 0.0000 0.000
> 1st Qu. 0.000 0.0000 0.0000 0.000 0.000 0.000 0.000 0.000 0.000 0.0000 0.000
> Median  0.000 0.0000 0.0000 0.000 0.000 0.000 0.000 0.000 0.000 0.0000 0.000
> Mean    0.107 0.0945 0.0851 0.101 0.162 0.162 0.144 0.173 0.119 0.0851 0.119
> 3rd Qu. 0.000 0.0000 0.0000 0.000 0.100 0.100 0.050 0.100 0.000 0.0000 0.000
> Max.    1.000 1.0000 1.0000 1.000 1.000 1.000 1.000 1.000 1.000 1.0000 1.000
>         [,12]
> Min.    0.000
> 1st Qu. 0.000
> Median  0.000
> Mean    0.144
> 3rd Qu. 0.000
> Max.    1.000

This summary shows a shift in the mean values for survival probabilities associated with the fifth through eighth matrices, suggesting that we have, indeed, edited the matrices. To quickly assess this, we might try code like the following, which simply looks for differences and summarizies them.

# Patch A
summary(as.vector(cypmatrix3rp_boostB$A[[1]] - cypmatrix3rp$A[[1]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0
summary(as.vector(cypmatrix3rp_boostB$A[[2]] - cypmatrix3rp$A[[2]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0
summary(as.vector(cypmatrix3rp_boostB$A[[3]] - cypmatrix3rp$A[[3]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0
summary(as.vector(cypmatrix3rp_boostB$A[[4]] - cypmatrix3rp$A[[4]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0

# Patch B
summary(as.vector(cypmatrix3rp_boostB$A[[5]] - cypmatrix3rp$A[[5]]))
>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
>     0.000     0.000     0.000     3.311     0.000 17500.000
summary(as.vector(cypmatrix3rp_boostB$A[[6]] - cypmatrix3rp$A[[6]]))
>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
>     0.000     0.000     0.000     2.354     0.000 10000.000
summary(as.vector(cypmatrix3rp_boostB$A[[7]] - cypmatrix3rp$A[[7]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>    0.00    0.00    0.00    0.37    0.00 2500.00
summary(as.vector(cypmatrix3rp_boostB$A[[8]] - cypmatrix3rp$A[[8]]))
>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
>    0.0000    0.0000    0.0000    0.4269    0.0000 2500.0000

# Patch C
summary(as.vector(cypmatrix3rp_boostB$A[[9]] - cypmatrix3rp$A[[9]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0
summary(as.vector(cypmatrix3rp_boostB$A[[10]] - cypmatrix3rp$A[[10]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0
summary(as.vector(cypmatrix3rp_boostB$A[[11]] - cypmatrix3rp$A[[11]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0
summary(as.vector(cypmatrix3rp_boostB$A[[12]] - cypmatrix3rp$A[[12]]))
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>       0       0       0       0       0       0

All looks good! The extremely large values in the maxima for the fifth through eighth matrices correspond to the doubling of fecundity there, which is a result of doubled germination.

Users may use further strengths in this approach, including the use of proxy stages and stage groups, just as in supplemental().

14.6 Points to remember

  1. MPMs may be subset from larger lefkoMat objects using the subset_lM() function.
  2. Matrices may be added or deleted with add_lM() and delete_lM().
  3. Function edit_lM() allows matrices to be edited easily within lefkoMat objects, and these edits may be automated across groups of matrices or single matrices.