I Used the R Package EpiCurve to Make an Epidemiologic Curve. Here’s How It Turned Out.

Epidemiologic Curve of 2015 Middle East Respiratory Virus Outbreak Using R EpiCurve Package

With all this talk about “flattening the curve” of the coronavirus, I thought I would get into the weeds about what curve we are talking about when we say that. We are talking about what’s called an epidemiologic curve, or epicurve for short. And to demonstrate what an epicurve is and what it means, I used the R package EpiCurve to demonstrate making an epidemiologic curve so you can see how it works.

Source Data for my Epicurve

I actually back-engineered the data for my dataset based on viewing the epicurve in this article from Morbidity and Mortality Weekly Report of a nosocomial outbreak of Middle East respiratory syndrome (MERS) at King Abdulaziz Medical City in Riyadh, Saudi Arabia in June through August, 2015.

The downside to back-engineering data is that it might have errors, but the advantage is that I can assemble it into the perfect structure for the R package EpiCurve. If you look in the article, they have an epicurve, but they don’t include the raw numbers, so I just guessed the data. The data I guessed are below.


Variables for the R Package EpiCurve

Let’s look more closely at each variable:

  • RowID: This is just the number of the row. I always include this as a unique identifier in tables.
  • Start_of_Week: The epicurve in the article presents each frequency bar as representing a week. This variable is the day at the start of the week.
  • End_of_Week: This is the day at the end of the week.
  • Frequency: This is the number of cases for the week from the specified Source (see next column).
  • Source: You will see in the epicurve in the article, each bar is color-coded for healthcare worker (HCW) or non-HCW. To facilitate this color coding, each category needs its own row. You will see in the first rows, there were no cases, so I just put one row, randomly assigned it a source, and put 0. In rows 6 and 7, only non-HCW’s were cases, so those weeks have only one row. Whenever there were HCW and non-HCW identified as cases in the same week, they were each expressed in their own row.

Programming for the R EpiCurve Package

First, I knew I’d need two colors – one for HCW and one for non-HCW so I used the Coolors app and the logo for the Saudi Arabia Ministry of Health to pick my colors in hexadecimal. Once I selected them, I loaded them into variables, one called saudi_gold and one called saudi_green.

saudi_gold <- c("#806222")
saudi_green <- c("#045c20")

That way, I could just refer to them as saudi_gold and saudi_green when programming.

Next, after installing the EpiCurve package, I called up the library, and made these specifications to get the epicurve shown at the top the post.

date= "Start_of_Week",
period = "day",
to.period = "week",
freq = "Frequency",
cutvar = "Source",
colors = c(saudi_gold, saudi_green),
title = c("MERS at KAMC 2015 from MMWR"),
ylabel = c("Number of cases"),
xlabel = c("Week of 2015"))

Here are some important parts of the code above:

  • The EpiCurve package handles dates in factor format! In fact, converting them to date won’t work. So as you can see in the code above, where date=”Start_of_Week”, that date variable – as well as the date variable End_of_Week – are both actually in factor format.
  • Period and to.period: These had be correct in order for the date frequency to collapse and come out right as weeks. Many epicurves are by days, so if period=day and the data are daily so they do not have to be collapsed into weeks or months, the to.period argument can be left out.
  • Cutvar is the command to specify the categories: The argument curtvar=”Source” is what facilitated the epicurve looking at the categorical column Source that separates HCW and Non-HCW data.
  • Colors are specified using the variables set: This makes it easier to see what colors are being specified. You will see that between cutvar and the colors specification, the legend is automatically output.

Where the Epicurve Starts Flattening

Epidemiologic Curve of 2015 Middle East Respiratory Virus Outbreak Using R EpiCurve Package Annotated

The reason we make an epicurve during an epidemic is that in addition to tracking the mortality rate, we are interested in tracking when the curve actually begins to flatten. Note where I annotated the epicurve from the MERS article to point out the week that that curve finally flattened. When the curve finally flattens, you are literally “on the other side” of the outbreak. The problem is that during the epidemic you never really know you are there until you get there, and the day or week you are in finally sees fewer new cases than the day or week before.

As you proceed through the epidemic, as long as you keep formatting your data the way I demonstrated, you can keep adding to it and generating a new epicurve every day.

Read all of our data science blog posts!

At the beginning of the pandemic, I used the EpiCurve package in R to make an early epicurve. I show you my code in this blog post.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights