Skip to content

Commit

Permalink
Replaced some use of $ with get since with $ you can get NULL if the …
Browse files Browse the repository at this point in the history
…element does not exist in the named list.
  • Loading branch information
lorenzoFabbri committed Sep 20, 2023
1 parent de6deda commit cd58201
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 353 deletions.
63 changes: 30 additions & 33 deletions R/create_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
#' marginal comparisons.
#'
#' @export
create_df_marginal_comparisons <- function(dat, var, percentiles, by_var) {
# Checks
if (percentiles[1] < 0 | percentiles[2] > 1) {
stop("The percentiles must lie between 0 and 1.",
call. = TRUE)
}
create_df_marginal_comparisons <-
function(dat, var, percentiles, by_var) {
# Checks
if (percentiles[1] < 0 | percentiles[2] > 1) {
stop("The percentiles must lie between 0 and 1.",
call. = TRUE)
}

# Compute `low` and `high` values of the variable of interest, by group
ret <- dat |>
tidylog::group_by(.data[[by_var]]) |>
tidylog::mutate(
low = quantile(.data[[var]], percentiles[1]),
high = quantile(.data[[var]], percentiles[2])
) |>
tidylog::ungroup()
# Compute `low` and `high` values of the variable of interest, by group
ret <- dat |>
tidylog::group_by(.data[[by_var]]) |>
tidylog::mutate(low = quantile(.data[[var]], percentiles[1]),
high = quantile(.data[[var]], percentiles[2])) |>
tidylog::ungroup()

return(ret)
}
return(ret)
}

#' Create a variable's dictionary for its labels
#'
Expand Down Expand Up @@ -101,11 +100,9 @@ add_metadata <- function(dat, metadat, categorical_types) {
metadat <- metadat |>
tidylog::mutate(
type = as.character(type),
type = ifelse(
type %in% categorical_types,
"categorical",
type
)
type = ifelse(type %in% categorical_types,
"categorical",
type)
)

# Add metadata to each column of dataset
Expand All @@ -117,23 +114,23 @@ add_metadata <- function(dat, metadat, categorical_types) {
info <- metadat |>
tidylog::filter(variable == x) |>
as.list()
info$description <- .tidy_string(info$description)
info$remark <- .tidy_string(info$remark)
info$type <- stringr::str_to_lower(info$type)
info$comments <- stringr::str_to_lower(info$comments)
info$description <- .tidy_string(get("description", info))
info$remark <- .tidy_string(get("remark", info))
info$type <- stringr::str_to_lower(get("type", info))
info$comments <- stringr::str_to_lower(get("comments", info))
info <- lapply(info, as.character)

# Add metadata
attr(dat_modified[[x]], "label") <- info$description
attr(dat_modified[[x]], "units") <- info$comments
attr(dat_modified[[x]], "remarks") <- info$remark
attr(dat_modified[[x]], "dag_var") <- info$dag
attr(dat_modified[[x]], "period") <- info$period
if (info$type == "categorical") {
attr(dat_modified[[x]], "label") <- get("description", info)
attr(dat_modified[[x]], "units") <- get("comments", info)
attr(dat_modified[[x]], "remarks") <- get("remark", info)
attr(dat_modified[[x]], "dag_var") <- get("dag", info)
attr(dat_modified[[x]], "period") <- get("period", info)
if (get("type", info) == "categorical") {
dat_modified[[x]] <- labelled::labelled(
dat_modified[[x]],
labels = create_mapping_labels(info$label, info$code),
label = info$description
labels = create_mapping_labels(get("label", info), get("code", info)),
label = get("description", info)
) |>
labelled::to_factor()
}
Expand Down
19 changes: 11 additions & 8 deletions R/ctd.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ load_ctd <- function(path, filter_evidence) {

if (filter_evidence == TRUE) {
ctd <- ctd |>
tidylog::filter(DirectEvidence %in% c("marker/mechanism",
"therapeutic",
"marker/mechanism|therapeutic"))
tidylog::filter(
DirectEvidence %in% c(
"marker/mechanism",
"therapeutic",
"marker/mechanism|therapeutic"
)
)
}

ctd <- ctd |>
tidyr::separate_longer_delim(DiseaseCategories,
delim = "|")

ctd$DiseaseCategories <- factor(ctd$DiseaseCategories,
levels = sort(unique(ctd$DiseaseCategories),
decreasing = TRUE))
Expand All @@ -44,10 +49,8 @@ plot_ctd <- function(dat, group = NULL) {
plt <- dat |>
tidylog::filter(DiseaseCategories != "") |>
tidylog::group_by(DiseaseCategories) |>
ggplot2::ggplot(mapping = ggplot2::aes(
x = DiseaseCategories,
fill = factor(ChemicalName)
)) +
ggplot2::ggplot(mapping = ggplot2::aes(x = DiseaseCategories,
fill = factor(ChemicalName))) +
ggplot2::geom_bar() +
ggplot2::coord_flip() +
ggplot2::theme_minimal() +
Expand All @@ -57,7 +60,7 @@ plot_ctd <- function(dat, group = NULL) {

if (!is.null(group)) {
plt <- plt +
ggplot2::facet_grid(~.data[[ group ]])
ggplot2::facet_grid( ~ .data[[group]])
}

return(plt)
Expand Down
68 changes: 43 additions & 25 deletions R/dags.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ minimize_missings <- function(dat, meta, adjustment_sets,

# List of dataframes with covariates from adjustment sets
dfs_covars <- lapply(adjustment_sets, function(x) {
mapping_covars <- meta[meta$dag %in% x, ]$variable |>
mapping_covars <- meta[get("dag", meta) %in% x,]$variable |>
as.character()
tmp <- dat |>
tidylog::select(dplyr::all_of(c(by_var,
Expand All @@ -68,7 +68,11 @@ minimize_missings <- function(dat, meta, adjustment_sets,
purrr::reduce(dplyr::bind_cols))
colnames(ret_miss) <- paste0("adj_set_", 1:ncol(ret_miss))
ret_miss <- ret_miss |>
tidylog::mutate({{by_var}} := levels_group_var) |>
tidylog::mutate({
{
by_var
}
} := levels_group_var) |>
tidylog::relocate(.data[[by_var]])

# Sum of missing values, for each adjustment set
Expand All @@ -94,9 +98,13 @@ visualize_dag <- function(dag) {
ret <- dag |>
ggdag::tidy_dagitty() |>
ggdag::node_status() |>
ggplot2::ggplot(ggplot2::aes(x, y,
xend = xend, yend = yend,
color = status)) +
ggplot2::ggplot(ggplot2::aes(
x,
y,
xend = xend,
yend = yend,
color = status
)) +
ggdag::geom_dag_edges() +
ggdag::geom_dag_point() +
geom_dag_text_repel() +
Expand Down Expand Up @@ -124,7 +132,7 @@ from_dagitty_to_ggdag <- function(dag) {
dag <- dagitty::dagitty(dag)

to_ggdag <- ggdag::tidy_dagitty(dag) |>
(\(x) x$data) () |>
(\(x) get("data", x)) () |>
tidylog::select(name, to) |>
tidylog::rename(from = name) |>
tidylog::group_by(to) |>
Expand Down Expand Up @@ -173,13 +181,15 @@ test_npsem <- function(dag, dat, meta, params) {
call. = TRUE)

# Step 1: extract adjustment set(s)
dag_as <- dagitty::adjustmentSets(x = dag,
type = params$type_mas,
effect = params$type_effect)
dag_as <- dagitty::adjustmentSets(
x = dag,
type = get("type_mas", params),
effect = get("type_effect", params)
)

res <- lapply(dag_as, function(as) {
ret <- list()
ret$mapping_covars <- meta[meta$dag %in% as, ] |>
ret$mapping_covars <- meta[get("dag", meta) %in% as,] |>
tidylog::distinct(dag, .keep_all = TRUE) |>
tidylog::select(variable) |>
c() |> unname() |> unlist() |> as.character()
Expand All @@ -189,25 +199,33 @@ test_npsem <- function(dag, dat, meta, params) {
covariates <- dat$covariates |>
tidylog::select(params$identifier,
dplyr::all_of(ret$mapping_covars))
colnames(covariates) <- c(params$identifier,
meta[meta$variable %in% ret$mapping_covars, ]$dag |>
colnames(covariates) <- c(get("identifier", params),
meta[get("variable", meta) %in% get("mapping_covars",
ret),]$dag |>
as.character())
covariates <- covariates[, !duplicated(colnames(covariates))]
covariates <- covariates[,!duplicated(colnames(covariates))]

# Step 3: test independencies for each exposure
exposure_list <- setdiff(colnames(dat$exposures), params$identifier)
exposure_list <- setdiff(colnames(get("exposures", dat)),
get("identifier", params))
ret$tests <- lapply(exposure_list, function(expo) {
dat_test <- purrr::reduce(list(covariates,
dat$exposures |>
tidylog::select(params$identifier,
.data[[expo]]),
dat$outcome),
tidylog::full_join,
by = params$identifier)
test <- dagitty::localTests(x = dag,
data = dat_test,
type = "cis.loess",
R = 3)
dat_test <- purrr::reduce(
list(
covariates,
dat$exposures |>
tidylog::select(get("identifier", params),
.data[[expo]]),
dat$outcome
),
tidylog::full_join,
by = get("identifier", params)
)
test <- dagitty::localTests(
x = dag,
data = dat_test,
type = "cis.loess",
R = 3
)
return(test)
}) # End lapply over exposures

Expand Down
Loading

0 comments on commit cd58201

Please sign in to comment.