Friday, February 25, 2011

Recommended text editors

This list is by no means intended to be exhaustive, and merely represents editors that I have used and which work well with at least one statistical language.

• jEdit - Free/Open Source: Powerful and easy to use

• UltraEdit - Commercial/$60: Powerful and easy to use

• Eclipse with StatET plugin - Free/Open Source: Capable of being used as a text editor or a full IDE

• EMACS with ESS plugin - Free/Open Source: Steep initial learning curve but extremely powerful

• AutoIt is not a text editor but rather a Windows tool which helps link text editors to your statistical package

Monday, February 7, 2011

R: Functions and environments and a debugging utility, oh my!

The Mark Fredrickson blog has a superb post on R functions and environments that's well worth checking out.

He also includes a handy function for debugging:

> fnpeek <- function(f, name = NULL) {
+ env <- environment(f)
+ if (is.null(name)) {
+ return(ls(envir = env))
+ }
+ if (name %in% ls(envir = env)) {
+ return(get(name, env))
+ }
+ return(NULL)
+ }
> fnpeek(f1)
[1] "n"
> fnpeek(f1, "n")
[1] 7

Friday, February 4, 2011

R: Colors

"It ain't easy being green. ~ Kermit T. F.

Matt Blackwell at the SSSB has made it easy to access all the Craylola(tm) colors in R.

And in case you're not familiar with the way R handles color, here are a few resources:

* The best color chart for R.
* Color palettes in R (allows plotting a spectrum or coordinated palette of colors easily).

AutoIt: The basics

The setup:

Your boss cracks the whip and says, "Get me this data by tomorrow or I'll turn your pet rat into a human and send him to Azkaban!" The data you need though, is stored in some evil organization's crazy computer program, and the only way you can figure out to get it out is to copy and paste a number, click the "Next" button, scroll down three lines, rinse, and repeat. How dreary, and you'd much rather be hanging out with your cool non-data-monkey friends on a Friday night anyway, right?

The solution:

What's that do? Well, it automates all the clicky/typey things that bore humans but make computers leap with excitement. Basically, if you can type it or click it (or both), you can figure out a way to make AutoIt automate it. It's almost like having your very own wand.

Find it here:

Best way to get started is to poke through some of the included scripts and play around. Online docs are here:

Thursday, February 3, 2011

Regular expressions, an example

Why regular expressions are your friend. This is written in Stata but applies to any language where regular expressions exist.

Original version
if length("`qx'")==3 { /*ex: 5q0*/
local a = substr("`qx'", 1, 1)
local b = substr("`qx'", 3, 1)
else if substr("`qx'", 2, 1) == "q" & length("`qx'")==4 { /*ex: 5q20*/
local a = substr("`qx'", 1, 1)
local b = substr("`qx'", 3, 2)
else if substr("`qx'", 3, 1) == "q" & length("`qx'")==4 { /*ex: 10q5*/
local a = substr("`qx'", 1, 2)
local b = substr("`qx'", 4, 1)
else if length("`qx'")==5 { /*ex: 10q20*/
local a = substr("`qx'", 1, 2)
local b = substr("`qx'", 4, 2)

Regular expression version
foreach q in `qx' {
local a = regexr("`q'","q.+","")
local b = regexr("`q'",".+q","")

Wednesday, February 2, 2011

STATA: Input

Quick tip: you can use -input- to send someone a test data set by e-mail without having to deal with attachments.

Try copying and pasting this into your Stata window and look at the dataset that results.

input x y
1 4
2 6
3 7
1 2
1 5

Tuesday, February 1, 2011

STATA: Tempfile

-tempfile- creates a local containing a file address. This file will automatically be removed by Stata once your do file ends, so you don't have to worry about cleaning up your mess. It will always (except for rare occasions in Stata 9 at least when it would mess up) be unique, even if you run two copies of the same do file simultaneously. In short, they're cool.

All -tempfile- does is create the local. It's exactly like if you'd created a local containing a file that you want to exist but that doesn't yet exist. If you want to have a file already existing (e.g. for using with -append- in a loop), check out -findit touch-.

If you call -tempfile- twice with the same name, the first file is gone forever, replaced with a new pointer to a temporary file. This is good in loops.

tempfile c2file
use $xdrive/projects/SeattleStinks/countries_original, clear
rename ex0 ex0_original
sort iso
save `c2file'
use $xdrive/projects/SeattleStinks/countries_new, clear
sort iso
merge iso using `c2file'