R/run_exwas_cohort_out.R

Defines functions run_exwas_cohort_out

Documented in run_exwas_cohort_out

run_exwas_cohort_out <- function(input_data, variable_list, sdq, subcohort = "cohort_out", file_name) {

    exwas_result <- data.frame()

    all_imps <- mids_to_complete(input_data, variable_list)

    # extract cohorts names
    cohorts <- levels(all_imps$cohort)

    for (coh in cohorts) {

        # Filter datasets removing one cohort at a time
        sub_cohort <- dplyr::filter(all_imps, cohort != coh) %>%
            droplevels()

        # select one exposure
        for (item in variable_list$Term) {

            # change input dataset to dataframe
            pool_fit <- sub_cohort %>%

                # perform regression for each imputed dataset (group by
                # imputation number)
                dplyr::group_by(.imp) %>%

                # run simple regression for sdq ~ exposure + covariates
                dplyr::do(model = MASS::glm.nb(formula = get(sdq) ~ get(item) +
                                                   cohort + smoking + mother_bmi + mother_age +
                                                   mother_edu + parity + breastf_cat +
                                                   mother_work + child_age + sex, data = .)) %>%

                # transform the regression output to a list
                as.list() %>%

                # pick the list item containing numerical regression output
                # (coefficient and other statistics for the intercept,
                # confounders and exposure), ignore other results provided by
                # the glm.nb function
                .[[-1]] %>%

                # pool the regression outocomes for each imputed dataset
                mice::pool() %>%

                # extract the final coefficient with 95% confidence intervals
                summary(conf.int = TRUE, exponentiate = TRUE)

            tidy_res <- pool_fit %>%
                mutate(sub_cohort = paste0("No_", coh)) %>%
                tidy_exwas_result()

            exwas_result <- rbind(exwas_result, tidy_res)
        }
    }

    exwas_tidy_result <- tidy_exwas_outcomes(input_data, variable_list, exwas_result, subcohort = "cohort_out", file_name)
    return(exwas_tidy_result)
}
groovearmada/exwas documentation built on May 29, 2019, 12:02 a.m.