Skip to content

Instantly share code, notes, and snippets.

@stuartjonesstats
Last active April 10, 2023 10:14
Show Gist options
  • Save stuartjonesstats/d791d433d35aeac09becbfe3b8a2deb1 to your computer and use it in GitHub Desktop.
Save stuartjonesstats/d791d433d35aeac09becbfe3b8a2deb1 to your computer and use it in GitHub Desktop.
monsterlist <- c() #This will hold the number of disabled in each sim run
for (h in 1:100){ #Repeat simulation 100 times
disabled <- c() #This will hold the disabled within each sim run
population <- as.vector(c(rep(0,10000))) #The population of 10,000
for(j in 1:30) { # Number of Years
infected <- sample(1:length(population),floor(0.1*length(population))) # infect x% of the population every year
population[infected] <- population[infected]-1 #infected get an increasingly-negative counter
# The next 2 paragraphs control who gets LC. One should be commented out.
# The first puts everyone in the same risk category.
# The second creates low, medium, and high risk categories.
# for (k in 1:length(population)){ #Use this paragraph if risk to everyone of LC is equal.
#Use next paragraph if LC is based on low risk, medium risk, and high risk groups.
# #if (runif(1) <= pweibull(abs(population[k]),scale=6,shape=2)){ #Use this one if successive Covid infections increase LC risk
# if (runif(1) <= 0.1) { #Use this one if risk of LC is constant. Comment out one of these.
# population[k] <- population[k]+ 100 #positive number means Long Covid
# }
#
# }
for (p in 1:length(population)){
if (p <= 3000) { #The first 3000 in the population are "low risk" for LC.
if(runif(1) <= 0.05) { #Only a 5% chance of getting LC with each infection.
population[p] <- population[p] + 100
}
} else if (p >= 7000) { #High risk group for LC.
if(runif(1) <= 0.25) { #This group has a 25% chance of getting LC with each infection.
population[p] <- population[p] + 100
}
} else { #This is the "medium" risk group of getting LC with each infection.
if(runif(1) <= 0.1) { #This group has a 10% chance of getting LC with each infection.
population[p] <- population[p] + 100
}
}
}
for (n in 1:length(population)){ # give them a 50% chance to recover from LC this year
if (population[n] > 0){
if(runif(1)>=0.5){
population[n] <- population[n] - 100
}
}
}
print(j)
del_list <- c()
for (m in 1:length(population)){
if (population[m] > 0){
if(runif(1)>=0.9){
disabled <- c(disabled,population[m])
del_list <- c(del_list,m)
}
}
}
if (length(del_list)>0){
population <- population[-del_list]
}
}
monsterlist <- c(monsterlist,length(disabled))
}
sum(monsterlist)/10000 #gives the % (not the decimal) average over 100 simulations
@rcsmit
Copy link

rcsmit commented Aug 15, 2022

Here I automated it https://gist.github.com/rcsmit/4c4e32f738855c159a0e61e17a4e449b

and this is my output

[1] "infected: 0.1  result 2.555 %"
[1] "infected: 0.2  result 2.538 %"
[1] "infected: 0.3  result 2.524 %"
[1] "infected: 0.4  result 2.504 %"
[1] "infected: 0.5  result 2.481 %"
[1] "infected: 0.6  result 2.494 %"
[1] "infected: 0.7  result 2.44 %"
[1] "infected: 0.8  result 2.438 %"
[1] "infected: 0.9  result 2.526 %"
[1] "infected: 1  result 2.517 %"

@InaKrappDeveloper
Copy link

Hello,
you created a really nice model. One suggestion for improvement:
You can simply create the population vector with the code 'population <- rep(0,10000)'
There's no need to use the as.vector-function (or even the c), because the rep-function creates a vector by default.
Best Wishes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment