By atmathew
Given a data frame with multiple columns which contain time series data, let’s say that we are interested in executing an automatic forecasting algorithm on a number of columns. Furthermore, we want to train the model on a particular number of observations and assess how well they forecast future values. Based upon those testing procedures, we will estimate the full model. This is a fairly simple undertaking, but let’s walk through this task. My preference for such procedures is to loop through each column and append the results into a nested list.
First, let’s create some data.
ddatWe want to forecast future values of the three columns. Because we want to save the results of these models into a list, lets begin by creating a list that contains the same number of elements as our data frame.
lst.namesI've gone ahead and written a user defined function that handles the batch forecasting process. It takes two arguments, a data frame and default argument which specifies the number of observations that will be used in the training set. The model estimates, forecasts, and diagnostic measures will be saved as a nested list and categorized under the appropriate variable name.
batch = 0.025if(cond1|cond2){
mfcas = forecast(ma(data[,i], order=3), h=5)
lst[[i]][["moving_average"]]This isn't the prettiest code, but it gets the job done. Note that lst was populated within a function and won't be available in the global environment. Instead, I chose to simply print out the contents of the list after the function is evaluated.
Related
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, ...read moreSource:: r-bloggers.com