Skip to content

Commit

Permalink
Use partial correlations as weights
Browse files Browse the repository at this point in the history
  • Loading branch information
psychelzh committed Nov 21, 2024
1 parent f6a1059 commit b104f68
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
21 changes: 12 additions & 9 deletions R/mediation.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ fit_med <- function(model, data, ...,
}

combine_data_ccas <- function(data) {
data_weight <- data |>
summarise(
across(
c(igs, iss),
\(x) cor(x, dprime),
.names = "weight_{.col}"
),
.by = cca_id
)
data_wider <- data |>
pivot_wider(names_from = cca_id, values_from = c(igs, iss))
params_iss <- lm(dprime ~ iss_1 + iss_2 + iss_3, data_wider) |>
parameters::model_parameters(standardize = "refit")
params_igs <- lm(dprime ~ igs_1 + igs_2 + igs_3, data_wider) |>
parameters::model_parameters(standardize = "refit")
data_weight <- bind_rows(params_iss, params_igs) |>
filter(Parameter != "(Intercept)") |>
separate_wider_delim(Parameter, "_", names = c("var", "cca_id")) |>
select(var, cca_id, coef = Coefficient) |>
mutate(cca_id = as.integer(cca_id)) |>
pivot_wider(names_from = var, values_from = coef, names_prefix = "weight_")
data |>
left_join(data_weight, by = "cca_id") |>
summarise(
Expand Down
Loading

0 comments on commit b104f68

Please sign in to comment.