Skip to content

Commit 35bbdae

Browse files
committed
added components in the app to manage epochs with extremely high values detected in the Axis 1
1 parent c187426 commit 35bbdae

File tree

9 files changed

+133
-23
lines changed

9 files changed

+133
-23
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# activAnalyzer (development version)
22

3+
* Added the possibility to see a threshold highlighting abnormal values for axis 1 in the nonwear/wear graph. In addition, abnormal values for Axis 1 and dependant metrics (e.g. VM) can now be replaced by NA.
4+
35
# activAnalyzer 2.1.2
46
* Removed an undesired comma from a req() function in app_server.R that caused an error with more recent versions of package dependencies (likely Shiny).
57
* Updated the figures for the comparisons with norms and recommendations (legend is now correctly placed at the top).

R/app_server.R

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ app_server <- function(input, output, session) {
262262
# Ehcv
263263
observeEvent(input$validate,
264264
shinyFeedback::feedbackWarning(
265-
"ehcv",
266-
(is.numeric(input$ehcv) == FALSE | input$ehcv < 0),
265+
"ehcv_val1",
266+
(is.numeric(input$ehcv_val1) == FALSE | input$ehcv_val1 < 0),
267267
"Please choose a number >= 0."
268268
)
269269
)
@@ -365,7 +365,7 @@ app_server <- function(input, output, session) {
365365
# Returning to default values for the wear time detection algorithm and ehcv
366366
observeEvent(input$reset_nonwear, {
367367
updateNumericInput(inputId = "to_epoch", value = 60)
368-
updateNumericInput(inputId = "ehcv", value = 15000)
368+
updateNumericInput(inputId = "ehcv_val1", value = 15000)
369369
updateSelectInput(inputId = "axis_weartime", selected = "vector magnitude")
370370
updateNumericInput(inputId = "frame_size", value = 90)
371371
updateNumericInput(inputId = "allowanceFrame_size", value = 2)
@@ -420,19 +420,19 @@ app_server <- function(input, output, session) {
420420

421421
observeEvent(input$update_graphic,
422422
shinyFeedback::feedbackWarning(
423-
"ehcv",
424-
(is.numeric(input$ehcv) == FALSE | input$ehcv < 0),
423+
"ehcv_val1",
424+
(is.numeric(input$ehcv_val1) == FALSE | input$ehcv_val1 < 0),
425425
"Please choose a number >= 0."
426426
)
427427
)
428428

429429

430-
ehcv <- eventReactive(input$validate, input$ehcv)
430+
ehcv_val1 <- eventReactive(input$validate, input$ehcv_val1)
431431

432432
graph <- eventReactive(input$validate | input$update_graphic, {
433433

434434
# Waiting for correct inputs
435-
req(zoom_param$zoom_from_weartime < zoom_param$zoom_to_weartime & is.numeric(input$ehcv) & input$ehcv >= 0)
435+
req(zoom_param$zoom_from_weartime < zoom_param$zoom_to_weartime & is.numeric(input$ehcv_val1) & input$ehcv_val1 >= 0)
436436

437437
# Making the plot
438438
if (as.numeric(df()$time[2] - df()$time[1]) < 10) {
@@ -452,7 +452,7 @@ app_server <- function(input, output, session) {
452452
plot_data(
453453
data = df(),
454454
metric = zoom_param$metric,
455-
ehcv = ehcv(),
455+
ehcv = ehcv_val1(),
456456
zoom_from = zoom_param$zoom_from_weartime,
457457
zoom_to = zoom_param$zoom_to_weartime
458458
)
@@ -851,6 +851,14 @@ app_server <- function(input, output, session) {
851851
)
852852
)
853853

854+
# Threshold for data removal
855+
observeEvent(input$Run,
856+
shinyFeedback::feedbackWarning(
857+
"ehcv_val2",
858+
((is.numeric(input$ehcv_val2) == FALSE | input$ehcv_val2 < 0)),
859+
"Please provide a value >=0."
860+
)
861+
)
854862

855863
# Intensity bins parameters
856864
observeEvent(input$Run,
@@ -1402,6 +1410,14 @@ app_server <- function(input, output, session) {
14021410
return(list)
14031411

14041412
})
1413+
1414+
# Setting ehcv for dealing with the removal of abnormal epochs
1415+
ehcv_val2 <- eventReactive(input$Run, {
1416+
1417+
if (!(input$ehcv_check)) {ehcv <- "none"}
1418+
if (input$ehcv_check) {ehcv <- input$ehcv_val2}
1419+
ehcv
1420+
})
14051421

14061422
# Building the list
14071423
results_list <- eventReactive(input$Run, {
@@ -1544,11 +1560,13 @@ app_server <- function(input, output, session) {
15441560
period_info_12$corr_mets() >= 0,
15451561
period_info_13$corr_mets() >= 0,
15461562
period_info_14$corr_mets() >= 0,
1547-
period_info_15$corr_mets() >= 0
1548-
)
1549-
1550-
1551-
1563+
period_info_15$corr_mets() >= 0,
1564+
1565+
# Settings for abnormal epoch removal
1566+
is.numeric(input$ehcv_val2) & input$ehcv_val2 >= 0
1567+
)
1568+
1569+
15521570
# Building the dataframe with intensity marks
15531571
df_with_computed_metrics <-
15541572
df() %>%
@@ -1561,7 +1579,8 @@ app_server <- function(input, output, session) {
15611579
age = input$age,
15621580
weight = input$weight,
15631581
sex = input$sex,
1564-
dates = input$selected_days
1582+
dates = input$selected_days,
1583+
ehcv = ehcv_val2()
15651584
)
15661585

15671586
shiny::setProgress(0.5) # set progress to 50%

R/app_ui.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ app_ui <- function(request) {
226226
),
227227
fluidRow(
228228
column(12,
229-
numericInput("ehcv", "Threshold to highlight abnormal values for Axis 1 (counts/min)", value = 15000, min = 0)
229+
numericInput("ehcv_val1", "Threshold to highlight abnormal values for Axis 1 (counts/min)", value = 15000, min = 0)
230230
),
231231
),
232232
fluidRow(
@@ -404,6 +404,24 @@ app_ui <- function(request) {
404404
),
405405
),
406406

407+
#*************************
408+
# Removing abnormal values
409+
#*************************
410+
411+
fluidRow(
412+
column(12,
413+
h3("Tick the box and adjust the threshold to detect epochs corresponding to abnormal counts in Axis 1 (not mandatory)."),
414+
),
415+
),
416+
417+
fluidRow(
418+
column(6,
419+
hr(),
420+
checkboxInput("ehcv_check", "Use threshold to remove abnormal epochs (Detected epochs will be considered as nonwear epochs with NAs)."),
421+
numericInput("ehcv_val2", "Threshold related to Axis 1 (counts/min)", value = 15000, min = 0))
422+
),
423+
424+
407425
#*************************************
408426
# Choosing intensity bins
409427
#*************************************

R/mark_intensity.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#' @param weight A numeric value in kg.
3838
#' @param sex A character value.
3939
#' @param dates A character vector containing the dates to be retained for analysis. The dates must be with the "YYYY-MM-DD" format.
40+
#' @param ehcv A numeric value to set the threshold above which Axis 1 data should be considered as extremely high (abnormal).
41+
#' The value should be in counts/min. Default is "none". If a value is set, all Axis 1/2/3 data, VM data and related metrics corresponding to the epochs for which the counts
42+
#' in Axis 1 are equal or higher to the threshold will be replaced by NA.
4043

4144
#' @return A dataframe.
4245
#' @export
@@ -63,6 +66,7 @@
6366
#' age = 32,
6467
#' weight = 67,
6568
#' sex = "male",
69+
#' ehcv = 15000
6670
#' )
6771
#' head(mydata_with_intensity_marks)
6872
#'
@@ -81,7 +85,9 @@ mark_intensity <- function(data,
8185
age = 40,
8286
weight = 70,
8387
sex = c("male", "female", "intersex", "undefined", "prefer not to say"),
84-
dates = NULL) {
88+
dates = NULL,
89+
ehcv = "none"
90+
) {
8591

8692

8793
if (is.null(dates)) {selected_dates <- attributes(as.factor(data$date))$levels}
@@ -144,7 +150,28 @@ mark_intensity <- function(data,
144150
df$bout <- cumsum(c(1, as.numeric(diff(df$intensity_category_num))!= 0))
145151

146152

153+
# Setting count-based data and related data to NA for abnormal metric if required
154+
155+
if (ehcv != "none" & is.numeric(ehcv) & ehcv >= 0) {
156+
157+
ehcv <- ehcv / cor_factor
147158

159+
df$axis1 <- dplyr::if_else(df$axis1 >= ehcv, NA, df$axis1)
160+
df$axis2 <- dplyr::if_else(df$axis1 >= ehcv, NA, df$axis2)
161+
df$axis3 <- dplyr::if_else(df$axis1 >= ehcv, NA, df$axis3)
162+
df$vm <- dplyr::if_else(df$axis1 >= ehcv, NA, df$vm)
163+
df$steps <- dplyr::if_else(df$axis1 >= ehcv, NA, df$steps)
164+
df$wearing <- dplyr::if_else(df$axis1 >= ehcv, "nw", df$wearing)
165+
df$non_wearing_count <- dplyr::if_else(df$axis1 >= ehcv, 1, df$non_wearing_count)
166+
df$wearing_count <- dplyr::if_else(df$axis1 >= ehcv, 0, df$wearing_count)
167+
df$SED <- dplyr::if_else(df$axis1 >= ehcv, NA, df$SED)
168+
df$LPA <- dplyr::if_else(df$axis1 >= ehcv, NA, df$LPA)
169+
df$MPA <- dplyr::if_else(df$axis1 >= ehcv, NA, df$MPA)
170+
df$VPA <- dplyr::if_else(df$axis1 >= ehcv, NA, df$VPA)
171+
df$METS <- dplyr::if_else(df$axis1 >= ehcv, NA, df$METS)
172+
df$kcal <- dplyr::if_else(df$axis1 >= ehcv, NA, df$kcal)
173+
df$mets_hours_mvpa <- dplyr::if_else(df$axis1 >= ehcv, NA, df$mets_hours_mvpa)
174+
}
148175

149176
# Providing information about the parameters used for computing results
150177
message(paste0("You have computed intensity metrics with the mark_intensity() function using the following inputs:

R/plot_data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param col_time A character value to indicate the name of the variable to plot time data.
99
#' @param col_nonwear A character value to indicate the name of the variable used to count nonwear time.
1010
#' @param col_wear A character value to indicate the name of the variable used to count wear time.
11-
#' @param ehcv A numeric value to set the threshold above which vertical axis data should be considered as extremely high (abnormal).
11+
#' @param ehcv A numeric value to set the threshold above which Axis 1 data should be considered as extremely high (abnormal).
1212
#' The value should be in counts/min.
1313
#' @param zoom_from A character value with the HH:MM:SS format to set the start of the daily period to visualize.
1414
#' @param zoom_to A character value with the HH:MM:SS format to set the end of the daily period to visualize.

R/recap_by_day.R

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@
6868
#' @param start_first_bin A numeric value to set the lower bound of the first bin of the intensity band (in counts/epoch duration).
6969
#' @param start_last_bin A numeric value to set the lower bound of the last bin of the intensity band (in counts/epoch duration).
7070
#' @param bin_width A numeric value to set the width of the bins of the intensity band (in counts/epoch duration).
71-
#'
71+
#' @param ehcv A numeric value to set the threshold above which Axis 1 data should be considered as extremely high (abnormal).
72+
#' The value should be in counts/min. Default is "none". If a value is set, step-based metrics corresponding to the epochs for which the counts
73+
#' in Axis 1 are equal or higher to the threshold will be replaced by NA. The correction of the other metrics should be done when marking the dataset
74+
#' with the different categories of intensity.
75+
#'
7276
#' @return A list of objects: `df_all_metrics`, `p_band`, and `p_log`.
7377
#' `df_all_metrics` is a dataframe containing all the metrics for each day.
7478
#' `p_band` is a figure that shows the distribution of time spent in the configured bins of intensity for each day of the dataset.
@@ -130,7 +134,8 @@ recap_by_day <- function(
130134
sex = c("male", "female", "intersex", "undefined", "prefer not to say"),
131135
start_first_bin = 0,
132136
start_last_bin = 10000,
133-
bin_width = 500
137+
bin_width = 500,
138+
ehcv = "none"
134139
) {
135140

136141
sex <- match.arg(sex)
@@ -191,11 +196,22 @@ recap_by_day <- function(
191196

192197
if (as.numeric(data[[col_time]][2] - data[[col_time]][1]) == 60) {
193198

199+
## Prepare dataset
194200
df_step_metrics <-
195201
data %>%
196202
dplyr::mutate(timestamp = format(timestamp, "%Y-%m-%d %H:%M:%S")) %>%
197203
tidyr::separate("timestamp", c("date", "time"), sep = " ") %>%
198-
dplyr::mutate(date = as.Date(date), time = hms::as_hms(time)) %>%
204+
dplyr::mutate(date = as.Date(date), time = hms::as_hms(time))
205+
206+
## Remove epochs with abnormal counts
207+
if (ehcv != "none" & is.numeric(ehcv) & ehcv >= 0) {
208+
ehcv <- ehcv / cor_factor
209+
df_step_metrics$steps <- dplyr::if_else(df_step_metrics$axis1 >= ehcv, NA, df_step_metrics$steps)
210+
}
211+
212+
## Compute step-based metrics
213+
df_step_metrics <-
214+
df_step_metrics %>%
199215
dplyr::select(
200216
date,
201217
time,
@@ -221,6 +237,7 @@ recap_by_day <- function(
221237
)
222238
} else {
223239

240+
## Prepare dataset
224241
df_step_metrics <-
225242
PhysicalActivity::dataCollapser(
226243
dataset = data,
@@ -229,7 +246,17 @@ recap_by_day <- function(
229246
) %>%
230247
dplyr::mutate(timestamp = format(timestamp, "%Y-%m-%d %H:%M:%S")) %>%
231248
tidyr::separate("timestamp", c("date", "time"), sep = " ") %>%
232-
dplyr::mutate(date = as.Date(date), time = hms::as_hms(time)) %>%
249+
dplyr::mutate(date = as.Date(date), time = hms::as_hms(time))
250+
251+
## Remove epochs with abnormal counts
252+
if (ehcv != "none" & is.numeric(ehcv) & ehcv >= 0) {
253+
ehcv <- ehcv / cor_factor
254+
df_step_metrics$steps <- dplyr::if_else(df_step_metrics$axis1 >= ehcv, NA, df_step_metrics$steps)
255+
}
256+
257+
## Compute step-based metrics
258+
df_step_metrics <-
259+
df_step_metrics %>%
233260
dplyr::select(
234261
date,
235262
time,

man/mark_intensity.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_data.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/recap_by_day.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)