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;
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.
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.
Woohoo! So now,
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 :-)