For those that frequent the application of time series analysis in your workplace, you may already be aware of Visual Forecasting (VF) in SAS Viya that utilizes Model Studio. Analysts can create pipelines in a SAS VF Model Studio project and generate multitudes of time series models across all types of model classes for many time series all at once. However, did you know there is a way to access the code behind the scenes and customize your models even farther? In this post, let’s discuss In-Line Code access within Visual Forecasting.
Why would you want to access the in-line code? Access to the code expands the functionality of the user interface. You can modify the generated code, save and then rerun the project to view the modified results inside the Model Studio project. You can even take customized components from the project and share them using the Exchange.
How do you access this In-Line Code? Several modeling nodes in a SAS VF project provide access to the In-Line code. This discussion focuses on the In-Line code accessible from the Auto-Forecasting node. To start, we’ll select the Open code editor button in the properties panel.
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
Clicking this button will open a code editor window pop up where you can scroll through the produced code, make your edits, and save the changes to the node within your project.
The SAS Viya procedure that executes the user-defined programs on time series data is PROC TSMODEL. This procedure analyzes timestamped transactional data and accumulates the data into a time series. Additional packages that enhance the functionality of the procedure are also accommodated by TSMODEL. These packages include Automatic Time Series Modeling (ATSM), Time Series Models (TSM), and Time Series Analysis (TSA). The ATSM package supports automatic time series modeling and automatic forecasting. The TSM package enables users to specify and fit their own custom forecast models. The TSA package contains time series diagnostic functions that can be used as part of the programming statements in TSMODEL.
There are specific steps in using the packages and the objects contained within them. Package objects need to be declared then initialized before actions can be run on them. Let’s look at a general sample of this.
The time series data frame (TSDF) object is declared and named mytsdata. This object is then initialized. The addY method is then run on this object to populate the target variable name, mytarget, used in the analysis.
For more specifics, let’s look at the in-line code from the Auto-Forecasting node and walk through some of the pieces.
proc tsmodel data = &vf_libIn.."&vf_inData"n
outobj = (
outfor = &vf_libOut.."&vf_outFor"n
outstat = &vf_libOut.."&vf_outStat"n
outSelect = &vf_libOut.. "&vf_outSelect"n
outmodelinfo = &vf_libOut..outmodelinfo ) ;
TSMODEL begins by specifying the in-memory table that will be used in the analysis. The macro variables that are seen throughout the code are references to items set by default or selected by the user. The output objects, for example OUTFOR. are then listed. These will contain the results of the modeling and forecasting process.
*define time series ID variable and the time interval;
id &vf_timeID interval = &vf_timeIDInterval
Setmissing = &vf_setMissing trimd = LEFT;
*define time series and the corresponding accumulation methods;
%vf_varsTSMODEL;
*define the BY variables if exist;
%if "&vf_byVars" ne "" %then %do;
by &vf_byVars;
%end;
Next, TSMODEL defines the time series that results from the accumulation of the transactional data. The vf_timeID macro resolves to the variable that was given the Time role in the Data node of the project. The vf_varsTSMODEL macro resolves to the list of dependent and potential independent variables that were selected in the Data tab. This macro also contains their corresponding accumulation methods. If any BY variables are selected, the macro vf_byVars will contain them.
*using the ATSM (Automatic Time Series Model) package;
require atsm;
*starting user script;
submit;
The REQUIRE statement specifies which package will be used. In this example, we use the ATSM package. The SUBMIT statement indicates the start of the package related or DATA step like functionality of the TSMODEL procedure.
*declaring the ATSM objects;
declare object dataframe(tsdf);
declare object mydiagnose(diagnose);
declare object mydiagspec(diagspec);
declare object inselect(selspec);
declare object forecast(foreng);
Next, the objects for this package are declared and initialized. The time series data frame is used to group series variables for DIAGNOSE and FORENG objects. The DIAGNOSE object (mydiagnose) controls the automatic time series model generation. The FORENG (forecast) object controls the automatic time series model selection and forecasting. The DIAGSPEC object (mydiagspec) controls the options for the DIAGNOSE object.
*initialize the tsdf object and assign the time series roles;
rc = dataframe.initialize();
rc = dataframe.addY(&vf_depVar);
*add independent variables to the tsdf object if there are any;
%if "&vf_indepVars" ne "" %then %do;
%vf_addXTSMODEL(dataframe);
%end;
The vf_addXTSMODEL macro populates the independent variables in the dataframe.
rc = mydiagspec.open();
%if %UPCASE("&_esmInclude") eq "TRUE" %then %do;
rc = mydiagspec.setESM('METHOD', 'BEST');
%end;
%if %UPCASE("&_arimaxInclude") eq "TRUE" %then %do;
rc = mydiagspec.setARIMAX('IDENTIFY', 'BOTH');
%end;
%if %UPCASE("&_idmInclude") eq "TRUE" %then %do;
rc = mydiagspec.setIDM('INTERMITTENT', &_intermittencySensitivity);
rc = mydiagspec.setIDM('METHOD', "&_idmMethod");
%end;
%else %do;
rc = mydiagspec.setIDM('INTERMITTENT', 10000);
%end;
%if %UPCASE("&_ucmInclude") eq "TRUE" %then %do;
rc = mydiagspec.setUCM();
%end;
rc = mydiagspec.setOption('CRITERION', "&_modelSelection_criteria");
rc = mydiagspec.close();
The DIAGSPEC object regulates the model identification step in the project. Depending on the choice of model families in the forecasting node properties, this object will create a list of model generation instructions that is passed to the DIAGNOSE object.
*set the diagnose object using the diagspec object and run the diagnose process;
rc = mydiagnose.initialize(dataframe);
rc = mydiagnose.setSpec(mydiagspec);
…
rc = mydiagnose.run();
The DIAGNOSE object is initialized. Model identification restrictions contained in the DIAGSPEC object are read into DIAGNOSE object. The run method then starts the generation of the model specifications for the project.
*initialize the forecast object with the diagnose result and run model selecting and generate forecasts
(there are multiple ways to do this with code);
rc = forecast.initialize(MyDiagnose);
rc = forecast.setOption('criterion', … …&_modelSelection_criteria");
rc = forecast.setOption('lead',&vf_lead);
rc = forecast.setOption('horizon',&vf_horizonStart);
rc = forecast.setOption('minobs.trend',&_minobsTrend);
rc = forecast.setOption('minobs.mean',&_minobs);
%if "&vf_allowNegativeForecasts" eq "FALSE" %then %do;
rc = forecast.setOption('fcst.bd.lower',0);
%end;
…
rc = forecast.run();
The FORENG object is initialized next. The results of the DIAGNOSE step are loaded. Forecasting, model selection, and other options are set using the setOption methods. These options include defining the lead horizon and the model selection criterion. The run method starts the automatic model selection and forecasting process.
*collect the forecast and statistic-of-fit from running the Forecast object.;
rc = outfor.collect(forecast);
rc = outstat.collect(forecast);
rc = outSelect.collect(forecast);
rc = outmodelinfo.collect(forecast);
endsubmit;
quit;
The final step is to collect the results of the forecasts into the tables that were defined at the start of the syntax.
Visual Forecasting is a powerful tool that is meant to take the bulk of the work in time series modeling and forecasting off you. This provides you the time and resources to focus on any time series that needs additional time and exploration. With the in-line code access, you can increase this power with additional customization. Give the in-line code access a try and see what you think.
Find more articles from SAS Global Enablement and Learning here.
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.