@@ -98,18 +98,16 @@ compute_accumulation_metrics <- function(
98
98
zoom_to = " 23:59:59"
99
99
){
100
100
101
- # Filtering data based on selected dates and time periods, and adding a
102
- # column containing "SED", NON-SED", or "Nonwear" labels
101
+ # Getting selected dates
103
102
if (is.null(dates )) {
104
103
selected_dates <- attributes(as.factor(data $ date ))$ levels
105
104
} else {
106
105
selected_dates <- attributes(as.factor(dates ))$ levels
107
106
}
108
107
109
- # Fix bug: Convert Nas to "Nonwear"
110
- data [[col_cat_int ]] <- dplyr :: if_else(is.na(data [[col_cat_int ]]), " Nonwear" , data [[col_cat_int ]])
111
-
112
- data <-
108
+ # Filtering data based on selected dates and time periods, and adding a
109
+ # column 'new_intensity_category' containing "SED", PA, or "Nonwear" values
110
+ data <-
113
111
data %> %
114
112
dplyr :: filter(
115
113
date %in% as.Date(selected_dates ) &
@@ -127,28 +125,28 @@ if (is.null(dates)) {
127
125
" SED" ,
128
126
dplyr :: if_else(
129
127
.data [[col_cat_int ]] == " Nonwear" ,
130
- " Nonwear" ,
131
- dplyr :: if_else(is.na(.data [[col_cat_int ]]), " Nonwear" , " Nonwear" )
128
+ " Nonwear" , NA
132
129
)
133
130
)
134
131
)
135
132
)
136
133
137
- # Updating bouts IDs
134
+ # Setting IDs for the new PA/SED/Nonwear bouts
138
135
data $ new_intensity_category <- as.factor(data $ new_intensity_category )
139
136
data $ new_intensity_category_num <- as.numeric(as.character(forcats :: fct_recode(data $ new_intensity_category , " 0" = " Nonwear" , " 1" = " SED" , " 2" = " PA" )))
140
137
data $ new_bout <- cumsum(c(1 , as.numeric(diff(data $ new_intensity_category_num ))!= 0 ))
141
138
142
- # Getting arguments
139
+ # Getting the type of activity bout to be analyzed
143
140
behaviour <- match.arg(behaviour )
144
141
if (behaviour == " sed" ) {BEHAV <- " SED" ; color_fill = c(" #D9DBE5" , " #A6ADD5" , " #6A78C3" , " #3F51B5" ); auto_text = " sedentary" }
145
142
if (behaviour == " pa" ) {BEHAV <- " PA" ; color_fill = c(" #EDD3DD" , " #F38DB6" , " #FA3B87" , " #FF0066" ); auto_text = " physical activity" }
146
143
147
- # Getting correction factor related to the epoch length (reference epoch = 60 s);
144
+ # Getting the correction factor related to the epoch length (reference epoch = 60 s);
148
145
# bout durations are computed in minutes
149
146
cor_factor = 60 / (as.numeric(data [[col_time ]][2 ] - data [[col_time ]][1 ]))
150
147
151
- # Summarising bout durations (in minutes) of interest by day
148
+ # Getting all the identified bouts of the kind of interest and their respective
149
+ # durations (in minutes)
152
150
recap_bouts_by_day <-
153
151
data %> %
154
152
dplyr :: group_by(date , new_bout , new_intensity_category ) %> %
@@ -166,7 +164,7 @@ recap_bouts_by_day <-
166
164
)
167
165
168
166
169
- # Computing mean daily number of breaks
167
+ # Computing the mean of the daily number of breaks
170
168
mean_breaks <-
171
169
recap_bouts_by_day %> %
172
170
dplyr :: ungroup(new_bout , new_intensity_category ) %> %
@@ -206,19 +204,21 @@ mean_breaks <-
206
204
fill = dur_cat
207
205
)
208
206
) +
209
- geom_rect(aes(
207
+ annotate(
208
+ geom = " rect" ,
210
209
xmin = hms :: as_hms(0 ),
211
210
xmax = hms :: as_hms(valid_wear_time_start ),
212
211
ymin = - Inf ,
213
- ymax = Inf ) ,
212
+ ymax = Inf ,
214
213
color = " grey" ,
215
214
fill = " grey"
216
215
) +
217
- geom_rect(aes(
216
+ annotate(
217
+ geom = " rect" ,
218
218
xmin = hms :: as_hms(valid_wear_time_end ),
219
219
xmax = hms :: as_hms(" 23:59:59" ),
220
220
ymin = - Inf ,
221
- ymax = Inf ) ,
221
+ ymax = Inf ,
222
222
color = " grey" ,
223
223
fill = " grey"
224
224
) +
@@ -266,7 +266,8 @@ mean_breaks <-
266
266
geom_vline(aes(xintercept = 3600 * 22 ), linetype = " dotted" , color = " grey50" ) +
267
267
geom_vline(aes(xintercept = 3600 * 23 ), linetype = " dotted" , color = " grey50" )
268
268
269
- # Summarising bout durations (in minutes) of interest without grouping by day
269
+ # Getting all the identified bouts of the kind of interest and their respective
270
+ # durations (in minutes) without grouping by day
270
271
recap_bouts <-
271
272
data %> %
272
273
dplyr :: group_by(new_bout , new_intensity_category ) %> %
@@ -308,7 +309,7 @@ summarised_bouts <-
308
309
)
309
310
310
311
311
- # Fitting cumulated fraction of time vs bout duration
312
+ # Fitting cumulated fraction of time vs bout duration relationship
312
313
model <- nls(
313
314
cum_frac_time ~ duration ^ x / (duration ^ x + UBD ^ x ),
314
315
data = summarised_bouts ,
@@ -333,26 +334,26 @@ max_bout_duration <- max(summarised_bouts$duration)
333
334
duration = seq(xmin , max_bout_duration , 0.1 )
334
335
) %> %
335
336
dplyr :: mutate(
336
- pred = duration ^ (- alpha ) ,
337
+ pred = duration ^ (- alpha ),
337
338
pred = duration ^ (- alpha ) / max(pred ) * max(summarised_bouts $ n , na.rm = TRUE )
338
339
)
339
340
340
341
# Building the graphic
341
342
p_alpha <-
342
- ggplot(data = recap_bouts ) +
343
+ ggplot(data = recap_bouts | > dplyr :: ungroup( new_bout ) ) +
343
344
geom_histogram(aes(x = duration , fill = dur_cat ), binwidth = xmin ) +
344
345
scale_fill_manual(values = color_fill ) +
345
346
labs(x = " Bout duration (min)" , y = " n" , fill = " Duration (min)" ) +
346
- geom_line(data = df_pred_alpha , aes(x = duration , y = pred ), linewidth = 0.8 , color = " grey10" ) +
347
+ geom_line(data = df_pred_alpha , aes(x = duration , y = pred ), linewidth = 0.5 , color = " grey10" ) +
347
348
annotate(" text" , x = max_bout_duration / 2 , y = max(summarised_bouts $ n , na.rm = TRUE )/ 2 , label = paste(" alpha =" , round(alpha , 2 )), hjust = 0.5 , size = 6 , vjust = 0.5 ) +
348
349
theme_bw() +
349
350
theme(legend.position = " bottom" )
350
351
351
352
# Building a graphic for MBD
352
353
p_MBD <-
353
- ggplot(data = recap_bouts ) +
354
+ ggplot(data = recap_bouts | > dplyr :: ungroup( new_bout ) ) +
354
355
geom_histogram(aes(x = duration , fill = dur_cat ), binwidth = xmin ) +
355
- geom_segment(aes( x = MBD , xend = MBD , y = 0 , yend = max(summarised_bouts $ n , na.rm = TRUE ) ), linetype = " dashed" ) +
356
+ annotate( geom = " segment " , x = MBD , xend = MBD , y = 0 , yend = max(summarised_bouts $ n , na.rm = TRUE ), linetype = " dashed" ) +
356
357
scale_fill_manual(values = color_fill ) +
357
358
labs(x = " Bout duration (min)" , y = " n" , fill = " Duration (min)" ) +
358
359
geom_segment(
@@ -377,10 +378,10 @@ p_MBD <-
377
378
p_UBD <-
378
379
ggplot(data = summarised_bouts , aes(x = duration , y = cum_frac_time )) +
379
380
geom_point(aes(color = dur_cat ), size = 6 ) +
380
- geom_segment(aes( x = 0 , y = 0.5 , xend = UBD , yend = 0.5 ) , linetype = " dashed" , linewidth = 0.5 ) +
381
- geom_segment(aes( x = UBD , y = 0.5 , xend = UBD , yend = 0 ) , linetype = " dashed" , linewidth = 0.5 ) +
382
- geom_line(data = df_pred_UBD , aes(x = duration , y = pred ), linewidth = 0.8 , color = " grey10" ) +
383
- geom_segment(aes( x = max_bout_duration / 2 , y = 0.4 , xend = UBD , yend = 0 ) , arrow = arrow(length = unit(0.02 , " npc" ))) +
381
+ annotate( geom = " segment " , x = 0 , y = 0.5 , xend = UBD , yend = 0.5 , linetype = " dashed" , linewidth = 0.5 ) +
382
+ annotate( geom = " segment " , x = UBD , y = 0.5 , xend = UBD , yend = 0 , linetype = " dashed" , linewidth = 0.5 ) +
383
+ geom_line(data = df_pred_UBD , aes(x = duration , y = pred ), linewidth = 0.5 , color = " grey10" ) +
384
+ annotate( geom = " segment " , x = max_bout_duration / 2 , y = 0.4 , xend = UBD , yend = 0 , arrow = arrow(length = unit(0.02 , " npc" ))) +
384
385
annotate(" text" , x = max_bout_duration / 2 , y = 0.4 , label = paste(" UBD =" , round(UBD , 1 ), " min" ), hjust = 0 , size = 6 , vjust = 0 ) +
385
386
labs(x = " Bout duration (min)" , y = paste(" Cumulated fraction of total" , auto_text , " time" ), color = " Duration (min)" ) +
386
387
scale_color_manual(values = color_fill ) +
@@ -417,7 +418,7 @@ p_gini <-
417
418
geom_ribbon(aes(x = cum_frac_bout , ymin = cum_frac_time , ymax = cum_frac_bout ), fill = alpha(color_fill [[2 ]], 0.3 )) +
418
419
geom_point(data = summarised_bouts2 , aes(color = dur_cat ), size = 6 ) +
419
420
geom_segment(x = 0 , xend = 1 , y = 0 , yend = 1 , linewidth = 0.3 ) +
420
- geom_line(linewidth = 0.6 ) +
421
+ geom_line(linewidth = 0.5 ) +
421
422
scale_color_manual(values = color_fill ) +
422
423
coord_cartesian(xlim = c(0 , 1 ), ylim = c(0 , 1 )) +
423
424
labs(
0 commit comments