Monday, October 12, 2009

Multiple graphics windows in R

I have been thinking for the longest time that R could only handle one graphics windows, I should have looked a little deeper. It turns out that it is very easy to do.

To create a new window, simply type: windows() or X11(). This will open a new graphics device and give it the next available number.
To make window k the active window, the command is windows(k).
To close window k, dev.off(k) is the way to go.

For more commands and information, see help(dev.cur) in Rgui.

TiddlyWiki

If you would find useful to have the ability to create web pages on the fly, using your web browser as editor, but with WYSIWYG capabilities, you've got to try TiddlyWiki (http://www.tiddlywiki.com/). From the creators:

"TiddlyWiki is a single html file which has all the characteristics of a wiki - including all of the content, the functionality (including editing, saving, tagging and searching) and the style sheet. Because it's a single file, it's very portable - you can email it, put it on a web server or share it via a USB stick."

This is open source, free, fast, portable across platforms, and easy to use. I found a very nice place for hosting my personal wiki, www.tiddlyspot.com. If you are fond of html, this does not get better.

Friday, October 9, 2009

Puthesis for AGEC thesis

The puthesis template does 99% of your thesis formatting. The 1% left, however, is a pain in the neck. For example, using puthesis out of the shelve gave me the following problems (Ask Lou Ann for the format requirements, do it early):

1. The margins were not as required
2. The spaces between headings and subheadings were not as specified.
3. The little dots of the table of contents did not touch the number pages.
4. The table captions did not met the requirements.
5. The headings and subheadings had a format different than what is needed.
6. The original template I got did not indent the first line after Chapter headings.

To fix 1-3 and 5 I modified the puthesis.cls file and renamed it as
puthesis_mod.cls. (Download puthesis_mod.cls ) You can search over comments starting with NV to see what I did. I counted 16 modifications; sadly, I do not remember why I did half of them. Moreover, the brackets specifying the options for agecon include several tweaks. My fix to the table of contents leaders only works for two subdivisions, if you have more, have fun trying to add extra divisions in a similar way to the ones that I added. To fix 4, and other little things I do not remember, puthesis.cls has built-in options. So, in addition to using puthesis_mod.cls, I used this as the first line of the preamble:

\documentclass[agecon,ajae,dissertation,nochapterblankpages,nononchapterblankpages,uglyheadings]{puthesis_mod}

Notice that puthesis_mod.cls is genetically engineered to include the bibliographical AAEA style, which was mentioned in an earlier post.

I also noticed that other people working with puthesis had not the same problems I had, and I ignore why that is the case.

Two more things:

1. Margins work --- but you have to be sure that Page Scaling is set to none when you use Adobe Acrobat Reader to print.
2. Figures and tables that are aligned at the top of the page tend to break the 1 inch top margin. My solution was to use \begin{}[hb]. Itworked (h for here, b for bottom). As a very last resource, for tables that occupy an entire (or more) page, a \vspace{-.1in} statement did the trick. Put it after the \begin{} and before the \caption.

Tuesday, October 6, 2009

Merging PDFs (and other useful tasks)

If you need to merge two pdfs, as for example, when attaching forms 9 and 20 to the thesis manuscript, pdftk can be useful. Free, portable across platforms, and without the need of administrative privileges, it does a lot of useful things. From the creators: "If PDF is electronic paper, then pdftk is an electronic staple-remover, hole-punch, binder, secret-decoder-ring, and X-Ray-glasses." Can be downloaded from http://www.accesspdf.com/pdftk/.

Friday, September 25, 2009

LaTeX to Word (Quick and Dirty)

Although Nelson's method is probably more thorough, I have had some luck recently with GrindEQ for converting LaTeX documents to Word. It does a great job with equations and text. Not the best with figures and citations, but I think it does a fairly good job overall. At least better than converting from pdf to doc or docx.

You can check it out here.

Thursday, September 17, 2009

page counter in beamer presentations

suppose you want to put a page counter in your beamer presentation. Add to the preamble the following:

\usefoottemplate{\hfill \insertframenumber{} / \inserttotalframenumber}


The nice thing about this way of using this method is that it counts each slide once, even if the slide have a transition.

Monday, August 31, 2009

AJAE standard references in Latex and Puthesis

The package economic has a bibliography style based on AJAE standards:
The package is at:
http://www.ctan.org/tex-archive/biblio/bibtex/contrib/economic/
and should be easy to install using mike/live tex standard installers.
To use it with puthesis, add this line to puthesis.cls (I added it where all the bib styles are defined, I do not know if that matters):
\DeclareOption{ajae}{\set@@optionbibstyle{ajae}}.
Then, declare it in the document class:
\documentclass[agecon,ajae,dissertation]{puthesis} (thanks Todd for this hint) and be sure to declare it as bib style in the bibliography section:
\bibliographystyle{ajae}.

Saturday, August 22, 2009

Selectively removing objects in R

Suppose you have created a bunch of objects but want to keep only a, b, and c. You can do the following:

# Create an character vector with the objects to keep:

keepthis<-c("a", "b","c")

# Use the set operator setdiff() to delete only the difference
# between the set objects() and the set keepthis.

rm(list=c(setdiff(objects(), keepthis)))

#You can progressively add objects to keepthis using append():

keepthis<-append(keepthis,c("d", "e"))

Friday, August 21, 2009

Transposing a Dataset


# A common problem is to transpose a dataset from the long to the wide
# format,e.g., given the dataset below:
> s <- c("a","b","c") # Set up a group of source
> d <- expand.grid(s,s) # Create the cartesian product of s
> d$flow <- ifelse(d$Var1==d$Var2,0,0.2534469)# Assign the values
> print(d)
Var1 Var2 flow
1 a a 0.0000000
2 b a 0.2534469
3 c a 0.2534469
4 a b 0.2534469
5 b b 0.0000000
6 c b 0.2534469
7 a c 0.2534469
8 b c 0.2534469
9 c c 0.0000000
#we would like something like this:
# Var2
#Var1 a b c
# a 0.0000000 0.2534469 0.2534469
# b 0.2534469 0.0000000 0.2534469
# c 0.2534469 0.2534469 0.0000000
#The function reshape() is handy for this, but more often than not is
#cumbersome to use. The package reshape with the functions cast() and
#melt() is way more intuitive. An alternative way takes advantage of
#R's vectorization function tapply():
> tapply(d[,3],d[,c(1,2)],c)
Var2
Var1 a b c
a 0.0000000 0.2534469 0.2534469
b 0.2534469 0.0000000 0.2534469
c 0.2534469 0.2534469 0.0000000
# contrast the tapply version with the reshape one:
> reshape(d, idvar="Var1",timevar="Var2",direction="wide")
Var1 flow.a flow.b flow.c
1 a 0.0000000 0.2534469 0.2534469
2 b 0.2534469 0.0000000 0.2534469
3 c 0.2534469 0.2534469 0.0000000

Other Software

Here are few other open source econometrics and statistics programs I stumbled across through econometricslinks.com. The only one I've tried is OpenBugs for Bayesian analysis, but I thought a few of you may enjoy playing around with some of the others.
  1. OpenBugs
  2. Draco
  3. JWAVE
Enjoy!

Sunday, August 16, 2009

GeoDa

I'm not sure everyone was aware, but an alpha release of Open GeoDa is available here. I've played around with it a bit, and the only difference I can see at this time is that is looks much nicer.

Saturday, August 15, 2009

Want to convert your Excel table to LaTeX?

This may be old news, but in my never ending quest to automate table building, I discovered this Excel to LaTeX table converter. It consists of an Add-On to Excel. I've noticed other similar programs online, but this one appears to be the simplest.

-Mesbah

Review notes in the margin

I recently found an interesting package called "todonotes". This package puts review notes in the margin and can also complete a list of all changes.

Tuesday, August 11, 2009

Sophisticated axis labels in R

Sometimes you need to put Greek letters or simply a sub index in an axis label of a plot. You could do the following:
xlab=expression(paste("US Markets",~~(omega[kt]),sep=""))
where omega is, well, omega, and [kt] will put a subindex kt. It works for ylab, main, etc.

Monday, August 10, 2009

Cover Letters from Purdue in LaTex

Some of us, at some point of our early careers will leave Purdue for higher salary [academic] jobs. First step is applying for a job, which entails writing a nice cover letter that will "sell" us as the top-notch applicants. Long story short, I prepared a template of the cover letter. Purdue logo can be accessed here. Just save it in the same folder with the tex file, and alter school information as needed. Any augmentations and improvements to the provided code are welcome and appreciated.





\documentclass[stdletter]{newlfm}
\newlfmP{letrh=pu,addrtoleft,dateright,sigleft,sigsize=30pt,orderfromdateto,addrfromphone,addrfromfax,headermarginskip=-20pt,dateskipbefore=0pt}

\PhrPhone{Phone}
\PhrFax{Fax}

\newsavebox{\pulogo}
\sbox{\pulogo}{
\parbox[b]{1.75in}{
\vspace{0.5in}
\includegraphics[scale=.4,ext=.png]
{PU_signature}
}
}
\makeletterhead{pu}{\Lheader{\usebox{\pulogo}}}

\newcommand{\person}{name} % substitute with the name of the person you are applying to
\newcommand{\department}{department } % substitute with the Department name you are applying
\newcommand{\university}{university} % substitute with the University name you are applying
\newcommand{\street}{street } % substitute with the actual street address
\renewcommand{\city}{city} % substitute with the actual city
\renewcommand{\state}{state } % substitute with the actual state
\newcommand{\zipcode}{zip-code } % substitute with the actual zip-code

\renewcommand{\position}{position } % substitute with the position title you are applying for
\newcommand{\advertisement}{advertisement } % substitute with the actual source of information
\newcommand{\graduation}{date} % substitute with the expected graduation date

\nameto{\person}
\addrto{
\department \\
\university \\
\street \\
\city, \state \hspace{2pt} \zipcode}

\namefrom{Your Name}
\addrfrom{
Department of Agricultural Economics \\
Purdue University \\
403 W. State St. \\
West Lafayette, IN \hspace{2pt} 47907-2056}
\phonefrom{(765) 494-4191}
\faxfrom{(765) 494-9176}

\begin{document}
\greetto{Dear \person,}
\closeline{Sincerely,}
\begin{newlfm}

Here goes introduction. For example, I am writing to express my interest in the position announcement for an \position in the \department at \university, as advertised on the \advertisement website, etc.

Here goes body.

Here goes conclusion. For example, I would enjoy discussing this position with you in the near future, etc.

\end{newlfm}
\end{document}


Clickable links [hyperref]

This may or may not be news to most of you, but I would imagine that this is something that virtually every writer likes to use every once in a while.

Sunday, August 9, 2009

A couple of nice sources

Here are a couple of references I always find helpful.
  1. LaTeX wikibook - This is a pretty nice general source that just appears to get better and better.
  2. A pretty comprehensive list of math symbols. Good for quick scanning or when you are trying to come up with additional Greek letters.

Saturday, August 8, 2009

Large letter beginning paragraph

I have been trying to figure out how to add that large and nice looking first letter of a paper that some journals use (much like how it is in books for children). This is what I came up with and I would appreciate any feedback.

[The picture below is from code appearing in a later comment in the post.]

Thursday, August 6, 2009

Aligning Equations in Latex

Let's say that you have an equation like this:

And want to align it in a pretty way, say:

or:

as opposite to the sloppy:

Then, you can use the environment split within the package amsmath, just as in the example in the box below. By the way, the sloppy way is the last bit of code in the example.


\documentclass{article} \usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{split}
x = a + b + c
\end{split}
\end{equation}

\begin{equation}
\begin{split}
x = {} & a \\
+ & b \\
+ & c \\
\end{split}
\end{equation}

\begin{equation}
\begin{split}
x = {} & a + b \\
+ & c \\
\end{split}
\end{equation}

\begin{equation}
\begin{array}{l}
x = a+ b \\
+ c \\
\end{array}
\end{equation}
\end{document}


Tuesday, August 4, 2009

HTML Scroll Down Box for Code

To keep the size of the code chunks manageable, we can generate a scroll down box like the one below. This is easily done by using the code in the box when you write the post.
The following code creates a box:
<div style="overflow: auto; height: 150px; width: 400px;">
<table style="width: 250px;" cellpadding="0" cellspacing="0"><tbody>
<tr><td><code> PUT YOUR CODE HERE.
</code>
</td>
</tr>
</tbody></table>
</div>


Note that the tags <code> and </code> are not necessary, however, they are handy to put things in courier font to differentiate the code from the rest of the text.

Monday, August 3, 2009

Maps in R

The maptools package in R allows plotting data in a map --- besides keeping your work in only one environment, it saves you the like 10 minutes it takes for ArcGIS just to open, and the 10 minutes that it takes to render the map each time you modify something.

I have here a little example plotting percapita gdp growth in the world (data from the World Band Development Indicators Online). The final map looks like this (the code is below):




## read gdp growth in 2007 originally taken from the World Bank Development Indicators On-line:
dat<-read.table("http://web.ics.purdue.edu/~nvillori/blog/gdpgrowth2007.txt",header=TRUE)
## Load package maptools --- wrld_simpl is a polygon map of the world included in this package.
require(maptools)
data(wrld_simpl)
## Index ctry.names by position in the map.
dat$pos<-match(dat$Country.Code,unique(wrld_simpl$ISO3))
dat<-dat[order(dat$pos),]
## Classes & Palettes: Use packages RColorBrewer for cool palettes and classInt for easy sectioning of the values to be mapped:
### Variable to be plotted
plotvar <- dat$YR2007
### Palettes and intervals:
require(RColorBrewer)
nclr<-9
plotclr <- brewer.pal(nclr,"YlOrRd")
require(classInt)
class <- classIntervals(plotvar, nclr, style="pretty")
colcode <- findColours(class, plotclr)
### Define margins and other graphic parameters:
par(mar=c(1,1,1,1), lheight = .8)
### Plot an "empty" map with borders in grey --- aspect ratio is modified to distort image a bit:
plot(wrld_simpl,axes=TRUE,border="darkgrey",xlim=c(-110,150),bg="lightcyan", ylim=c(-45,60),asp=1.5,main="GDP Growth in 2007")
### Plot the gdp growth rates:
plot(wrld_simpl[wrld_simpl$ISO3 %in% dat$Country.Code,],col=colcode,axes=TRUE,add=TRUE)
### Add country codes to the map:
text(wrld_simpl$LON[wrld_simpl$ISO3 %in% dat$Country.Code],wrld_simpl$LAT[wrld_simpl$ISO3 %in% dat$Country.Code],wrld_simpl$ISO3[wrld_simpl$ISO3 %in% dat$Country.Code],cex=.4,col="black")
### Add a legend:
legend(title="GDP growth",-45, -15, legend=names(attr(colcode, "table")), fill=attr(colcode, "palette"), cex=.6, bty="y",bg="lightgrey")


Saturday, August 1, 2009

From Latex to MS Word

Latex is not necessarily a standard format, e.g., the working paper series of many places require MS word documents. There are good and expensive converters --- Latex to MS Word and vice-versa --- as well as a host of free tools (see here). One possibility that I have found to work particularly well is first to convert from Latex to HTML, and then from HTML to MS Word --- or the processor of your choice. Here is how:

1. From Latex to HTML: Use TeX4ht (LaTeX and TeX for Hypertext). TeX4ht is part of the Miktex distribution so chances are that it's already in your machine. Just run a command like this:

> htlatex filename "html,word" "symbol/!" "-cvalidate

if it's the first time, it will install by itself, and then start the conversion. This command is tuned towards MS Word. Such a format relies on bitmaps for mathematical formulas. For the right conversion of formula/pictures to bitmaps Tex4ht needs imagemagik (which by the way, is very useful for converting across different formats). I had to modify tex4ht.env in the following way:

#Gconvert -trim +repage -density 110x110 -transparent "#FFFFFF" zz%%4.ps %%3 Gc:"\Program Files\ImageMagick-6.5.1-Q16\convert" -trim +repage -density 110x110 -transparent "#FFFFFF" zz%%4.ps %%3


2. From HTML to MS Word: Open your html document in MS Word and be careful to follow these instructions to be sure that your images (math and figures, basically) get embedded. If you do not do this, they'll get lost if you only keep the .doc file. Don't blame that on me.

3. For Ubuntu users:
$ sudo apt-get install tex4ht
$ sudo apt-get install dvipng
Use the same command above:
$ htlatex filename "html,word" "symbol/!" "-cvalidate"

And step 2 is unchanged.

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