6

Is there any difference between the predict() and forecast() functions in R?

If yes, in which specific cases should they be used?

Mark Rucker
  • 6,952
  • 4
  • 39
  • 65
Abhay Bhadani
  • 119
  • 1
  • 3
  • 13
  • I suggested your question to be moved to "cross validated", as it's not really about programming, but about concepts – PavoDive Jul 14 '15 at 14:33
  • 2
    @PavoDive - They are asking about the differences between the functions which isn't necessarily a statistical issue. I'm not sure it's a good question but I wouldn't automatically jump on the move it to CV train. – Dason Jul 14 '15 at 14:34
  • @Dason what motivated me to flag the move was the "in which cases..." part – PavoDive Jul 14 '15 at 14:39
  • In what situation are you seeing `forecast()`? I've never used it before, it is specific to a particular library? – Mhairi McNeill Jul 14 '15 at 14:42
  • 3
    `predict` is a generic function so it can be redefined for different types of object. This question is too broad to be answerable. You really should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) showing which context you are using these functions. Include some sample data, show that you are getting different results and then we can help you understand why. – MrFlick Jul 14 '15 at 14:50
  • @MhairiMcNeill: `forecast()` is available in _forecast_ package. – Abhay Bhadani Jul 15 '15 at 03:18
  • 1
    @MrFlick: I was trying to predict time series using ARIMA model. I had seen few examples where `predict()` was used. Robert's answer helped me understand and as per your suggestion, both the functions yielded the same results. Thanks! – Abhay Bhadani Jul 15 '15 at 03:56

1 Answers1

3

Intro

  • predict -- for many kinds of R objects (models). Part of the base library.
  • forecast -- for time series. Part of the forecast package. (See example).

Example

#load training data
trnData = read.csv("http://www.bodowinter.com/tutorial/politeness_data.csv")

model <- lm(frequency ~ attitude + scenario, trnData)

#create test data
tstData <- t(cbind(c("H1", "H", 2, "pol", 185),
                   c("M1", "M", 1, "pol", 115),
                   c("M1", "M", 1, "inf", 118),
                   c("F1", "F", 3, "inf", 210)))

tstData <- data.frame(tstData,stringsAsFactors = F)
colnames(tstData) <- colnames(trnData)
tstData[,3]=as.numeric(tstData[,3])
tstData[,5]=as.numeric(tstData[,5])

cbind(Obs=tstData$frequency,pred=predict(model,newdata=tstData))

#forecast
x <- read.table(text='day       sum
                    2015-03-04   44           
                    2015-03-05   46           
                    2015-03-06   48           
                    2015-03-07   48           
                    2015-03-08   58           
                    2015-03-09   58           
                    2015-03-10   66           
                    2015-03-11   68           
                    2015-03-12   85           
                    2015-03-13   94           
                    2015-03-14   98           
                    2015-03-15  102           
                    2015-03-16  102           
                    2015-03-17  104           
                    2015-03-18  114', header=TRUE, stringsAsFactors=FALSE)
library(xts)
dates=as.Date(x$day,"%Y-%m-%d")
xs=xts(x$sum,dates)

library("forecast")
fit <- ets(xs)
plot(forecast(fit))
forecast(fit, h=4)
Community
  • 1
  • 1
Robert
  • 5,038
  • 1
  • 25
  • 43