Forecasting time series data

Here’s what I did: x$Date = as.Date(x$Date,format=”%m/%d/%Y”) x = xts(x=x$Used, order.by=x$Date) # To get the start date (305) # > as.POSIXlt(x = “2011-11-01″, origin=”2011-11-01”)$yday ## [1] 304 # Add one since that starts at “0” x.ts = ts(x, freq=365, start=c(2011, 305)) plot(forecast(ets(x.ts), 10)) Resulting in: What can we learn from this: Many of your steps … Read more

Converting a data frame to xts

This is clearly documented — xts and zoo objects are formed by supplying two arguments, a vector or matrix carrying data and Date, POSIXct, chron, … type supplying the time information (or in the case of zoo the ordering). So do something like qxts <- xts(q[,-1], order.by=q[,1]) and you should be set.

Why apply() returns a transposed xts matrix?

That’s what apply is documented to do. From ?apply: Value: If each call to ‘FUN’ returns a vector of length ‘n’, then ‘apply’ returns an array of dimension ‘c(n, dim(X)[MARGIN])’ if ‘n > 1’. In your case, ‘n’=48 (because you’re looping over rows), so apply will return an array of dimension c(48, 7429). Also note … Read more

Basic lag in R vector/dataframe

I had the same problem, but I didn’t want to use zoo or xts, so I wrote a simple lag function for data frames: lagpad <- function(x, k) { if (k>0) { return (c(rep(NA, k), x)[1 : length(x)] ); } else { return (c(x[(-k+1) : length(x)], rep(NA, -k))); } } This can lag forward or … Read more

Conversion of a data into time series object

The question’s use of the xts function is incorrect but it doesn’t really matter since xts cannot represent that sort of data in the first place. xts requires an index class that represents dates or date/times such as Date or POSIXct but here we have plain numbers. Create a zoo series instead. library(zoo) # test … Read more