Valeria Trivellone, Sabrina B.L. Araujo, Bernd Panassiti

created: 15.04.2021, last modified: 05.03.2023


The ‘HostSwitch’ package uses an individual-based model to simulate dispersion and host switch events by a consumer. As a consequence to the adaptation to a new resource, the consumer may expand its host range. The HostSwitch R package provides functions to explore host switch events on a novel resource over the time. Based on the Individual-Based Model (IBM) published in Araujo et al. (2015), the main function of this R package simulates the colonization dynamics of consumers over resources that are randomly offered. The HostSwitch package was implemented by including all functionalities in the original IBM model as well as numerous additions, such as comparison and evaluation of simulations with several customized parameter options and an in-depth description of the parameters in an ecological context.

We aim to reach a broader audience by providing an interactive visualization of the model and linking the theoretical model to real world scenarios. The dataset (“data$parli”) provided with the present package provide a useful series of example with some parameters retrived from the literature.

Theoretical background

The evolution of the biological associations may include several concatenated events of speciation affecting one or both species and it is driven by four main processes: cospeciation, host switch, failure to speciate and “missing the boat” (Page 2002). While the prevalent paradigm wants the cospeciation to be the main process driving the coevolution of most of the biological associations, recent evidence showed that host-switching, favored by the intrinsic capability of adaptation in the sub-optimal environment, may explain rapid novel associations (i.e. colonization of novel hosts at the ecological time scale) eventually followed by speciation (at the evolutionary time) as well the observed incongruences of the paired phylogenies (see (Brooks, Hoberg, and Boeger 2019) for a review).


The simulation model of HostSwitch aims to measure the dynamics of the host switch (extent and frequency) in the population of an organism (hereafter Consumer) that interacts with current and potential hosts (hereafter Resource) over the generations. A host switch implies that a Consumer may colonize new Resource, which in turn imposes selection pressure that impacts the Consumers’ survival. The host-switching relies on a mechanism of ecological readjustment or ecological fitting, i.e. the capability of the Consumer to use a similar Resource even if sub-optimal (Janzen 1985) (Agosta and Klemens 2008). The fundamental aspect of the HostSwitch simulation model is to track, summarize and compare the dispersion and successful host switch events in a new Resource by the populations of the Consumer.

The HostSwitch package offers simple functions to evaluate the role of the single parameters included in the model in defining the probability and the amplitude of the host switch. The extension in R code allows the users to choose among different consumer-resource scenarios which accommodate all the symbiotic (sensu lato) associations.

This package will be expanded with further scenarios of biological interactions. Please feel free to contact the authors to contribute by making suggestions, or sending bug fixes or extension proposals.

Using HostSwitch

Installing, loading and citing the package

Install the HostSwitch package from CRAN


Install the recent release of HostSwitch package using GitHub

devtools::install_github(repo = "berndpanassiti/HostSwitch",build_vignettes = TRUE)

Loading and citation

## Um Paket 'HostSwitch' in Publikationen zu zitieren, nutzen Sie bitte:
##   Trivellone V, Araujo SB, Panassiti B (????). _HostSwitch: Simulate
##   the Extent of Host Switching by Consumers_. R package version 0.1.3,
##   <>.
## Ein BibTeX-Eintrag für LaTeX-Benutzer ist
##   @Manual{,
##     title = {HostSwitch: Simulate the Extent of Host Switching by Consumers},
##     author = {Valeria Trivellone and Sabrina B.L. Araujo and Bernd Panassiti},
##     note = {R package version 0.1.3},
##     url = {},
##   }

Interactive plot using Shiny


This function opens a new window with an interactive plot of the HostSwitch simulation model. This plot can be used to interact with the model and explore how different settings affect dispersion and host switch events by a consumer.

Run the model

Simulate dispersal and host switch events by a consumer

# using only 20 generations and 1 simulation, others arguments left to default values
# see ?simHostSwitch for more arguments
simulated_quantities = simHostSwitch(seed=123,n_sim=1,n_generation=20) 

The function simHostSwitch generates a list containing all quantities of interest which can be used for summary statistics or plots. Quantities of interest are optimum phenotypes that Consumers should have to be favored by the current Resource (pRes_sim), that Consumers should have to be favored by the novel Resource (pRes_new_sim), individual phenotype values of the Consumers (pInd), number of migrating individuals at each generation (pInd_jump_sim), individual phenotype values of the Consumers who disperse in a novel Resource (pInd_whichjump_sim), and individual phenotype values of the Consumers who successful colonize a novel Resource (pInd_whichsurv_sim). These parameters are available for each generation step.

Summary of a HostSwitch object

## An object of class summaryHostSwitch
## Summary of HostSwitch simulations
## General settings of individual based model:
## K:100, b:10, mig:0.01, sd:0.2, sigma:1, pRes_min:1, pRes_max:10
## n_generations:20, jump_back:no, seed:123, n_sim:1, warmup:1, nInitConsumer:20
## Summary of phenotypes:
##          Value (simulation average)
## pRes                           6.31
## pRes_new                       5.48
## pInd                           5.86
## Summary of host switches by consumers:
##                                     Value (simulation average)
## Total events of dispersion:                                 11
## Number of successful host switches:                          3

The function summaryHostSwitch creates a summary of basic statistics for phenotypes, dispersion and host switch events.

Plot one HostSwitch simulation

gg1 = plotHostSwitch(simulated_quantities, sim_n = 1) # use n_sim to select a specific simulation

#gg1 + xlim(0,10) # limit x-axis from 0 to 10 generations

The S3 method plotHostSwitch function graphically summarize the simulated output.

Comparing two HostSwitch objects

To investigate the effect of selected parameters on dispersal and host switch events, the testHostSwitch function can be used to compare estimated quantities from two HostSwitch objects.

The three estimated quantities are: j total number of dispersing events; s total number of successful host switch events; *d distance between the pRes_sim andpRes_new_sim for the generations where a successful host switch occurs, or phenotype distance.

In the example below, we investigated how different values of net reproduction rate (parameter b) affect the average number of dispersal and host switch events to a new resource by a consumer.
First, we created two HostSwitch objects with different b (m1 and m2), other parameters set to default values. Second, we compared m1 and m2 choosing the estimated quantity of interest. If plot = TRUE a boxplot will be generated in the plot pane in RStudio.

# see ?simHostSwitch for more arguments
m1 = simHostSwitch(seed=123,n_sim=100,b=10) 
m2 = simHostSwitch(seed=123,n_sim=100,b=15) 

# see ?testHostSwitch for more arguments
## An object of class testHostSwitch
## Test result comparing 2 HostSwitch simulations using Welch Two Sample t-test 
## t: -3.636677, df:196.6477, p.value:0.0003527228

Note, to test for differences between HostSwitch objects a reasonable high number of simulations is recommended (e.g., n_sim >= 100).


Agosta, Salvatore J, and Jeffrey A Klemens. 2008. “Ecological Fitting by Phenotypically Flexible Genotypes: Implications for Species Associations, Community Assembly and Evolution.” Ecology Letters 11 (11): 1123–34.
Araujo, Sabrina B. L., Mariana Pires Braga, Daniel R. Brooks, Salvatore J. Agosta, Eric P. Hoberg, Francisco W. von Hartenthal, and Walter A. Boeger. 2015. “Understanding Host-Switching by Ecological Fitting.” PLOS ONE 10 (10): e0139225.
Brooks, Daniel R, Eric P Hoberg, and Walter A Boeger. 2019. The Stockholm Paradigm: Climate Change and Emerging Disease. University of Chicago Press.
Janzen, Daniel H. 1985. “On Ecological Fitting.” Oikos 45 (3): 308–10.
Page, Roderic DM. 2002. Tangled Trees: Phylogeny, Cospeciation, and Coevolution. University of Chicago Press.