Viewing all the objects in R in a buffer in Emacs

You also probably did not read the complete Emacs-Speaks-Statistics manual… ? By chance, I discovered a nice feature, called Rdired.
This command gives you a dired-buffer with all your objects. In this buffer you can view, edit and plot your R-objects. It is documented in chapter 12.9 of the manual.
 
Here is small example: In the buffer in the middle, you see the original code taken from the manual. I invoked ess-rdired and the lower buffer is shown. I then put the cursor on the line with the parameter s and hit “v”. This gave me the upper buffer with all the values. Then I hit the “p” and a nice plot is shown. With “d” you could delete the object.
 
 
 
 
You have to put the following lines in your .emacs file:
Share

Auto-Completion in Emacs, LaTeX and Gams

The fun with Emacs is that you can discover new (or old) stuff that makes your life much easier on an almost weekly base. I was reading the manual on Emacs Speaks Statistics and discovered that there is a mode called auto-completion. I googled it, installed it and after some experimenting, I got it running. You can find it here. It comes with autocompletion for some programming languages (Python, Ruby, C+, etc.) but not for LaTeX, so I googled further and discovered an extension called auto-complete-latex (you can download it here.
Here are some screenshots: You just start typing and it suggests a keyword or if you wait a little bit longer it gives you a dropdown llist. You can jump to the next suggestions using the tab-key.
It doesn’t matter if you use capitals or not:
and it also keeps track of words in the actual document. Here an example of my paper on parking. As you see the word “Parking appears, when I typ “par”.
For Gams there is no dictionary, so I wrote one myself and added it to the dictionaries. I only did not manage to start auto-complete for Gams automatically. But this can be done with M-x auto-complete-mode.
Update:
;; ac-modes – major modes ac can run on
(add-to-list ‘ac-modes ‘gams-mode)

;; toggle auto-complete in all buffers

(global-auto-complete-mode t)

(with thanks to a reader)
These extensions make Emacs now to a modern text editor.
Share

Bibliography with abstracts in LaTeX

If you ever have written a scientific paper, you probably know how hard it is to remember what was in the papers you cited. Usually you cite them once and forget about them, but if you want to discuss your paper with other scientist, it would be nice to have at least an idea of what you exactly cited.
In LaTeX you have a nice way of producing a bibliography with abstracts. It is quite easy. You only have to use biblatex, an improved bibtex. Information on biblatex can be found at the CTAN repository (just google it). There are also two good articles in German from http://biblatex.dominik-wassenhoven.de/dtk.shtml.
 
Here is an example. Your LaTeX file looks like this, where the file jabref is my bib-file with all my citations (for which I use jabref):
 
 
The results looks like:
 
 
If you have some annotations, they also will be reproduced.
Share

Using Gams, R and LaTeX I

Wow, I just discovered how easy it is to produce nice LaTeX tables from your Gams results. Usually I capture my model results in some parameters that are exported (using gdx or gdxxrw) to excel. Then I make my tables in Excel and use an excel add-in to export the table to LaTeX (I have written about this add-in in this blog). This works fine, but is tedious because as soon as my results are in excel, I can’t do things automatically and have to click my way to get my LaTeX tables.
Today, I was working on the parking model and wanted to automate my LaTeX tables. I already wrote on how to produce nice plots using R Statistics, but that might have been too geeky for a lot of you (“I don’t do network modelling and Excel or Gnuplot produces plots that will do…”). However, what I show you today is worth trying to start working with R if you are doing your modeling with Gams.
My workflow now looks like this:
  1. Run my model and produce some parameter with results and export the parameters to a gdx file
  2. Read the parameters from the gdx file with R using the R package (gdxrrw).
  3. Recast the data using “cast” and “mold” from the package reshape (see for an excellent introduction (http://www.jstatsoft.org/v21/i12/paper)
  4. Use the R package “xtable” (see http://cran.r-project.org/web/packages/xtable/xtable.pdf) to produce for each table a separate tex file.
  5. Write my paper and include the tex files with tables.
Let us have a look at the code for every step.
1. Run my model and produce some parameter with results  and export the parameters to a gdx file
Most of this is straight forward. Note that I save my gdx files in my R directory. In this directory I have my R-scripts.
2. Read the parameters from the gdx file with R using the R package (gdxrrw).
Gams Corporation is working on a R-package that allows you to transfer data from R to Gams and the other way around. The package is called gdxrrw (the “rrw” stands for R read and write)  is still work-in-progress, but you can already use it (just download it from (http://support.gams.com/doku.php?id=gdxrrw:interfacing_gams_and_r).
When you have it installed, you can use it as follows:
The gams parameter is now a R data frame with the following structure:
3. Recast the data using “cast” and “mold” from the package reshape
I want to produce two different tables: one for the model statistisc and one for the road results. I saved all this results in one parameter, so now I have to take a subset of the results for every table.
I defined a subset “modelstatistics” and use them to subset my dataframe using %in%. This gives us the following dataframe
I want to have a table with the parameter as columns and the scenarios as rows. For this I first rename the columns and then I use the cast command:
If I combine this with the xtable command, I already have my LaTeX table:
4. Use the R package “xtable” to produce for each table a separate tex file.
Now we are almost ready: I set some options for saving the LaTeX table (table title, label and filename) and save the file using the print command on the table:
5. Write my paper and include the tex files with tables.
I jump in my paper tex file and add the following line for including the table:
and produce my LaTeX file:
In your xtable-code in the R-script you can a lot of additonal options to format your table.
The great thing is that I can do it all now without having to adjust my tables every time I run a different or addtional scenario. I just add in gams the following line to run the R-script:
And for the Emacs fans: I didn’t have to leave Emacs at all: I run my Gams model, my R code and my LaTeX from within Emacs (and for those with the good eyes: I can even clock this task…)
In a next post, I will write more on the use of cast: you can use this R command for lots of interesting stuff (building tables with the sum, mean, min, max, etc.).
Share

Biblatex and folder contents

I was looking for a way to make nice index for my folders using LaTeX. I usually have lots of papers saved in a folder and I was looking for a way to use my paper references stored in JabRef (an open source bibliography reference manager which integrates neatly with AucTexi in Emacs, see http://jabref.sourceforge.net/). The problem is that I want to have the title of the author, year and the title of the paper (who remembers something like Arrow 1955?), so I looked for a way to print the paper titles. BibTex and all the citation packages don’t seem to manage, but then I stumbled on bibllatex (see http://ctan.org/pkg/biblatex).
On the homepage you find the following description of biblatex:
“Biblatex is a complete reimplementation of the bibliographic facilities provided by LaTeX in conjunction with BibTeX. It redesigns the way in which LaTeX interacts with BibTeX at a fairly fundamental level. With biblatex, BibTeX is only used (if it is used at all) to sort the bibliography and to generate labels. Formatting of the bibliography is entirely controlled by TeX macros (the BibTeX-based mechanism embeds some parts of formatting in the BibTeX style file. Good working knowledge in LaTeX should be sufficient to design new bibliography and citation styles; nothing related to BibTeX’s language is needed.”
 
Biblatex allows you to cite the title using \citetitle{bibtexkey}. Using this and, after some surfing, a little bit more LaTeX code, I managed to use LaTeX to produce something like:
 
Looks nice!

 
Share

R-Graphics for Networks

With R you can make beautiful graphics and the nice thing is, you can design your own customized graphics. For some nice graphs produced with R, have a look at http://addictedtor.free.fr/graphiques/.
 
At the moment I am working on a parking model (searching for parking lots in the city) and have a simple grid for the streets and the junctions. If you start working with network models you have the problem that there is basic information for every node and on top of that you have for every node your results. Trying to interprete the results is hard. Either you have a huge excel sheet with lots of numbers for every node or you try to catch specific results in your model by filtering the output and looking at your model results listing.
 
R gives you the possibility to draw your network with all the information near the nodes. I first wrote all the numbers near the nodes with different colors, then I used arrows in different colors and different sizes. That was all nice, but still hard to read.
What I really wanted was to have something like a piechart or bars for every node directly plotted on the grid.
 
In R this is possible. You can use the x- and y-coordinates of your nodes and the drawing function polygon(). With some basic geometry (wihich I had to look up….) and ideas from Bálint Czúcz (http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=143) ,
I managed to write a R-script that takes the number of results per nodes (in my case 3, but you can have as many as you want), the grid size (in my case 5×5, but you can also use a real grid for a specific citiy). The script calculates the relative sizes of the results or basic information (for example number of old people, number of young people, number of adults = (0.3, 0.2,0.5)) and the relative size of the total (in node a we have the most people, in node b we have only 50%) and uses this information to draw scaled piecharts.
 
In the nex figure you see the results. You  now can easily see that in this case mose people live in the node with the coordinates (4,6) and that every group has about the same size.
 
 
Another way would be using bars:
 
 
Here is the most important of the code (pie chart)
 
 
 
Share

Directory Opus: A great Windows Explorer replacement

I experimented with different replacements for the Windows Explorer. I tried several free replacements, but finally decided to buy Directory Opus (“Opus”). It can be found at http://www.gpsoft.com.au/index.html. It costs about 90 US Dollar. It has a manual of over 700 pages, so you can probably imagine that it will have many features. I have been using Opus for several years now, but never took the time of configuring it properly for my needs. Yesterday I discovered a nice article by Andy  (http://www.asiteaboutnothing.net/c_dopus.html) who shows how to make Opus more user-friendly. It is a great article and I implemented almost all his ideas.
 
Here is a screenshot of my normal Opus view with some of its features highlighed
 
dopus1.png (800x676 pixels)
 
There are several display modes possible (Windows Explorer Style, Dual Horizontal (my favorite), Dual Vertical, Commander, etc.).
One of the “killer” features of Opus is the possibility of using tabs. I have my most often used folders as tabs (see callout in the figure). I now can easily jump to these folders. Another nice feature is the grouped listing of files (lower pane) . You can adjust the background colors (the orignal background color is white). More features are the possibility of defining your own menubars, macros and easy filtering.
 
A nice feature is the flat view. In this view all the files in a directory and its subdirectories are listed. The next figure shows the flat view in which I filtered for all the files with the extension “gms”. I often use the flat view to clean up a directory by grouping all the files according to their extension and then sorting them according to their svn status.
.
 
As you can see, I added a colum svn-status, so I can see if I have to commit my files (TortoiseSVN, the shell extension for Subversion nicely integrates with Opus: You can see the status also by looking at the file icons). I use the colums SVN Status for sorting according status. In one of the usergroup posts (http://resource.dopus.com/viewtopic.php?f=18&t=13293&p=68105&hilit=button+svn#p68105), is a post with how to add buttons in the menu bar with subversion commands (see first figure for the buttons). This allows me one click to commit, update or check the status of a file or directory (all subversion commands can be found by using right click too).
 
You can download a trial version for 60 days. There is a 32 and 64bit Version. Unfortunately, I don’t earny a penny if you buy Opus…
Share

Compiling LaTeX files to pdf: a second try

In a previous topic, I showed how to use a macro to kill your pdf-reader when you compile LaTeX files. Most pdf-readerss do not allow to refresh a pdf-file after compilation, so you have to close the file in the pdf-reader before compiling. My solution was to kill the pdf-reader completely. A reader of my blog wrote that you can use Sumatra, a free pdf-reader, which allows to refresh your pdf-file.
I decided to give it a try, but I wanted to keep my normal pdf-reader for all other pdf-files, because it has lots of options like commenting etc. I regularly use.
Another thing that bothered me with my pdf-reader is, that I could not jump back to Emacs (inverse-searching). Sumatra has this option.
Here are my settings in Emacs:
Sumatra will not reopen if you add the option -reuse-instance:
 Do not forget to set the inverse search command in the options of Sumatra to:
     c:\program files\emacs\bin\emacsclientw.exe +%l “%f”
Share

Using the identifier-list in Gams-mode

Gams-mode has lots of features and I am still amazed how powerful it is. I just discovered the use of the identifier-list (C-c C-a). I have a rather complex model with lots of include files and easily forget all the parameter names I use. Was it scale_mrd or mrd_scale? What was the name of the set HH or HHG? Here is where the identifier list comes in handy. Just hit C-c C-a and all the parameters, sets, variables appear as a new buffer.
 
 
 
 
You can change the way this information is listed. For example hit in this buffer “j” and all the information is grouped. You can hit the “+” or “-” sign to show or hide a certain group. Now you can work in your gams file and scroll or search through the identifier list. It is also helpful if you have forgotten to write the text describing an identifier: just scroll through the list and use “space” to jump to the identifier in you gams file and add the description. If you hit “enter” you jump to the identifier and the identifier-list buffer is closed.
 
Share

Adding Gams file extensions in gams-mode

In Gams-Mode all files with the extension “gms” are recognized as gams-files and all keywords are highlighted properly. You can add other extensions that should be recognized by setting the variable gams-file-extension.
An easy way to find this in Emacs is by going to “Options” – “Customize Emacs”  Settings matching regexp” where you search for gams-file-extension (it is in the group “gams”).
 
Just click INS and add your extension (I added inc as extension).
 
Do not forget to save your settings.
Share