user1471980
user1471980

Reputation: 10646

how do you run predict on time series data?

I am trying to create prediction based on time series data.

My data frame call dat looks like this:

dput(head(dat))

dat <- structure(list(out = c(5, 0, 0, 0, 0, 0), Date = c(1423825200000, 
1423825500000, 1423825800000, 1423826100000, 1423826400000, 1423826700000
)), .Names = c("out", "Date"), row.names = c(NA, 6L), class = "data.frame")

Currently there are 81 rows in my data frame dat. My columns are called out and Date, Date column is in epoch.

I need to build a linear model first:

 lin <- lm(dat[,1]~dat[,2], data=dat)

based on this model, I need to predict hourly data points for 7 days etc so, I do this:

t<-3600
newdata <- seq(tail(dat$Date,1), tail(dat$Date,1)+604800, t)
newdata<-data.frame(newdata)
    colnames(newdata)<-c("Date")
    predictions <- predict(lin, newdata=newdata, level=0.95, interval="prediction")
    predictions <- data.frame(predictions)
    f<-predictions
    f<-data.frame(f)
    f<-cbind(f, newdata)
    f<-f[,c("fit", "Date")]
    colnames(f)<-c("Forecast", "Date")

I get this error:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 81, 169 In addition: Warning message:
'newdata' had 169 rows but variables found have 81 rows

Regardless of the size of my data frame (dat), I should be able to build a linear model and based on newdata, I should execute the predict function. Any ideas what might be going on here?

Upvotes: 2

Views: 1248

Answers (1)

Hao
Hao

Reputation: 7856

Try this. In this way, you can still keep everything dynamic.

variable.list<-names(dat)
lin <- lm(as.formula(paste(variable.list[1],variable.list[2], sep="~") ), data=dat)

Let me know if it works

Upvotes: 4

Related Questions