# CauMedi

## Introduction

Welcome to the CauMedi vignette! This document provides an overview of the CauMedi package. CauMedi is a causal mediation framework for cell type-specific single-cell data based on joint mediator multilevel models. The framework jointly models overdispersed counts and zero inflation for the mediators.

### Installation

To install the package, please use:

    #install.packages("CauMedi")

## Example Usage

Run prepare_CauMedi_data first to prepare inputs before running main function.

    library(CauMedi)
    set.seed(42)
    n_subj <- 100
    n_gene <- 50

    gene_ids <- paste0("gene", seq_len(n_gene))
    M_mat <- matrix(rgamma(n_subj * n_gene, shape = 2, rate = 1),
                    nrow = n_subj, ncol = n_gene,
                    dimnames = list(paste0("subj", seq_len(n_subj)), gene_ids))
    F_mat <- matrix(runif(n_subj * n_gene, min = 0, max = 1),
                    nrow = n_subj, ncol = n_gene,
                    dimnames = list(paste0("subj", seq_len(n_subj)), gene_ids))
    Y <- rnorm(n_subj)
    X <- rbinom(n_subj, 1, 0.5)

    feature_meta <- data.frame(
      feature_id = gene_ids,
      cell_type  = "Vasculature_cells",
      gene       = gene_ids,
      stringsAsFactors = FALSE
    )

    pd <- prep_CauMedi_data(M_mat = M_mat, F_mat = F_mat, feature_meta = feature_meta, Y = Y, X = X)

Use outputs from prep_CauMedi_data function as inputs for main function CauMedi.

    res <- CauMedi(data = pd$data, feature_meta = pd$feature_meta)
    print(res)
    #> $coef_outcome
    #> (Intercept)           X    M_gene48    M_gene23    F_gene32    F_gene36
    #>  0.01066148  0.17497867  0.04849605 -0.09506033 -0.09519435 -0.12306433
    #>     F_gene9    F_gene19    F_gene45    F_gene49
    #>  0.01960922  0.10651115 -0.06703089  0.06287628
    #>
    #> $se_outcome
    #> (Intercept)           X    M_gene48    M_gene23    F_gene32    F_gene36
    #>   0.1659187   0.2581165   0.1139251   0.1119261   0.1115699   0.1128999
    #>     F_gene9    F_gene19    F_gene45    F_gene49
    #>   0.1126850   0.1096564   0.1110100   0.1098068
    #>
    #> $gamma_coef_vec
    #>   M_gene48   M_gene23
    #>  0.4532473 -0.3922426
    #>
    #> $gamma_se_vec
    #> NULL
    #>
    #> $alpha_coef_vec
    #>   F_gene32   F_gene36    F_gene9   F_gene19   F_gene45   F_gene49
    #>  0.5678107 -0.5490598 -0.4615541 -0.4605079 -0.4458847  0.4250634
    #>
    #> $alpha_se_vec
    #> NULL
    #>
    #> $signif_M
    #> named integer(0)
    #>
    #> $signif_F
    #> named integer(0)
    #>
    #> $M_summary
    #>           cell_type mediator   gene      gamma    p_gamma      beta_M  p_beta_M
    #> 1 Vasculature_cells M_gene48 gene48  0.4532473 0.02266345  0.04849605 0.6713546
    #> 2 Vasculature_cells M_gene23 gene23 -0.3922426 0.04934008 -0.09506033 0.3979606
    #>   joint_pval  adj_pval
    #> 1  0.6713546 0.6713546
    #> 2  0.3979606 0.6713546
    #>
    #> $F_summary
    #>           cell_type mediator   gene      alpha     p_alpha      beta_F
    #> 1 Vasculature_cells F_gene32 gene32  0.5678107 0.004007658 -0.09519435
    #> 2 Vasculature_cells F_gene36 gene36 -0.5490598 0.005459492 -0.12306433
    #> 3 Vasculature_cells  F_gene9  gene9 -0.4615541 0.020233166  0.01960922
    #> 4 Vasculature_cells F_gene19 gene19 -0.4605079 0.020526354  0.10651115
    #> 5 Vasculature_cells F_gene45 gene45 -0.4458847 0.025022176 -0.06703089
    #> 6 Vasculature_cells F_gene49 gene49  0.4250634 0.032854105  0.06287628
    #>    p_beta_F joint_pval  adj_pval
    #> 1 0.3957991  0.3957991 0.6820051
    #> 2 0.2786097  0.2786097 0.6820051
    #> 3 0.8622421  0.8622421 0.8622421
    #> 4 0.3339938  0.3339938 0.6820051
    #> 5 0.5474771  0.5474771 0.6820051
    #> 6 0.5683376  0.5683376 0.6820051