I have been curious for a while as to how R might be used for the construction of an individually-based model (IBM), or agent-based model (ABM). In particular, what R objects lend themselves best to storing information on individuals, and allow for new individuals to be added or subtracted throughout the simulation?
Tuesday, October 29, 2013
A first attempt at an individual-based model in R
I have been curious for a while as to how R might be used for the construction of an individually-based model (IBM), or agent-based model (ABM). In particular, what R objects lend themselves best to storing information on individuals, and allow for new individuals to be added or subtracted throughout the simulation?
Thursday, April 25, 2013
A plea for less word clouds
![]() |
| Word cloud of DOMA hearing transcripts |
Word clouds are pervasive - even in the science world. I was somewhat spurred to write this given the incredibly wasteful summaries of EGU General Assembly survey results that include several useless word clouds (link to document). Capitalization of words isn't even considered; e.g. "Nice" vs."nice". I have been hesitant to equate word clouds to the hilariously labeled "mullets of the internet" but, on second thought, it is entirely appropriate. They were once fad, but seem reluctant to die...
Oh, and yes, a "tag cloud" is a type of word cloud - I have fallen into the trap myself by including such a thing on this blog! I honestly didn't make the connection at first, because, at least, it had the function of showing the relative importance of terms that I personally defined as topics - not an arbitrary puking up of all the words that I have ever written here. Nevertheless, I think it must be removed now - I can't tell you how many times that I have wanted to go to a specific blog post by clicking on a tag, only to be forced to search into the nether regions of (extremely) small font size. Simple alphabetical arrangement probably makes more sense.
There are some attempts at making word clouds with R (most notable the "wordcloud" package), but they don't seem to be as visually appealing as those easily produced by sites such as Wordle. Nevertheless, you continue to see such things produced - just do a search for "word cloud" on R-bloggers for many examples.
I decided to give Wordle a try, and chose the Defence of Marriage Act (DOMA) hearing transcripts as a source for text. The above word cloud shows the results (with some beautiful patriotic colonial-looking font to boot!). It doesn't reveal much to me. An initial attempt caught me off-guard in that the dominant word was "justice" (below), which would have possibly been insightful if it hadn't been a construct of the prevalence of the speakers titles (i.e. "Justice Kagan"):
![]() |
| An even more worthless word cloud of DOMA hearing transcripts |
Anyway, I'm glad I'm not alone in this thinking - I have come across
many discussions along the same lines; in particular, the nice article Jacob Harris.
Unfortunately, it seems they are here to stay, and I will just have to
learn to better avert my eyes from their alluring power in the future...
Monday, January 28, 2013
My template for controlling publication quality figures
The following is a template that I usually start with when producing figures for publication. It allows me to control:
- The overall size of the figure (in inches) (WIDTH, HEIGHT)
- The layout of figure subplots (using the layout() function) (LO)
- The resolution of the figure (for a .png file) (RESO)
I define margins (mar, oma) in terms of point size (ps), since this relates to the height of text, which allows of control of axis labeling. By defining the outer margins (OMA) and point size (PS) before calling layout, you will have these margins incorporated. Then, by running the x11() device (after the #), you can check your figure layout with layout.show(n):
I learned recently that the layout() function will adjust the character expansion size (par()$cex) depending on how your device is split up. For that reason, I usually include another line of code resetting par(cex=1) before proceeding with individual plots.
Finally, the three different device types included in the template are:
- x11(), for initial tweaking of the layout and general functionality of the plotting code
- png(), for producing a compact figure useful in pasting into Word documents, and for cases where the figure contains a lot of information and would be slow to loading as a .pdf
- pdf(), for a vector-based figure that is fully scalable / zoomable. When not too big, these figures look the best, and can also be embedded in LaTeX documents
Figure template:
Friday, January 18, 2013
Choosing colors visually with 'getcolors'
For the example, I chose 4 colors, which are saved in a vector. These colors were subsequently used to make the following line plot:
the getcolors function:
Thursday, January 10, 2013
Lomb-Scargle periodogram for unevenly sampled time series
The above figure shows a Lomb-Scargle periodogram of a time series of sunspot activity (1749-1997) with 50% of monthly values missing. As expected (link1, link2), the periodogram displays a a highly significant maximum peak at a frequency of ~11 years.
The function comes from a nice set of functions that I found here: http://research.stowers-institute.org/efg/2005/LombScargle/R/index.htm. An accompanying paper focusing on its application to time series of gene expression can be found here.
Below is a comparison to an FFT of the full time series. For another great resource on spectral analysis, and time series-related R methods in general, see the following website: http://zoonek2.free.fr/UNIX/48_R/15.html.
Labels:
Fourier transform,
function,
gappy data,
Lomb-Scargle,
periodogram,
R,
sparse data,
time series
Wednesday, January 2, 2013
Producing animated GIFs and Videos
One is also able to make great videos:
To reproduce the example (requires 'spirographR' function):
Sunday, December 30, 2012
Spirograph with R
Tuesday, December 4, 2012
Finding a pin in a haystack - PCA image filtering
I found the following post regarding the anomalous metal object observed in a Curiosity Rover photo to be fascinating - specifically, the clever ways that some programmers used for filtering the image for the object. The following answer on mathematica.stackexchange.com was especially illuminating for its use of a multivariate distribution to describe the color channels for a test region of "sand". This distribution was subsequently used to assess if the rest of the image colors belonged to the same distribution.
Tuesday, October 30, 2012
DINEOF (Data Interpolating Empirical Orthogonal Functions)
I finally got around to reproducing the DINEOF method (Beckers and Rixon, 2003) for optimizing EOF analysis on gappy data fields - it is especially useful for remote sensing data where cloud cover can result in large gaps in data. Their paper gives a nice overview of some of the various methods that have been used for such data sets. One of these approaches, which I have written about before, involves deriving EOFs from a covariance matrix as calculated from available data. Unfortunately, as the author's point out, such covariance matrices are no longer positive-definite, which can lead to several problems. The DINEOF method seems to overcome several of these issues.
Labels:
algorithm,
DINEOF,
EOF,
function,
gappy data,
interpolation,
optimization,
PCA,
R,
sparse data,
spatial,
SVD
Friday, April 27, 2012
Create polygons from a matrix
The following function matrix.poly allows for the addition of polygons to a plot based on a matrix and defined matrix positions. I have used this function on occasion to highlight specific matrix locations (e.g. in the above figure). You can do the same by overlaying another image (left in above plot) but with this function you will have all other polygon plotting possibilities (e.g. borders etc.).
Subscribe to:
Posts (Atom)













