Skip to content

Commit 805d9a4

Browse files
authored
Merge pull request #7 from Ben-Sacks/main
add generate_shams() and documentation
2 parents a4997f8 + 2c3e914 commit 805d9a4

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Collate:
5050
'compute_lagcorr.R'
5151
'corpus_analytics.R'
5252
'data.R'
53+
'generate_shams.R'
5354
'globals.R'
5455
'prep_dyads.R'
5556
'read_1file.R'

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(corpus_analytics)
4+
export(generate_shams)
45
export(prep_dyads)
56
export(read_1file)
67
export(read_dyads)
@@ -18,6 +19,7 @@ importFrom(dplyr,lag)
1819
importFrom(dplyr,left_join)
1920
importFrom(dplyr,matches)
2021
importFrom(dplyr,mutate)
22+
importFrom(dplyr,n)
2123
importFrom(dplyr,n_distinct)
2224
importFrom(dplyr,na_if)
2325
importFrom(dplyr,rename)

R/generate_shams.R

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

man/generate_shams.Rd

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

0 commit comments

Comments
 (0)