Skip to content

Commit

Permalink
added extra.R to ensure document() builds ok
Browse files Browse the repository at this point in the history
  • Loading branch information
ben18785 committed Aug 12, 2024
1 parent d2107f9 commit 0d93ce1
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 29 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^pkgdown$
^\.github$
^vignettes/articles$
^README\.Rmd$
62 changes: 62 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
exclusions <- "src/stanExports_epifilter_covariates.h|src/stanExports_epifilter.h"
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
file: ./cobertura.xml
plugin: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(fit_epifilter)
export(fit_epifilter_covariates)
export(simulate_renewal_epidemic)
exportPattern("^[[:alpha:]]+")
import(Rcpp)
import(methods)
importFrom(rstan,sampling)
useDynLib(epidp, .registration = TRUE)
useDynLib(epidp)
6 changes: 6 additions & 0 deletions R/extra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#' @useDynLib epidp
#' @import Rcpp
#' @import methods
#' @importFrom rstan sampling
#' @exportPattern "^[[:alpha:]]+"
NULL
5 changes: 3 additions & 2 deletions R/simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ generate_vector_serial <- function(nt, mean_si, sd_si) {
#' rt_fun <- function(t) { 1.5 * exp(-0.05 * t) }
#' simulate_renewal_epidemic(rt_fun, 100, 5, 2, 10)
#' }
#' @export
simulate_renewal_epidemic <- function(Rt_fun, nt, mean_si, sd_si, i_0, X=NULL){

# Input validation
Expand Down Expand Up @@ -109,9 +110,9 @@ simulate_renewal_epidemic <- function(Rt_fun, nt, mean_si, sd_si, i_0, X=NULL){
Rt <- vector(length = nt)
for(i in seq_along(Rt)) {
if(is.null(X))
Rt[i] = rt_fun(t[i])
Rt[i] = Rt_fun(t[i])
else
Rt[i] <- rt_fun(t[i], X[i, ])
Rt[i] <- Rt_fun(t[i], X[i, ])
}

# Total infectiousness and incidence with initial imports
Expand Down
112 changes: 112 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# epidp

<!-- badges: start -->
[![codecov](https://codecov.io/github/ben18785/epidp/graph/badge.svg?token=STG0INT235)](https://codecov.io/github/ben18785/epidp)
<!-- badges: end -->

The goal of `epidp` is to allow covariate information to inform estimates of the time-varying reproduction number, $R_t$.

## Installation

You can install the development version of epidp from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("ben18785/epidp")
```

## Example
### Step function in $R_t$
We first generate case data assuming a step function for $R_t$.
```{r}
library(epidp)
library(ggplot2)
library(dplyr)
library(magrittr)
library(purrr)
library(tidyr)
rt_fun = function(t){
if(t <= 60)
R = 2
else if (t <= 90)
R = 0.5
else
R = 1
R
}
# simulation parameters
nt <- 200
mean_si <- 6.5
sd_si <- 4.03
i_0 <- 10
# data frame of outputs
epidemic_df <- simulate_renewal_epidemic(rt_fun, nt, mean_si, sd_si, i_0)
# plot
transform_factor <- 300
epidemic_df %>%
select(-c(w_dist, lambda_t)) %>%
mutate(R_t = R_t * transform_factor) %>%
pivot_longer(c(i_t, R_t)) %>%
ggplot(aes(x = t, y = value, colour = name)) +
geom_line() +
scale_y_continuous(
name = "Incidence (i_t)",
sec.axis = sec_axis(~ . / transform_factor, name = "Reproduction Number (R_t)")
) +
labs(x = "Time") +
theme_minimal() +
scale_color_brewer("Series", palette = "Dark2")
```

We now use a Stan version of EpiFilter to estimate the maximum a posteriori estimates
of $R_t$ and overlay these on top of the actual values. Note, these estimates do
not have uncertainty associated with them but the benefit of this is that estimation
is instantaneous. The estimates are close to the actual $R_t$ values after an initial
period when case counts are low.
```{r}
# fit model
fit <- fit_epifilter(
N=length(epidemic_df$i_t),
C=epidemic_df$i_t,
w=epidemic_df$w_dist,
is_sampling=FALSE,
as_vector=FALSE
)
# plot
R <- fit$par$R
epidemic_df %>%
mutate(estimated=R) %>%
rename(true=R_t) %>%
select(t, estimated, true) %>%
pivot_longer(c(estimated, true)) %>%
ggplot(aes(x=t, y=value)) +
geom_line(aes(colour=name)) +
scale_color_brewer("R_t", palette = "Dark2") +
ylab("R_t") +
xlab("Time") +
theme_minimal()
```

## Contributing guidelines
We welcome contributions from collaborators. Before doing so, we ask that you read
our [contributing guidelines](contributing-guidelines.md) section.
77 changes: 61 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# epidp

The goal of `epidp` is to allow covariate information to inform estimates of the time-varying reproduction number, $R_t$.
<!-- badges: start -->

[![codecov](https://codecov.io/github/ben18785/epidp/graph/badge.svg?token=STG0INT235)](https://codecov.io/github/ben18785/epidp)
<!-- badges: end -->

The goal of `epidp` is to allow covariate information to inform
estimates of the time-varying reproduction number, $R_t$.

## Installation

You can install the released version of `epidp` with:
You can install the development version of epidp from
[GitHub](https://github.com/) with:

``` r
devtools::install_github(ben18785/epidp)
# install.packages("devtools")
devtools::install_github("ben18785/epidp")
```

## Example

### Step function in $R_t$

We first generate case data assuming a step function for $R_t$.
```{r}

``` r
library(epidp)
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(magrittr)
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#>
#> set_names
library(tidyr)
#>
#> Attaching package: 'tidyr'
#> The following object is masked from 'package:magrittr':
#>
#> extract

rt_fun = function(t){
if(t <= 60)
Expand All @@ -41,25 +73,32 @@ i_0 <- 10
epidemic_df <- simulate_renewal_epidemic(rt_fun, nt, mean_si, sd_si, i_0)

# plot
transform_factor <- 300
epidemic_df %>%
select(-c(w_dist, lambda_t)) %>%
mutate(R_t = R_t * transform_factor) %>%
pivot_longer(c(i_t, R_t)) %>%
ggplot(aes(x = t, y = value, colour = name)) +
geom_line() +
scale_y_continuous(
name = "Incidence (i_t)",
sec.axis = sec_axis(~ . / 1000, name = "Reproduction Number (R_t)")
sec.axis = sec_axis(~ . / transform_factor, name = "Reproduction Number (R_t)")
) +
labs(x = "Time", colour = "Variable") +
theme_minimal()
labs(x = "Time") +
theme_minimal() +
scale_color_brewer("Series", palette = "Dark2")
```

We now use a Stan version of EpiFilter to estimate the maximum a posteriori estimates
of $R_t$ and overlay these on top of the actual values. Note, these estimates do
not have uncertainty associated with them but the benefit of this is that estimation
is instantaneous. The estimates are close to the actual $R_t$ values after an initial
period when case counts are low.
```{r}
<img src="man/figures/README-unnamed-chunk-2-1.png" width="100%" />

We now use a Stan version of EpiFilter to estimate the maximum a
posteriori estimates of $R_t$ and overlay these on top of the actual
values. Note, these estimates do not have uncertainty associated with
them but the benefit of this is that estimation is instantaneous. The
estimates are close to the actual $R_t$ values after an initial period
when case counts are low.

``` r
# fit model
fit <- fit_epifilter(
N=length(epidemic_df$i_t),
Expand All @@ -79,9 +118,15 @@ epidemic_df %>%
ggplot(aes(x=t, y=value)) +
geom_line(aes(colour=name)) +
scale_color_brewer("R_t", palette = "Dark2") +
ylab("R_t")
ylab("R_t") +
xlab("Time") +
theme_minimal()
```

<img src="man/figures/README-unnamed-chunk-3-1.png" width="100%" />

## Contributing guidelines
We welcome contributions from collaborators. Before doing so, we ask that you read
our [contributing guidelines](contributing-guidelines.md) section.

We welcome contributions from collaborators. Before doing so, we ask
that you read our [contributing guidelines](contributing-guidelines.md)
section.
Binary file added man/figures/README-unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 12 additions & 7 deletions man/fit_epifilter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0d93ce1

Please sign in to comment.