Creating only several hours of a day as a time series I have many zeros in my hourly time series data but for sure I know that from 22:00-7:00 the values are all 0. So, I don't want to put these in my forecasting model. Otherwise I have a very sparse dataset that I couldn't find a good model.
I want to create a time series only for the hours between 7:00 to 22:00 for each day. Can it be possible? How?
Thanks a lot.
Ceylan
 A: Certainly you can do that. Simply extract the periods that are "active" and encode them as a ts object. Just make sure to set the correct frequency, which in your case would be 15, since there are 15 "active" hourly buckets in every day.
When forecasting, you may want to consider models for multiple-seasonalities, since you will probably have time of day and day of week seasonality. (And possibly also longer periods, like day of year.)
As an example, I create hourly data for 15 days with zeros in 9 out of 24 hours below, then plot it, then extract the "active" hours, turn these into a ts object, and finally fit a very simple ets() model.
n_days <- 21
set.seed(1)
all_data <- cbind(matrix(0,nrow=n_days,ncol=7),
    matrix(rnorm(n_days*15,20,5),nrow=n_days),
    matrix(0,nrow=n_days,ncol=2))
dimnames(all_data) <- list(paste0("day_",sprintf("%02i",1:n_days)),paste0("hour_",sprintf("%02i",0:23)))
all_data

plot(as.vector(t(all_data)),type="o",pch=19,xlab="Hour",ylab="",las=1)
abline(v=24*(1:n_days),lty=2)

data_without_zeros <- all_data[,8:22]
data_without_zeros_ts <- ts(as.vector(t(data_without_zeros)),frequency=15)

library(forecast)
ets(data_without_zeros_ts)


