Skip to content

Commit 7130c8b

Browse files
committed
Add min_data argument to sri() function
1 parent ea24009 commit 7130c8b

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# actverse 0.1.0.9000 Development version
1+
# actverse Development version
2+
3+
- `sri()` now `min_data` as an argument, which specifies the minimum proportion of non-missing values required to compute the SRI for each time point. If the proportion of non-missing values is below this threshold, the SRI will be set to `NA` for that time point. This helps to avoid computing the SRI when there is insufficient data.
4+
- `sri()` now returns a `valid_data` column, which indicates the proportion of non-missing values in the `agreement` column for each time point. This provides additional information about the data quality used to compute the SRI.
25

36
# actverse 0.1.0
47

R/sri.R

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
#' @param awake_states (optional) An
4141
#' [`integerish`][checkmate::assert_integerish] vector indicating which states
4242
#' values are considered as awake states.
43+
#' @param min_data (optional) A number indicating the minimum proportion of
44+
#' non-missing values in the agreements required to compute the SRI for each
45+
#' time point. The SRI will only be computed if the proportion of non-missing
46+
#' values is greater than or equal to this threshold. This is useful to avoid
47+
#' computing the SRI when there is insufficient data. Values below this
48+
#' threshold will result in an SRI of `NA` (default: `0.75`).
4349
#'
4450
#' @return A [`tsibble`][tsibble::tsibble()] object with the following columns:
4551
#'
@@ -54,7 +60,7 @@
5460
#' - `sri`: A [`numeric`][base::numeric()] vector representing the Sleep
5561
#' Regularity Index (SRI). See the Details section to learn more about how
5662
#' the SRI is computed.
57-
#' - `valid_agreement`: A [`numeric`][base::numeric()] vector representing the
63+
#' - `valid_data`: A [`numeric`][base::numeric()] vector representing the
5864
#' proportion of non-missing values in the `agreement` column, i.e., the
5965
#' amount of information available to compute the SRI for each time point.
6066
#'
@@ -109,7 +115,8 @@ sri <- function(
109115
data,
110116
state_col = "state",
111117
sleeping_states = 1,
112-
awake_states = c(0, 2)
118+
awake_states = c(0, 2),
119+
min_data = 0.75
113120
) {
114121
assert_tsibble(data)
115122
assert_regularity(data, strict = TRUE)
@@ -120,10 +127,12 @@ sri <- function(
120127
checkmate::assert_integerish(awake_states, min.len = 1)
121128
checkmate::assert_subset(awake_states, data[[state_col]])
122129
prettycheck::assert_posixt(data[[tsibble::index_var(data)]])
130+
checkmate::assert_number(min_data, lower = 0, upper = 1)
123131

124132
# R CMD Check variable bindings fix
125133
# nolint start
126134
timestamp <- time <- state <- previous_state <- agreement <- NULL
135+
valid_data <- sri <- NULL
127136
# nolint end
128137

129138
interval <- data |> find_epoch() |> magrittr::extract2("best_match")
@@ -162,9 +171,14 @@ sri <- function(
162171
agreement |>
163172
purrr::map_dbl(\(x) prop(x, TRUE, na_rm = TRUE)) |>
164173
scales::rescale(to = c(-100, 100), from = c(0, 1)),
165-
valid_agreement =
174+
valid_data =
166175
agreement |>
167-
purrr::map_dbl(\(x) prop(!is.na(x), TRUE))
176+
purrr::map_dbl(\(x) prop(!is.na(x), TRUE)),
177+
sri = dplyr::if_else(
178+
valid_data < min_data,
179+
NA_real_,
180+
sri
181+
)
168182
) |>
169183
dplyr::arrange(time) |>
170184
tsibble::as_tsibble(index = time, regular = TRUE)

man/sri.Rd

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

0 commit comments

Comments
 (0)