Skip to content

Instantly share code, notes, and snippets.

@rhilfi
Created September 11, 2021 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhilfi/b47c0c7fb46a168d25fd87a17bd701dd to your computer and use it in GitHub Desktop.
Save rhilfi/b47c0c7fb46a168d25fd87a17bd701dd to your computer and use it in GitHub Desktop.
simulation_case_when_rnorm #R
library(rio)
library(tidyverse)
library(summarytools)
size=100000
age_years<-rnorm(size, mean=50, sd=12)
gender<-sample(c("1", "2"), size=size,prob=c(0.51, 0.49), replace=TRUE)
pos_test<-sample(c(0,1), size=size, prob=c(0.9,0.1), replace=TRUE)
df<-data.frame(gender, age_years, pos_test)
df<-df %>%
mutate(id=1:size) %>%
mutate(sick_covid=case_when(
pos_test==1~sample(c(0,1), size=length(id), prob=c(0.01,0.99), replace=TRUE),
pos_test==0~sample(c(0,1), size=length(id), prob=c(0.9, 0.1), replace=TRUE))) %>%
mutate(med_care_covid=case_when(
sick_covid==0~0,
sick_covid==1~sample(c(0,1), size=length(id), prob=c(0.01, 0.99), replace=TRUE))) %>%
mutate(icu_n_days=case_when(
sick_covid==0~0,
sick_covid==1~rgamma(n=length(id), shape=2, rate = 1)))%>%
mutate(hospital_n_days=case_when(
sick_covid==0~0,
sick_covid==1&icu_n_days==0~rgamma(n=length(id), shape=2, rate = 1),
icu_n_days>0~icu_n_days+rgamma(n=length(id), shape=5, rate = 0.3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment