|
| 1 | +#' generate_shams |
| 2 | +#' |
| 3 | +#' Generates a permutation of each individual dyad. Shuffled dyads may act as controls to their originals. |
| 4 | +#' |
| 5 | +#' @name generate_shams |
| 6 | +#' @param df_prep Output dataframe of prep_dyads(). |
| 7 | +#' @param seed (Optional) a seed for reproducibility in random sampling |
| 8 | +#' @returns |
| 9 | +#' A dataframe similar to prepped dyads, with each participant's time series randomly shuffled. |
| 10 | +#' @importFrom magrittr %>% |
| 11 | +#' @importFrom dplyr group_by |
| 12 | +#' @importFrom dplyr summarize |
| 13 | +#' @importFrom dplyr across |
| 14 | +#' @importFrom dplyr mutate |
| 15 | +#' @importFrom dplyr n |
| 16 | +#' @export |
| 17 | + |
| 18 | +generate_shams <- function(df_prep, seed = NULL) { |
| 19 | + # if a seed is given, set it |
| 20 | + if (is.null(seed)) { # if not given, pick a random seed |
| 21 | + seed = sample(1:100000, size = 1) |
| 22 | + } |
| 23 | + |
| 24 | + # summarize down to turn means |
| 25 | + turn_mean_df <- df_prep %>% |
| 26 | + dplyr::group_by(Event_ID, Exchange_Count, Participant_ID) %>% |
| 27 | + dplyr::summarize( |
| 28 | + dplyr::across( |
| 29 | + matches("^(emo_|lex_|phon_|sem_|df_)"), |
| 30 | + ~mean(.x, na.rm = T) |
| 31 | + ), |
| 32 | + # these can be included as a sanity check |
| 33 | + Text_Prep = paste(Text_Prep, collapse = " "), |
| 34 | + Text_Clean = paste(Text_Clean, collapse = " "), |
| 35 | + .groups = "drop" |
| 36 | + ) |
| 37 | + |
| 38 | + # define function that will allow each column to be sampled identically |
| 39 | + sample_seed <- function(x, seed) { |
| 40 | + set.seed(seed) |
| 41 | + return(sample(x, size = length(x), replace = F)) |
| 42 | + } |
| 43 | + |
| 44 | + # shuffle each participant's time series |
| 45 | + sham_df <- turn_mean_df %>% |
| 46 | + dplyr::group_by(Event_ID, Participant_ID) %>% |
| 47 | + dplyr::mutate( |
| 48 | + dplyr::across( |
| 49 | + c(matches("^(emo_|lex_|phon_|sem_|df_)"), Text_Prep, Text_Clean), |
| 50 | + ~sample_seed(.x, seed = seed) |
| 51 | + ) |
| 52 | + ) %>% |
| 53 | + dplyr::group_by(Event_ID) %>% |
| 54 | + dplyr::mutate(Turn_Count = 1:dplyr::n(), .after = Event_ID) |
| 55 | + |
| 56 | + return(sham_df) |
| 57 | +} |
0 commit comments