R Package: EIAdata

I just put my functions to interact with the EIA's API into a R package which I've called 'EIAdata'.  The package is available here: EIAdata_0.1.tar.gz. Once downloaded, you can install it with R CMD INSTALL EIAdata_0.1.tar.gz while in the same directory.

The package contains the functions getCatEIA and getEIA which will get data categories/series IDs, and the time series data for a given series ID, respectively.

FIN 476: Pairs Trade Estimation Period Code (3/25/2014)

I have totally rewritten the pairs trade estimation period code.  It will create a csv file in your working directory which has the average squared deviation, and the standard deviation of the squared deviations, for all pairs.  The csv is sorted from lowest (best pair) to highest (worst pair) average squared deviation.  The code is here estim_period.

Quarterly and Annual R Functions to Pull Time Series from the EIA's API

Below are functions to pull quarterly and annual time series from the EIA's API. The functions return time series of class xts.

You'll need the following libraries loaded (quantmod is just for charting):

library(XML)
library(plyr)
library(xts)
library(quantmod)

Then make sure you have your key:

key <- "[insert your key in quotes]"

If you don't yet have a key, see the earlier post on the function for getting categories.

The function for quarterly data is:

getQEIA <- function(ID, key) {

    ID <- unlist(strsplit(ID, ";"))
    key <- unlist(strsplit(key, ";"))

    url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, 
        "&out=xml", sep = "")

    doc <- xmlParse(file = url, isURL = TRUE)

    df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

    df <- arrange(df, df$date)

    date <- as.yearqtr(df$date)
    values <- as.numeric(levels(df[, -1]))[df[, -1]]

    xts_data <- xts(values, order.by = date)
    names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

    assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data, envir = .GlobalEnv)
}

The function for annual data is:

getAnnEIA <- function(ID, key) {

    ID <- unlist(strsplit(ID, ";"))
    key <- unlist(strsplit(key, ";"))

    url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, 
        "&out=xml", sep = "")

    doc <- xmlParse(file = url, isURL = TRUE)

    df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

    df <- arrange(df, df$date)
    date <- as.Date(paste(as.character(levels(df[, 1]))[df[, 1]], "-12-31", 
        sep = ""), "%Y-%m-%d")
    values <- as.numeric(levels(df[, -1]))[df[, -1]]

    xts_data <- xts(values, order.by = date)
    names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

    assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data, envir = .GlobalEnv)
}

Then, to use the function type:

getQEIA(ID = "ELEC.PLANT.GEN.13-WAT-ALL.Q", key = key)

Which will return an time series object of class xts named ELEC.PLANT.GEN.13.WAT.ALL.Q to the global environment (note the substitution of . for -). This time series is the number of megawatt hours of electricity produced by the Jordan Hydroelectric Dam per quarter. The time series is ready to be used by quantmod:

chartSeries(ELEC.PLANT.GEN.13.WAT.ALL.Q)

plot of chunk unnamed-chunk-6

Similarly for annual data:

getAnnEIA("NG.N3050MA3.A", key = key)

This is the annual natural gas citygate price in Massachusetts.

chartSeries(NG.N3050MA3.A)

plot of chunk unnamed-chunk-8

Note all of these functions pull the full time series history. However, since the resulting time series object is of class xts, it is very easy to return a subset of the time series. For example, if you just want the gas prices from 2005 onward you can use:

NG.N3050MA3.A["2005/"]
##            NG.N3050MA3.A
## 2005-12-31         10.64
## 2006-12-31         11.00
## 2007-12-31          9.34
## 2008-12-31         10.29
## 2009-12-31          8.29
## 2010-12-31          7.74
## 2011-12-31          7.04
## 2012-12-31          6.03

For more information on how to use xts series use ?xts in R.

 

 

R Functions to get Daily, Weekly, or Monthly Time Series from the EIA's API

Below are functions to pull daily, weekly, and monthly data from the EIA. The function returns a time series of class xts. The next step is to roll all of these functions into one getEIA() function which will handle any time series frequency.

You'll need the following libraries loaded (quantmod is just for charting):

library(XML)
library(plyr)
library(xts)
library(quantmod)

Then make sure you have your key:

key <- "[insert your key in quotes]"

If you don't yet have a key, see the earlier post on the function for getting categories.

The function for daily or weekly time series is:

getWDEIA <- function(ID, key) {

    ID <- unlist(strsplit(ID, ";"))
    key <- unlist(strsplit(key, ";"))

    url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, 
        "&out=xml", sep = "")

    doc <- xmlParse(file = url, isURL = TRUE)

    df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

    df <- arrange(df, df$date)

    date <- as.Date(df$date, "%Y%m%d")
    values <- as.numeric(levels(df[, -1]))[df[, -1]]

    xts_data <- xts(values, order.by = date)
    names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

    assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data, envir = .GlobalEnv)
}

The function for monthly data is:

getMonEIA <- function(ID, key) {

    ID <- unlist(strsplit(ID, ";"))
    key <- unlist(strsplit(key, ";"))

    url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, 
        "&out=xml", sep = "")

    doc <- xmlParse(file = url, isURL = TRUE)

    df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

    df <- arrange(df, df$date)

    date <- as.Date(paste(as.character(levels(df[, 1]))[df[, 1]], "01", sep = ""), 
        "%Y%m%d")
    values <- as.numeric(levels(df[, -1]))[df[, -1]]

    xts_data <- xts(values, order.by = date)
    names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

    assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data, envir = .GlobalEnv)
}

Then you can use the function like:

getWDEIA(ID = "NG.RNGC1.W", key = key)

Which will return an time series object of class xts named NG.RNGC1.W to the global environment. The time series is ready to be used by quantmod:

chartSeries(NG.RNGC1.W)

plot of chunk unnamed-chunk-7

Similarly for monthly data:

getMonEIA("ELEC.GEN.ALL-AL-99.M", key = key)

Now note, R will not allow the resulting object to be named ELEC.GEN.ALL-AL-99.M because of the “-” signs, so the function will automatically change the “-” to “.”. The object is thus named ELEC.GEN.ALL.AL.99.M and is assinged to the global environment.

chartSeries(ELEC.GEN.ALL.AL.99.M)

plot of chunk unnamed-chunk-9

I'll post the quarterly and yearly functions soon. Enjoy!