library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
set.seed(100)
age <- sample(seq(as.Date('1960-01-01'), as.Date('1985-01-01'), by="day"), 1000)
date1 <- sample(seq(as.Date('2000-01-01'), as.Date('2020-01-01'), by="day"), 1000)
status <- sample(c(0, 1), size = 1000, replace = TRUE, prob = c(0.2, 0.8))
df <- data.frame(age, date1, status)
df <- df %>% mutate(id = row_number())
head(df, n=5)
## age date1 status id
## 1 1970-05-13 2003-01-01 0 1
## 2 1961-05-17 2010-03-22 1 2
## 3 1969-05-22 2005-06-05 1 3
## 4 1970-02-12 2007-12-16 0 4
## 5 1971-03-13 2018-09-18 1 5
Mean average entery to VA care for 0= epi group vs. 1= non-epi group without simulation
library(ggplot2)
df %>%
ggplot(aes(x = factor(status),
y = as.numeric(difftime(date1, age, unit = 'w'))/52,
fill = factor(status))) +
geom_boxplot(width = 0.6) +
guides(fill = guide_none()) +
labs(x = 'Status', y = 'Age (years)')
df$date2 <- df$date1 - df$status * round(rnorm(nrow(df), 0, 365))
Mean average entery to VA care for 0= epi group vs. 1= non-epi group with simulation
df %>%
ggplot(aes(x = factor(status),
y = as.numeric(difftime(date2, age, unit = 'w'))/52,
fill = factor(status))) +
geom_boxplot(width = 0.6) +
guides(fill = guide_none()) +
labs(x = 'Status', y = 'Age (years)')
hist(df$date1, breaks = 10)
hist(df$date2, breaks = 10)
# real data
df %>%
filter(status==1) %>%
mutate( y = as.numeric(difftime(date1, age, unit = 'days'))) %>%
summarise(
n = n(),
mean = mean(y, na.rm=T)/365)
## n mean
## 1 808 37.22442
# sim data
df %>%
filter(status==1) %>%
mutate( y = as.numeric(difftime(date2, age, unit = 'days'))) %>%
summarise(
n = n(),
mean = mean(y, na.rm=T)/365)
## n mean
## 1 808 37.23418
Mean average entery to VA care for 0= epi group vs. 1= non-epi group without simulation
library(ggplot2)
df %>%
ggplot(aes(x = factor(status),
y = as.numeric(difftime(date1, age, unit = 'w'))/52,
fill = factor(status))) +
geom_boxplot(width = 0.6) +
guides(fill = guide_none()) +
labs(x = 'Status', y = 'Age (years)')
df$date2 <- df$date1 - df$status * round(rnorm(nrow(df), -978, 365))
Mean average entery to VA care for 0= epi group vs. 1= non-epi group with simulation
df %>%
ggplot(aes(x = factor(status),
y = as.numeric(difftime(date2, age, unit = 'w'))/52,
fill = factor(status))) +
geom_boxplot(width = 0.6) +
guides(fill = guide_none()) +
labs(x = 'Status', y = 'Age (years)')
hist(df$date1, breaks = 10)
hist(df$date2, breaks = 10)
# real data
df %>%
filter(status==1) %>%
mutate( y = as.numeric(difftime(date1, age, unit = 'days'))) %>%
summarise(
n = n(),
mean = mean(y, na.rm=T)/365)
## n mean
## 1 808 37.22442
# sim data
df %>%
filter(status==1) %>%
mutate( y = as.numeric(difftime(date2, age, unit = 'days'))) %>%
summarise(
n = n(),
mean = mean(y, na.rm=T)/365)
## n mean
## 1 808 39.91363