0
$\begingroup$

When I forecast from a linear Regression model in R using the following code, I get an arguments of length zero error , which I understand as a null pointer:

library(forecast)
Mwh = c(16.3,16.8,15.5,18.2,15.2,17.5,19.8,19.0,17.5,16.0,19.6,18.0)
temp = c(29.3,21.7,23.7,10.4,29.7,11.9,9.0,23.4,17.8,30.0,8.6,11.8)
t=data.frame(Mwh,temp)
fit = lm(Mwh~temp,data=t)
fcast=forecast(fit,newdata=35)

However, the following code, which looks very, very similar to me, does not produce the error, but the forecast as desired - why? Where is my blind spot? This works:

library(forecast)
electricity =c(16.3,16.8,15.5,18.2,15.2,17.5,19.8,19.0,17.5,16.0,19.6,18.0) 
maximumDailyTemp= c(29.3,21.7,23.7,10.4,29.7,11.9,9.0,23.4,17.8,30.0,8.6,11.8) 
d = data.frame(maximumDailyTemp, electricity)      
fit<-lm(electricity ~ maximumDailyTemp , data=d)
electricityForecast<-forecast(fit,newdata=35)

Why is there an error above and no error below? The order of variables in the data Frames cannot be the reason, right?

$\endgroup$

2 Answers 2

3
$\begingroup$

This problem seems to happen because of using $t$ for the dataframe. However, in R, $t$ is a command for the transpose operation. The below code (using $d$) works!

library(forecast)
Mwh = c(16.3,16.8,15.5,18.2,15.2,17.5,19.8,19.0,17.5,16.0,19.6,18.0)
temp = c(29.3,21.7,23.7,10.4,29.7,11.9,9.0,23.4,17.8,30.0,8.6,11.8)
d = data.frame(Mwh, temp)
fit = lm(Mwh~temp, data=d)
fcast=forecast(fit, newdata=35)
$\endgroup$
4
  • $\begingroup$ thank you, this helps - cross-checked: the above code works with d and the below code does not work with t $\endgroup$
    – Statos
    Commented Mar 13, 2015 at 8:07
  • $\begingroup$ Hi Delyan again, I've checked the discussion at stackoverflow.com/questions/6135868/… $\endgroup$
    – Statos
    Commented Mar 17, 2015 at 11:32
  • $\begingroup$ The discussion in my above comment shows, that there is no general problem naming at data frame t, but that any confusion comes from human confusion inside the code, e.g. confusing t(...) and t[...]. Wrt to your answer: how can such a confusion happen here? How does the name of the data frame get "lost" while executing forecast? Remarkeably, this is e.g. not the case, when renaming the data frames in stat.ethz.ch/R-manual/R-patched/library/stats/html/… to t instead of new - now null pointer exception there, useable predictions instead. $\endgroup$
    – Statos
    Commented Mar 17, 2015 at 11:39
  • $\begingroup$ Wow, this makes it even more interesting, I have no idea where this error is coming from then. Perhaps, you could ask Rob Hyndman, who is the author of the forecast package and a founder of this site. $\endgroup$ Commented Mar 18, 2015 at 12:23
0
$\begingroup$

I took a look at the results of the debugger of the forecast()-function digesting "t" and "d" from the above discussion. Moreover, I asked Rob Hyndman as the author of forecast() for help. The problem can clearly be avoided by using "speaking" variable names. At one point a variable "t" will be confused with the function "t()", as Rob pointed out in his answer:

"The problem here is that the lm() function does not actually save anything to data (unlike tslm). So in the following lines, the function follows the else path

if (!is.null(object$data)) 
        origdata <- object$data
else if (!is.null(object$call$data)) 
    origdata <- object$data <- eval(object$call$data)

But when it tries to eval() the name of the data object, it finds it is called t which then gets evaluated as the transpose function because there is no t object in the local environment (although there is one at the parent environment).

Lesson: never use existing function names for your data objects." (Rob Hyndman)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.