Time Series Database Interface: R MySQL (TSMySQL)



Similar documents
Package TSfame. February 15, 2013

Chapter 30 Exporting Inventory Management System Data

A Brief Introduction to MySQL

A table is a collection of related data entries and it consists of columns and rows.

Package sjdbc. R topics documented: February 20, 2015

Integrating VoltDB with Hadoop

Bidirectional replication for InterBase and Firebird

Accessing Your Database with JMP 10 JMP Discovery Conference 2012 Brian Corcoran SAS Institute

Web Development using PHP (WD_PHP) Duration 1.5 months

Databases and SQL. Homework. Matthias Danner. June 11, Matthias Danner Databases and SQL June 11, / 16

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

Setting Up Specify to use a Shared Workstation as a Database Server

Linas Virbalas Continuent, Inc.

!"# $ %& '( ! %& $ ' &)* + ! * $, $ (, ( '! -,) (# *&23. mysql> select * from from clienti;

Lab 2: PostgreSQL Tutorial II: Command Line

Database Migration : An In Depth look!!

LABSHEET 1: creating a table, primary keys and data types

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

news from Tom Bacon about Monday's lecture

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

Webapps Vulnerability Report

Understanding Sql Injection

Product: DQ Order Manager Release Notes

Firebird. Embedded SQL Guide for RM/Cobol

Exposed Database( SQL Server) Error messages Delicious food for Hackers

ODBC Client Driver Help Kepware, Inc.

User Migration Tool. Note. Staging Guide for Cisco Unified ICM/Contact Center Enterprise & Hosted Release 9.0(1) 1

TV Server Configuration Fails To Start - get_cards() error. Overview... 2 Origin of the error... 2 Resolution Related... 5

SQL. Short introduction

Tutorial: How to Use SQL Server Management Studio from Home

Bloomberg 1 Database Extension for EViews

ArpViewer Manual Version Datum

Lecture 25: Database Notes

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

Application note: Connecting the to a Database

"SQL Database Professional " module PRINTED MANUAL

The SkySQL Administration Console

Oracle Database 10g: Introduction to SQL

Using SQL Server Management Studio

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Zend Framework Database Access

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports Server 6i

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

Oracle Database 10g Express

B.1 Database Design and Definition

Using Databases in R

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

Working with xts and quantmod

Version of this tutorial: 1.06a (this tutorial will going to evolve with versions of NWNX4)

MS ACCESS DATABASE DATA TYPES

Add User to Administrators Group using SQL Lookup Table

ODBC Chapter,First Edition

Published. Technical Bulletin: Use and Configuration of Quanterix Database Backup Scripts 1. PURPOSE 2. REFERENCES 3.

Percona Server features for OpenStack and Trove Ops

Architecting the Future of Big Data

Note: With v3.2, the DocuSign Fetch application was renamed DocuSign Retrieve.

XEP-0043: Jabber Database Access

How to schedule and automate backups of SQL Server databases in SQL Server Express Editions

SQL Server An Overview

Using ODBC with MDaemon 6.5

Talend for Data Integration guide

Using IRDB in a Dot Net Project

There are numerous ways to access monitors:

The MariaDB Audit Plugin

Package survpresmooth

Business Intelligence Tutorial: Introduction to the Data Warehouse Center

WebSphere Business Monitor V6.2 KPI history and prediction lab

Developing SQL and PL/SQL with JDeveloper

Your Best Next Business Solution Big Data In R 24/3/2010

Installation and configuration op5 Oracle Extension

Setting up SQL Translation Framework OBE for Database 12cR1

Abstract. For notes detailing the changes in each release, see the MySQL for Excel Release Notes. For legal information, see the Legal Notices.

Computing with large data sets

Customer Bank Account Management System Technical Specification Document

Database Extension 1.5 ez Publish Extension Manual

SQL Injection January 23, 2013

CAPIX Job Scheduler User Guide

Package RSQLite. February 19, 2015

Migrate Topaz databases from One Server to Another

An Introduction to Developing ez Publish Extensions

Detecting (and even preventing) SQL Injection Using the Percona Toolkit and Noinject!

CASHNet Secure File Transfer Instructions

Upgrade Guide BES12. Version 12.1

Setting Up ALERE with Client/Server Data

DaimlerChrysler EBMX HTTP/s Quick Start Guide

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

Adam Rauch Partner, LabKey Software Extending LabKey Server Part 1: Retrieving and Presenting Data

PROCESSES LOADER 9.0 SETTING. Requirements and Assumptions: I. Requirements for the batch process:

Mechanics Bank Mobile Banking Mobile Finance Manager (MFM) Application Windows Mobile Phone Installation

CPE111 COMPUTER EXPLORATION

Package fimport. February 19, 2015

DBX. SQL database extension for Splunk. Siegfried Puchbauer

MariaDB Cassandra interoperability

PassMark Software BurnInTest Management Console. Quick start guide

Transcription:

Time Series Database Interface: R MySQL (TSMySQL) March 14, 2011 1 Introduction The code from the vignette that generates this guide can be loaded into an editor with edit(vignette( TSMySQL )). This uses the default editor, which can be changed using options(). It should be possible to view the pdf version of the guide for this package with print(vignette( TSMySQL )). WARNING: running these example will overwrite tables in the MySQL test database on the server. Once R is started, the functions in this package are made available with > library("tsmysql") This will also load required packages TSdbi, DBI, RMySQL, methods, and tframe. Some examples below also require zoo, and tseries. The MySQL user, password, and hostname should be set in MySQL client configuration file (.my.cnf) before starting R. Alternatively, this information can be set with environment variables MYSQL USER, MYSQL PASSWD and MYSQL HOST. (An environment variable MYSQL DATABASE can also be set, but test is specified below.) Below, the environment variable MYSQL USER is used to determine which of these methods is being used. If this environment variable is empty then it is assumed the configuration file will be used. > user <- Sys.getenv("MYSQL_USER") > if (""!= user) { host <- Sys.getenv("MYSQL_HOST") if ("" == host) host <- Sys.info()["nodename"] passwd <- Sys.getenv("MYSQL_PASSWD") if ("" == passwd) passwd <- NULL } 1

The next small section of code is necessary to setup database tables that are used in the examples below. It needs to be done only once for a database and might typically be done by an administrator setting up the database, rather than by an end user. > m <- dbdriver("mysql") > con <- if ("" == user) dbconnect(m, dbname = "test") else dbconnect(m, dbname = "test", username = user, password = passwd, host = host) > source(system.file("tssql/createtables.tssql", package = "TSdbi")) > dbdisconnect(con) More detailed description of the instructions for building the database tables is given in the vignette for the TSdbi package. Those instruction show how to build the database using database utilites rather than R, which might be the way a system administrator would build the database. 2 Using the Database - TSdbi Functions This section gives several simple examples of putting series on and reading them from the database. (If a large number of series are to be loaded into a database, one would typically do this with a batch process using the database program s utilities for loading data.) The first thing to do is to establish a connection to the database: > con <- if ("" == user) TSconnect("MySQL", dbname = "test") else TSconnect("MySQL", dbname = "test", username = user, password = passwd, host = host) TSconnect uses dbconnect from the DBI package, but checks that the database has expected tables, and checks for additional features. (It cannot be used before the tables are created, as done in the previous section.) This puts a series called vec on the database and then reads is back > z <- ts(rnorm(10), start = c(1990, 1), frequency = 1) > seriesnames(z) <- "vec" > if (TSexists("vec", con)) TSdelete("vec", con) > TSput(z, con) > z <- TSget("vec", con) If the series is printed it is seen to be a ts time series with some extra attributes. TSput fails if the series already exists on the con, so the above example checks and deletes the series if it already exists. TSreplace does not fail if the series does not yet exist, so examples below use it instead. Several plots below show original data and the data retrieved after it is written to the database. One is added to the original data so that both lines are visible. And now more examples: 2

> z <- ts(matrix(rnorm(20), 10, 2), start = c(1990, 1), frequency = 1) > seriesnames(z) <- c("matc1", "matc2") > TSreplace(z, con) [1] TRUE > TSget("matc1", con) Time Series: Start = 1990 End = 1999 Frequency = 1 1 2 3 4 5 6 7 0.1725396 2.3826461-0.5901714 0.4358222-0.8322126-0.7889940 0.6209193 8 9 10-1.0179090 0.9788937-1.9953646 attr(,"seriesnames") [1] matc1 attr(,"tsrefperiod") attr(,"tsmeta") An object of class "TSmeta" Slot "TSdescription": Slot "TSdoc": Slot "TSlabel": Slot "serids": [1] "matc1" Slot "contype": [1] "TSMySQLConnection" attr(,"package") [1] "TSMySQL" Slot "DateStamp": Slot "dbname": [1] "test" Slot "hasvintages": [1] FALSE 3

Slot "haspanels": [1] FALSE > TSget("matc2", con) Time Series: Start = 1990 End = 1999 Frequency = 1 1 2 3 4 5 6-0.12259586-0.01188149 0.68504092-0.55204329-0.01688320-0.93190808 7 8 9 10-0.13139334 0.61621547 0.96787042-0.92756835 attr(,"seriesnames") [1] matc2 attr(,"tsrefperiod") attr(,"tsmeta") An object of class "TSmeta" Slot "TSdescription": Slot "TSdoc": Slot "TSlabel": Slot "serids": [1] "matc2" Slot "contype": [1] "TSMySQLConnection" attr(,"package") [1] "TSMySQL" Slot "DateStamp": Slot "dbname": [1] "test" Slot "hasvintages": [1] FALSE 4

Slot "haspanels": [1] FALSE > TSget(c("matc1", "matc2"), con) Time Series: Start = 1990 End = 1999 Frequency = 1 matc1 matc2 1990 0.1725396-0.12259586 1991 2.3826461-0.01188149 1992-0.5901714 0.68504092 1993 0.4358222-0.55204329 1994-0.8322126-0.01688320 1995-0.7889940-0.93190808 1996 0.6209193-0.13139334 1997-1.0179090 0.61621547 1998 0.9788937 0.96787042 1999-1.9953646-0.92756835 attr(,"tsrefperiod") NA attr(,"tsmeta") An object of class "TSmeta" Slot "TSdescription": Slot "TSdoc": Slot "TSlabel": Slot "serids": [1] "matc1" "matc2" Slot "contype": [1] "TSMySQLConnection" attr(,"package") [1] "TSMySQL" Slot "DateStamp": Slot "dbname": [1] "test" 5

Slot "hasvintages": [1] FALSE Slot "haspanels": [1] FALSE > tfplot(z + 1, TSget(c("matc1", "matc2"), con), lty = c("solid", "dashed"), col = c("black", "red")) matc1 0.5 1.0 1990 1992 1994 1996 1998 matc2 2 0 1 2 1990 1992 1994 1996 1998 > z <- ts(matrix(rnorm(20), 10, 2), start = c(1990, 1), frequency = 4) > seriesnames(z) <- c("matc1", "matc2") > TSreplace(z, con) [1] TRUE > TSget(c("matc1", "matc2"), con) matc1 matc2 1990 Q1-0.89786217-0.4774821 1990 Q2-0.61361927 1.2965363 1990 Q3-0.50145648 0.8126968 1990 Q4-0.02688195 1.2376217 6

1991 Q1 1.45516215-0.5137718 1991 Q2 0.68594976-1.7003727 1991 Q3-1.44348840-0.6430569 1991 Q4-0.59398575 1.4088410 1992 Q1 1.01629681-0.3762690 1992 Q2 1.15631994 0.6770099 attr(,"tsrefperiod") NA attr(,"tsmeta") An object of class "TSmeta" Slot "TSdescription": Slot "TSdoc": Slot "TSlabel": Slot "serids": [1] "matc1" "matc2" Slot "contype": [1] "TSMySQLConnection" attr(,"package") [1] "TSMySQL" Slot "DateStamp": Slot "dbname": [1] "test" Slot "hasvintages": [1] FALSE Slot "haspanels": [1] FALSE > tfplot(z + 1, TSget(c("matc1", "matc2"), con), lty = c("solid", "dashed"), col = c("black", "red")) 7

matc1 1 1 2 1990.0 1990.5 1991.0 1991.5 1992.0 matc2 2 0 2 1990.0 1990.5 1991.0 1991.5 1992.0 > z <- ts(matrix(rnorm(200), 100, 2), start = c(1995, 1), frequency = 12) > seriesnames(z) <- c("mat2c1", "mat2c2") > TSreplace(z, con) [1] TRUE > tfplot(z + 1, TSget(c("mat2c1", "mat2c2"), con), lty = c("solid", "dashed"), col = c("black", "red")) 8

mat2c1 2 0 2 4 1996 1998 2000 2002 mat2c2 3 0 2 4 1996 1998 2000 2002 The following extract information about the series from the database, although not much information has been added for these examples. > TSmeta("mat2c1", con) > TSmeta("vec", con) > TSdates("vec", con) > TSdescription("vec", con) > TSdoc("vec", con) Below are exampoles that make more use of TSdescription and codetsdoc. Often it is convenient to set the default connection: > options(tsconnection = con) and then the con specification can be omitted from the function calls unless another connection is needed. The con can still be specified, and some examples below do specify it, just to illustrate the alternative syntax. > z <- TSget("mat2c1") > TSmeta("mat2c1") An object of class "TSmeta" Slot "TSdescription": [1] "NA" 9

Slot "TSdoc": [1] "NA" Slot "TSlabel": Slot "serids": [1] "mat2c1" Slot "contype": [1] "TSMySQLConnection" attr(,"package") [1] "TSMySQL" Slot "DateStamp": Slot "dbname": [1] "test" Slot "hasvintages": [1] FALSE Slot "haspanels": [1] FALSE Data documentation can be in two forms, a description specified by TSdescription or longer documentation specified by TSdoc. These can be added to the time series object, in which case they will be written to the database when TSput or TSreplace is used to put the series on the database. Alternatively, they can be specified as arguments to TSput or TSreplace. The description or documentation will be retrieved as part of the series object with TSget only if this is specified with the logical arguments TSdescription and TSdoc. They can also be retrieved directly from the database with the functions TSdescription and TSdoc. > z <- ts(matrix(rnorm(10), 10, 1), start = c(1990, 1), frequency = 1) > TSreplace(z, serids = "Series1", con) [1] TRUE > zz <- TSget("Series1", con) > TSreplace(z, serids = "Series1", con, TSdescription = "short rnorm series", TSdoc = "Series created as an example in the vignette.") [1] TRUE 10

> zz <- TSget("Series1", con, TSdescription = TRUE, TSdoc = TRUE) > start(zz) [1] 1990 1 > end(zz) [1] 1999 1 > TSdescription(zz) [1] "short rnorm series" > TSdoc(zz) [1] "Series created as an example in the vignette." > TSdescription("Series1", con) [1] "short rnorm series" > TSdoc("Series1", con) [1] "Series created as an example in the vignette." > z <- ts(rnorm(10), start = c(1990, 1), frequency = 1) > seriesnames(z) <- "vec" > TSreplace(z, con) [1] TRUE > zz <- TSget("vec", con) > z <- ts(matrix(rnorm(20), 10, 2), start = c(1990, 1), frequency = 1) > seriesnames(z) <- c("matc1", "matc2") > TSreplace(z, con) [1] TRUE > tfplot(z + 1, TSget(c("matc1", "matc2"), con), lty = c("solid", "dashed"), col = c("black", "red")) 11

matc1 1 1 2 1990 1992 1994 1996 1998 matc2 2 0 1 2 1990 1992 1994 1996 1998 > z <- ts(matrix(rnorm(20), 10, 2), start = c(1990, 1), frequency = 4) > seriesnames(z) <- c("matc1", "matc2") > TSreplace(z, con) [1] TRUE > tfplot(z + 1, TSget(c("matc1", "matc2"), con), lty = c("solid", "dashed"), col = c("black", "red")) 12

matc1 1 1 2 1990.0 1990.5 1991.0 1991.5 1992.0 matc2 1 1 3 1990.0 1990.5 1991.0 1991.5 1992.0 > z <- ts(matrix(rnorm(200), 100, 2), start = c(1995, 1), frequency = 12) > seriesnames(z) <- c("mat2c1", "mat2c2") > TSreplace(z, con) [1] TRUE > tfplot(z + 1, TSget(c("mat2c1", "mat2c2"), con), lty = c("solid", "dashed"), col = c("black", "red")) 13

mat2c1 2 0 2 4 1996 1998 2000 2002 mat2c2 2 0 2 1996 1998 2000 2002 The following examples use dates and times which are not handled by ts, so the zoo time representation is used. > require("zoo") > z <- zoo(matrix(rnorm(200), 100, 2), as.date("1990-01-01") + 0:99) > seriesnames(z) <- c("zooc1", "zooc2") > TSreplace(z, con, Table = "D") [1] TRUE > tfplot(z + 1, TSget(c("zooc1", "zooc2"), con), lty = c("solid", "dashed"), col = c("black", "red")) 14

zooc1 2 0 2 Jan Mar zooc2 2 0 2 Jan Mar > z <- zoo(matrix(rnorm(200), 100, 2), as.date("1990-01-01") + 0:99 * 7) > seriesnames(z) <- c("zoowc1", "zoowc2") > TSreplace(z, con, Table = "W") [1] TRUE > tfplot(z + 1, TSget(c("zooWc1", "zoowc2"), con), col = c("black", "red"), lty = c("dashed", "solid")) 15

zoowc1 2 0 2 1990 1991 zoowc2 2 0 2 4 1990 1991 > dbdisconnect(con) 3 Examples Using Web Data This section illustrates fetching data from a web server and loading it into the database. This would be a very slow way to load a database, but provides examples of different kinds of time series data. The fetching is done with TShistQuote which provides a wrapper for get.hist.quote from package tseries to give syntax consistent with the TSdbi. Fetching data may fail due to lack of an Interenet connection or delays. First establish a connection to the database where data will be saved: > con <- if ("" == user) TSconnect("MySQL", dbname = "test") else TSconnect("MySQL", dbname = "test", username = user, password = passwd, host = host) Now connect to the web server and fetch data: > require("tshistquote") > Yahoo <- TSconnect("histQuote", dbname = "yahoo") > x <- TSget("^gspc", quote = "Close", con = Yahoo) > plot(x) > tfplot(x) > TSrefperiod(x) 16

[1] "Close" > TSdescription(x) [1] "^gspc Close from yahoo" > TSdoc(x) [1] "^gspc Close from yahoo retrieved 2011-03-14 17:01:20" > TSlabel(x) [1] "^gspc Close" Then write the data to the local server, specifying table B for business day data (using TSreplace in case the series is already there from running this example previously): > TSreplace(x, serids = "gspc", Table = "B", con = con) [1] TRUE and check the saved version: > TSrefperiod(TSget(serIDs = "gspc", con = con)) [1] "Close" > TSdescription("gspc", con = con) [1] "^gspc Close from yahoo" > TSdoc("gspc", con = con) [1] "^gspc Close from yahoo retrieved 2011-03-14 17:01:20" > TSlabel("gspc", con = con) > tfplot(tsget(serids = "gspc", con = con)) 17

gspc 400 600 800 1000 1200 1400 1600 1995 2000 2005 2010 > x <- TSget("ibm", quote = c("close", "Vol"), con = Yahoo) > TSreplace(x, serids = c("ibm.cl", "ibm.vol"), con = con, Table = "B", TSdescription. = c("ibm Close", "IBM Volume"), TSdoc. = paste(c("ibm Close retriev "IBM Volume retrieved on "), Sys.Date())) [1] TRUE > z <- TSget(serIDs = c("ibm.cl", "ibm.vol"), TSdescription = TRUE, TSdoc = TRUE, con = con) > TSdescription(z) [1] "IBM Close" "IBM Volume" > TSdoc(z) [1] "IBM Close retrieved on 2011-03-14" [2] "IBM Volume retrieved on 2011-03-14" > tfplot(z, xlab = TSdoc(z), Title = TSdescription(z)) > tfplot(z, Title = "IBM", start = "2007-01-01") 18

IBM Close IBM Volume ibm.cl 50 150 250 1995 2000 2005 2010 IBM Close retrieved on 2011 03 14 ibm.vol 0e+00 4e+07 1995 2000 2005 2010 IBM Volume retrieved on 2011 03 14 Oanda has maximum of 500 days, so the start date is specified here so as to not exceed that. > Oanda <- TSconnect("histQuote", dbname = "oanda") > x <- TSget("EUR/USD", start = Sys.Date() - 495, con = Oanda) > TSreplace(x, serids = "EUR/USD", Table = "D", con = con) [1] TRUE Then check the saved version: > z <- TSget(serIDs = "EUR/USD", TSlabel = TRUE, TSdescription = TRUE, con = con) > tfplot(z, Title = TSdescription(z), ylab = TSlabel(z)) > tfplot(z, Title = "EUR/USD", start = "2007-01-01") > tfplot(z, Title = "EUR/USD", start = "2007-03-01") > tfplot(z, Title = "EUR/USD", start = Sys.Date() - 14, end = Sys.Date(), xlab = format(sys.date(), "%Y")) 19

EUR/USD Close from oanda 1.20 1.25 1.30 1.35 1.40 1.45 1.50 2010 2011 > dbdisconnect(con) > dbdisconnect(yahoo) > dbdisconnect(oanda) 3.1 Examples Using TSdbi with ets These examples use a database called ets which is available at the Bank of Canada. This set of examples illustrates how the programs might be used if a larger database is available. Typically a large database would be installed using database scripts directly rather than from R with TSput or TSreplace. The following are wrapped in if (!inherits(conets, try-error )) so that the vignette will build even when the database is not available. This seems to require an explicit call to print(), but that is not usually needed to display results below. Another artifact of this is that results printed in the if block do not display until the end of the block. > m <- dbdriver("mysql") > conets <- try(if ("" == user) TSconnect(m, dbname = "ets") else TSconnect(m, dbname = "ets", username = user, password = passwd, host = host)) > if (!inherits(conets, "try-error")) { options(tsconnection = conets) print(tsmeta("m.sdr.ccusma02.st")) 20

} EXCH.IDs <- t(matrix(c("m.sdr.ccusma02.st", "SDR/USD exchange rate", "M.CAN.CCUSMA02.ST", "CAN/USD exchange rate", "M.MEX.CCUSMA02.ST", "MEX/USD exchange rate", "M.JPN.CCUSMA02.ST", "JPN/USD exchange rate", "M.EMU.CCUSMA02.ST", "Euro/USD exchange rate", "M.OTO.CCUSMA02.ST", "OECD /USD exchange rate", "M.G7M.CCUSMA02.ST", "G7 /USD exchange rate", "M.E15.CCUSMA02.ST", "Euro 15. /USD exchange rate"), 2, 8)) print(tsdates(exch.ids[, 1])) z <- TSdates(EXCH.IDs[, 1]) print(start(z)) print(end(z)) tfplot(tsget(serids = "V122646", conets)) An object of class "TSmeta" Slot "TSdescription": [1] "Special Drawing Right---Currency Conversions/US$ exchange rate/average of daily rates/n Slot "TSdoc": [1] "Special Drawing Right---Currency Conversions/US$ exchange rate/average of daily rates/n Slot "TSlabel": Slot "serids": [1] "M.SDR.CCUSMA02.ST" Slot "contype": [1] "TSMySQLConnection" attr(,"package") [1] "TSMySQL" Slot "DateStamp": Slot "dbname": [1] "ets" Slot "hasvintages": [1] FALSE Slot "haspanels": [1] FALSE [,1] [1,] "M.SDR.CCUSMA02.ST from 1960 1 to 2009 2 M NA " 21

[2,] "M.CAN.CCUSMA02.ST from 1960 1 to 2009 2 M NA " [3,] "M.MEX.CCUSMA02.ST from 1963 1 to 2009 2 M NA " [4,] "M.JPN.CCUSMA02.ST from 1960 1 to 2009 2 M NA " [5,] "M.EMU.CCUSMA02.ST from 1979 1 to 2009 2 M NA " [6,] "M.OTO.CCUSMA02.ST not available" [7,] "M.G7M.CCUSMA02.ST not available" [8,] "M.E15.CCUSMA02.ST not available" [[1]] [1] 1960 1 [[2]] [1] 1960 1 [[3]] [1] 1963 1 [[4]] [1] 1960 1 [[5]] [1] 1979 1 [[6]] [[7]] [[8]] [[1]] [1] 2009 2 [[2]] [1] 2009 2 [[3]] [1] 2009 2 [[4]] [1] 2009 2 [[5]] [1] 2009 2 22

[[6]] [[7]] [[8]] V122646 0e+00 1e+05 2e+05 3e+05 4e+05 1970 1980 1990 2000 2010 > if (!inherits(conets, "try-error")) { print(tsdescription(tsget("v122646", TSdescription = TRUE))) print(tsdescription("v122646")) print(tsdoc(tsget("v122646", TSdoc = TRUE))) print(tsdoc("v122646")) tfplot(tsget("v122646", names = "V122646", conets)) } [1] "Total short-term business credit, Seasonally adjusted, average of month-end" [1] "Total short-term business credit, Seasonally adjusted, average of month-end" [1] "Same as B171" [1] "Same as B171" 23

V122646 0e+00 1e+05 2e+05 3e+05 4e+05 1970 1980 1990 2000 2010 > if (!inherits(conets, "try-error")) { z <- TSget("V122646", TSdescription = TRUE) tfplot(z, Title = strsplit(tsdescription(z), ",")) } 24

V122646 0e+00 1e+05 2e+05 3e+05 4e+05 Total short term business credit Seasonally adjusted average of month end 1970 1980 1990 2000 2010 > if (!inherits(conets, "try-error")) { z <- TSget("SDSP500", TSdescription = TRUE) tfplot(z, Title = TSdescription(z)) plot(z) } 25

S&P/TSX Volatility SDSP500 0 20 40 60 80 1980 1990 2000 2010 > if (!inherits(conets, "try-error")) { z <- TSget(c("DSP500", "SDSP500"), TSdescription = TRUE) tfplot(z, xlab = TSdescription(z)) } 26

DSP500 0.05 0.05 1980 1990 2000 2010 SDSP500 0 40 80 1980 1990 2000 2010 S&P/TSX Volatility > if (!inherits(conets, "try-error")) { plot(z) } 27

z SDSP500 0 20 40 60 80 DSP500 0.05 0.00 0.05 1980 1990 2000 2010 Index > if (!inherits(conets, "try-error")) { ETSgdp <- annualizedgrowth(aggregate(tsget("v1992067"), nfrequency = 4, FUN = mean), lag = 4, names = "GDP Y/Y Growth") tfplot(etsgdp) } 28

GDP Y/Y Growth 4 2 0 2 4 6 8 1970 1980 1990 2000 2010 > if (!inherits(conets, "try-error")) { dbdisconnect(options()$tsconnection) options(tsconnection = NULL) } 4 Examples Using DBI and direct SQL Queries The following examples are queries using the underlying DBI functions. They should not often be needed to access time series, but may be useful to get at more detailed information, or formulate special queries. > m <- dbdriver("mysql") > con <- if ("" == user) TSconnect(m, dbname = "test") else TSconnect(m, dbname = "test", username = user, password = passwd, host = host) > options(tsconnection = con) > dblisttables(con) [1] "A" "B" "D" "I" "M" "Meta" "Q" "S" "T" "U" [11] "W" This is Mysql specific. Below is a generic sql way to do this. 29

> dbgetquery(con, "show tables;") Tables_in_test 1 A 2 B 3 D 4 I 5 M 6 Meta 7 Q 8 S 9 T 10 U 11 W > dbgetquery(con, "describe A;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 year int(11) YES MUL <NA> 3 v double YES <NA> > dbgetquery(con, "describe B;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 date date YES MUL <NA> 3 period int(11) YES MUL <NA> 4 v double YES <NA> > dbgetquery(con, "describe D;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 date date YES MUL <NA> 3 period int(11) YES MUL <NA> 4 v double YES <NA> > dbgetquery(con, "describe M;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 year int(11) YES MUL <NA> 3 period int(11) YES MUL <NA> 4 v double YES <NA> > dbgetquery(con, "describe Meta;") 30

Field Type Null Key Default Extra 1 id varchar(40) NO PRI <NA> 2 tbl char(1) YES MUL <NA> 3 refperiod varchar(10) YES <NA> 4 description text YES <NA> 5 documentation text YES <NA> > dbgetquery(con, "describe U;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 date timestamp NO MUL CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP 3 tz varchar(4) YES <NA> 4 period int(11) YES MUL <NA> 5 v double YES <NA> > dbgetquery(con, "describe Q;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 year int(11) YES MUL <NA> 3 period int(11) YES MUL <NA> 4 v double YES <NA> > dbgetquery(con, "describe S;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 year int(11) YES MUL <NA> 3 period int(11) YES MUL <NA> 4 v double YES <NA> > dbgetquery(con, "describe W;") Field Type Null Key Default Extra 1 id varchar(40) YES MUL <NA> 2 date date YES MUL <NA> 3 period int(11) YES MUL <NA> 4 v double YES <NA> If schema queries are supported then the above can be done in a generic SQL way, but on some systems this will fail because users do not have read priveleges on the INFORMATION SCHEMA table, so the following are wrapped in try(). (SQLite does not seem to support this at all.) > z <- try(dbgetquery(con, paste("select COLUMN_NAME FROM INFORMATION_SCHEMA.Columns ", " WHERE TABLE_SCHEMA='test' AND table_name='a' ;"))) > if (!inherits(z, "try-error")) print(z) 31

COLUMN_NAME 1 id 2 year 3 v > z <- try(dbgetquery(con, paste("select COLUMN_NAME, COLUMN_DEFAULT, COLLATION_NAME, DATA_T "CHARACTER_SET_NAME, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION", "FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA='test' AND table_name='a' ;"))) > if (!inherits(z, "try-error")) print(z) COLUMN_NAME COLUMN_DEFAULT COLLATION_NAME DATA_TYPE CHARACTER_SET_NAME 1 id <NA> latin1_swedish_ci varchar latin1 2 year <NA> <NA> int <NA> 3 v <NA> <NA> double <NA> CHARACTER_MAXIMUM_LENGTH NUMERIC_PRECISION 1 40 NA 2 NA 10 3 NA 22 > z <- try(dbgetquery(con, paste("select COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, N "FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA='test' AND table_name='m';"))) > if (!inherits(z, "try-error")) print(z) COLUMN_NAME DATA_TYPE CHARACTER_MAXIMUM_LENGTH NUMERIC_PRECISION 1 id varchar 40 NA 2 year int NA 10 3 period int NA 10 4 v double NA 22 Finally, to disconnect gracefully, one should > dbdisconnect(con) > dbdisconnect(options()$tsconnection) > options(tsconnection = NULL) 32