Skip to content

Instantly share code, notes, and snippets.

@mcfrank
Created March 5, 2024 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfrank/34b4c675f15e8e7d422440b3480e37fb to your computer and use it in GitHub Desktop.
Save mcfrank/34b4c675f15e8e7d422440b3480e37fb to your computer and use it in GitHub Desktop.
Finding habituators
# relies on zoo package
# function to find the baseline value
baseline_looking <- function (lts) {
# select lts > 12s
lts_12 <- lts[lts > 12 & !is.na(lts)]
# sum the first three
baseline <- sum(lts_12[1:3], na.rm=TRUE)
return(baseline)
}
# function to decide whether looking time decreased by half
habituated <- function(lts) {
# get baseline
baseline <- baseline_looking(lts)
# cumulative sum less than baseline
three_trial_window <- zoo::rollsum(lts, 3, align = "left", fill = NA)
# if there's no baseline then habituation is false
if (is.na(baseline)) {
habituated <- FALSE
} else {
habituated <- any(three_trial_window < baseline/2)
}
return(habituated)
}
habituators <- d |>
group_by(lab_id, subj_num) |>
summarise(habituated = habituated(lts))
d <- left_join(d, habituators)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment