Skip to content

Commit

Permalink
R 4.0 compatibility (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk authored Jul 5, 2023
1 parent 2eb93f7 commit 318c800
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: fitR
Title: Tool box for fitting dynamic infectious disease models to time series
Version: 0.2
Version: 0.2.1
Authors@R: c(person(given="Anton", family="Camacho", email="[email protected]",
role=c("aut")),person(given="Sebastian", family="Funk",
email="[email protected]", role=c("aut", "cre")))
Description: This package contains helper functions for model fitting and
inference for infectious disease dynamics. It has been designed mainly as
a teaching tool for a one-week course on the topic.
Depends:
R (>= 3.5.0),
R (>= 4.0.0),
adaptivetau
Imports:
ggplot2,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# fitR 0.2.1

* Replace all instances of `\(x)` with `function(x)` for R 4.0 compatibility

# fitR 0.2

* Complete restyled package using `lintr`
Expand Down
2 changes: 1 addition & 1 deletion R/logLikelihood.r
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ rTrajObs <- function(fitmodel, theta, initState, times) {
## each row of traj. The parameter value theta as passed as
## extra argument to fitmodel$rPointObs
obs <- split(traj, f = traj$time)
obs <- map(obs, \(x) {
obs <- map(obs, function (x) {
data.frame(
time = unique(x$time),
obs = fitmodel$rPointObs(x, theta = theta)
Expand Down
10 changes: 5 additions & 5 deletions R/plot.r
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ plotTraj <- function(traj = NULL, stateNames = NULL, data = NULL,
infected = eval(parse(text = paste(nonExtinct, collapse = "+")), traj)
)
dfPExt <- split(traj, f = traj[[timeColumn]])
dfPExt <- map(dfPExt, \(df) {
dfPExt <- map(dfPExt, function(df) {
tmp <- data.frame(value = sum(df$infected == 0) / nrow(df))
tmp[[timeColumn]] <- unique(df[[timeColumn]])
return(tmp)
Expand All @@ -129,7 +129,7 @@ plotTraj <- function(traj = NULL, stateNames = NULL, data = NULL,
message("Compute confidence intervals")

trajCI <- split(dfTraj, dfTraj[c(timeColumn, "state")])
trajCI <- map(trajCI, \(df) {
trajCI <- map(trajCI, function (df) {
tmp <- as.data.frame(
t(quantile(df$value, prob = c(0.025, 0.25, 0.5, 0.75, 0.975)))
)
Expand Down Expand Up @@ -352,7 +352,7 @@ plotSMC <- function(smc, fitmodel, theta, data = NULL, summary = TRUE,
names(traj) <- seq_along(traj)

traj <- map(traj, function(df) {
obs <- apply(df, 1, \(x) fitmodel$rPointObs(x, theta = theta))
obs <- apply(df, 1, function (x) fitmodel$rPointObs(x, theta = theta))
trajObs <- left_join(df, obs, by = "time")

return(trajObs)
Expand Down Expand Up @@ -446,7 +446,7 @@ plotPosteriorDensity <- function(trace, prior = NULL, colour = NULL,

if (inherits_any(trace, c("mcmc.list", "list"))) {
## convert to data farmes
trace <- map(trace, \(x) {
trace <- map(trace, function(x) {
as.data.frame(as.matrix(x))
})

Expand Down Expand Up @@ -605,7 +605,7 @@ plotPosteriorFit <- function(trace, fitmodel, initState, data,
if (inherits(trace, "mcmc")) {
trace <- as.data.frame(trace)
} else if (inherits(trace, "mcmc.list")) {
trace <- map(trace, \(x) {
trace <- map(trace, function(x) {
as.data.frame(as.matrix(x))
})
trace <- bind_rows(trace)
Expand Down
8 changes: 4 additions & 4 deletions R/smc.r
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ particleFilter <- function(fitmodel, theta, initState, data, nParticles,
currentStateParticles <- currentStateParticles[indexResampled]

# propagate particles (this for loop could be parallelized)
propagate <- map(currentStateParticles, \(currentState) {
propagate <- map(currentStateParticles, function (currentState) {
# simulate from previous observation to current observation time
traj <- fitmodel$simulate(
theta = theta, initState = currentState, times = times
Expand All @@ -87,13 +87,13 @@ particleFilter <- function(fitmodel, theta, initState, data, nParticles,
})

# collect parallel jobs
currentStateParticles <- map(propagate, \(x) {
currentStateParticles <- map(propagate, function (x) {
x$state
})
weightParticles <- unlist(map(propagate, \(x) {
weightParticles <- unlist(map(propagate, function (x) {
x$weight
}))
trajParticles <- map(seq_along(propagate), \(j) {
trajParticles <- map(seq_along(propagate), function (j) {
rbind(trajParticles[[j]], c(dataPoint["time"], propagate[[j]]$state))
})

Expand Down

0 comments on commit 318c800

Please sign in to comment.