Skip to content

Commit 2d981da

Browse files
authored
Merge pull request #1 from cmu-delphi/pkg-init
Just initialize the R package
2 parents 4ba2be7 + e2c9958 commit 2d981da

11 files changed

+198
-0
lines changed

.Rbuildignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^epipredict\.Rproj$
2+
^\.Rproj\.user$
3+
^LICENSE\.md$

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata

DESCRIPTION

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Package: epipredict
2+
Title: Basic epidemiology forecasting methods
3+
Version: 0.0.1
4+
Authors@R:
5+
c(
6+
person(given = "Jacob",
7+
family = "Bien",
8+
role = "aut"),
9+
person(given = "Daniel",
10+
family = "McDonald",
11+
role = "aut"),
12+
person(given = "Ryan",
13+
family = "Tibshirani",
14+
role = c("aut", "cre"),
15+
email = "[email protected]"))
16+
Description: What the package does (one paragraph).
17+
License: MIT + file LICENSE
18+
Encoding: UTF-8
19+
Roxygen: list(markdown = TRUE)
20+
RoxygenNote: 7.1.2
21+
Imports:
22+
dplyr,
23+
magrittr,
24+
data.table

LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YEAR: 2022
2+
COPYRIGHT HOLDER: epipredict authors

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2022 epipredict authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export("%>%")
4+
importFrom(magrittr,"%>%")

R/prob_ar.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
prob_ar <- function(y, lags = c(0, 7, 14), ahead = 7, min_train_window = 20,
2+
lower_level = 0.05, upper_level = 0.95, symmetrize = TRUE,
3+
nonneg = TRUE) {
4+
# Return NA if insufficient training data
5+
if (length(y) < min_train_window + max(lags) + ahead) {
6+
return(data.frame(point = NA, lower = NA, upper = NA))
7+
}
8+
9+
# Build features and response for the AR model
10+
dat <- do.call(
11+
data.frame,
12+
purrr::map(lags, function(lag) dplyr::lag(y, n = lag))
13+
)
14+
dat$y <- dplyr::lead(y, n = ahead)
15+
16+
# Now fit the AR model and make a prediction
17+
obj <- lm(y ~ ., data = dat)
18+
point <- predict(obj, newdata = tail(dat, 1))
19+
20+
# Compute a band
21+
r <- residuals(obj)
22+
s <- ifelse(symmetrize, -1, NA) # Should the residuals be symmetrized?
23+
q <- quantile(c(r, s * r), probs = c(lower_level, upper_level), na.rm = TRUE)
24+
lower <- point + q[1]
25+
upper <- point + q[2]
26+
27+
# Clip at zero if we need to, then return
28+
if (nonneg) {
29+
point = max(point, 0)
30+
lower = max(lower, 0)
31+
upper = max(upper, 0)
32+
}
33+
return(data.frame(point = point, lower = lower, upper = upper))
34+
}

R/prob_arx.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
prob_arx <- function(x, y, geo_value, time_value, lags = c(0, 7, 14),
2+
ahead = 7, min_train_window = 20, lower_level = 0.05,
3+
upper_level = 0.95, symmetrize = TRUE, nonneg = TRUE) {
4+
# Return NA if insufficient training data
5+
if (length(y) < min_train_window + max(lags) + ahead) {
6+
return(data.frame(point = NA, lower = NA, upper = NA))
7+
}
8+
9+
# Useful transformations
10+
if (!missing(x)) x <- data.frame(x, y)
11+
else x <- data.frame(y)
12+
if (!is.list(lags)) lags <- list(lags)
13+
lags = rep(lags, length.out = ncol(x))
14+
15+
# Build features and response for the AR model, and then fit it
16+
dat <- do.call(
17+
data.frame,
18+
unlist( # Below we loop through and build the lagged features
19+
purrr::map(1:ncol(x), function(i) {
20+
purrr::map(lags[[i]], function(lag) dplyr::lag(x[,i], n = lag))
21+
}),
22+
recursive = FALSE
23+
)
24+
)
25+
dat$y <- dplyr::lead(y, n = ahead)
26+
obj <- lm(y ~ ., data = dat)
27+
28+
# Use LOCF to fill NAs in the latest feature values, make a prediction
29+
data.table::setnafill(dat, type = "locf")
30+
dat <- cbind(dat, data.frame(geo_value, time_value))
31+
point <- predict(obj, newdata = dat %>%
32+
dplyr::group_by(geo_value) %>%
33+
dplyr::filter(time_value == max(time_value)))
34+
35+
# Compute a band
36+
r <- residuals(obj)
37+
s <- ifelse(symmetrize, -1, NA) # Should the residuals be symmetrized?
38+
q <- quantile(c(r, s * r), probs = c(lower_level, upper_level), na.rm = TRUE)
39+
lower <- point + q[1]
40+
upper <- point + q[2]
41+
42+
# Clip at zero if we need to, then return
43+
if (nonneg) {
44+
point = pmax(point, 0)
45+
lower = pmax(lower, 0)
46+
upper = pmax(upper, 0)
47+
}
48+
return(data.frame(geo_value = unique(geo_value), # Must include geo value!
49+
point = point, lower = lower, upper = upper))
50+
}

R/utils-pipe.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' Pipe operator
2+
#'
3+
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
4+
#'
5+
#' @name %>%
6+
#' @rdname pipe
7+
#' @keywords internal
8+
#' @export
9+
#' @importFrom magrittr %>%
10+
#' @usage lhs \%>\% rhs
11+
#' @param lhs A value or the magrittr placeholder.
12+
#' @param rhs A function call using the magrittr semantics.
13+
#' @return The result of calling `rhs(lhs)`.
14+
NULL

epipredict.Rproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: No
4+
SaveWorkspace: No
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
LineEndingConversion: Posix
18+
19+
BuildType: Package
20+
PackageUseDevtools: Yes
21+
PackageInstallArgs: --no-multiarch --with-keep.source
22+
PackageRoxygenize: rd,collate,namespace

man/pipe.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)