中文 | Español | English | português | Turkish

# Set default parameters for R chunks in Rmd
knitr::opts_chunk$set(echo = FALSE, warning = FALSE)

# Load the ecoevoapps package
library(ecoevoapps)
library(ggplot2)

Some populations are best modeled as a collection of individuals in distinct age classes (Tenhumberg, 2010). The growth of such structured populations depends on the transition rates from one age class to another. For example, the survival rates of juveniles into young adults may be different from the survival rate of young adults into older adults. We can model the dynamics of such populations by knowing the rates of survival from one age class into the next, and by knowing how individuals in each age contribute to the population by giving birth to new individuals.

The Leslie matrix

The Leslie Matrix is a convenient way of organizing age transition rates (survival and fecundity). The Leslie matrix is an $NxN$ matrix, where $N$ indicates the total number of ages in the population cycle. The elements of the matrix tell us about how the individuals in each age column contribute to each age row. For example, the second column in the first row tells us how individuals who are of age 2 contribute to the age 1 category.

In addition to being a convenient way to account for survival and fecundity information, the matrix also has three key properties:

  1. To simulate the population at the next time step (time $t+1$), one simply needs to multiply the Leslie matrix by the matrix with the initial population sizes (at time $t$).

  2. The largest eigenvalue of the Leslie matrix is $\lambda$, the asymptotic growth rate for the whole population. When $\lambda < 1$ the population declines to extinction, $\lambda > 1$ the population grows exponentially, and when $\lambda = 1$ the population size remains constant through time).

  3. The eigenvector that corresponds to the largest eigenvalue gives the stable population structure, i.e., the proportions of each age class after the population reached equilibrium.

Use the app below to explore these properties of structured population growth:

### APP
fluidPage(
  ## INPUT
  sidebarPanel(

    # Initial sizes
    numericInput(inputId = "N1",
                  label = "Initial number of individuals at age 1",
                  min = 0,
                  max = NA,
                  value = 100),
    numericInput(inputId = "N2",
                   label = "Initial number of individuals at age 2",
                   min = 0,
                   max = NA,
                   value = 100),
    numericInput(inputId = "N3",
                   label = "Initial number of individuals at age 3",
                   min = 0,
                   max = NA,
                   value = 100),

    # Leslie matrix
    numericInput(inputId = "F1",
                  label = "Fecundity at age 1",
                  min = 0,
                  max = NA,
                  value = 0),
    numericInput(inputId = "F2",
                  label = "Fecundity at age 2",
                  min = 0,
                  max = NA,
                  value = 8),          
    numericInput(inputId = "F3",
                  label = "Fecundity at age 3",
                  min = 0,
                  max = NA,
                  value = 1),
    sliderInput(inputId = "S12",
                  label = "Survival from age 1 to 2 (%)",
                  min = 1,
                  max = 100,
                  value = 40),
    sliderInput(inputId = "S23",
                  label = "Survival from age 2 to 3 (%)",
                  min = 0,
                  max = 100,
                  value = 80),
    width=4),

  ## OUTPUT
  mainPanel(
  tabsetPanel(type = "pills",
              tabPanel("Population diagram and Leslie matrix",
                       renderPlot({ diagr() }, width = 500),
                       br(),
                       column(width = 5,
                              style='border-right: 0.02px solid grey',
                              br(),
                              h4("Leslie matrix"),
                              renderTable({ Leslie_to_print() })),
                       column(width = 7,
                              h4("Eigen analysis"),
                              em(strong("Asymptotic population growth rate (lambda)")),
                              renderText({ eigenval() }),
                              br(), # br(), br(),
                              em(strong("Stable population structure")),
                              renderTable({ eigenvec() })
                       )),
              tabPanel("Population Trajectories",
                       h4("Population trajectory of each age class"),
                       renderPlot({ plot_pop_trajectory() }),
                       h4("Trajectory of the age distribution"),
                       renderPlot({ plot_pop_agedist() }),
                       h4("Trajectory of the overall population growth rate"),
                       renderPlot({ plot_pop_lambda() }))
  )
  )
)


## Leslie matrix
Leslie_matrix <- reactive({
  matrix(
      c(input$F1,input$F2, input$F3,
        input$S12/100,0,0,
        0, input$S23/100,0),
      ncol = 3, byrow = T
      )
  })

Leslie_to_print <- reactive({
  ll <- Leslie_matrix()
  colnames(ll) <- paste("Age",1:3)
  ll
})

init <- reactive({
  c(input$N1,input$N2,input$N3)
})

## Largest eigenvalue
eigenval <- reactive({
  paste0("First Eigenvalue of the Leslie matrix = ",round(eigen(Leslie_matrix())$values,2)[1])
  })

## Eigenvector corresponding to largest eigenvalue
tmp <- reactive({
  round(eigen(Leslie_matrix())$vectors[,1]*100/
          sum(eigen(Leslie_matrix())$vectors[,1]))
})

eigenvec <- reactive({
  matrix(
    c("Eigenvector", round(eigen(Leslie_matrix())$vectors[,1],2),
      "%", tmp()[1], tmp()[2], tmp()[3]),
    ncol=4, byrow=T,
    dimnames = list(NULL, c(" ","Age 1","Age 2", "Age 3"))
  )
})


diagr <- reactive({ 
  plot_leslie_diagram(Leslie_matrix())
})

# Simulation plots

pop_trajectory <- reactive({
  run_structured_population_simulation(leslie_mat = Leslie_matrix(), 
                                       init = init(), time = 1000)
})

plot_pop_trajectory <- reactive({
  plot_structured_population_size(pop_trajectory()) +
    labs(title = "") +
    theme(legend.text = element_text(size = 13),
          legend.title = element_text(size = 13))
})

plot_pop_agedist <- reactive({
  plot_structured_population_agedist(pop_trajectory(), 
                                     leslie_mat = Leslie_matrix())+
    labs(title = "") +
    theme(legend.text = element_text(size = 13),
          legend.title = element_text(size = 13),
          plot.caption = element_text(size = 13))
})

plot_pop_lambda <- reactive({
  plot_structured_population_lambda(pop_trajectory(), 
                                    leslie_mat = Leslie_matrix()) +
    labs(title = "") +
    theme(legend.text = element_text(size = 13),
          legend.title = element_text(size = 13),
          plot.caption = element_text(size = 13))
})

suppressWarnings(ecoevoapps::print_app_footer())


gauravsk/ecoevoapps documentation built on July 9, 2024, 9:37 p.m.