How to calculate R₀ from an infection diagram

On this page

R0Calc is a free web calculator that computes the basic reproduction number R₀ of a compartmental epidemic model as a symbolic formula. You draw the model as an infection diagram, write each rate in LaTeX (β, σ, μ + γ…), and R0Calc returns R₀ in LaTeX together with the infection cycles that produce each term. You do not need to write equations or build matrices. If your model is written as a system of differential equations, the section on building the diagram from your equations shows how to translate each term into an arrow.

What is the basic reproduction number R₀?

The basic reproduction number R₀ (read "R nought", often written R0) is the average number of new infections that one infectious individual causes in a population where everyone is susceptible. It is the threshold of an epidemic model: if R₀ > 1 an infection can invade and spread, and if R₀ < 1 it dies out. For compartmental models R₀ is a formula in the model's parameters, such as β / γ in the simplest SIR model, so it shows which rates control the outbreak. In the SIR model, for example, a fraction 1 − 1/R₀ of the population must be immune to stop transmission. The standard way to obtain R₀ is the next-generation matrix (Diekmann, Heesterbeek and Metz, 1990; van den Driessche and Watmough, 2002), which requires separating new infections from other transitions and inverting a matrix by hand or with a computer algebra system.

What is an infection diagram?

An infection diagram is a graph that keeps only the part of an epidemic model that describes the infection itself: the infected compartments and the processes that bring individuals into that set, move them inside it, and take them out of it. Susceptible, vaccinated, immune or recovered compartments are not drawn, even though they appear in the transmission terms. The diagram is not a free sketch of the model: once the model is fixed, its diagram is fully determined by the equations of the infected compartments, so two people drawing it from the same model get the same graph.

Because it ignores demography and everything that happens outside the infected set, two models that differ in births, loss of immunity or vaccination schedules can share the same infection diagram. That makes the diagram a way to compare models by the structure of their transmission, not by their algebra.

Infected is not the same as infectious

A compartment belongs to the diagram if it is a stage an infected individual passes through, whether or not it transmits. An exposed class E that does not transmit yet is an infected compartment; it is drawn as a non-infectious node (blue). Compartments that generate new infections are infectious nodes (red). A variable that is not a group of people can also be an infected compartment when it takes part directly in transmission, for example free bacteria or virus in the environment. What decides is the role of the variable in the infection dynamics, not its name or biology. In the YAML, non-infectious infected compartments go under non_infectious_infested and infectious ones under infectious_infested.

The three kinds of arrows

Rates are written in LaTeX and can be any expression, such as \beta, \mu + \gamma or p \sigma. Two parallel arrows between the same compartments are added together.

How to build an infection diagram from a system of differential equations

Write the equation of each infected compartment xi as the sum of three kinds of terms:

dxidt=ℱi+𝒯i+𝒪i

LaTeX: \frac{dx_i}{dt} = \mathcal{F}_i + \mathcal{T}_i + \mathcal{O}_i

where ℱi collects the new infections, 𝒯i the transfers between infected compartments and 𝒪i the outflows from the infected set. Each group becomes one kind of arrow. Only the equations of the infected compartments are read; the equations of S, V, R and other uninfected compartments are used only to find their values at the disease-free equilibrium. If you know the next-generation matrix method, ℱ is what it calls F, and transfers and outflows together are what it calls V.

Read the meaning of each term, not only its sign

A positive term is not always a new infection: it can be a transfer arriving from another infected compartment. A negative term is not always an outflow: it can be a transfer leaving towards another infected compartment. Ask what process the term models. A transfer always appears twice, with a minus sign in the equation of the origin and a plus sign in the equation of the destination (−σE in dE/dt and +σE in dI/dt); the diagram joins the pair into a single arrow.

Four steps

  1. Choose the infected compartments. They are the nodes. Decide by role in the infection, not by name: an exposed class is in, a recovered class is out, a free pathogen can be in. Mark as infectious the ones that cause new infections.
  2. Draw the new infections. For every term that adds individuals to the infected set, draw a dashed arrow from the compartment that causes it to the compartment that receives them. Label it with the transmission parameter times the DFE value of the uninfected compartment in the term (β I S/N → β S*/N). External entries are arrows without a source.
  3. Draw the transfers. For every flow between two infected compartments, draw a solid arrow from origin to destination with its rate. If there are none, draw none: that also tells you something, namely that infected compartments are linked only through infection.
  4. Draw the outflows. For every term that removes individuals from the infected set, draw a solid arrow leaving the compartment, with its rate and no destination.

The same four steps work for a two-compartment model and for a model with many infected compartments; what changes is the diagram the model produces, not the rules. For a term-by-term walk-through, see the SEIR equations translated into their diagram.

How does R0Calc compute R₀?

R0Calc finds every infection cycle in the diagram: a closed path that leaves an infectious compartment, passes through new-infection and transfer arrows and returns to it. Each cycle is one route of transmission. Along a cycle, each compartment contributes the rate that brings individuals in divided by the total rate at which they leave (its transfers plus its outflows). The product of these ratios is the cycle's term; when a cycle passes through several infectious compartments, R0Calc takes the geometric mean per compartment, that is, the k-th root for k compartments. Cycles that share infectious compartments are combined into one value, and when the diagram has independent groups of cycles, R₀ is the largest of them. The View cycles page of the calculator shows each cycle, its term and how they combine into R₀, which helps explain the result in a thesis or a paper.

Step by step: R₀ of the SEIR model

  1. Open the calculator. From Add to diagram, add a Non-infectious compartment named E and an Infectious compartment named I.
  2. Add a New infection arrow: select I, then E, and label it \beta (β S*/N with S* = N; see how arrows are labelled).
  3. Add a Transfer arrow from E to I labelled \sigma.
  4. Add an Outflow arrow from E labelled \mu and another from I labelled \mu + \gamma.
  5. Choose Update YAML, the → button between the diagram and the YAML (or Ctrl+S). R₀ = βσ / ((σ + μ)(γ + μ)) appears under the diagram; Copy LaTeX copies it.

The same model is available in one click with Load this example in the YAML panel. The SEIR model page explains every term of the formula.

More examples: what the infection diagram shows

The same four steps apply to models with several groups, several transmission routes or a pathogen that lives outside its hosts. Two examples, each with its own page:

Two groups and imperfect vaccination

In an SIS model of a sexually transmitted infection with women and men and an imperfect vaccine (Saldaña et al., 2023), the infected compartments are only If and Im. Both susceptible and vaccinated people can be infected, so there are two dashed arrows in each direction, one per route, and no transfers.

Infection diagram of the two-group SIS model with vaccination: two dashed new-infection arrows from I_f to I_m, labelled β_fm S_m*/N_f and (1 − ε_m) β_fm V_m*/N_f, two dashed new-infection arrows from I_m to I_f, labelled β_mf S_f*/N_m and (1 − ε_f) β_mf V_f*/N_m, and outflow arrows α_f + d_f from I_f and α_m + d_m from I_m. No transfer arrows. βfm Sm*/Nf (1 − εm) βfm Vm*/Nf βmf Sf*/Nm (1 − εf) βmf Vf*/Nm αf + df αm + dm If Im
Infection diagram of the two-group SIS model with vaccination. Both nodes are infectious (red); dashed arrows are new infections, solid arrows are outflows.

What it shows: vaccination adds an entry route and lowers its rate, but it does not add any infected compartment or change the cycle If → Im → If. The diagram separates changes in the intensity of transmission from changes in its structure. Its R₀ is √(βfm βmf Af Am / ((αf + df)(αm + dm))), where Ak is the fraction of group k that vaccination leaves effectively susceptible.

Formula and derivation →

A pathogen in the environment

In a model of leptospirosis in humans and animals (Baca-Carrasco, Olmos and Barradas, 2015), the infected compartments are infected animals IA, infected humans IH and free bacteria in the environment B. B is not a population, but it causes new infections directly, so it is a node. Bacterial shedding by animals and humans is drawn as new-infection arrows into B.

Infection diagram of the leptospirosis model: three infectious nodes, infected animals I_A, infected humans I_H and free bacteria B. Dashed new-infection arrows: a loop from I_A to itself (c_1 S_A*), B to I_A (c_2 S_A*), I_A to I_H (c_3 S_H*), B to I_H (c_4 S_H*), I_A to B (c_5) and I_H to B (c_6). Outflow arrows: α_2 from I_A, α_1 from I_H and k from B. No transfer arrows. c1 SA* c5 c2 SA* c6 c4 SH* c3 SH* α2 α1 k IA IH B
Infection diagram of the human–animal leptospirosis model. All three nodes are infectious (red), including the bacteria B; the loop on IA is animal-to-animal transmission.

What it shows: several transmission routes at once (animal to animal, animal to human, through the environment) and a node that is not a population, all with the same four steps. Here R₀ is the largest root of a cubic equation built from the four infection cycles of the diagram.

Formula and derivation →

Common mistakes when drawing an infection diagram

Most wrong R₀ formulas come from a diagram that mixes up what an arrow means. Check yours against this list:

Writing a model in YAML

Every diagram has an equivalent YAML description, shown next to the canvas. You can edit it, paste a model, open a .yaml file or download the current one. The keys are:

KeyContent
non_infectious_infestedNames of the non-infectious infected compartments (E…)
infectious_infestedNames of the infectious compartments (I…)
new_infections_arrowsList of from, to, param
transfer_arrowsList of from, to, param
outs_arrowsList of from, param

Empty lists can be written as [].

non_infectious_infested:
  - E
infectious_infested:
  - I
new_infections_arrows:
  - from: I
    to: E
    param: \beta
transfer_arrows:
  - from: E
    to: I
    param: \sigma
outs_arrows:
  - from: E
    param: \mu
  - from: I
    param: \mu + \gamma

R0Calc compared with other ways to get R₀

Unlike next-generation matrix calculators, which ask you to type the matrices F and V and usually return a number, R0Calc starts from a drawing of the model and returns R₀ as a symbolic formula. Unlike tools that estimate R₀ from case counts, it works from the model structure, before any data exists.

R0CalcNext-generation matrix by hand or with a CASEstimation from case data
InputInfection diagram or YAMLMatrices F and VDaily or weekly case counts
OutputSymbolic formula in LaTeXFormula or numberA number with uncertainty
Shows each transmission routeYes, one term per infection cycleNoNo
Needs programmingNo, runs in the browserAlgebra, or Maple, Mathematica, SymPy or RUsually R or Python
Best forDesigning and teaching models, writing up derivationsFormal proofsDescribing a real outbreak

Frequently asked questions

Is R0Calc free?

Yes. R0Calc is free and runs in the browser. You can use it without an account; signing up lets you keep named diagrams under My diagrams and share them by link.

Does R0Calc estimate R₀ from case data?

No. R0Calc derives R₀ as a formula from the structure of a compartmental model. Estimating a number from an epidemic curve needs a statistical method, such as the exponential growth rate with a generation-interval distribution (Wallinga and Lipsitch, 2007).

Does R0Calc give the same R₀ as the next-generation matrix?

For standard models such as SIR and SEIR it returns the same expressions as the next-generation matrix method, βσ / ((σ + μ)(γ + μ)) for SEIR for example, without you writing the matrices F and V.

Can I export the R₀ formula?

Yes. Copy LaTeX copies R₀ ready to paste into a paper or a LaTeX document, and the model itself can be downloaded and opened again as a YAML file.

How large can a model be?

Any number of compartments can be drawn, but the number of infection cycles grows very fast in densely connected models. Each calculation is stopped after 15 seconds; models with several infectious compartments that are all connected to each other can reach that limit.

Is an infection diagram the same as a compartmental flow diagram?

No. A flow diagram shows every compartment and every flow of the model. An infection diagram keeps only the infected compartments, and it separates new infections (dashed arrows) from transfers and outflows (solid arrows), which a flow diagram does not.

Why are susceptible and recovered compartments not in the diagram?

R₀ depends only on what happens inside the infected set near the disease-free equilibrium. Uninfected compartments enter only through their equilibrium values, which appear in the labels of the new-infection arrows (for example S*/N).

Can a pathogen in the environment be a node?

Yes. If a variable such as free bacteria or virus causes new infections directly, it is an infected compartment and is drawn as a node, even though it is not a population of hosts. See the leptospirosis model.

How do I label a new-infection arrow?

Multiply the transmission parameter by the disease-free equilibrium value of the uninfected compartment in that term: β I S/N becomes β S*/N, which is β when S* = N.

References