##### ##### NMST440: Computational Environment for Statistical Data Analysis ##### ##### --------------------------------------------------------------------- ##### ##### Calculate many tables, prepare many figures ##### to be included in the Sweave document ##### ##### --------------------------------------------------------------------- ##### ##### Arnošt Komárek ##### http://msekce.karlin.mff.cuni.cz/~komarek ##### komarek@karlin.mff.cuni.cz ##### ##### ====================================================================== rm(list = ls()) ROOT <- "/home/komarek/teach/mff_2020/nmst440_VypocProstr/Tutorial08/" setwd(ROOT) ### Load packages, provide smaller functions ### ---------------------------------------------- library("colorspace") library("xtable") ### Functions referenced here have been provided with Tutorial 4 source(paste(ROOT, "../Tutorial04/formatOut.R", sep = "")) source(paste(ROOT, "../Tutorial04/funTabDescr.R", sep = "")) ### Load a dataset (also provided with Tutorial 4) ### ---------------------------------------------------------------- print(load("../Tutorial04/Data/nelsNE2.RData")) dim(nelsNE2) head(nelsNE2) ### Below, it is our aim to repeat again a series ### of analyzes performed within Tutorial 4 ### = series of t-tests for each combination ### of response (yVars) and factor (xVars) ### which determines groups being compared by t-test ### --------------------------------------------------- ### Variables to process ### ----------------------------------------- yVars <- c("sco.math", "sco.sci", "sco.soc", "sco.read") xVars <- c("fam.comp", "gender", "f2.menrol", "f2.arrest") ### Human readable labels of considered variables ### for tables and plots yLabs <- paste(c("Math", "Science", "Social science", "Reading"), "score") names(yLabs) <- yVars print(yLabs) xLabs <- c("Family composition", "Gender", "Math enrollment past 2 years", "Arrested") names(xLabs) <- xVars print(xLabs) ### ********************************* ### ****** INSERTION ****** ### ********************************* ### One more handy function to work with strings ### (replacement) ### --------------------------------------------- strVec <- c("máslo", "řeřicha", "omáčka") print(strVec) gsub(pattern = "ř", replacement = "r", x = strVec) gsub(pattern = "má", replacement = "ma", x = strVec) ## dot is slightly tricky as it means "any character" ## in the regular expression expected in the pattern ## argument gsub(pattern = "\\.", replacement = "_", x = c("oo.txt", "kkk.r")) ### ********************************* ### ****** END OF INSERTION ****** ### ********************************* ### Calculate tables, create figures ### -> more or less repetition of Tutorial 4, ### for each t-test, related suitable plot is also created ### - subdirectory 'Figures' must exist in the ROOT directory ### or the path below must be modified ### - all files and other things are named as, e.g., yVar_xVar.pdf, ### all dots in the variable names are replaced by _ ### (I do not like more than one dot in a file name) ### -------------------------------------------------------------- (COL <- heat_hcl(2)) ### Run the loop first manually ### with, e.g., # yv <- yVars[1] # xv <- xVars[1] tabs <- list() for (yv in yVars){ for (xv in xVars){ ### Name of the output table/figure ### (replace . by _ as dot in the file name of ### pdf figure causes problems with pdflatex) tname <- paste(yv, xv, sep = "-") tname <- gsub("\\.", "_", tname) ### Table tabs[[tname]] <- funTabDescr(paste("f2.", yv, sep = ""), xv, yLab = yLabs[yv], xLab = xLabs[xv], data = nelsNE2, digits = 2) ### Figure pdf(paste(ROOT, "/Figures/fig-", tname, ".pdf", sep = ""), width = 6, height = 6) plot(formula(paste("f2.", yv, " ~ ", xv, sep = "")), data = nelsNE2, col = COL, xlab = xLabs[xv], ylab = yLabs[yv]) dev.off() } } ### Figues are now on the disk ### ---------------------------- ### Check the 'Figures' subdirectory of the ROOT directory ### Tables ### are now in the object 'tabs' which is a 'list' ### ------------------------------------------------- length(tabs) names(tabs) tabs[[1]] tabs[["sco_math-fam_comp"]] ## 'tab' element is a data.frame with calculated numbers ## 'tex' element is the LaTeX code of a "nice" table ## with calculated numbers cat(tabs[["sco_math-fam_comp"]][["tex"]]) ### No need to create many .tex files with tables ### to be input[ed] in the TeX document (as was done in Tutorial 4). ### Just save the R object and reuse it within the ### Sweave document, see further. ### Also figures will be used there. ### ------------------------------------------------ save(list = c("tabs", "xVars", "yVars", "xLabs", "yLabs"), file = paste(ROOT, "/RResult/nmst440-tabsfigs.RData", sep = "")) ### File nmst440-tabsfigs.RData will be saved ### in the RResult subdirectory of the ROOT directory. load(paste(ROOT, "/RResult/nmst440-tabsfigs.RData", sep = "")) ### Results loaded again ### ********************************* ### ****** INSERTION ****** ### ********************************* ### Few notes concerning citations ### ------------------------------ ### How to cite R itself in publications: citation() print(citation(), bibtex = TRUE) ### How to cite a specific R package in publications ### (all contributed packages used by you to prepare ### some output should be cited in that output) citation("colorspace") print(citation("colorspace"), bibtex = TRUE) ### Here: authors of the 'colorspace' package ### explicitely specified on how to cite their package. citation("xtable") ### This is automatically created citation record. ### Even that should be used in your output, if the 'xtable' ### package helped you to prepare the output. ### ********************************* ### ****** END OF INSERTION ****** ### *********************************