May 2008


So I was up near a job re-training facility near Springfield MA. They had a list of companies from which employees could utilize their service if they were terminated. What jumped out at me was that the Federal Reserve Bank was on the list. That is right, the place that prints our paper money, is firing people. Top this off, I was on my way to lunch, and a lady comes up to me begging for money. I was shocked, I only saw this sort of thing when I traveled overseas to developing countries. This was Massachusetts. If my short riff was not enough to scare you, take a read of this article that included a discussion on a recent panel with Nouriel Roubini.Eye Opener

I recently was searching for a way to do kernel density estimatation. I came across a module for matlab, but I do not have a copy of matlab. I tried porting it over to perl, but I was having some problems getting the inverse discrete fourier transforms working in Math::FFT the way I wanted them to work. In the end, I ended up compiling the latest version of the R language 2.7.0 and then installing RSPerl which is a bridge to allow you to call R from perl and vice versa. One of the maintainers helped me out. There are some environment scripts you should source in your shell rc file first. Once you have that done, one of the perl modules that came with RSPerl had an extra =cut that I had to take out. The tie call in the RReferences.pm was not needed since no TIESCALAR was ever defined. After these small changes, the whole thing worked like a charm. I am including an example here since I did not find anything like it in any of the other examples. Given a sample of some random variable, this will perform a kernel density estimation using gaussian kernel. It will create a plot. Then it will dump the X and Y values seen in the plot to the screen. You can of course use these values for other things.

#!/usr/local/bin/perl -w
use strict;

# stuff to get R started
use R;
use RReferences;
&R::initR("--silent");

# sample of random variable
my $x = [1,2,2,3];

# do the kernel density estimation  $h will be the reference to the result
my $h = &R::callWithNames(”density”, {’x',$x,’kernel’,'gaussian’,'na.rm’,'TRUE’});

# plot the result
&R::callWithNames(”plot”, {’x',$h,’main’,'gaussian’});

# sleep for a few seconds so the plot window does not vanish
sleep(3);

# get the x values from the result object
my @xv = $h->getEl(’x');

# get the y values from the result object
my @yv = $h->getEl(’y');

print “X values\n”;
print join(”\n”,@xv) . “\n”;

print “Y values\n”;
print join(”\n”,@yv) . “\n”;

Today is the day that I put this blog up on a couple of places to get more exposure. I hope my posts on using perl for everyday things will help out the community.

Technorati Profile