tidy scripts and using age as criteria for adults

This commit is contained in:
Tanushree Tunstall 2020-10-29 13:10:39 +00:00
parent 49c18d390f
commit 1e6e9eaef1
3 changed files with 110 additions and 115 deletions

View file

@ -3,45 +3,54 @@ getwd()
setwd("~/git/mosaic_2020/")
getwd()
############################################################
# TASK: unpaired (time) analysis of mediators: SAM
# TASK: unpaired (time) analysis of mediators:
# sample type: SAM
# data: Flu positive adult patients
# group: obesity
############################################################
my_sample_type = "sam"
#=============
# Input
#=============
source("data_extraction_formatting.R")
table(metadata_all$flustat[metadata_all$adult == 1])
# check: adult variable and age variable discrepancy!
metadata_all$mosaic[metadata_all$adult==1 & metadata_all$age<=18]
# clear variables
rm(npa_adults_lf, npa_df_adults_clean
, serum_adults_lf, serum_df_adults_clean)
rm(colnames_npa_df, expected_rows_npa_lf
, colnames_serum_df, expected_rows_serum_lf)
rm(pivot_cols)
my_sample_type = "sam"
#=============
# Output: unpaired analysis of time for sam
#=============
outfile_name = paste0("flu_stats_time_unpaired_", my_sample_type, ".csv")
flu_stats_time_unpaired = paste0(outdir_stats, outfile_name)
#%%========================================================
#===============================
# data assignment for stats
wf = sam_df_adults_clean[sam_df_adults_clean$flustat == 1,]
lf = sam_adults_lf[sam_adults_lf$flustat == 1,]
#%%========================================================
#================================
wf = sam_wf[sam_wf$flustat == 1,]
lf = sam_lf[sam_lf$flustat == 1,]
lf$timepoint = paste0("t", lf$timepoint)
########################################################################
# clear variables
rm(npa_lf, npa_wf
, serum_lf, serum_wf)
rm(colnames_npa_df, expected_rows_npa_lf
, colnames_serum_df, expected_rows_serum_lf)
rm(pivot_cols)
# sanity checks
table(lf$timepoint)
length(unique(lf$mosaic))
lf$timepoint = paste0("t", lf$timepoint)
if (table(lf$flustat) == table(sam_adults_lf$flustat)[[2]]){
if (table(lf$flustat) == table(sam_lf$flustat)[[2]]){
cat("Analysing Flu positive patients for:", my_sample_type)
}else{
cat("FAIL: problem with subsetting data for:", my_sample_type)
quit()
}
########################################################################
# Unpaired stats at each timepoint b/w groups: wilcoxon UNpaired analysis
# with correction
@ -55,10 +64,10 @@ my_adjust_method = "BH"
lf_t1 = lf[lf$timepoint == "t1",]
sum(is.na(lf_t1$value))
foo = lf_t1[which(is.na(lf_t1$value)),]
ci = which(is.na(lf_t1$value))
#foo = lf_t1[which(is.na(lf_t1$value)),]
#ci = which(is.na(lf_t1$value))
#lf_t1_comp = lf_t1[-ci,]
lf_t1_comp = lf_t1[-which(is.na(lf_t1$value)),]
stats_un_t1 = compare_means(value~obesity
, group.by = "mediator"
@ -69,8 +78,8 @@ stats_un_t1 = compare_means(value~obesity
foo$mosaic[!unique(foo$mosaic)%in%unique(lf_t1_comp$mosaic)]
# add timepoint and convert to df
stats_un_t1$timepoint = "t1"
stats_un_t1 = as.data.frame(stats_un_t1)
class(stats_un_t1)
@ -79,7 +88,9 @@ n_t1 = data.frame(table(lf_t1_comp$mediator))
colnames(n_t1) = c("mediator", "n_obs")
n_t1$mediator = as.character(n_t1$mediator)
# merge stats + n_obs df
#==================================
# Merge: merge stats + n_obs df
#===================================
merging_cols = intersect(names(stats_un_t1), names(n_t1)); merging_cols
if (all(n_t1$mediator%in%stats_un_t1$mediator)) {
cat("PASS: merging stats and n_obs on column/s:", merging_cols)
@ -116,8 +127,8 @@ stats_un_t2 = compare_means(value~obesity
, data = lf_t2_comp
, paired = FALSE
, p.adjust.method = my_adjust_method)
# add timepoint and convert to df
stats_un_t2$timepoint = "t2"
stats_un_t2 = as.data.frame(stats_un_t2)
class(stats_un_t2)
@ -126,7 +137,9 @@ n_t2 = data.frame(table(lf_t2_comp$mediator))
colnames(n_t2) = c("mediator", "n_obs")
n_t2$mediator = as.character(n_t2$mediator)
# merge stats + n_obs df
#==================================
# Merge: merge stats + n_obs df
#===================================
merging_cols = intersect(names(stats_un_t2), names(n_t2)); merging_cols
if (all(n_t2$mediator%in%stats_un_t2$mediator)) {
cat("PASS: merging stats and n_obs on column/s:", merging_cols)
@ -163,9 +176,8 @@ stats_un_t3 = compare_means(value~obesity
, data = lf_t3_comp
, paired = FALSE
, p.adjust.method = my_adjust_method)
# add timepoint and convert to df
stats_un_t3$timepoint = "t3"
stats_un_t3 = as.data.frame(stats_un_t3)
class(stats_un_t3)
@ -174,7 +186,9 @@ n_t3 = data.frame(table(lf_t3_comp$mediator))
colnames(n_t3) = c("mediator", "n_obs")
n_t3$mediator = as.character(n_t3$mediator)
# merge stats + n_obs df
#==================================
# Merge: merge stats + n_obs df
#==================================
merging_cols = intersect(names(stats_un_t3), names(n_t3)); merging_cols
if (all(n_t3$mediator%in%stats_un_t3$mediator)) {
cat("PASS: merging stats and n_obs on column/s:", merging_cols)
@ -202,10 +216,10 @@ stats_un_t3$p_adj_bonferroni = p.adjust(stats_un_t3$p, method = "bonferroni")
rm(n_t3)
rm(lf_t3_comp)
#==============
########################################################################
#=================
# Rbind these dfs
#==============
#=================
str(stats_un_t1);str(stats_un_t2); str(stats_un_t3)
n_dfs = 3
@ -322,6 +336,7 @@ colnames(combined_unpaired_stats_f) = c("mediator"
, "p_bon_signif")
colnames(combined_unpaired_stats_f)
#######################################################################
#******************
# write output file
#******************