Happy New Year with R

December 31, 2009 · Posted in R bloggers · Comments Off 

I have to admit that the previous post on Christmas is actually not much fun. Today I received another pResent from Yixuan which is more interesting:

Basically the code deals with letter polygons (i.e. glyphs) and plot them with proper projections from 3D to 2D space:

## original code by Yixuan <yixuan.qiu@cos.name>, with my slight modification
h.x = c(0.193, 0.295, 0.295, 0.703, 0.703, 0.804, 0.804, 0.703, 0.703,
    0.295, 0.295, 0.193, 0.193)
h.y = c(0.935, 0.935, 0.575, 0.575, 0.935, 0.935, 0.063, 0.063, 0.475,
    0.475, 0.063, 0.063, 0.935)
a1.x = c(0.433, 0.546, 0.865, 0.746, 0.656, 0.328, 0.242, 0.136)
a1.y = c(0.935, 0.935, 0.063, 0.063, 0.326, 0.326, 0.063, 0.063)
a2.x = c(0.488, 0.629, 0.355)
a2.y = c(0.841, 0.418, 0.418)
y.x = c(0.136, 0.259, 0.496, 0.734, 0.853, 0.543, 0.543, 0.441, 0.441)
y.y = c(0.935, 0.935, 0.523, 0.935, 0.935, 0.431, 0.063, 0.063, 0.427)
n.x = c(0.189, 0.295, 0.707, 0.707, 0.804, 0.804, 0.701, 0.287, 0.287, 0.189)
n.y = c(0.935, 0.935, 0.248, 0.935, 0.935, 0.063, 0.063, 0.747, 0.063, 0.063)
e.x = c(0.222, 0.787, 0.787, 0.324, 0.324, 0.757, 0.757, 0.324, 0.324,
    0.804, 0.804, 0.222)
e.y = c(0.935, 0.935, 0.834, 0.834, 0.564, 0.564, 0.464, 0.464, 0.163,
    0.163, 0.063, 0.063)
w.x = c(0, 0.099, 0.254, 0.439, 0.564, 0.738, 0.902, 1, 0.789, 0.687,
    0.5, 0.31, 0.205)
w.y = c(0.935, 0.935, 0.187, 0.935, 0.935, 0.189, 0.935, 0.935, 0.063,
    0.063, 0.826, 0.063, 0.063)
p.x = c(0.218, 0.56, 0.56, 0.56, 0.56, 0.32, 0.32, 0.218, 0.32, 0.32)
p.y = c(0.935, 0.933, 0.832, 0.518, 0.418, 0.418, 0.063, 0.063, 0.518, 0.834)
p.cir.th = seq(pi/2, -pi/2, length.out = 50)
p1.cir.x = 0.56 + 0.2575 * cos(p.cir.th)
p1.cir.y = 0.6755 + 0.2575 * sin(p.cir.th)
p2.cir.x = 0.56 + 0.157 * cos(p.cir.th)
p2.cir.y = 0.6755 + 0.157 * sin(p.cir.th)
p1.x = c(0.218, p1.cir.x, 0.32, 0.32, 0.218)
p1.y = c(0.935, p1.cir.y, 0.418, 0.063, 0.063)
p2.x = c(0.32, p2.cir.x, 0.32)
p2.y = c(0.832, p2.cir.y, 0.518)
r1.cir.th = seq(pi/2, -7/18 * pi, length.out = 50)
r2.cir.th = seq(pi/2, -pi/2, length.out = 50)
r1.cir.x = 0.578 + 0.243 * cos(r1.cir.th)
r1.cir.y = 0.6955 + 0.243 * sin(r1.cir.th)
r2.cir.x = 0.578 + 0.145 * cos(r2.cir.th)
r2.cir.y = 0.6955 + 0.145 * sin(r2.cir.th)
r1.x = c(0.191, r1.cir.x, 0.877, 0.75, 0.54, 0.297, 0.297, 0.191)
r1.y = c(0.937, r1.cir.y, 0.059, 0.059, 0.448, 0.448, 0.059, 0.059)
r2.x = c(0.297, r2.cir.x, 0.297)
r2.y = c(0.841, r2.cir.y, 0.55)
draw.ch = function(x1, y1, x2 = NULL, y2 = NULL, center.x,
    color, alpha, xscale) {
    rgb.col = col2rgb(color)/255
    x1 = x1 * xscale + center.x - 0.5 * xscale
    x2 = x2 * xscale + center.x - 0.5 * xscale
    polygon(x1, y1, col = rgb(rgb.col[1], rgb.col[2], rgb.col[3],
        alpha = alpha), border = NA)
    polygon(x2, y2, col = "black", border = NA)
}
x1 = list(h.x, a1.x, p1.x, p1.x, y.x, n.x, e.x, w.x,
    y.x, e.x, a1.x, r1.x)
y1 = list(h.y, a1.y, p1.y, p1.y, y.y, n.y, e.y, w.y,
    y.y, e.y, a1.y, r1.y)
x2 = list(NULL, a2.x, p2.x, p2.x, NULL, NULL, NULL,
    NULL, NULL, NULL, a2.x, r2.x)
y2 = list(NULL, a2.y, p2.y, p2.y, NULL, NULL, NULL,
    NULL, NULL, NULL, a2.y, r2.y)
th = seq(pi/6, 2 * pi, length.out = 12)
cols = rainbow(200)
## if you don't want to save the animation into SWF,
## just comment the function saveSWF()
saveSWF({
    for (j in 1:238) {
        th = th - pi/120
        center.x = 3 + 5 * cos(th)
        cols = c(cols[-1], cols[1])
        alpha = 0.1 + (50 * (1 - sin(th)))/100
        alpha = ifelse(alpha > 1, 1, alpha)
        xscale = -sin(th) * 1.2
        plot(1, xlim = c(-2, 8), ylim = c(-2.5, 3.5), type = "n")
        plot.order = (1:12)[order(xscale > 0)]
        for (k in 1:12) {
            i = plot.order[k]
            draw.ch(x1[[i]], y1[[i]], x2[[i]], y2[[i]], center.x[i],
                color = cols[k + 60 * ( i >= 6 & i <= 8 ) + 120 *
                    (i > 8)], alpha[i], xscale[i])
        }
        text(8, -2.5, "Yixuan @ 2009-12-31 (http://yixuan.cos.name)",
            adj = c(1, 0), col = "white", cex = 0.8)

    }
}, interval = 0.05, dev = "pdf", para = list(bg = "black", mar = rep(0,
    4)), width = 7, height = 4)
## in the 'fun' package (currently on R-Forge),
## see demo('HappyNewYear2010Yixuan')

Thanks, Yixuan!

By the way, there are even more interesting demos in the fun package, just install the package by install.packages('fun', repos = 'http://r-forge.r-project.org') to see demo(package = 'fun')$results[, 'Item'].

Related Posts

Updates about R-bloggers, a Happy new year, and one request

December 31, 2009 · Posted in R-bloggers news · Comments Off 

Hello dear reader of R-bloggers,

I am excited to inform you that R-bloggers has grown amazingly in the last few weeks. There are now 29 blogs participating and sharing their R related articles and tutorials with the rest of us. I didn’t even know that there where so many bloggers writing about R until now.

r-bloggers-stats

Until now (according to google analytics, click on the image above to see it in full) R-bloggers had about 2000 unique visitors, giving us (about) 4000 visits and a total of 9,400 page views. So to all of you R bloggers out there, It seems that there is much interest in what you have to say – please keep up your great writing.

Final note: a request:

If you like R-bloggers, please tell your friend about it!

Send them an e-mail or share on facebook/twitter.  If you blog about R, consider giving a link or even write a short post (some of you already did – thank you!).

It will help the bloggers here to get more exposure, and to the new reader to find more interesting reading about R.

I wish you a wonderful and amazing new year!

Tal Galili

Because it’s Friday: The inner life of a cell

December 31, 2009 · Posted in R bloggers · Comments Off 

Happy New Year, everyone. In celebration of the New Year, enjoy this celebration of the workings of life from Harvard University.



More at the link below:

3 Quarks Daily: The inner life of the cell

R/Finance 2010, April 16-17 in Chicago

December 31, 2009 · Posted in R bloggers · Comments Off 

Today is the last day to submit abstracts for the R/Finance 2010 conference to be held in Chicago on April 16-17. If you're not planning on speaking, but are interested in applications of R in Finance, be sure to add this to your calendar -- last year's conference was an outstanding event. Here's some more information about the conference from the website:

The two-day conference will cover topics including portfolio management, time series analysis, advanced risk tools, high-performance computing, and econometrics. All will be discussed within the context of using R as a primary tool for financial risk management and trading. We strongly encourage R users, as well as package authors to submit an abstract for presentation at this years conference.

The 2010 conference will build upon the success of last year's event. Including traditional keynotes from leading names in the R and finance community, presentation of contributed papers, short "lightning-style" presentations as well as the chance to meet and discuss colloboratively the future of the R in Finance community.

R/Finance 2010: Applied Finance with R

Brief Analysis of Abdulmutallab (Christmas Day bomber) Web Posting Data

December 30, 2009 · Posted in R bloggers · Comments Off 

Thanks to Evan Kohlman at the NEFA Foundation for compiling, and Danger Room for publicizing, the data set of all of Farouk Abdulmutallab’s posts to the Islamic Forum on Gawaher.com. Since Evan took the initiative to download and save the raw HTML data, I thought it would be useful to go one step further and parse it into a more useable (analyzable?) format. With a little work in Python and html5lib I was able to convert the HTML into a long comma-delimited file with observation data for post date, time, title, contents, number of views, and number of replies.

With this new data set I did some arm-chair analysis to see what—if any—interesting could be found. Using R along with ggplot2 I generated several visualizations that contain some notable observations. As I sure you are eager to get your hands on the data, I will say before moving on that the CSV file can be downloaded at the ZIA Code Repository along with an R file used to generate the visualizations and analysis after the jump.

UPDATE: The intrepid Michael Bommarito of Computational Legal Studies took it one step further, and downloaded and parsed all of Abdulmutallab’s postings and correspondences on the web forum. More data for the hungry masses, thanks Mike!

First, I am not one for content analysis, but friend and R expert Josh Reich pulled the data and created a nice Wordle of Abdulmutallab’s posts. Some interesting things here, notably the prominence of words like “think,” “help,” and “want.” Rather than look at the posts themselves, I was interested in the activity; and as such I begin with a histogram of his postings over the time perdiod.

hist.png

Other than having a very approximate seasonal tendency with two peaks, it is difficult to claim that Abdulmutallab’s posting density followed any strict pattern. Instead, at this binning (every 30 days from the first post, ignore the date label–they are wrong) it appears his activity came in ebbs and flows. Given this bustiness may be useful to also examine the popularity of Abdulmutallab’s posts. Although we have no benchmark for mean post popularity on Gawaher.com, we can examine within the sample. Below is a scatter plot of his post’s views versus replies, with points sized and colored by the ratio of replies to views.

scatter.png

This data is quite skewed, so logs were taken, and from this we can see the relationship is rather linear. There are; however, several notable posts that receive nearly one reply for every two views (large red points). For the final analysis I combine both time and activity to see if there were any periods where Abdulmutallab received notably high attention from forum users. The plot below shows his post’s view and reply counts in chronological order.

activity.png

Two interesting observation from this visualzation. First, while the previous plot indicates that several posts receive a high ratio of replies to views, this plot shows that these high ratio posts are not the most popular. In fact, the most popular posts (by view count) have relatively few replies, and all happen within a short time span. This latter observations is most interesting, as clearly Abdulmutallab was writing on something very interesting to the Gawaher.com audience during this span. The next step would be to go back into the data, examine that time period and analyze the posts’ content.

Another idea would be to create a model to examine if lagged number of replies predicted length or content of a post. There may also be something interesting to say about the content of a post’s title and the how that attracts users. Unfortunately, without the content of those replies and only the count it is hard to determine what the relationship is.

What are your ideas?

Photo: Danger Room

Use plyr instead of _apply() in R

December 30, 2009 · Posted in R bloggers · Comments Off 
I've covered plyr once before, showing you how to get means and variances for two quantitative traits across multilocus genotypes. JD Long over at Cerebral Mastication recently posted a nice screencast illustrating how plyr "just works" as an alternative to R's family of apply commands.  There's a set of R functions (apply, sapply, lapply, tapply, eapply, and rapply) that can apply a command or

What’s up with Darwin’s weather?

December 30, 2009 · Posted in R bloggers · Comments Off 

Darwin is a the capital city of Australia's Northern Territory. Lying on the coast of far Northern Australia, it's situated well in the tropics and as a result has hot, steamy, monsoonal weather. Darwin's weather has already had impact on urban culture, and now it seems it's had a political impact too: it's been at in the middle of the recent "Climategate" "scandal". A climate-change skeptic observed a discontinuity in the raw temperature records at Darwin airport in 1942 (which, as it turned out, was caused by a change in the equipment), and claimed that the standardization of the data was an overt attempt to present a cooling trend as a warming trend. This claim was roundly denounced in The Economist (prompting yet another reply and rebuttal).

Matthew Markus wasn't satisfied with drawing conclusions from a back-and-forth in the blogosphere, though. He decided to get hold of the raw data from the Global Historical Climate Network and attempt to reproduce this "smoking gun" graphic for himself in R

Darwin temps

Markus provides all the details and R code for accessing and plotting the temperature data in a blog post. There's lots of practical advice there, including how use the various command-line tools to uncompress and inspect the data (although you'll need a Unix-ish machine to follow along) as well as all the R commands for reading in the data. (It makes for a nice demonstration of reading fixed-format data files into R, actually.) You'll also see how to standardize and plot the data, with a result fairly close to the original above:

Darwin-R

Matthew Markus: Raw Darwin Airport Temperature Data with R

Start your engines; it’s a Linux era!

December 29, 2009 · Posted in R bloggers · Comments Off 

Well, I’m writing this from my new system. After years on hiatus I migrated to Linux, once again. Setting up a full system on Linux for a Greek user had been one of the greatest challenges.

First,of all setting up writing, reading & printing in Greek was the biggest obstacle, I still recall memories of 2000/2001 when installing the 9th (?) edition of Mandrake Linux, supposed to have full Greek language integration. As if? Second, comes networking with Linux. Win-modems ruled Greek market and made it really painful to read all these articles to even start hoping that you might be near configuring your Internet connection.

Yet, I used Linux till 2003 or 2004 with no Internet connection and used my Windows to surf the net… Courageous, right? Last night I was surfing for open source ideas and stumbled upon Wuby. Wubi is a windows-ish application for Ubuntu, the hottest for users of my clan! I installed on my disk, chose my graphical environment (Gnome, no regrets) and one thing left to check…the Internet highway.

Turns out that, routers (those using Ethernet ports!) are plug & go. Neat! Unfortunatelly, I’m stuck with my USB model my provider gave a couple of  years ago ;( Sad thoughts paid a short visit I must admit. But, even this  was no real problem, UbuDSL has everything under control :thumps up:

Of, course I installed RKWard, there’s a Windows version, too. I never got to make it work, though…


C++ exceptions at the R level

December 29, 2009 · Posted in R bloggers · Comments Off 

I've recently offered an extra set of hands to Dirk to work on the Rcpp package, this serves a good excuse to learn more about C++

Exception management was quite high on my list. C++ has nice exception handling (well not as nice as java, but nicer than C).

With previous versions of Rcpp, the idiom was to wrap up everything in a try/catch block and within the catch block, call the Rf_error function to send an R error, equivalent of calling stop. Now things have changed and, believe it or not, you can now catch a C++ exception at the R level, using the standard tryCatch mechanism

, so for example when you throw a C++ exception (inheriting from the class std::exception) at the C++ level, and the exception is not picked up by the C++ code, it automatically sends an R condition that contain the message of the exception (what the what member function of std::exception gives) as well as the class of the exception (including namespace)

This, combined with the new inline support for Rcpp, allows to run this code, (also available in the inst/examples/RcppInline directory of Rcpp)

require(Rcpp)
require(inline)
funx <- cfunction(signature(), '
throw std::range_error("boom") ;
return R_NilValue ;
', Rcpp=TRUE, verbose=FALSE)

Here, we create the funx "function" that compiles itself into a C++ function and gets dynamically linked into R (thanks to the inline package). The relevant thing (at least for this post) is the throw statement. We throw a C++ exception of class "std::range_error" with the message "boom", and what follows shows how to catch it at the R level:

tryCatch(  funx(), "C++Error" = function(e){
    cat( sprintf( "C++ exception of class '%s' : %s\n", class(e)[1L], e$message  ) )
} )
# or using a direct handler 
tryCatch(  funx(), "std::range_error" = function(e){
        cat( sprintf( "C++ exception of class '%s' : %s\n", class(e)[1L], e$message  ) )
} )

... et voila

Under the carpet, the abi unmangling namespace is at work, and the function that grabs the uncaught exceptions is much inspired from the verbose terminate handler that comes with the GCC

Part of this was inspired from the new java exception handling that came with the version 0.8-0 of rJava, but cooked with C++ ingredients

RGedit

December 29, 2009 · Posted in R bloggers · Comments Off 
RGedit is A plugin for Gedit to run R script.

Next Page »

Diag| Memory: Current usage: 35950 KB
Diag| Memory: Peak usage: 36143 KB