R/quickReg.R

Defines functions quickReg

Documented in quickReg

#' @title quickReg
#' @description quickReg will produce a regression plot between a miRNA:mRNA
#' pair. An abline wil be generated. Odds-ratio will be calculated from
#' the coefficient. Confident intervals at 95% will be caclulated from the
#' coefficient also. This is a useful method to identify the relationship
#' between a miRNA:mRNA pair over time. The higher the odds-ratio the greater
#' the correlation between the two time series. The smaller the 95% confidence
#' intervals range, the more confident we can be about plotting the abline.
#' @param reg_df Matrix/assay which which was generated by the multiReg function.
#' Should be in the MAE used during the multiReg function.
#' @param colselect Integer which represents a column within reg_df containing
#' mRNA/ miRNA which the gene of interest (column 2) is to be contrasted agianst.
#' @return Regression plot between a miRNA:mRNA pair.
#' @export
#' @usage quickReg(reg_df, colselect)
#' @importFrom stats confint.default coef
#' @examples
#' library(org.Mm.eg.db)
#'
#' miR <- mm_miR[1:50,]
#'
#' mRNA <- mm_mRNA[1:100,]
#'
#' MAE <- startObject(miR = miR, mRNA = mRNA)
#'
#' MAE <- getIdsMir(MAE, assay(MAE, 1), orgDB = org.Mm.eg.db, 'mmu')
#'
#' MAE <- getIdsMrna(MAE, assay(MAE, 2), "useast", 'mmusculus', orgDB = org.Mm.eg.db)
#'
#' MAE <- diffExpressRes(MAE, df = assay(MAE, 1), dataType = 'Log2FC',
#'                      genes_ID = assay(MAE, 3),
#'                       idColumn = 'GENENAME',
#'                       name = "miRNA_log2fc")
#'
#' MAE <- diffExpressRes(MAE, df = assay(MAE, 2), dataType = 'Log2FC',
#'                      genes_ID = assay(MAE, 7),
#'                      idColumn = 'GENENAME',
#'                      name = "mRNA_log2fc")
#'
#' Filt_df <- data.frame(row.names = c("mmu-miR-145a-3p:Adamts15",
#'                                    "mmu-miR-146a-5p:Acy1"),
#'                      corr = c(-0.9191653, 0.7826041),
#'                      miR = c("mmu-miR-145a-3p", "mmu-miR-146a-5p"),
#'                      mRNA = c("Adamts15", "Acy1"),
#'                      miR_Entrez = c(387163, NA),
#'                      mRNA_Entrez = c(235130, 109652),
#'                      TargetScan = c(1, 0),
#'                      miRDB = c(0, 0),
#'                      Predicted_Interactions = c(1, 0),
#'                      miRTarBase = c(0, 1),
#'                      Pred_Fun = c(1, 1))
#'
#' MAE <- matrixFilter(MAE, miningMatrix = Filt_df, negativeOnly = FALSE,
#'                    threshold = 1, predictedOnly = FALSE)
#'
#' MAE <- multiReg(MAE = MAE, gene_interest = "Adamts15",
#'                 mRNAreg =TRUE, filt_df=MAE[[11]], miRNA_exp=MAE[[9]],
#'                 mRNA_exp=MAE[[10]])
#'
#' model1 <- linearRegr(mreg = MAE[[12]], colselect =2, colpair =3)
#'
#' summary(model1$regression)
#'
#' quickReg(reg_df = MAE[[12]], colselect = 3)
quickReg <- function(reg_df, colselect) {

  if (missing(reg_df)) stop('reg_df is missing. Add matrix/assay generated by the multiReg function.')

  if (missing(colselect)) stop('colselect is missing. Add integer which represents column to contrast against the gene of interest (column 2).')

  DF <- as.data.frame(reg_df)

  simpleReg <- lm(data = as.data.frame(DF), DF[,colselect]~DF[,2])

  ORandCint <- exp(cbind("Odds ratio" = coef(simpleReg),

                         confint.default(simpleReg, level = 0.95)))

  OR <- round(ORandCint[2], 2)

  lowCI <- round(ORandCint[4],2)

  highCI <- round(ORandCint[6],2)

  TargetmRNA <- colnames(DF)[2]

  TargetmiRNA <- colnames(DF)[colselect]

  par(mar = c(1, 1, 1, 1))

  ggplot(DF, aes(DF[,colselect], DF[,2])) + geom_point(size=4, pch =15,
                                                       col="Blue") +

    geom_smooth(method ="lm", col="Red", size=2.5)+

    theme_classic()+

    labs(title= paste0(TargetmRNA, ":", TargetmiRNA, " Regression"),

         x=TargetmiRNA,

         y=TargetmRNA,

         subtitle=paste0("OR = ", OR, " | 95% CI =", lowCI, "-", highCI))+

    theme(plot.title=element_text(size=20, face="bold",hjust = 0.5),

          axis.text.x=element_text(size=20),

          axis.text.y=element_text(size=20),

          axis.title.x=element_text(size=20),

          axis.title.y=element_text(size=20))+

    theme(plot.subtitle=element_text(size=20, hjust=0.8,

                                     face="italic", color="black"))
}
Krutik6/TimiRGeN documentation built on Jan. 27, 2024, 7:46 p.m.