Coherence timestamps and observations order

Packages: dplyr


Ensuring the coherence of the timestamps and the order of the observations is crucial for the analysis of EMA data. We will focus on this critical task, with specific objectives including examining:

  • key timetamps variables: We delve into the ‘scheduled’, ‘sent’, ‘start’, and ‘end’ timestamps (see the Terminology page for further details on the variables). It’s essential that these variables exhibit coherence both within individual observations and across the dataset. For instance, a ‘start’ timestamps should never be before a ‘sent’ timestamps within an observation.
  • the ‘obsno’ variable: this variable is expected to follow the serial order of the observation, recorded by the ‘sent’ timestamps variable. The ‘obsno’ variable might already be included in the imported dataset, or it could have been generated in Step 1 (see Create time variables).

To showcase how to check those aspects, we will use a subset of data in which only the id numbers, the timestamp variables, and the observation numbers are included.

df = data[,c("id", "scheduled", "sent", "start", "end", "obsno")]
  id           scheduled                sent               start
1  1 2018-10-17 08:00:08 2018-10-17 08:00:11                <NA>
2  1 2018-10-17 09:00:01 2018-10-17 09:00:22                <NA>
3  1 2018-10-17 09:59:56 2018-10-17 10:00:08                <NA>
4  1 2018-10-17 10:59:48 2018-10-17 10:59:52 2018-10-17 11:00:12
5  1 2018-10-17 12:00:12 2018-10-17 12:00:15                <NA>
6  1 2018-10-18 07:59:47 2018-10-18 08:00:08                <NA>
                  end obsno
1                <NA>     1
2                <NA>     2
3                <NA>     3
4 2018-10-17 11:03:01     4
5                <NA>     5
6                <NA>     6

Coherence of timestamps variables

We often encounter scenarios where timestamp variables display inconsistencies in various forms. To address these issues, we will conduct a series of logical tests on our dataframe to identify potential discrepancies. Following these tests, we’ll filter the data to isolate and emphasize the problematic rows. In this process, we aim to scrutinize two primary types of inconsistency.

Coherence within observations

We check the chronological sequence of the timestamps within a single observation. Specifically, we ensure that the order of timestamp values follows a logical timeline: ‘scheduled’ should precede ‘sent’, which in turn should be followed by ‘start’, and finally ‘end’ (i.e., ‘scheduled’ < ‘sent’ < ‘start’ < ‘end’). Hence, we specifically check if timestamps values are after the following one (e.g., df\(scheduled > df\)sent). When TRUE is returned, it indicates that the timestamp is not in the expected order. For instance, below, we can see that three observations have a ‘sent’ timestamp that is earlier than the ‘scheduled’ timestamp.

sent_after_sched = df$scheduled > df$sent & !is.na(df$sent)
start_after_sent = df$sent > df$start & !is.na(df$start)
end_after_end = df$start > df$end & !is.na(df$end)

df[sent_after_sched | start_after_sent | end_after_end, ]
     id           scheduled                sent               start
648  10 2018-02-05 10:59:44 2018-02-05 09:59:52 2018-02-05 11:00:05
3047 44 2018-09-12 09:00:05 2018-09-12 08:00:28 2018-09-12 09:00:28
3182 46 2018-02-16 10:00:06 2018-02-16 09:30:09 2018-02-16 10:00:19
NA   NA                <NA>                <NA>                <NA>
NA.1 NA                <NA>                <NA>                <NA>
                     end obsno
648  2018-02-05 11:01:18    18
3047 2018-09-12 09:00:34    37
3182 2018-02-16 10:01:30    32
NA                  <NA>    NA
NA.1                <NA>    NA

Coherence between observations

Our evaluation extends to ensuring that the timestamps are chronologically coherent across successive observations. We specifically verify that the timestamps at a given beep (time t) precede those at the subsequent beep (time t + 1). Note that this analysis needs the dataset to be ordered according to one of the timestamp variables. Additionally, we must use the ‘group_id()’ function to create lagged variables and check the sequential integrity within each participant. For instance, below, we can see that one observation has a scheduled timestamp value that occurs before the scheduled timestamp value of the previous observation which is not expected.

df %>%
    arrange(id, sent) %>%
    group_by(id) %>%
    mutate(sched_lag_issue = lag(scheduled) > scheduled,
           sent_lag_issue = lag(sent) > sent,
           start_lag_issue = lag(start) > start,
           end_lag_issue = lag(end) > end) %>%
    filter(sched_lag_issue | sent_lag_issue | start_lag_issue | end_lag_issue)
  id           scheduled                sent               start
1 10 2018-02-05 10:00:11 2018-02-05 10:00:20 2018-02-05 10:01:09
2 31 2018-11-19 10:00:03                <NA>                <NA>
                  end obsno sched_lag_issue sent_lag_issue start_lag_issue
1 2018-02-05 10:03:01    17            TRUE          FALSE            TRUE
2                <NA>    12            TRUE             NA              NA
  end_lag_issue
1          TRUE
2            NA

Check observation order

The ‘obsno’ variable may already pre-exist in the dataset, or you may have already computed it (see Create time variables). Regardless of its origin, it’s crucial to verify that ‘obsno’ aligns correctly with the other timestamp variables. We suggest two methods.

Recreate and compare ‘obsno’ variables

In this approach, we create a new ‘obsno’ variable (called ‘obsno_test’) reflecting the order of the ‘sent’ timestamps. Then, we compare it with the existing ‘obsno’ variable to identify discrepancies. Specifically, we test if the ‘obsno’ values differ from the ‘obsno_test’ values. The R code below shows that in one row the ‘obsno’ values are inconsistent. It seems to be inverted with the previous observation.

df = df[order(df$id, df$sent),]
obsno_test = ave(seq_along(df$id), df$id, FUN = seq_along)
df[which(obsno_test != df$obsno),]
     id           scheduled                sent               start
67    1 2018-10-30 10:00:18 2018-10-30 10:00:25 2018-10-30 10:00:35
648  10 2018-02-05 10:59:44 2018-02-05 09:59:52 2018-02-05 11:00:05
647  10 2018-02-05 10:00:11 2018-02-05 10:00:20 2018-02-05 10:01:09
2113 31 2018-11-19 11:00:12 2018-11-19 11:00:36 2018-11-19 11:01:00
2114 31 2018-11-19 12:00:07 2018-11-19 12:00:11 2018-11-19 12:00:27
2115 31 2018-11-19 12:59:55 2018-11-19 13:00:01 2018-11-19 13:00:14
2116 31 2018-11-20 09:00:13 2018-11-20 09:00:16 2018-11-20 09:00:34
2117 31 2018-11-20 09:59:53 2018-11-20 09:59:59 2018-11-20 10:00:31
2118 31 2018-11-20 10:59:52 2018-11-20 10:59:59 2018-11-20 11:00:21
2119 31 2018-11-20 12:00:02 2018-11-20 12:00:11 2018-11-20 12:00:20
2120 31 2018-11-20 12:59:59 2018-11-20 13:00:15 2018-11-20 13:00:27
2121 31 2018-11-21 09:00:10 2018-11-21 09:00:18 2018-11-21 09:00:37
2122 31 2018-11-21 10:00:13 2018-11-21 10:00:22 2018-11-21 10:00:27
2123 31 2018-11-21 10:59:41 2018-11-21 10:59:51 2018-11-21 10:59:57
2124 31 2018-11-21 12:00:13 2018-11-21 12:00:22 2018-11-21 12:00:35
2125 31 2018-11-21 12:59:46 2018-11-21 12:59:53 2018-11-21 13:00:05
2126 31 2018-11-22 09:00:08 2018-11-22 09:00:31                <NA>
2127 31 2018-11-22 09:59:59 2018-11-22 10:00:12 2018-11-22 10:00:20
2128 31 2018-11-22 11:00:00 2018-11-22 11:00:04 2018-11-22 11:00:19
2129 31 2018-11-22 12:00:19 2018-11-22 12:00:30 2018-11-22 12:00:52
2130 31 2018-11-22 12:59:49 2018-11-22 13:00:00                <NA>
2131 31 2018-11-23 08:59:51 2018-11-23 09:00:01 2018-11-23 09:00:19
2132 31 2018-11-23 10:00:19 2018-11-23 10:00:22 2018-11-23 10:00:33
2133 31 2018-11-23 10:59:50 2018-11-23 10:59:59 2018-11-23 11:00:14
2134 31 2018-11-23 11:59:58 2018-11-23 12:00:01 2018-11-23 12:00:16
2135 31 2018-11-23 13:00:03 2018-11-23 13:00:18 2018-11-23 13:00:53
2136 31 2018-11-24 09:00:09 2018-11-24 09:00:13 2018-11-24 09:00:26
2137 31 2018-11-24 10:00:09 2018-11-24 10:00:18 2018-11-24 10:00:49
2138 31 2018-11-24 10:59:46 2018-11-24 10:59:50 2018-11-24 10:59:54
2139 31 2018-11-24 11:59:53 2018-11-24 12:00:03 2018-11-24 12:00:14
2140 31 2018-11-24 13:00:05 2018-11-24 13:00:10 2018-11-24 13:00:18
2141 31 2018-11-25 09:00:00 2018-11-25 09:00:06 2018-11-25 09:00:11
2142 31 2018-11-25 10:00:11 2018-11-25 10:00:18 2018-11-25 10:00:30
2143 31 2018-11-25 10:59:58 2018-11-25 11:00:03 2018-11-25 11:00:10
2144 31 2018-11-25 12:00:16 2018-11-25 12:00:42                <NA>
2145 31 2018-11-25 13:00:22 2018-11-25 13:00:57 2018-11-25 13:01:10
2146 31 2018-11-26 08:59:58 2018-11-26 09:00:04 2018-11-26 09:00:11
2147 31 2018-11-26 10:00:09 2018-11-26 10:00:20 2018-11-26 10:00:34
2148 31 2018-11-26 11:00:12 2018-11-26 11:00:25                <NA>
2149 31 2018-11-26 12:00:06 2018-11-26 12:00:11 2018-11-26 12:00:26
2150 31 2018-11-26 13:00:05 2018-11-26 13:00:11 2018-11-26 13:00:17
2151 31 2018-11-27 09:00:12 2018-11-27 09:00:23                <NA>
2152 31 2018-11-27 09:59:57 2018-11-27 09:59:59 2018-11-27 10:00:08
2153 31 2018-11-27 10:59:59 2018-11-27 11:00:18 2018-11-27 11:00:39
2154 31 2018-11-27 11:59:50 2018-11-27 12:00:02 2018-11-27 12:00:14
2155 31 2018-11-27 12:59:50 2018-11-27 13:00:08 2018-11-27 13:00:18
2156 31 2018-11-28 08:59:50 2018-11-28 09:00:01 2018-11-28 09:00:19
2157 31 2018-11-28 10:00:02 2018-11-28 10:00:07 2018-11-28 10:00:26
2158 31 2018-11-28 10:59:53 2018-11-28 10:59:57                <NA>
2159 31 2018-11-28 12:00:09 2018-11-28 12:00:41 2018-11-28 12:00:55
2160 31 2018-11-28 12:59:54 2018-11-28 13:00:05 2018-11-28 13:00:10
2161 31 2018-11-29 09:00:06 2018-11-29 09:00:10                <NA>
2162 31 2018-11-29 10:00:12 2018-11-29 10:00:20 2018-11-29 10:00:41
2163 31 2018-11-29 10:59:42 2018-11-29 10:59:47 2018-11-29 11:00:01
2164 31 2018-11-29 12:00:10 2018-11-29 12:00:24 2018-11-29 12:00:48
2165 31 2018-11-29 13:00:19 2018-11-29 13:00:29 2018-11-29 13:00:49
2166 31 2018-11-30 09:00:09 2018-11-30 09:00:19                <NA>
2167 31 2018-11-30 09:59:55 2018-11-30 10:00:03                <NA>
2168 31 2018-11-30 11:00:12 2018-11-30 11:00:25 2018-11-30 11:00:37
2169 31 2018-11-30 12:00:06 2018-11-30 12:00:24 2018-11-30 12:00:36
2170 31 2018-11-30 12:59:55 2018-11-30 13:00:01 2018-11-30 13:00:24
2112 31 2018-11-19 10:00:03                <NA>                <NA>
                     end obsno
67   2018-10-30 10:03:19    68
648  2018-02-05 11:01:18    18
647  2018-02-05 10:03:01    17
2113 2018-11-19 11:02:36    13
2114 2018-11-19 12:02:43    14
2115 2018-11-19 13:01:43    15
2116 2018-11-20 09:02:38    16
2117 2018-11-20 10:00:35    17
2118 2018-11-20 11:02:45    18
2119 2018-11-20 12:02:05    19
2120 2018-11-20 13:02:00    20
2121 2018-11-21 09:02:34    21
2122 2018-11-21 10:02:29    22
2123 2018-11-21 11:01:47    23
2124 2018-11-21 12:02:32    24
2125 2018-11-21 13:02:17    25
2126                <NA>    26
2127 2018-11-22 10:01:42    27
2128 2018-11-22 11:01:52    28
2129 2018-11-22 12:01:40    29
2130                <NA>    30
2131 2018-11-23 09:01:56    31
2132 2018-11-23 10:03:09    32
2133 2018-11-23 11:01:09    33
2134 2018-11-23 12:01:00    34
2135 2018-11-23 13:03:20    35
2136 2018-11-24 09:02:20    36
2137 2018-11-24 10:02:11    37
2138 2018-11-24 11:01:06    38
2139 2018-11-24 12:01:30    39
2140 2018-11-24 13:01:06    40
2141 2018-11-25 09:02:08    41
2142 2018-11-25 10:02:15    42
2143 2018-11-25 11:03:01    43
2144                <NA>    44
2145 2018-11-25 13:03:31    45
2146 2018-11-26 09:01:44    46
2147 2018-11-26 10:02:40    47
2148                <NA>    48
2149 2018-11-26 12:02:26    49
2150 2018-11-26 13:00:56    50
2151                <NA>    51
2152 2018-11-27 10:00:26    52
2153 2018-11-27 11:02:32    53
2154 2018-11-27 12:02:35    54
2155 2018-11-27 13:01:52    55
2156 2018-11-28 09:01:53    56
2157 2018-11-28 10:02:03    57
2158                <NA>    58
2159 2018-11-28 12:02:45    59
2160 2018-11-28 13:01:32    60
2161                <NA>    61
2162 2018-11-29 10:02:15    62
2163 2018-11-29 11:02:42    63
2164 2018-11-29 12:03:15    64
2165 2018-11-29 13:03:26    65
2166                <NA>    66
2167                <NA>    67
2168 2018-11-30 11:01:32    68
2169 2018-11-30 12:01:59    69
2170 2018-11-30 13:02:32    70
2112                <NA>    12

Identifying gaps in the ‘obsno’ sequence

We can compute the difference in row positions between the existing ‘obsno’ value and its lagged counterpart. We expect this difference to be exactly 1; deviation from this (either greater or less than 1) indicates a gap or overlap in the numbering sequence. It’s important to note that using this approach, a single discrepancy can impact adjacent rows. Therefore, this method may flag more rows as problematic compared to the previous method. In the analysis below, we observe that the inconsistency identified using the previous method is also detected here, but two rows are now reported.

# Sorting the dataframe by 'id' and 'sent'
df = df[order(df$id, df$sent), ]

# Splitting the dataframe by 'id'
df_split = split(df, df$id)

# Function to apply lag calculations
apply_lag_calculations <- function(data) {
    data$obsno_lag <- c(NA, data$obsno[-length(data$obsno)])
    data$obsno_dif <- data$obsno - data$obsno_lag
    return(data)
}

# Applying the function to each group and recombining
df_lagcal = do.call("rbind", lapply(df_split, apply_lag_calculations))

# Filtering the rows where difference is not 1
df_lagcal[df_lagcal$obsno_dif != 1 & !is.na(df_lagcal$obsno_dif), ]
        id           scheduled                sent               start
1.67     1 2018-10-30 10:00:18 2018-10-30 10:00:25 2018-10-30 10:00:35
1.68     1 2018-10-30 10:59:50 2018-10-30 11:00:21 2018-10-30 11:01:06
10.648  10 2018-02-05 10:59:44 2018-02-05 09:59:52 2018-02-05 11:00:05
10.647  10 2018-02-05 10:00:11 2018-02-05 10:00:20 2018-02-05 10:01:09
10.649  10 2018-02-05 11:59:56 2018-02-05 12:00:03 2018-02-05 12:00:12
31.2113 31 2018-11-19 11:00:12 2018-11-19 11:00:36 2018-11-19 11:01:00
31.2112 31 2018-11-19 10:00:03                <NA>                <NA>
                        end obsno obsno_lag obsno_dif
1.67    2018-10-30 10:03:19    68        66         2
1.68    2018-10-30 11:01:31    68        68         0
10.648  2018-02-05 11:01:18    18        16         2
10.647  2018-02-05 10:03:01    17        18        -1
10.649  2018-02-05 12:00:58    19        17         2
31.2113 2018-11-19 11:02:36    13        11         2
31.2112                <NA>    12        70       -58