Chapter 16 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 includes 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, although later on we will also use other MPMs.

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 of this object, we see that this MPM comprises nine 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.

16.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. For example, we may have a lefkoMat object that covers several patches or populations, and we might wish 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 mat_num argument to select specific matrices from the \(\mathbf{A}\) list (equivalent to selecting the row of interest 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

16.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. 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 separated from survival terms. If matrix elements holding fecundity rates also include survival probabilities, then the user may wish to set this option to FALSE across the board and edit the \(\mathbf{U}\) and \(\mathbf{F}\) matrices by hand to set the matrices properly.

16.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.

16.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")
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 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.

16.5 Adding stages to MPMs

Situations may arise in which users wish to alter an MPM by adding a stage, or perhaps even several stages. One such situation is in the creation of age-hybrid MPMs, in which a Leslie MPM is altered with the addition of a non-age-specific stage. We describe such a situation in section 6.6 of chapter 6, but repeat the code here to show how this can be done.

lathvert_base <- verticalize3(lathyrus, noyears = 4, firstyear = 1988,
  patchidcol = "SUBPLOT", individcol = "indiv_id", blocksize = 9,
  sizeacol = "Volume88", repstracol = "FCODE88", fecacol = "Intactseed88",
  deadacol = "Dead1988", censorcol = "Missing1988", censorkeep = NA,
  censorRepeat= TRUE, censor = TRUE, NAas0 = TRUE, NRasRep = TRUE,
  NOasObs = TRUE)
lathvert_age <- subset(lathvert_base, firstseen > 1988)

lathmodels2_age <- modelsearch(lathvert_age, historical = FALSE,
  approach = "mixed", suite = "cons", bestfit = "AICc&k", age = "obsage",
  vitalrates = c("surv", "fec"), fecdist = "negbin", indiv = "individ",
  year = "year2", year.as.random = TRUE, patch.as.random = TRUE,
  show.model.tables = TRUE, fec.zero = TRUE, global.only = TRUE,
  test.age = TRUE, quiet = "partial")
> 
> Developing global model of survival probability...
> 
> Global model of survival probability developed. Proceeding with model dredge...
> 
> Developing global model of fecudity...
> 
> Global model of fecundity developed. Proceeding with model dredge...

lathsupp2_ageonly <- supplemental(age2 = c(2, 3), multiplier = c(0.345, 0.345),
  type = c(3, 3), historical = FALSE, stagebased = FALSE, agebased = TRUE)

lathmat2ageonly_func <- fleslie(year = "all", data = lathvert_age,
  supplement = lathsupp2_ageonly, modelsuite = lathmodels2_age)

lathmat2_agehybrid <- add_stage(lathmat2ageonly_func, add_before = 1,
  stage_name = "DormSeed")

lathmat2_agehybrid$ahstages$propstatus[1] <- 1 # Dormant seeds are propagules

lathmat2_agehybrid <- edit_lM(lathmat2_agehybrid,
  stage3 = c("DormSeed", "Age1", "Age1", "DormSeed"),
  stage2 = c("DormSeed", "DormSeed", "Age3", "Age3"),
  eststage3 = c(NA, NA, NA, "Age1"),
  eststage2 = c(NA, NA, NA, "Age3"),
  givenrate = c(0.5, 0.2, NA, NA),
  multiplier = c(NA, NA, 0.5, 1),
  type = c(1, 1, 3, 2))

summary(lathmat2_agehybrid)
> 
> This ahistorical lefkoMat object contains 2 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 5 per matrix.
> A total of 6 fecundity transitions were estimated, with 3 per matrix.
> This lefkoMat object covers 1 population, 1 patch, and 2 time steps.
> 
> The dataset contains a total of 345 unique individuals and 531 unique transitions.
> 
> Vital rate modeling quality control:
> 
> Survival estimated with 345 individuals and 531 individual transitions.
> Observation probability not estimated.
> Primary size transition not estimated.
> Secondary size transition not estimated.
> Tertiary size transition not estimated.
> Reproduction probability not estimated.
> Fecundity estimated with 345 individuals and 531 individual transitions.
> Juvenile survival not estimated.
> Juvenile observation probability not estimated.
> Juvenile primary size transition not estimated.
> Juvenile secondary size transition not estimated.
> Juvenile tertiary size transition not estimated.
> Juvenile reproduction probability not estimated.
> Juvenile maturity transition probability not estimated.
> 
> Survival probability sum check (each matrix represented by column in order):
>          [,1]  [,2]
> Min.    0.700 0.700
> 1st Qu. 0.782 0.710
> Median  0.816 0.722
> Mean    0.792 0.723
> 3rd Qu. 0.826 0.736
> Max.    0.835 0.749

We begin by creating a purely age-based MPM, called lathmat2ageonly_func. We then create a new MPM using the add_stage() function, telling that function to copy the previous MPM but to include a new stage called DormSeed just before the first stage in the ahstages object of the Leslie MPM. The function creates this stage, adding it to the ahstages object (also the hstages or agestages objects, if appropriate) and to all of the matrices. These new rows and columns are filled with zeroes, and the default description of the stage in the ahstages object details the stage as immature and observable. So, we need to edit the propstatus term associated with it to reflect this stage’s status as a propagule (a dormant seed), and then we still need to use the edit_lM() function to add in the appropriate new transition values in the new rows and columns of the \(\mathbf{A}\), \(\mathbf{U}\), and \(\mathbf{F}\) matrices. Here is the full MPM.

lathmat2_agehybrid
> $A
> $A[[1]]
>      [,1]      [,2]      [,3]      [,4]
> [1,]  0.5 0.0000000 0.0000000 0.1724972
> [2,]  0.2 0.9884574 0.3448508 0.1724972
> [3,]  0.0 0.8089559 0.0000000 0.0000000
> [4,]  0.0 0.0000000 0.8226008 0.8354693
> 
> $A[[2]]
>      [,1]      [,2]      [,3]      [,4]
> [1,]  0.5 0.0000000 0.0000000 0.1724985
> [2,]  0.2 0.9937995 0.3449203 0.1724985
> [3,]  0.0 0.7130815 0.0000000 0.0000000
> [4,]  0.0 0.0000000 0.7312994 0.7487682
> 
> 
> $U
> $U[[1]]
>      [,1]      [,2]      [,3]      [,4]
> [1,]  0.5 0.0000000 0.0000000 0.0000000
> [2,]  0.2 0.0000000 0.0000000 0.0000000
> [3,]  0.0 0.8089559 0.0000000 0.0000000
> [4,]  0.0 0.0000000 0.8226008 0.8354693
> 
> $U[[2]]
>      [,1]      [,2]      [,3]      [,4]
> [1,]  0.5 0.0000000 0.0000000 0.0000000
> [2,]  0.2 0.0000000 0.0000000 0.0000000
> [3,]  0.0 0.7130815 0.0000000 0.0000000
> [4,]  0.0 0.0000000 0.7312994 0.7487682
> 
> 
> $F
> $F[[1]]
>      [,1]      [,2]      [,3]      [,4]
> [1,]    0 0.0000000 0.0000000 0.1724972
> [2,]    0 0.9884574 0.3448508 0.1724972
> [3,]    0 0.0000000 0.0000000 0.0000000
> [4,]    0 0.0000000 0.0000000 0.0000000
> 
> $F[[2]]
>      [,1]      [,2]      [,3]      [,4]
> [1,]    0 0.0000000 0.0000000 0.1724985
> [2,]    0 0.9937995 0.3449203 0.1724985
> [3,]    0 0.0000000 0.0000000 0.0000000
> [4,]    0 0.0000000 0.0000000 0.0000000
> 
> 
> $ahstages
>   stage_id    stage original_size original_size_b original_size_c min_age
> 1        4 DormSeed             0               0               0       0
> 2        1     Age1            NA              NA              NA       1
> 3        2     Age2            NA              NA              NA       2
> 4        3     Age3            NA              NA              NA       3
>   max_age repstatus obsstatus propstatus immstatus matstatus entrystage
> 1       0         0         1          1         1         0          1
> 2       1         1         1          0         0         1          1
> 3       2         1         1          0         0         1          0
> 4      NA         1         1          0         0         1          0
>   indataset binhalfwidth_raw sizebin_min sizebin_max sizebin_center
> 1         0                1         0.5         1.5              1
> 2         1               NA          NA          NA             NA
> 3         1               NA          NA          NA             NA
> 4         1               NA          NA          NA             NA
>   sizebin_width binhalfwidthb_raw sizebinb_min sizebinb_max sizebinb_center
> 1           0.5                 1          0.5          1.5               1
> 2            NA                NA           NA           NA              NA
> 3            NA                NA           NA           NA              NA
> 4            NA                NA           NA           NA              NA
>   sizebinb_width binhalfwidthc_raw sizebinc_min sizebinc_max sizebinc_center
> 1            0.5                 1          0.5          1.5               1
> 2             NA                NA           NA           NA              NA
> 3             NA                NA           NA           NA              NA
> 4             NA                NA           NA           NA              NA
>   sizebinc_width group       comments alive almostborn
> 1            0.5     0      new stage     1          0
> 2             NA     0 No description     1          0
> 3             NA     0 No description     1          0
> 4             NA     0 No description     1          0
> 
> $hstages
>   X1
> 1 NA
> 
> $agestages
>   X1
> 1 NA
> 
> $labels
>   pop patch year2
> 1   1     1  1989
> 2   1     1  1990
> 
> $dataqc
> [1] 345 531
> 
> $matrixqc
> [1] 10  6  2
> 
> $modelqc
>               vital_rate individuals transitions distribution  accuracy
> 1               survival         345         531     binomial 0.7608286
> 2            observation           0           0     binomial        NA
> 3                   size           0           0     gaussian        NA
> 4                  sizeb           0           0         <NA>        NA
> 5                  sizec           0           0         <NA>        NA
> 6           reproduction           0           0     binomial        NA
> 7              fecundity         345         531       negbin 0.6548547
> 8      juvenile_survival           0           0     binomial        NA
> 9    juvnile_observation           0           0     binomial        NA
> 10         juvenile_size           0           0     gaussian        NA
> 11        juvenile_sizeb           0           0         <NA>        NA
> 12        juvenile_sizec           0           0         <NA>        NA
> 13 juvenile_reproduction           0           0     binomial        NA
> 14     juvenile_maturity           0           0     binomial        NA
> 
> attr(,"class")
> [1] "lefkoMat"

The add_stage() function can be used to add new stages anywhere in the matrix, and can be used in stage-based ahistorical, historical, or age-by-stage MPMs, as well. For example, here we add a new juvenile stage to the age-by-stage Lathyrus MPM, lathmat2p_raw_ageedit. We will make this the new third stage, this time using the add_after option (this option can be used to add stages to the end of a matrix, as well).

lath_new_juv_mpm <- add_stage(lathmat2p_raw_ageedit, add_after = 2,
  stage_name = "NewJuv")
summary(lath_new_juv_mpm)
> 
> This ahistorical lefkoMat object contains 12 matrices.
> 
> Each matrix is square with 24 rows and columns, and a total of 576 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.399 0.000 0.450 0.000 0.842 0.000 0.599 0.000 0.562 0.000 0.000
> Mean    0.246 0.467 0.228 0.493 0.240 0.607 0.235 0.546 0.195 0.532 0.205 0.365
> 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

This summary suggests that three new rows and columns have been added. This reflects the fact that the stage structure of the stageframe is repeated in every age in an age-by-stage MPM. Since this object has three ages, that means three rows and columns have been added.

Let’s see the ahstages object first.

lath_new_juv_mpm$ahstages
>   stage_id  stage original_size original_size_b original_size_c min_age max_age
> 1        1     Sd             0              NA              NA       0      NA
> 2        2    Sdl           100              NA              NA       0      NA
> 3        8 NewJuv             0               0               0       0       0
> 4        3    VSm            13              NA              NA       0      NA
> 5        4     Sm           127              NA              NA       0      NA
> 6        5    VLa          3730              NA              NA       0      NA
> 7        7   Dorm             0              NA              NA       0      NA
> 8        6    Flo          3800              NA              NA       0      NA
>   repstatus obsstatus propstatus immstatus matstatus entrystage indataset
> 1         0         0          1         1         0          1         0
> 2         0         1          0         1         0          1         1
> 3         1         1          0         0         1          0         0
> 4         0         1          0         0         1          0         1
> 5         0         1          0         0         1          0         1
> 6         0         1          0         0         1          0         1
> 7         0         0          0         0         1          0         1
> 8         1         1          0         0         1          0         1
>   binhalfwidth_raw sizebin_min sizebin_max sizebin_center sizebin_width
> 1              0.0         0.0         0.0              0           0.0
> 2            100.0         0.0       200.0            100         200.0
> 3              1.0         0.5         1.5              1           0.5
> 4             11.0         2.0        24.0             13          22.0
> 5            103.0        24.0       230.0            127         206.0
> 6           3500.0       230.0      7230.0           3730        7000.0
> 7              0.5        -0.5         0.5              0           1.0
> 8           3800.0         0.0      7600.0           3800        7600.0
>   binhalfwidthb_raw sizebinb_min sizebinb_max sizebinb_center sizebinb_width
> 1                NA           NA           NA              NA             NA
> 2                NA           NA           NA              NA             NA
> 3                 1          0.5          1.5               1            0.5
> 4                NA           NA           NA              NA             NA
> 5                NA           NA           NA              NA             NA
> 6                NA           NA           NA              NA             NA
> 7                NA           NA           NA              NA             NA
> 8                NA           NA           NA              NA             NA
>   binhalfwidthc_raw sizebinc_min sizebinc_max sizebinc_center sizebinc_width
> 1                NA           NA           NA              NA             NA
> 2                NA           NA           NA              NA             NA
> 3                 1          0.5          1.5               1            0.5
> 4                NA           NA           NA              NA             NA
> 5                NA           NA           NA              NA             NA
> 6                NA           NA           NA              NA             NA
> 7                NA           NA           NA              NA             NA
> 8                NA           NA           NA              NA             NA
>   group       comments alive almostborn
> 1     0 No description     1          0
> 2     0 No description     1          0
> 3     0      new stage     1          0
> 4     0 No description     1          0
> 5     0 No description     1          0
> 6     0 No description     1          0
> 7     0 No description     1          0
> 8     0 No description     1          0

We see that we have a new stage, named NewJuv and marked as stage 8, in the third row of this stageframe.

Let’s now see the agestages object.

lath_new_juv_mpm$agestages
>    stage_id  stage age
> 1         1     Sd   1
> 2         2    Sdl   1
> 3         8 NewJuv   1
> 4         3    VSm   1
> 5         4     Sm   1
> 6         5    VLa   1
> 7         7   Dorm   1
> 8         6    Flo   1
> 9         1     Sd   2
> 10        2    Sdl   2
> 11        8 NewJuv   2
> 12        3    VSm   2
> 13        4     Sm   2
> 14        5    VLa   2
> 15        7   Dorm   2
> 16        6    Flo   2
> 17        1     Sd   3
> 18        2    Sdl   3
> 19        8 NewJuv   3
> 20        3    VSm   3
> 21        4     Sm   3
> 22        5    VLa   3
> 23        7   Dorm   3
> 24        6    Flo   3

Our new stage appears in rows 3, 11, and 19, corresponding to the rows and columns for that stage in the matrices.

Finally, let’s take a look at a single matrix.

lath_new_juv_mpm$A[[1]]
>        [,1]      [,2] [,3]      [,4]   [,5]  [,6] [,7]  [,8]  [,9] [,10] [,11]
>  [1,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 1.035 0.000     0     0
>  [2,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.162 0.000     0     0
>  [3,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>  [4,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>  [5,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>  [6,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>  [7,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>  [8,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>  [9,] 0.345 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [10,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [11,] 0.054 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [12,] 0.000 0.9047619    0 0.7142857 0.1875 0.250    0 0.000 0.000     0     0
> [13,] 0.000 0.0000000    0 0.1428571 0.6875 0.500    0 1.000 0.000     0     0
> [14,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [15,] 0.000 0.0000000    0 0.0000000 0.0625 0.125    0 0.000 0.000     0     0
> [16,] 0.000 0.0000000    0 0.0000000 0.0000 0.125    0 0.000 0.000     0     0
> [17,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [18,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.345     0     0
> [19,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.054     0     0
> [20,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [21,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [22,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [23,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
> [24,] 0.000 0.0000000    0 0.0000000 0.0000 0.000    0 0.000 0.000     0     0
>       [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
>  [1,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [2,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [3,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [4,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [5,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [6,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [7,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [8,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>  [9,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [10,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [11,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [12,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [13,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [14,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [15,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [16,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [17,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [18,]     0     0     0     0     0     0 0.345     0     0     0     0     0
> [19,]     0     0     0     0     0     0 0.054     0     0     0     0     0
> [20,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [21,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [22,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [23,]     0     0     0     0     0     0 0.000     0     0     0     0     0
> [24,]     0     0     0     0     0     0 0.000     0     0     0     0     0
>       [,24]
>  [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,]     0
> [21,]     0
> [22,]     0
> [23,]     0
> [24,]     0

Close inspection will reveal that rows and columns 3, 11, and 19 are empty, filled only with zeros. Happy editing!

16.6 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")
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 find other aspects of this approach useful, including the characterization of proxy stages and stage groups, just as in supplemental().

16.7 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.