18 Mar 2013 nutella   » (Master)

I am glad to see that Pedro is also having fun with R. There are, however, times when it drives me crazy. For example, carrying out simple linear regression, then using the result to predict a value. Note that in the code below I have replaced my preferred assignment operator (less_than + hyphen) with Pascal's := because Advogato doesn't like stray angle brackets, so rewrite it if you want to run the code;
# Let's create some x and y values
xlist := c(1:5);
ylist := c(0.9, 2.1, 3.2, 4.3, 5.1);
# Now simple linear regression
regobject := lm(ylist ~ xlist, data=data.frame(cbind(xlist, ylist)));

If I look at the contents of regobject then all is well. However, now for the prediction. In a sane world this should be as simple as;
predict(regobject, newdata=testvalue);

Where testvalue has the x-value to be used in the prediction. Unfortunately R fails worse than silently as it just provides the predicted values for everything in xlist. Okay, maybe I need to coerce the value into a data frame.
predict(regobject, newdata=data.frame(testvalue));

Nope. It needs a data frame with a column with the same name as that used to generate the regression object. I have not seen that written explicitly in any book or help file. Thankfully a few other folk have ranted about this so there are explanations out there.
predict(regobject, newdata=data.frame(xlist=testvalue));

Woohoo! So now,
predict(regobject, newdata=data.frame(xlist=2.5));

gives me the predicted y-value for x=2.5 (y=2.59). My guess is that being an R programmer is now so lucrative that documentation is written in an obscure (but factually correct) manner deliberately. Prepare for this blog entry to receive a takedown notice :-)

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!