I wrote some code to run a simulation model- I want to run this model at least 100 times. After running 100 times, I want it to calculate a mean for the sensitivity values produced for each screen age- for example, after 100 runs of my code, I will have 100 sensitivity values associated with screen.age = 50, I want to calculate the mean of all these values as well as a confidence interval to get a mean sensitivity for screen.age 50.
This is my initial data frame that was generated when I only ran my code once.
screen.age | screen | interval | sensitivity |
---|---|---|---|
50 | 5055 | 932 | 0.8443294 |
51 | 2597 | 450 | 0.8523138 |
There is a sensitivity value associated with each screen age and the sensitivity value is generated through my simulation code.
Preferably, after the simulation has ran 100 times, it will generate a new table/dataframe that has the mean sensitivities for each screen.age and their confidence intervals.
This is my current code:
library(tidyverse)
library(caret)
library(dplyr)
t_0 <- 40 #age assume all are healthy (no cancer)
startage <- 50
n <- 100000
k <- 30
x <- 0.58
lambda <- -(log(1-x))/k
y <- 5 #months = 5 years
mu <- 1/y
num.screen = 1:10
pr <- 0.5
counter <- 100
repeat{
id <- c()
t_p <- t_0 + rexp(n, lambda)
t_c <- t_p + rexp(n, mu)
newdf <- data.frame(t_p, t_c) %>%
filter (t_c <= 100)
cancerdf <- mutate(newdf, id = row_number()) %>%
select(id, everything())
result <- reframe(cancerdf, screen.num = num.screen,.by = id)
ddset <- cancerdf %>%
right_join(result, by= id) %>%
mutate(screen.age = screen.num + startage - 1) %>%
filter(t_c > startage,
t_c > screen.age) %>%
filter(t_c > screen.age | t_c-screen.age <= 1)
ddset$test.result = rbinom(nrow(ddset), 1, prob = pr)
#cuts out pt if cancer is detected (do not move on to next screen)
new.ddset <- ddset %>%
filter(cumsum(test.result)<=1
& test.result==cummax(test.result), .by=id) %>%
filter(t_p < screen.age, t_c > screen.age) %>%
group_by(id) %>%
mutate(screen = (ifelse(test.result == 1 & t_c > screen.age, 1,0))) %>%
mutate(interval = ifelse(t_c - screen.age > 1, 0,1 ),
interval = ifelse(screen == 1 & interval == 1, 0,interval))
simulation.sensitivity <- new.ddset %>%
group_by(screen.age) %>%
summarise(screen = (sum(screen)),
interval = (sum(interval)),
sensitivity = (screen/ (screen + interval)))
counter <- counter - 1
if (counter == 0) {
break
}
}
I used the repeat function to run my code multiple times but I can’t tell if it is actually calculating a mean correctly.