Friday, July 31, 2009

Creating Dummy Matrices in R

The following R function creates a matrix of dummy variables. You can access the R code here, and the associated description with the example here.
This is possibly not the most efficient way of creating dummy variables, but it works for what I care.

Tracking changes in Latex files

Sometimes it is useful to compare two versions of the same Latex document in a way that highlights changes, just as the track changes module of MSWord would do (e.g., when working with collaborators, after revising a paper, or just if you are in doubt of which document is the most recent.) It turns out that issuing the command (for Windows users, it is already included in the Miktex distribution of latex) :

> latexdiff-so version1.tex
version2.tex > diff.tex

from the DOS command line interface, will compare the original document "version1" against the modified "version2" and output a document highlighting changes as "diff".

The only requirement that you will have is a Perl interpreter that, for Windows users, can be obtained by installing the suite Rtools (incidentally, this furnishes you with all you need to build native R in MS Windows.)

Important! For running the command above you must have Perl in the Windows Path. The Rtool default installation asks you for permission for such a modification --- if you do not have administrator's privileges, you have to do this by hand using the user's path in the environmental variables option of the MS Windows system dialog.

Thursday, July 30, 2009

Thesis in LaTeX

Is anyone else using puthesis? I have had a lot of success with it so far. Just wanted to get a feel for who is using it, any problems you are facing, any tips, or what-have-yous.

Currently Using

Maybe it would be a good idea for all of us to share what freeware we currently use/endorse. Feel free to post yours in the comment section.

1. TeXnicCenter
2. R through JGR (Jaguar) - I prefer to TinnR just because I like it.
3. JabRef - I can't say enough good things about this citation manager.
4. Zotero Firefox extension - This is a really easy way to get references in BibTex form. It does a great job of recognizing sources on a page full of things. Like an entire page of google scholar results or an amazon.com search.

Also, I'm sure almost everyone knows you can output references in BibTex format from googlar scholar (under scholar preferences), but if not, I thought it was worth mentioning.

Viewing Latex changes as PDFs in real time

The Adobe PDF reader locks in the file that is open such it is not editable unless you close it. That's the reason of the error "I can't write yourfile.pdf" when you compile from a shell before closing the pdf view; or why the document is closed before compilation when you use something like TeXnicenter. A lean, portable (no need for admin. rights), pdf viewer that allows you to compile and having an automatic preview of your work is Sumatra PDF, available at:

http://blog.kowalczyk.info/software/sumatrapdf/index.html

Simply open your document using Sumatra, edit and compile using your favorite way, and you will have the changes right in front of you without the need to navigating the entire document to see if the changes took place.

Installing all Econometrics packages

We all know how much of a pain it is the install a new version of R because it loses all previously downloaded packages. This is where " task views" come into play. You can download and install all econometrics related packages by simply installing the "Econometrics" task view (see related link on the right for a list of the packages).

This is how you do it:
1. install package "ctv"
2. Type in the GUI:
library(ctv)
install.views("Econometrics")


That's it. It will take a little while!

Note for the spatial people: the task view "Econometrics" does not include spdep

Bridging the Gap Between R and SAS

One of the books that was suggested to us at AAEA was R for SAS and SPSS Users by R.A. Muenchen. You can download a free (earlier) version of the book at http://rforsasandspssusers.com/. This could help us communicate better with our current or future colleagues.

Wednesday, July 29, 2009

Using formulas in your own code

The next four lines of code allow you to create your vector y (dependent variable) and matrix X of explanatory variables from a formula and a data.frame.

#creation of an object of class "terms" decomposing the formula into a usable form
trm <- terms(formula,data=data)

#creation of a model.frame i.e. a data.frame corresponding to the formula
modelfrm <- lm(formula,data,method="model.frame")

#extraction of the dependent variable
y <- model.extract(modelfrm,"response")

#extraction of the independent variables
X <- model.matrix(trm,modelfrm)

For substituting zeroes for NAs in a dataframe

For substituting zeroes for NAs in a dataframe:

x[is.na(x)] <- 0
Welcome everyone!

A little idea to make things easier for everyone on the blog. When posting, don't forget to add a label for your post (at the bottom of the "add post" window). For now, use R, Latex or Linux depending on what your tip or advice is about. You can even add multiple keywords of your choice.
The idea here is to share tips about R, Latex, and other open source tools. Hints, code snippets, and samples are welcome.

Nelson