formatted non-flu stats in the same way as the flu ones

This commit is contained in:
Tanushree Tunstall 2020-10-26 14:33:20 +00:00
parent efcdb1a34d
commit 61fbb2f8d6
10 changed files with 319 additions and 226 deletions

107
flu_stats_unpaired_sam.R Normal file → Executable file
View file

@ -32,9 +32,11 @@ lf = sam_adults_lf[sam_adults_lf$flustat == 1,]
table(lf$timepoint)
length(unique(lf$mosaic))
lf$timepoint = paste0("t", lf$timepoint)
my_sample_type = "sam"
########################################################################
# Unpaired stats at each timepoint b/w groups: wilcoxon UNpaired analysis with correction
# Unpaired stats at each timepoint b/w groups: wilcoxon UNpaired analysis
# with correction
#######################################################################
# with adjustment: fdr and BH are identical
my_adjust_method = "BH"
@ -88,6 +90,9 @@ if (all(n_t1$mediator%in%stats_un_t1$mediator)) {
, "\nncol:", ncol(stats_un_t1))
}
# add bonferroni adjustment as well
stats_un_t1$p_adj_bonferroni = p.adjust(stats_un_t1$p, method = "bonferroni")
rm(n_t1)
rm(lf_t1_comp)
@ -132,8 +137,8 @@ if (all(n_t2$mediator%in%stats_un_t2$mediator)) {
, "\nncol:", ncol(stats_un_t2))
}
# check: satisfied!!!!
wilcox.test()
# add bonferroni adjustment as well
stats_un_t2$p_adj_bonferroni = p.adjust(stats_un_t2$p, method = "bonferroni")
rm(n_t2)
rm(lf_t2_comp)
@ -184,6 +189,9 @@ if (all(n_t3$mediator%in%stats_un_t3$mediator)) {
# FIXME: supply the col name automatically?
wilcox.test(wf$ifna2a_sam3[wf$obesity == 1], wf$ifna2a_sam3[wf$obesity == 0])
# add bonferroni adjustment as well
stats_un_t3$p_adj_bonferroni = p.adjust(stats_un_t3$p, method = "bonferroni")
rm(n_t3)
rm(lf_t3_comp)
@ -223,46 +231,41 @@ if ( nrow(combined_unpaired_stats) == expected_rows && ncol(combined_unpaired_st
quit()
}
#===============================================================
#######################################################################
#=================
# formatting df
# delete unnecessary column
#=================
# delete: unnecessary column
combined_unpaired_stats = subset(combined_unpaired_stats, select = -c(.y.))
# reflect stats method correctly
combined_unpaired_stats$method
# add sample_type
cat("Adding sample type info as a column", my_sample_type, "...")
combined_unpaired_stats$sample_type = my_sample_type
# add: reflect stats method correctly i.e paired or unpaired
# incase there are NA due to LLODs, the gsub won't work!
#combined_unpaired_stats$method = gsub("Wilcoxon", "Wilcoxon_unpaired", combined_unpaired_stats$method)
combined_unpaired_stats$method = "wilcoxon unpaired"
combined_unpaired_stats$method
# replace "." in colnames with "_"
colnames(combined_unpaired_stats)
#names(combined_unpaired_stats) = gsub("\.", "_", names(combined_unpaired_stats)) # weird!!!!
colnames(combined_unpaired_stats) = c("mediator"
, "group1"
, "group2"
, "p"
, "p_adj"
, "p_format"
, "p_signif"
, "method"
, "timepoint"
, "n_obs")
colnames(combined_unpaired_stats)
combined_unpaired_stats$sample_type = "sam"
# add an extra column for padjust_signif
combined_unpaired_stats$padjust_signif = round(combined_unpaired_stats$p_adj, digits = 2)
# add appropriate symbols for padjust_signif
# add an extra column for padjust_signif: my_adjust_method
combined_unpaired_stats$padjust_signif = combined_unpaired_stats$p.adj
# add appropriate symbols for padjust_signif: my_adjust_method
combined_unpaired_stats = dplyr::mutate(combined_unpaired_stats, padjust_signif = case_when(padjust_signif == 0.05 ~ "."
, padjust_signif <=0.0001 ~ '****'
, padjust_signif <=0.001 ~ '***'
, padjust_signif <=0.01 ~ '**'
, padjust_signif <0.05 ~ '*'
, TRUE ~ 'ns'))
, padjust_signif <=0.0001 ~ '****'
, padjust_signif <=0.001 ~ '***'
, padjust_signif <=0.01 ~ '**'
, padjust_signif <0.05 ~ '*'
, TRUE ~ 'ns'))
# add an extra column for p_bon_signif
combined_unpaired_stats$p_bon_signif = combined_unpaired_stats$p_adj_bonferroni
# add appropriate symbols for p_bon_signif
combined_unpaired_stats = dplyr::mutate(combined_unpaired_stats, p_bon_signif = case_when(p_bon_signif == 0.05 ~ "."
, p_bon_signif <=0.0001 ~ '****'
, p_bon_signif <=0.001 ~ '***'
, p_bon_signif <=0.01 ~ '**'
, p_bon_signif <0.05 ~ '*'
, TRUE ~ 'ns'))
# reorder columns
print("preparing to reorder columns...")
colnames(combined_unpaired_stats)
@ -274,16 +277,18 @@ my_col_order2 = c("mediator"
, "group2"
, "method"
, "p"
, "p_format"
, "p_signif"
, "p_adj"
, "padjust_signif")
, "p.format"
, "p.signif"
, "p.adj"
, "padjust_signif"
, "p_adj_bonferroni"
, "p_bon_signif")
if( length(my_col_order2) == ncol(combined_unpaired_stats) && all(my_col_order2%in%colnames(combined_unpaired_stats)) ){
if( length(my_col_order2) == ncol(combined_unpaired_stats) && (all(my_col_order2%in%colnames(combined_unpaired_stats))) ){
print("PASS: Reordering columns...")
combined_unpaired_stats_f = combined_unpaired_stats[, my_col_order2]
print("Successful: column reordering")
print("formatted df called:'combined_unpaired_stats_f'")
print("formatted df called:'combined_unpaired_stats_f'")
cat('\nformatted df has the following dimensions\n')
print(dim(combined_unpaired_stats_f ))
} else{
@ -291,10 +296,26 @@ if( length(my_col_order2) == ncol(combined_unpaired_stats) && all(my_col_order2%
, "\nExpected column order for: ", ncol(combined_unpaired_stats)
, "\nGot:", length(my_col_order2)))
quit()
}
}
# assign nice column names like replace "." with "_"
colnames(combined_unpaired_stats_f) = c("mediator"
, "timepoint"
, "sample_type"
, "n_obs"
, "group1"
, "group2"
, "method"
, "p"
, "p_format"
, "p_signif"
, paste0("p_adj_fdr_", my_adjust_method)
, paste0("p_", my_adjust_method, "_signif")
, "p_adj_bonferroni"
, "p_bon_signif")
colnames(combined_unpaired_stats_f)
#******************
# write output file
#******************
cat("UNpaired stats for groups will be:", stats_time_unpaired_flu_sam)
write.csv(combined_unpaired_stats_f, stats_time_unpaired_flu_sam, row.names = FALSE)
write.csv(combined_unpaired_stats_f, stats_time_unpaired_flu_sam, row.names = FALSE)