Tuesday, July 5, 2016

Writing a new method for nlme

I recently took some time to figure out how to write a new method for nlme to enable structuring the variance-covariance matrix of the random effects in a specific way. My goal here was to be able to run Dave Kenny's social relations model (Kenny, 1994) using multilevel modeling and the approach described by Snijders and Kenny (1999). Taking this approach requires "tricking" the software in a way through the use of dummy variables and constraints on the variance-covariance matrix.

Figuring out how to write a new method was more challenging than I had initially expected. There are many twists and turns in lme and it took quite a bit of time to reverse engineer the software to figure out what was going on. Unfortunately, there isn't great documentation on the web for this process.

As part of my process, I created my own replication of one of the existing methods--pdCompSymm. I went through and commented each part of the different functions that are called, explaining my interpretation of what is going on. As you can see, there are some places where I'm just off and don't really know what's going on. I also converted some of the C code in nlme for running pdCompSymm into R code (this is the pdFactor.pdCompSymm function).

In the end, I was able to figure out enough of it to succeed in my goal of creating a new method for the social relations model through multilevel modeling in R. You can find this on my github page. I've called it pdSRM and it has some comments at the top that explain how to use it.

One lesson learned from this is that it is challenging--but not impossible!--to specify a structure for the variance-covariance matrix using nlme that is not already in the generic methods that are provided. I also learned a ton about how lme is working behind the scenes. This took a bunch of time, but did pay off in the end.

Saturday, September 7, 2013

Trying out Sublime Text 3

I've been a huge fan of TextWrangler for years. I use it for all of my coding (including with R), for taking notes during meetings, for taking notes on articles, and more. It's my most frequently used application. But, I'm ready for a change and am going to give Sublime Text 3 a try. It seems very powerful, relatively light, and incredibly extensible. 

I recently installed Sublime Text 3, added Package Control, and installed some R packages to try out. I'll give it a try for the next three weeks and see what I think. It might be time to say farewell to TextWrangler.

Monday, May 20, 2013

Recurrence Quantification Analysis

This page has some wonderful resources for recurrence analysis. One particularly useful resource on this site is the listing of software options for conducting recurrence analysis. After a fair amount of searching, I couldn't find an R package that computed the metrics from a recurrence quantification analysis. The tseriesChaos package provides a function for producing recurrence plots; but, I didn't see anything for quantifying these plots.

After digging through the different software options listed on this site, I tried out and really like the Commandline Recurrence Plots script offered by Norbert Marwan himself.

The script was very easy to setup on my Mac and, by using Rscript it was easy to combine with R code to (a) draw specific chunks of data for different individuals in my dataset; (b) compute and output the recurrence quantification metrics; (c) output the recurrence plot dataset for creating the actual plot; and, (d) producing the plot and creating a dataset of metrics.

I'll clean up, comment, and post the code that I used as soon as I can come up for air.

Monday, April 15, 2013

R Graphics Parameters -- Rows and Columns

For some reason I always forget the code for setting R's graphics parameters. And, I always need this same line. So, now I shan't forget it.


quartz(type="pdf",file="figure_NUM.pdf")
par(mfrow=c(3,2), cex=1, mar=c(2,2,2,2))
dev.off()

Wednesday, November 28, 2012

Convert CD tracks to mp3 using ffmpeg

Just a small chunk of code to convert CD tracks (aiff) to mp3 files:


#!/bin/bash
for i in {1..12}
do
  ffmpeg -i ${i}.aiff -f mp3 -acodec libmp3lame -ab 192000 -ar 44100 ${i}.mp3
done



Thursday, November 15, 2012

Using color in R graphics

I'm sticking this here because I always forget the simple ways to specify and manipulate color in R graphics:

Earl Glynn put together a very useful presentation, which covers a range of topics on using color in R graphics.

Wednesday, November 14, 2012

Ruby code to parse and combine text files

I use this ruby code to parse several tab-delimitted text files that contain individual raters' perceptions of a target (in this case a video). The rater id is embedded in the filename. The target video number is also embedded in the filename.


#! /usr/bin/env ruby

out = Dir.glob('*.txt')

# open the file to write to and add the column headers
columns = "group\trater\tmin\tengage\tprepare\tdiverge\tconverge\texecute\tcentralize\tattentive\ttone\tactivation\n" 
File.open("./all_ratings.txt", 'w') { |f| f.write(columns) }

out.each do |filename|
  rater = filename.split('.')[0].split('_')[0]
  group = filename.split('.')[0].split('_')[1]  
 
  # Assign a number for the rater
  case rater.downcase
    when "rater1"
      rater_id = 1
    when "rater2"
      rater_id = 2
    when "rater3"
      rater_id = 3
    when "rater4"
      rater_id = 4
    end
    puts "rater: " + rater + "(#{rater_id})" + " group: " + group

    # Open the file
    f = File.open(filename, "r").read
 
    # Split by lines - This will make sure that the end of line from Mac Classic is \n
    str = f.gsub!(/\r\n?/, "\n").split("\n")
 
    # Identify the line number that starts the data entry for this file by finding a specific expression in the text of the file
 
    linenum = 0
    exp = "- Low marked by sluggishness"
    line = str[linenum]
    puts line
    until line.include?(exp)    
      line = str[linenum] 
      linenum += 1
    end
 
    linenum.upto(linenum+30) do |currentline|
      min = (currentline-linenum)+1
      # add the rater_id and the group_id to the line
      line = group.to_s + "\t" + rater_id.to_s + "\t" + str[currentline] + "\n"
      File.open("./all_ratings.txt", 'a') { |f| f.write(line) }
    end
end