Limit forecasted values for an ARIMA_PLUS time series model
This tutorial teaches you how to use limits to narrow the forecasted results returned by an ARIMA_PLUS time series model. In this tutorial, you create two time series models over the same data, one model which uses limits and one model that doesn't use limits. This lets you compare the results returned by the models
and understand the difference that specifying limits makes.
You use the
new_york.citibike_trips data to train the models in this tutorial. This dataset contains information about Citi Bike trips in New York City.
Before following this tutorial, you should be familiar with single time series forecasting. Complete the Single time series forecasting from Google Analytics data tutorial for an introduction to this topic.
Required Permissions
To create the dataset, you need the
bigquery.datasets.createIAM permission.To create the model, you need the following permissions:
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateData
To run inference, you need the following permissions:
bigquery.models.getDatabigquery.jobs.create
For more information about IAM roles and permissions in BigQuery, see Introduction to IAM.
Objectives
In this tutorial, you use the following:
- The
CREATE MODELstatement: to create a time series model. - The
ML.FORECASTfunction: to forecast daily total visits.
Costs
This tutorial uses billable components of Google Cloud, including the following:
- BigQuery
- BigQuery ML
For more information about BigQuery costs, see the BigQuery pricing page.
For more information about BigQuery ML costs, see BigQuery ML pricing.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the BigQuery API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the BigQuery API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
Create a dataset
To create a BigQuery dataset, select one of the following options:Console
In the Google Cloud console, go to the BigQuery page.
In the left pane, click Explorer:
[Highlighted button for the Explorer pane.]If you don't see the left pane, click Expand left pane to open the pane.
In Explorer, expand your project, and then click Datasets.
On the Datasets page, click Create dataset.
In the Create dataset pane, do the following:
For Dataset ID, enter
bqml_tutorial.For Data location, select US.
Leave the remaining default settings as they are.
Click Create dataset.
bq
To create a new dataset, use the
bq mk --dataset command.
Create a dataset named
bqml_tutorialwith the data location set toUS:bq mk --dataset \ --location=US \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
Confirm that the dataset was created:
bq ls
API
Call the datasets.insert
method with a defined dataset resource:
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
BigQuery DataFrames
Before trying this sample, follow the BigQuery DataFrames setup instructions in the BigQuery quickstart using BigQuery DataFrames. For more information, see the BigQuery DataFrames reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up ADC for a local development environment.
Visualize the time series you want to forecast
Before creating the model, it is useful to see what your input time series looks like.
SQL
In the following query, the FROM bigquery-public-data.new_york.citibike_trips
clause indicates that you are querying the citibike_trips table in the
new_york dataset.
In the SELECT statement, the query uses the
EXTRACT function
to extract the date information from the starttime column. The query uses
the COUNT(*) clause to get the daily total number of Citi Bike trips.
#standardSQL SELECT EXTRACT(DATE from starttime) AS date, COUNT(*) AS num_trips FROM `bigquery-public-data`.new_york.citibike_trips GROUP BY date
To run the query, use the following steps:
In the Google Cloud console, click the Compose new query button.
Enter the following GoogleSQL query in the query editor.
#standardSQL SELECT EXTRACT(DATE from starttime) AS date, COUNT(*) AS num_trips FROM `bigquery-public-data`.new_york.citibike_trips GROUP BY date
Click Run. The query results similar to the following.
Use the Google Cloud console to chart the time series data. In the Query results pane, click the Visualization tab. In the Visualization configuration pane, choose Bar for the Visualization type:
BigQuery DataFrames
Before trying this sample, follow the BigQuery DataFrames setup instructions in the BigQuery quickstart using BigQuery DataFrames. For more information, see the BigQuery DataFrames reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up ADC for a local development environment.
In the following sample, bigquery-public-data.new_york.citibike_trips
indicates that you are querying the citibike_trips table in the
new_york dataset.
The result is similar to the following:
[Result_visualization]
Create a time series model
Create a time series model, using the NYC Citi Bike trips data.
The following GoogleSQL query creates a model that forecasts daily total
bike trips. The CREATE MODEL
statement creates and trains a model named bqml_tutorial.nyc_citibike_arima_model.
#standardSQL CREATE OR REPLACE MODEL bqml_tutorial.nyc_citibike_arima_model OPTIONS ( model_type = 'ARIMA_PLUS', time_series_timestamp_col = 'date', time_series_data_col = 'num_trips', time_series_id_col = 'start_station_id') AS SELECT EXTRACT(DATE FROM starttime) AS date, COUNT(*) AS num_trips, start_station_id FROM `bigquery-public-data`.new_york.citibike_trips WHERE starttime > '2014-07-11' AND starttime '2014-07-11' AND starttime '2014-07-11' AND starttime '2014-07-11' AND starttime '2014-07-11' AND starttime '2014-07-11' AND starttime
Web Proxy Viewer | New URL | Original Page