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.