<p>I am assuming you have a table (e.g. a data frame in R) of some sort where you have at least the following two columns:</p>
<ol>
<li><p>a column containing timepoints (timepoint of measurement)</p>
</li>
<li><p>a column containing corresponding measurements (&quot;severity&quot; in your case)</p>
</li>
</ol>
<p>Now, you need to apply one of a few available approximation formulas to calculate the AUC. You will only be able to approximate the real AUC, as you are limited by how often you measured, i.e. how many time points you have, but this is naturally the case in practically all experiments. To calculate the approximation we could, for example, use the trapezoidal method. To understand the trapezoidal method check out the second formula and the first two images on <a href="https://en.wikipedia.org/wiki/Trapezoidal_rule" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Trapezoidal_rule</a> .</p>
<p>As your time steps are all the same length, we can actually simplify the formula you see in the wikipedia article by excluding the &quot;b-a&quot; part, as this will always be a constant of 1 (or 5 seconds, depending on how you choose to encode your timepoints).</p>
<p>In simple terms, we thus get:
<code>AUC = 1/2 * (severity[current value] + severity[former value]).</code></p>
<p>In R this could be implemented as simply as:</p>
<pre><code>library(dplyr)

your_dataframe<span class="math-container">$AUC = 0.5 * ( your_dataframe$</span>severity + lag(your_dataframe$severity, default = 0) )
</code></pre>
<p>Originally, this is where my answer finished, but I reread your question and figured there is one last step missing. As I assume you need the cumulative AUC relative to the starting point, you still need to sum up your &quot;triangles&quot; or partial AUC's, as such:</p>
<pre><code>your_dataframe$cumulativeAUC = cumsum(your_dataframe$AUC)
</code></pre>
<p>PS: In case you are wondering when reading the article (as it never mentions the AUC), the definitive integral and the AUC are mathematically identical.</p>

CC BY-SA 4.0
2024-06-11T15:07:14.537Z
Thank you for your efforts! I am still thinking about it and will try to implement it before I can upvote the answer.
Brigitte
CC BY-SA 4.0
2024-06-10T10:56:23.267Z
2024-06-10T20:39:09.523Z
2024-06-10T20:39:09.523Z
Jannik M
<p>I have two variables measured for thousands of patients. One is time to event (with competiting risk), the covariate is severety of a variable measured every 5 seconds or so (variable time steps). I want to use the area under the curve for severity. The figure below might help.</p>
<p>Question:
<strong>How could I get a function that lets me calculate the area under the curve at arbitrary time points?</strong></p>
<p>I say arbitrary because I will need it at each timeo of event and  all the time point of the time dependent covariates, in order to combine everything with tmerge for my multistate model.</p>
<p><a href="https://i.sstatic.net/U7B9PCED.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/U7B9PCED.jpg" alt="Two variables for 3 patients" /></a></p>

<biostatistics>
Area under the curve at each time point
# Area under the curve at each time point

(Asked by: username_0 on 2024-06-08)

I have two variables measured for thousands of patients. One is time to event (with competiting risk), the covariate is severety of a variable measured every 5 seconds or so (variable time steps). I want to use the area under the curve for severity. The figure below might help.

Question:
**How could I get a function that lets me calculate the area under the curve at arbitrary time points?**

I say arbitrary because I will need it at each timeo of event and all the time point of the time dependent covariates, in order to combine everything with tmerge for my multistate model.

[![Two variables for 3 patients](https://i.sstatic.net/U7B9PCED.jpg)](https://i.sstatic.net/U7B9PCED.jpg)

**Tags:** biostatistics

---

## 1 answers

### Answer by username_1 on 2024-06-10. Score: 3

I am assuming you have a table (e.g. a data frame in R) of some sort where you have at least the following two columns:

a column containing timepoints (timepoint of measurement)

a column containing corresponding measurements ("severity" in your case)


Now, you need to apply one of a few available approximation formulas to calculate the AUC. You will only be able to approximate the real AUC, as you are limited by how often you measured, i.e. how many time points you have, but this is naturally the case in practically all experiments. To calculate the approximation we could, for example, use the trapezoidal method. To understand the trapezoidal method check out the second formula and the first two images on https://en.wikipedia.org/wiki/Trapezoidal_rule .
As your time steps are all the same length, we can actually simplify the formula you see in the wikipedia article by excluding the "b-a" part, as this will always be a constant of 1 (or 5 seconds, depending on how you choose to encode your timepoints).
In simple terms, we thus get:
AUC = 1/2 * (severity[current value] + severity[former value]).
In R this could be implemented as simply as:
library(dplyr)

your_dataframe$AUC = 0.5 * ( your_dataframe$severity + lag(your_dataframe$severity, default = 0) )

Originally, this is where my answer finished, but I reread your question and figured there is one last step missing. As I assume you need the cumulative AUC relative to the starting point, you still need to sum up your "triangles" or partial AUC's, as such:
your_dataframe$cumulativeAUC = cumsum(your_dataframe$AUC)

PS: In case you are wondering when reading the article (as it never mentions the AUC), the definitive integral and the AUC are mathematically identical.

> **Comments:**
> **username_0**: Thank you for your efforts! I am still thinking about it and will try to implement it before I can upvote the answer.  

---

