Quick and easy calories

Calories are necessary for sustaining academic work! Here are some of my favorite recipes/tips and tricks for keeping things running with a minimum of effort (and money) to:

  • Make a 1-pot tabbouleh that “cooks” itself overnight
  • Extract pomegranate seeds efficiently and without mess
  • Contribute to potlucks with minimum effort

Read the rest of this entry »


Downsampling audio recordings

Working with large audio files can be quite unwieldy. Unfortunately, popular handheld digital recorders like the Zoom H4n or Roland R-09HR do not have the option of recording uncompressed WAV files at less than 44.1kHz, presumably since the companies are marketing to musicians/music fans. For speech analysis, 16kHz is generally sufficient since energy in the speech signal generally falls below 8kHz.

It is possible to downsample files in Praat using the command Convert > Resample... when a Sound object (not a LongSound object) is selected. However, I have sometimes had trouble with running out of memory using this method.

Another method is to use command line tools, namely, sox, a very handy cross-platform command-line utility for sound processing. Here is an example of how to downsample an audio file called sound.wav to 16kHz (that’s the rate 16k) and also to reduce to a 16 bit recording (that’s the -b 16), to an output file named sound-down.wav:

sox sound.wav -b 16 sound-down.wav rate 16k

Some more on sox’s resampling algorithm here.


Some resources for R help (especially for GLMMs)

A quick pointer to some on-line resources for help on stats, especially on GLMMs (from the R user community):

  • The GLMM Wiki is a resource especially for researchers working with GLMMs and includes a FAQ from R-sig-ME.
  • RSeek allows searches by different categories, including support lists, functions, code, and blogs, includes R-sig-ME, and appears to be up-to-date.
  • R Site Search also includes R-sig-ME through 2010.
  • The R-lang archives are searchable, too.

/bin/sh: latex: command not found: setting R path to include TeX binaries path

In integrating statistical analysis in R with writing up documents in LaTeX, some useful functions for generating LaTeX tables are latex() from the Hmisc package and xtable() from the xtable package.

You also need R to be able to access TeX binaries! Sometimes, R might not be able to do this, if your path environmental variable in R isn’t set to include the path to TeX binaries. This may be a problem especially if R doesn’t inherit the path from the shell environment, e.g. if you load R from Finder in Mac OS X.

Here, we can see that the R path does not include the path to TeX binaries:

Sys.getenv("PATH")
#                                          PATH 
#"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" 

So if you try to call an R function that calls TeX binaries, it will give an error message:

library(Hmisc) # Load Hmisc to load latex()
x <- matrix(1:6, nrow=2, dimnames=list(c('a','b'),c('c','d','this that'))) # From latex() examples
latex(x)
#/bin/sh: latex: command not found
#Error in system(cmd, intern = TRUE, wait = TRUE) : 
 # error in running command
#sh: xdvi: command not found

To explicitly set the R path, you can create a text file called .Rprofile in your user directory (for Mac OS X, this is in the ~/ directory). In this text file, you can add to the path so that R can access LaTeX commands (following this post):

Sys.setenv(PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":")) # this adds /usr/texbin to the R path

This should work if you have some TeX Live distribution on a UNIX system.

(In .Rprofile you can also explicitly set the entire path, not just adding on /usr/texbin to the existing path, and define any functions you would like to always have in your R environment, etc.)

Sys.getenv("PATH")
#                                                      PATH 
#"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin" 

library(Hmisc)
x <- matrix(1:6, nrow=2, dimnames=list(c('a','b'),c('c','d','this that')))
latex(x)
#This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live 2010)
 #restricted \write18 enabled.
#entering extended mode

Now R can access the TeX binaries!


Installing Matlab 2008b Student on Mac Snow Leopard 10.6.6

When installing Matlab 2008b Student Version on Mac OSX 10.6.6, I got an exception error that said something like:

Cant load library: /Applications/MATLAB_R2008bSV.app/bin/maci64/libinstutil.jnilib

It turns out that Matlab 2008b is 32 bit, but the installer was running as 64 bit because of the Java preferences. Installation worked after I followed notes from this Matlab Central post.

Using the Java Preferences application (in /Applications/Utilities) go to the General tab, and at the bottom, under Java Applications, drag to reorder “Java SE 6 32-bit” to come before “Java SE 6 64-bit”. I only needed to do this while I was trying to run the activation program that is required before Matlab will run; after that you can reset the Java preferences to their original values.


Formatting numbers for printing in R: rounding and trailing zeroes

In journal articles, descriptive statistics results are often presented as a table of means, with standard deviation/standard error given in parentheses following the mean. Here is how you can prepare the print formatting when working in R: it’s rather trivial, but I wasted a sufficient amount of time on it that I thought it was worth mentioning. (I follow this by using latex() from the Hmisc package to generate LaTeX output of such a table.)

Suppose I have a vector of means for percent correct from 5 different experimental conditions, and a vector of the corresponding standard errors, and both are sorted in the same order:

all.res.corr$per.correct # Vector of mean percent correct, comes from a data frame called all.res.corr
#[1] 52.53561 60.51282 64.13105 66.38177 67.46439
all.res.corr$se.per.corr # Vector of standard errors, from same data frame
#[1] 2.409955 2.763525 2.831450 2.909737 2.902924
  1. First, use round() to round the numbers to two decimal places.
    round(all.res.corr$per.correct,2)
    #[1] 52.54 60.51 64.13 66.38 67.46
    round(all.res.corr$se.per.corr,2)
    #[1] 2.41 2.76 2.83 2.91 2.90
    
  2. Then use formatC() to access C-like formatting options to format the two vectors to print up to 2 digits after the decimal point. The argument specification format = "f" allows you to set the number of digits after the decimal point to print. This is a crucial step! If you don’t do it, you won’t get trailing zeroes to print, even though it looks like in the preceding code block, for instance, that round() preserves the trailing zero. It gets lost in a numeric to character data type conversion. See this post for an alternative using sprintf() (which may be familiar from many other programming languages).
    formatC(round(all.res.corr$per.correct,2), 2, format = "f"
    #[1] "52.54" "60.51" "64.13" "66.38" "67.46" # note conversion to character type
    formatC(round(all.res.corr$se.per.corr,2), 2, format = "f")
    [1] "2.41" "2.76" "2.83" "2.91" "2.90"
    
  3. Finally, use paste() to concatenate the vectors for publication, together with parentheses in the appropriate place. The code below concatenates the arguments given: the formatted mean vector all.res.corr$per.correct, a space and opening parentheses (, the formatted SE vector all.res.corr$se.per.corr, and a closing parentheses ). The separator sep is specified to be an empty string, for there to be no separator between the arguments concatenated.
  4. (per.all.res.corr.print &amp;lt;- paste(formatC(round(all.res.corr$per.correct,2), 2, format = "f"), " (", formatC(round(all.res.corr$se.per.corr,2), 2, format = "f"), ")", sep="") # n.b. Parentheses around an assignment command print the assigned value
    #[1] "52.54 (2.41)" "60.51 (2.76)" "64.13 (2.83)" "66.38 (2.91)" "67.46 (2.90)"
    

Installing lme4a in R (on Mac OSX, 10.6.4)

Douglas Bates has draft chapters out for a new book lme4: Mixed-effects Modeling with R up and I was trying to play with the development version lme4a. See this post for a note on how lme4a differs from lme4. It currently cannot be installed automatically using a command like

install.packages("lme4a", repos="http://R-Forge.R-project.org")

and I installed it via svn checkout following these posts. It requires the latest version of R (2.12.0), as well as at least Rcpp 0.8.8.1 (from R-forge, via svn checkout; I obtained Rcpp 0.8.9.3 today), and RcppArmadillo, and the packages minqa and MatrixModels, available from CRAN respositories. (RcppArmadillo is also available from CRAN respositories, but I went ahead and installed it from the Rcpp SVN repository).

Here are the steps I followed (on Mac OSX, 10.6.4)

  1. I downloaded and installed the most recent Mac binary for R, R 2.12.0.
  2. I updated all my packages in R using update.packages()
  3. I installed some dependencies for lme4a in R:
    install.packages(c("minqa", "MatrixModels"))
    
  4. I obtained Rcpp/RcppArmadillo and lme4 repositories via svn checkout and installed Rcpp and lme4a (all commands entered in Terminal)
    cd [your R sources directory]
    svn checkout svn://svn.r-forge.r-project.org/svnroot/rcpp
    svn checkout svn://svn.r-forge.r-project.org/svnroot/lme4
    cd rcpp/pkg
    sudo R CMD INSTALL Rcpp # without sudo, I couldn't get permission to access a necessary directory
    sudo R CMD INSTALL RcppArmadillo
    cd ../..
    cd lme4/pkg
    sudo R CMD INSTALL lme4a