From 318c800aa1626a6deb385ea91a11d3412487c98a Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Wed, 5 Jul 2023 12:10:02 +0100 Subject: [PATCH] R 4.0 compatibility (#21) --- DESCRIPTION | 4 ++-- NEWS.md | 4 ++++ R/logLikelihood.r | 2 +- R/plot.r | 10 +++++----- R/smc.r | 8 ++++---- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 468b328..d5a281b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ 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="ntncmch@gmail.com", role=c("aut")),person(given="Sebastian", family="Funk", email="sebastian.funk@lshtm.ac.uk", role=c("aut", "cre"))) @@ -8,7 +8,7 @@ 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, diff --git a/NEWS.md b/NEWS.md index 775c614..235f7aa 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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` diff --git a/R/logLikelihood.r b/R/logLikelihood.r index 0b0f7f5..b330cac 100644 --- a/R/logLikelihood.r +++ b/R/logLikelihood.r @@ -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) diff --git a/R/plot.r b/R/plot.r index e53cd3c..0174ddf 100644 --- a/R/plot.r +++ b/R/plot.r @@ -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) @@ -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))) ) @@ -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) @@ -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)) }) @@ -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) diff --git a/R/smc.r b/R/smc.r index e2205d3..ee7ca79 100644 --- a/R/smc.r +++ b/R/smc.r @@ -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 @@ -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)) })