mirzaghaderi / rtpcr

rtpcr: a package for statistical analysis and graphical display of real-time PCR data in R
GNU General Public License v3.0
1 stars 1 forks source link

raw data of t-test result #8

Open WWz33 opened 1 week ago

WWz33 commented 1 week ago

Dear Ghader!

Thanks for this great package. This is the result of the example data for the qpcrTTEST .

> test1[["Raw_data"]]

Var2 wDCt 1 1 2.39 2 1 2.59 3 1 2.44 4 1 4.34 5 1 2.89 6 1 3.77 7 2 2.19 8 2 1.99 9 2 2.44

If I want to use ggplot to create a bar plot comparing treatment and control groups for each gene (as shown below), how do I specify which group is the control ?

If my 'Condition' variable is 'col' vs. 'ck' instead of 'treatment' vs. 'control', how do I distinguish between them ?

image

subset_df <- data.frame(group = character(), Gene = character(), wDCt = numeric())

    ttest_result <- vector("list", length(levels_to_compare))

    for (i in 1:length(levels_to_compare)) {
      subset_df <- rbind(subset_df, 
                         data.frame(group = x[GENE == levels_to_compare[i], "Condition"],
                                    Gene = levels_to_compare[i],
                                    wDCt = x[GENE == levels_to_compare[i], "wCt"] - x[GENE == utils::tail(unique(GENE), 1), "wCt"]))

      ttest_result[[i]] <- stats::t.test(subset_df$wDCt[(nrow(subset_df) - 2*r + 1):(nrow(subset_df) - r)], 
                                         subset_df$wDCt[(nrow(subset_df) - r + 1):nrow(subset_df)], 
                                         paired = paired, var.equal = var.equal)

      res[i, ] <- c(levels_to_compare[i],
                    round(mean(subset_df$wDCt[(nrow(subset_df) - r + 1):nrow(subset_df)]) - mean(subset_df$wDCt[(nrow(subset_df) - 2*r + 1):(nrow(subset_df) - r)]), 4),
                    round(2^-((mean(subset_df$wDCt[(nrow(subset_df) - r + 1):nrow(subset_df)]) - mean(subset_df$wDCt[(nrow(subset_df) - 2*r + 1):(nrow(subset_df) - r)]))), 4),
                    round(2^(-ttest_result[[i]]$conf.int[2]), 4),
                    round(2^(-ttest_result[[i]]$conf.int[1]), 4),
                    round(ttest_result[[i]]$p.value, 4),
                    round(stats::sd(subset_df$wDCt[(nrow(subset_df) - r + 1):nrow(subset_df)]) / sqrt(r), 4))
    }

    res <- as.data.frame(res)
    res$FC <- as.numeric(res$FC)
    res$se <- as.numeric(res$se)
    res$dif <- NULL
    res <- data.frame(res,
                      Lower.se = round(2^(log2(res$FC) - res$se), 4),
                      Upper.se = round(2^(log2(res$FC) + res$se), 4))

    Raw_df <- subset_df 

    res <- list(Raw_data = Raw_df, Result = res)
    return(res)
  }
} 
  1. Using a data.frame to store subset data: Instead of using a matrix, we create a data.frame called subset_df and add data to it row by row within the loop. This way, we can store the corresponding "group" information in subset_df at the same time as calculating wDCt.
  2. Modifying the ttest_result calculation method: Since data is added to subset_df row by row, we need to dynamically calculate the data index required by the ttest function based on nrow(subset_df).
  3. Directly using subset_df as Raw_data: After the loop, subset_df already contains all the necessary information, so it can be directly returned as Raw_data.

    Can these code solve my confusion?

I appreciate your time and consideration.

Best!

mirzaghaderi commented 1 week ago

Hi WWz33

Using your suggested changes the qpcrTTEST function is now working perfectly. I involved your code here and will be added to the next version of the rtpcr in the CRAN repository.

Many thanks for your time and consideration.

WWz33 commented 1 week ago

Dear Ghader! Thanks for your reply. Can my suggestion be applied to other functions as well?(I didn't look closely at the source code for your other functions) I appreciate your time and consideration. Best!

mirzaghaderi commented 1 week ago

Dear WWz33, In the other functions, the raw or final data are presented clearly as you requested:

Example (in qpcrANOVAFC function):

a <- qpcrANOVAFC(data_1factor, numberOfrefGenes = 1, mainFactor.column = 1, block = NULL, fill = c("#CDC673", "#EEDD82"), fontsizePvalue = 5, y.axis.adjust = 0.1) a$Final_data

SA rep Etarget Cttarget Eref Ctref wDCt 1 L1 1 2 33.30 2 31.53 1.77 2 L1 2 2 33.39 2 31.57 1.82 3 L1 3 2 33.34 2 31.50 1.84 4 L2 1 2 32.73 2 31.30 1.43 5 L2 2 2 32.46 2 32.55 -0.09 6 L2 3 2 32.60 2 31.92 0.68 7 L3 1 2 33.48 2 33.30 0.18 8 L3 2 2 33.27 2 33.37 -0.10 9 L3 3 2 33.32 2 33.35 -0.03

Regards, Ghader

WWz33 commented 1 week ago

Dear Ghader! That‘s great! Thanks a bunch for taking the time to explain that to me. Best!

mirzaghaderi commented 1 week ago

You're welcome. Many thanks, Ghader