Giter VIP home page Giter VIP logo

Comments (5)

flaxter avatar flaxter commented on July 18, 2024

from covid19model.

flaxter avatar flaxter commented on July 18, 2024

from covid19model.

flaxter avatar flaxter commented on July 18, 2024

from covid19model.

maxbiostat avatar maxbiostat commented on July 18, 2024

Hi @flaxter ,

It's more likely I'm the buggy one:

Code

LogMean <- function(realMean, realSD){
  ## takes REAL mean and REAL standard deviation; returns LOG mean
  realVar <- realSD^2
  mu <- log(realMean/ sqrt(1 + (realVar/realMean^2)))
  return(mu)
}
LogVar <- function(realMean, realSD){
  ## takes REAL mean and REAL standard deviation; returns LOG variance
  realVar <- realSD^2
  sigmasq <- log(1 + (realVar/realMean^2))
  return(sigmasq)
}
###################
M <- 1e6
kappa_ic <- abs(rnorm(n = M, mean = 0, sd = 1/2))
R0_ic <- abs(rnorm(n = M, mean = 3.28, sd = kappa_ic))

kappa_bram <- abs(rnorm(n = M, mean = 0, sd = 1))
R0_bram <- abs(rnorm(n = M, mean = 2.4, sd = kappa_bram))

m <- 3 # E[R0]
v <- .9^2 # Var(R0)

# Gamma
alpha <- m^2/v
beta <- m/v

#Log-normal
mu <- LogMean(realMean = m, realSD = sqrt(v) )
sigma <- sqrt( LogVar(realMean = m, realSD = sqrt(v) )    ) 

R0_gamma <- rgamma(n = M, shape = alpha, rate = beta)
R0_ln <- rlnorm(n = M, meanlog = mu, sdlog = sigma)

forplot <- data.frame(R_0 = c(R0_ic,
                              R0_bram,
                              R0_gamma,
                              R0_ln),
                      study = rep(c("Imperial",
                                    "BRAM-COD",
                                    "Gamma",
                                    "Log-normal"), each = M))

library(ggplot2)

ggplot(forplot, aes(x = R_0, colour = study, fill = study)) +
  geom_density(alpha = .4) +
  scale_x_continuous(expression(R_0), expand = c(0, 0)) +
  scale_y_continuous("Density", expand = c(0, 0)) +
  geom_vline(xintercept = 1, linetype = "longdash") +
  theme_bw(base_size = 16)

summy <- function(x, na.rm = TRUE){
  ans <- data.frame(mean = mean(x, na.rm = na.rm),
                    sd = sd(x, na.rm = na.rm),
                    lwr = quantile(x, probs = .025, na.rm = na.rm),
                    uprr = quantile(x, probs = .975, na.rm = na.rm))
  return(ans)
} 


lapply(list(bramcod = R0_bram, imperial = R0_ic, Gamma = R0_gamma, LN = R0_ln), summy)

Last line gives

$bramcod
         mean       sd       lwr      upr
2.5% 2.435088 0.911255 0.5271803 4.580456

$imperial
         mean        sd      lwr      upr
2.5% 3.280113 0.4989465 2.190974 4.374173

$Gamma
         mean        sd      lwr      upr
2.5% 2.999272 0.8991958 1.503212 4.995466

$LN
        mean        sd      lwr      upr
2.5% 3.00032 0.8997761 1.617084 5.106452

BRAM-COD is a recent Brazilian study with a model based on your model from report 13.

from covid19model.

maxbiostat avatar maxbiostat commented on July 18, 2024

I realised the script above has a (as of yet inconsequential) bug in the generation of the random samples. Technically, the model specified in Stan is a truncated normal, not a folded normal, so the correct generation of the samples would be something like

M <- 1e6
library(truncnorm)
kappa_ic <- rtruncnorm(n = M, mean = 0, sd = 1/2, a = 0, b = Inf)
R0_ic <- rtruncnorm(n = M, mean = 3.28, sd = kappa_ic, a = 0, b = Inf)

kappa_bram <- rtruncnorm(n = M, mean = 0, sd = 1, a = 0, b = Inf)
R0_bram <- rtruncnorm(n = M, mean = 2.4, sd = kappa_bram, a = 0, b = Inf)

Giving

> lapply(list(bramcod = R0_bram, imperial = R0_ic, Gamma = R0_gamma, LN = R0_ln), summy)
$bramcod
         mean        sd       lwr    uprr
2.5% 2.471883 0.9082468 0.6756234 4.67399

$imperial
         mean       sd      lwr     uprr
2.5% 3.280978 0.497174 2.192721 4.371233

$Gamma
         mean        sd      lwr     uprr
2.5% 2.999731 0.9005028 1.502073 5.006691

$LN
        mean        sd      lwr     uprr
2.5% 3.00048 0.9005886 1.616915 5.110658

from covid19model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.