# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R
#     name: ir
# ---

# + [markdown] colab_type="text" id="view-in-github"
# <a href="https://colab.research.google.com/github/hongqin/Use-R-in-CoLab/blob/master/test_EpiEstim_colab.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="yaU2RH1eTY7v"
# # Test EpiEstim

# + [markdown] id="Sj4Jj74HTZne"
# https://cran.r-project.org/web/packages/EpiEstim/index.html
#
# https://cran.r-project.org/web/packages/EpiEstim/vignettes/demo.html
#

# + id="zlJqP9QCTUyu"
install.packages("EpiEstim", lib='/content')
install.packages("incidence", lib='/content')

library(EpiEstim, lib.loc='/content')
library(incidence, lib.loc='/content')

library(ggplot2)

# + id="yxFc1CddWjKQ"
data(Flu2009)

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="0cIfonoHXh0V" outputId="ba9c4666-a144-41ad-e819-9d54a4a6fcf2"
## incidence:
head(Flu2009$incidence)

# + colab={"base_uri": "https://localhost:8080/", "height": 35} id="DlaPrk09Xh3O" outputId="e408f737-b26e-43b0-95b4-6529a032288b"
## serial interval (SI) distribution:
Flu2009$si_distr

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="lW9NMKLMXh6P" outputId="bae64d31-e0db-4c63-9341-432d4efe7310"
## interval-ceonsored serial interval data:
## each line represents a transmission event, 
## EL/ER show the lower/upper bound of the symptoms onset date in the infector
## SL/SR show the same for the secondary case
## type has entries 0 corresponding to doubly interval-censored data
## (see Reich et al. Statist. Med. 2009).
head(Flu2009$si_data)


# + colab={"base_uri": "https://localhost:8080/", "height": 437} id="BiAidnA8Xh94" outputId="41e09ba8-4540-4d58-deb6-913b3039733b"
plot(as.incidence(Flu2009$incidence$I, dates = Flu2009$incidence$dates))

# + [markdown] id="wo_3lbS8Z6Nu"
# We can run estimate_R on the incidence data to estimate the reproduction number R. For this, we need to specify i) the time window(s) over which to estimate R and ii) information on the distribution of the serial interval.
#
# For i), the default behavior is to estimate R over weekly sliding windows. This can be changed through the config$t_start and config$t_end arguments (see below, “Changing the time windows for estimation”). For ii), there are several options, specified in the method argument.
#
# The simplest is the parametric_si method, where you only specify the mean and standard deviation of the SI.

# + [markdown] id="t1UKQInuZ-Yc"
# # Estimating R on sliding weekly windows, with a parametric serial interval
# In this example, we only specify the mean and standard deviation of the serial interval. In that case an offset gamma distribution is used for the serial interval. In the following example, we use the mean and standard deviation of the serial interval for flu from Ferguson et al., Nature, 2005:

# + colab={"base_uri": "https://localhost:8080/", "height": 69} id="jACCIuMNZzik" outputId="c99e1ceb-75b2-4ad4-d02a-06ff6179c46a"
res_parametric_si <- estimate_R(Flu2009$incidence, 
                                method="parametric_si",
                                config = make_config(list(
                                  mean_si = 2.6, 
                                  std_si = 1.5))
)

# + colab={"base_uri": "https://localhost:8080/", "height": 224} id="xwoB6RfyZzlT" outputId="1419865e-5bcf-4795-e1af-b6e0be54e3ef"
head(res_parametric_si$R)

# + colab={"base_uri": "https://localhost:8080/", "height": 437} id="SfrEWBjWZzoS" outputId="aa851096-a88e-48d0-bd4b-92c9db981a0d"
plot(res_parametric_si, legend = FALSE)

# + id="ZQ7ytSTnZzrT"


StarcoderdataJupyterScriptsDedupFiltered