---
tags: Rscripts
---
# Looped t-tests and averages/sd, written to CSV
### Author: Jocelyn P. Colella
### two-sample t-test (95% CI) between experiment pairs
_Sex_: F(female), M (male)
_Experiments:_ BL (baseline), hot, cold
_Response variables_: EE, RQ, VO2, VCO2, H2Omg
_Time period:_ daytime (day), nighttime (night) [transition time periods excluded]
NOTE:
+ Did not test for differences between sexes (see Dani's script for those details)
+ R code at the bottom (following tables)
NOT SIGNIFICANT COMPARISONS:
| Dataset1 | Dataset2 | Response Variable |p-value |
| -------- | -------- | -------- |-------- |
|Within each sex:|-----|-----|-----|
|BL_day_noOL_F| hot_day_noOL_F| VCO2 |0.50442478|
|hot_day_noOL_F| BL_day_noOL_F| VCO2 |0.50442478|
|hot_day_noOL_F| cold_night_noOL_F| H2Omg| 0.013006354
|cold_night_noOL_F| hot_day_noOL_F| H2Omg| 0.013006354|
|M v. F:|-----|-----|-----|
|BL_day_noOL_M| hot_day_noOL_F| VO2 |0.884593882|
|hot_night_noOL_M| cold_day_noOL_F|VO2| 0.369735615|
|hot_day_noOL_M| BL_day_noOL_F| VCO2| 0.876741418|
|hot_day_noOL_M| hot_day_noOL_F| VCO2| 0.330153573|
|BL_night_noOL_M| cold_night_noOL_F| VCO2| 0.313632651|
|hot_night_noOL_M| cold_day_noOL_F| VCO2| 0.104796886|
|cold_night_noOL_M| cold_day_noOL_F|RQ|0.57831791|
|hot_night_noOL_M| cold_day_noOL_F|EE| 0.293801663|
|BL_day_noOL_M| hot_day_noOL_F| EE| 0.253243466|
|cold_day_noOL_M| hot_night_noOL_F| H2Omg| 0.03916373|
|BL_night_noOL_M| cold_day_noOL_F| H2Omg| 0.012336656|
### Mean and standard deviations for each response variable for day and night across each experiment
| Response variable | Dataset | Mean |S.D. |
| -------- | -------- | -------- |-------- |
|EE| BL_day_noOL_F| 0.086602005| 0.046301842|
|RQ |BL_day_noOL_F |1.053873485 |0.166716943|
|VO2| BL_day_noOL_F |0.28457532 |0.156013733|
|VCO2| BL_day_noOL_F |0.291008514| 0.146937751|
|H2Omg| BL_day_noOL_F |0.143808705| 0.055656941|
|EE |BL_night_noOL_F |0.086602005| 0.046301842|
|RQ |BL_night_noOL_F |1.053873485| 0.166716943|
|VO2| BL_night_noOL_F| 0.28457532 |0.156013733|
|VCO2| BL_night_noOL_F| 0.291008514 |0.146937751|
|H2Omg| BL_night_noOL_F| 0.143808705 |0.055656941|
|EE |hot_day_noOL_F |0.086602005 |0.046301842|
|RQ |hot_day_noOL_F |1.053873485 |0.166716943|
|VO2| hot_day_noOL_F |0.28457532 |0.156013733|
|VCO2| hot_day_noOL_F |0.291008514| 0.146937751|
|H2Omg| hot_day_noOL_F |0.143808705| 0.055656941|
|EE |hot_night_noOL_F |0.086602005| 0.046301842|
|RQ |hot_night_noOL_F |1.053873485| 0.166716943|
|VO2| hot_night_noOL_F| 0.28457532| 0.156013733|
|VCO2| hot_night_noOL_F| 0.291008514| 0.146937751|
|H2Omg| hot_night_noOL_F| 0.143808705 |0.055656941|
|EE |cold_day_noOL_F |0.086602005| 0.046301842|
|RQ |cold_day_noOL_F |1.053873485| 0.166716943|
|VO2| cold_day_noOL_F |0.28457532 |0.156013733|
|VCO2| cold_day_noOL_F |0.291008514| 0.146937751|
|H2Omg| cold_day_noOL_F |0.143808705| 0.055656941|
|EE |cold_night_noOL_F |0.086602005| 0.046301842|
|RQ |cold_night_noOL_F| 1.053873485 |0.166716943|
|VO2| cold_night_noOL_F| 0.28457532| 0.156013733|
|VCO2| cold_night_noOL_F| 0.291008514| 0.146937751|
|H2Omg| cold_night_noOL_F| 0.143808705 |0.055656941|
|EE| BL_day_noOL_M| 0.086602005 |0.046301842|
|RQ |BL_day_noOL_M |1.053873485 |0.166716943|
|VO2| BL_day_noOL_M |0.28457532 |0.156013733|
|VCO2| BL_day_noOL_M |0.291008514| 0.146937751|
|H2Omg| BL_day_noOL_M |0.143808705| 0.055656941|
|EE |BL_night_noOL_M |0.086602005| 0.046301842|
|RQ |BL_night_noOL_M |1.053873485| 0.166716943|
|VO2| BL_night_noOL_M |0.28457532 |0.156013733|
|VCO2| BL_night_noOL_M |0.291008514| 0.146937751|
|H2Omg| BL_night_noOL_M| 0.143808705| 0.055656941|
|EE| hot_day_noOL_M| 0.086602005 |0.046301842|
|RQ |hot_day_noOL_M |1.053873485 |0.166716943|
|VO2| hot_day_noOL_M |0.28457532 |0.156013733|
|VCO2| hot_day_noOL_M |0.291008514| 0.146937751|
|H2Omg| hot_day_noOL_M |0.143808705| 0.055656941|
|EE |hot_night_noOL_M |0.086602005| 0.046301842|
|RQ |hot_night_noOL_M |1.053873485| 0.166716943|
|VO2| hot_night_noOL_M| 0.28457532| 0.156013733|
|VCO2| hot_night_noOL_M| 0.291008514 |0.146937751|
|H2Omg| hot_night_noOL_M| 0.143808705 |0.055656941|
|EE |cold_day_noOL_M |0.086602005 |0.046301842|
|RQ |cold_day_noOL_M |1.053873485 |0.166716943|
|VO2| cold_day_noOL_M |0.28457532 |0.156013733|
|VCO2| cold_day_noOL_M |0.291008514| 0.146937751|
|H2Omg| cold_day_noOL_M |0.143808705| 0.055656941|
|EE |cold_night_noOL_M |0.086602005| 0.046301842|
|RQ |cold_night_noOL_M| 1.053873485 |0.166716943|
|VO2| cold_night_noOL_M| 0.28457532| 0.156013733|
|VCO2| cold_night_noOL_M| 0.291008514 |0.146937751|
|H2Omg| cold_night_noOL_M| 0.143808705 |0.055656941|
```
# Subset males and females
# Subset each experiment (BL, Hot, Cold)
# Then subset daytime, nighttime, and transitions (t1, t2) for each experiment
##############################################
### M and F
MF_df_list = c("BL_day_noOL_F", "BL_night_noOL_F",
"hot_day_noOL_F", "hot_night_noOL_F",
"cold_day_noOL_F", "cold_night_noOL_F",
"BL_day_noOL_M", "BL_night_noOL_M",
"hot_day_noOL_M", "hot_night_noOL_M",
"cold_day_noOL_M", "cold_night_noOL_M")
#Write header to mean/sd file
meanSD_header <- paste('DV', 'Dataset', 'mean', 'sd', sep=',')
write.table(meanSD_header, "mean_sd_eachTreatment.csv", sep=',', col.names = FALSE, row.names = FALSE, quote = FALSE)
#write head to t test results file
t_header <- paste('Data1', 'Data2', 'DV', 'p-value', sep=',')
write.table(t_header, "ttest_results.csv", sep=',', col.names = FALSE, row.names = FALSE, quote = FALSE)
dependent_variables = c("EE", "RQ", "VO2", "VCO2", "H2Omg")
#For each data frame (day and night of each treatment)
#Calculate the average and SD of each depenedent variable
count = 1
for(df in MF_df_list){
print(df)
for (DV in dependent_variables){
print(DV)
this_mean = mean(BL_day_noOL_F[[DV]])
this_sd = sd(BL_day_noOL_F[[DV]])
line = paste(DV, df, this_mean, this_sd, sep=',')
print(line)
print('\n')
write.table(line, "mean_sd_eachTreatment.csv", sep = ',', append=TRUE, col.names = FALSE, row.names = FALSE, quote = FALSE)
#two sample t test - test whether the mean differs from other exp
templist <- list("BL_day_noOL_F", "BL_night_noOL_F","hot_day_noOL_F", "hot_night_noOL_F","cold_day_noOL_F", "cold_night_noOL_F")
templist <- templist[-count]
for(data in templist){
ts <- t.test(get(df)[[DV]], get(data)[[DV]], conf.level = 0.95)
#print(ts)
this_line=paste(df, DV, ts$p.value, sep = ',')
print(this_line)
write.table(this_line, "ttest_results.csv", sep=',', append = TRUE, col.names = FALSE, row.names = FALSE, quote = FALSE)
}
count = count + 1
}
}