Skip to content

Commit

Permalink
shuffle_spec refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yjunechoe committed Jul 25, 2023
1 parent a1672e7 commit 667b9bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion R/permute.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ permute_by_predictor <- function(jlmer_spec, predictors, predictor_type = c("gue
time <- jlmer_spec$meta$time
predictor_type <- match.arg(predictor_type)
if (predictor_type == "guess") {
predictor_type <- .jlmerclusterperm$jl$guess_shuffle_as(df_jl, predictors, subject, trial)
predictor_type <- .jlmerclusterperm$jl$guess_shuffle_spec(df_jl, predictors, subject, trial)$shuffle_type
cli::cli_alert_info("Guessed {.arg predictor_type} to be {.val {predictor_type}}")
}
predictor_group <- Filter(function(x) any(predictors %in% x), jlmer_spec$meta$term_groups)
Expand Down
4 changes: 2 additions & 2 deletions docs/articles/Garrison-et-al-2020.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 25 additions & 19 deletions inst/julia/JlmerClusterPerm/src/05-permute.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
function guess_and_shuffle_as!(
df::DataFrame,
predictor_cols::Union{String,Vector{String}},
participant_col::String,
trial_col::Union{Nothing,Integer,String},
)
shuffle_type = guess_shuffle_as(df, predictor_cols, participant_col, trial_col)
shuffle_as!(df, predictor_cols, participant_col, trial_col, shuffle_type)
end

function guess_shuffle_as(
function guess_shuffle_spec(
df::DataFrame,
predictor_cols::Union{String,Vector{String}},
participant_col::String,
Expand All @@ -17,30 +7,45 @@ function guess_shuffle_as(
subj_pred_pair = unique(df[!, vcat(participant_col, predictor_cols)])
unique_combinations = length(unique(df[!, participant_col])) == nrow(subj_pred_pair)
if unique_combinations
"between_participant"
shuffle_type = "between_participant"
else
if (ismissing(trial_col) || isnothing(trial_col))
throw("""Guessed "within_participant" but no column for `trial` supplied.""")
else
"within_participant"
shuffle_type = "within_participant"
end
end
make_shuffle_spec(df, shuffle_type, predictor_cols, participant_col)
end

function shuffle_as!(
function make_shuffle_spec(
df::DataFrame,
shuffle_type::String,
predictor_cols::Union{String,Vector{String}},
participant_col::String,
)
if shuffle_type == "between_participant"
subj_pred_pair = unique(df[!, vcat(participant_col, predictor_cols)])
(shuffle_type = shuffle_type, subj_pred_pair = subj_pred_pair)
elseif shuffle_type == "within_participant"
(shuffle_type = shuffle_type,)
end
end

function shuffle_as!(
df::DataFrame,
shuffle_spec::NamedTuple,
predictor_cols::Union{String,Vector{String}},
participant_col::String,
trial_col::Union{Nothing,Integer,String},
rng::AbstractRNG,
)
if shuffle_type == "between_participant"
subj_pred_pair = unique(df[!, vcat(participant_col, predictor_cols)])
if shuffle_spec.shuffle_type == "between_participant"
subj_pred_pair = shuffle_spec.subj_pred_pair
shuffle!(rng, subj_pred_pair[!, participant_col])
select!(df, Not(predictor_cols))
leftjoin!(df, subj_pred_pair, on = participant_col)
elseif shuffle_type == "within_participant"
elseif shuffle_spec.shuffle_type == "within_participant"
trial_pred_pair = unique(df[!, vcat(participant_col, trial_col, predictor_cols)])
combine(
groupby(trial_pred_pair, participant_col),
Expand All @@ -61,10 +66,11 @@ function permute_by_predictor(
global_opts::NamedTuple,
)
_df = copy(df)
shuffle_spec = make_shuffle_spec(df, shuffle_type, predictor_cols, participant_col)
out = insertcols(
shuffle_as!(
_df,
shuffle_type,
shuffle_spec,
predictor_cols,
participant_col,
trial_col,
Expand All @@ -79,7 +85,7 @@ function permute_by_predictor(
insertcols(
shuffle_as!(
_df,
shuffle_type,
shuffle_spec,
predictor_cols,
participant_col,
trial_col,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function permute_timewise_statistics(
for term_groups in term_groups_est
predictors = term_groups.p
permute_data = copy(data)
shuffle_type = guess_shuffle_as(
shuffle_spec = guess_shuffle_spec(
permute_data,
predictors,
participant_col,
Expand All @@ -70,7 +70,7 @@ function permute_timewise_statistics(
for i = 1:nsim
shuffle_as!(
permute_data,
shuffle_type,
shuffle_spec,
predictors,
participant_col,
trial_col,
Expand Down

0 comments on commit 667b9bd

Please sign in to comment.