Calculating AUC

I have to calculate AUC for glucose conc above 10, 11, 12......time is independent(x) and glucose concentration(y) dependent variable....I am not sure how to write a loop or something so it calculates only for those intervals above 10, with increment by 1

sapply(split(data,data$ID), function(data), trapz(data$time, data$conc))
get_auc(data, time="time", id="id", dv="conc")

Hi @peky,

So just to be clear, you want to calculate the definite integral for a glucose vs. time function for various 1-unit long intervals starting at 10. Is this correct?

Hey @mattearkentin, yes it is, but intervals are :

Intervals (AVAL=Conc) AUC for 10<AVAL<∞

11<AVAL<∞

12<AVAL<∞

13<AVAL<∞

14<AVAL<∞..... until 22<AVAL<∞....

And per ID!!

Because when I filter full data (AVAL = 0-22)for values AVAL >11 or >12 I get ,higher AUC for Aval>12 than full data set (AVAL=0-22) or AVAL >11...which doesnt make sense since it is smaller interval with lees glucose points...maybe smth due to the filtering...

So I would like to try loop or some other code to see results..

Thx

@mattwarkentin sorry for the wrong spelling, see comment above

I think I generally understand what you want to achieve, but I'm not sure it makes sense to integrate a function to infinity. Do you mean you want to integrate from a time point t to the last time?

Sharing a snippet of the data would go a long way to provide more specific help.

@mattwarkentin its not to infinite, the highest value is 22.2, , sorry for
misunderstanding....:slight_smile: ....the problem is that I am not allowed to share data due to the data-sharing agreement....I am not sure how to integrate using classical numerical integration since I don't know a function which describes the data.....but there is function trapz and get_auc in R for such case.... I want to integrate for these specific glucose levels, ...It is a really big data....thats why I use glucose levels, I have many time intervals with same glucose levels....but yws, i want to make it for "t" to the last time

@mattwarkentin data looks like this, but it is really big
time glu ID
1 5 1
6 8 1
11 4 3
16 12 3

This felt a little awkward to do. It's not terribly elegant, but I think this code should do it...I made some toy data representing glucose decaying over time.

library(purrr)
library(dplyr)
library(tidyr)
library(pracma)

# Toy data
x <- tibble(
  id = rep(c(1,2), each = 20),
  time = rep(seq(0, 19, 1), 2),
  glucose = 20 - (2 * time) + (0.05*time^2)
) 

#
x %>% 
  group_nest(id) %>%
  hoist(data, time = 'time', glucose = 'glucose', .remove = FALSE) %>% 
  mutate(cum_auc = map2(time, glucose, ~cumtrapz(.x, .y)[,1])) %>% 
  unnest(c(time, cum_auc))

thx it really worked @mattwarkentin :slight_smile:
and just one more question...do you know maybe how to deal with gaps in signal----cuz CGM device did not take measurements every 5min as it is supposed to do, sometimes it is every 30 min,

After staring at my solution a little longer, I now think that cum_auc isn't the value you wanted, right? The solution I gave in the last comment is the cumulative area under the function starting at zero and adding "trapezoids" to the right. But you wanted the area from time t to the last time (i.e. start adding trapezoids to the right from time t). I think this solution is now correct:

x %>% 
  group_nest(id) %>%
  hoist(data, time = 'time', glucose = 'glucose', .remove = FALSE) %>% 
  mutate(cum_auc = map2(time, glucose, ~cumtrapz(.x, .y)[,1])) %>% 
  unnest(c(time, cum_auc)) %>% 
  group_by(id) %>% 
  mutate(cum_auc = max(cum_auc) - cum_auc) %>% 
  ungroup()

Check it out and let me know what you think.

Regarding the gaps in signal. How would you like to handle this? Should it be linearly interpolated? Some other interpolation approach? There are definitely ways to handle gaps in data, but you'll have to think about what is appropriate for your data.

1 Like

@mattwarkentin yea that seems more appropriate thx ;)....since I have to calculate for specific intervals... regarding gaps in signal there are not many missing values for these hyperglycemic intervals, so it will just cause small noise I hope :slight_smile:
but my time is in seconds from 1.1.1960, with increments of 300sec so the first time observation starts like 12340242214....instead 5min or 300sec, 600,sec, 900sec.... maybe to ad new column as time sequence with an increase of 300 sec? cuz I have to connect it with each person ID...smth like this:
time<- ave(seq_along(data$USUBJID), u$USUBJID, fun=seq_along)

cuz nrow(ID)=nrow(time)

also sometimes mesuremnets are taken every 900sec or more

@mattwarkentin I managed to do it by subtraction, thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.