R中的Quantmod FRED元数据

时间:2014-07-27 20:19:36

标签: r quantmod

library(quantmod)

getSymbols("GDPC1",src = "FRED")

我正在尝试提取FRED中的数字经济/财务数据,但也提取元数据。我试图绘制CPI并将元数据作为标签/脚注。有没有办法使用quantmod包提取这些数据?

Title:               Real Gross Domestic Product
Series ID:           GDPC1
Source:              U.S. Department of Commerce: Bureau of Economic Analysis
Release:             Gross Domestic Product
Seasonal Adjustment: Seasonally Adjusted Annual Rate
Frequency:           Quarterly
Units:               Billions of Chained 2009 Dollars
Date Range:          1947-01-01 to 2014-01-01
Last Updated:        2014-06-25 7:51 AM CDT
Notes:               BEA Account Code: A191RX1

                     Real gross domestic product is the inflation adjusted value of the
                     goods and services produced by labor and property located in the
                     United States. 

                     For more information see the Guide to the National Income and Product
                     Accounts of the United States (NIPA) -
                     (http://www.bea.gov/national/pdf/nipaguid.pdf)

2 个答案:

答案 0 :(得分:6)

您可以使用getSymbools.FRED正文中的相同代码,但更改" .csv"到" .xls",然后从.xls文件中读取您感兴趣的元数据。

library(gdata)

Symbol <- "GDPC1"
FRED.URL <- "http://research.stlouisfed.org/fred2/series"

tmp <- tempfile()
download.file(paste0(FRED.URL, "/", Symbol, "/downloaddata/", Symbol, ".xls"),
              destfile=tmp)
read.xls(tmp, nrows=17, header=FALSE)
#                      V1                                                                    V2
# 1                Title:                                           Real Gross Domestic Product
# 2            Series ID:                                                                 GDPC1
# 3               Source:              U.S. Department of Commerce: Bureau of Economic Analysis
# 4              Release:                                                Gross Domestic Product
# 5  Seasonal Adjustment:                                       Seasonally Adjusted Annual Rate
# 6            Frequency:                                                             Quarterly
# 7                Units:                                      Billions of Chained 2009 Dollars
# 8           Date Range:                                              1947-01-01 to 2014-01-01
# 9         Last Updated:                                                2014-06-25 7:51 AM CDT
# 10               Notes:                                             BEA Account Code: A191RX1
# 11                         Real gross domestic product is the inflation adjusted value of the
# 12                           goods and services produced by labor and property located in the
# 13                                                                            United States. 
# 14                                                                                           
# 15                      For more information see the Guide to the National Income and Product
# 16                                                     Accounts of the United States (NIPA) -
# 17                                             (http://www.bea.gov/national/pdf/nipaguid.pdf)

您可以使用nrows=17来搜索包含数据标题的行,而不是硬编码grep,而只能在此之前包含行。

dat <- read.xls(tmp, header=FALSE, stringsAsFactors=FALSE)
dat[seq_len(grep("DATE", dat[, 1])-1),]

unlink(tmp)  # remove the temp file when you're done with it.

答案 1 :(得分:5)

FRED有一个简单,文档齐全的json接口http://api.stlouisfed.org/docs/fred/,它提供了所有经济系列的元数据和时间序列数据。访问权限需要FRED帐户和API密钥,但这些可以从http://api.stlouisfed.org/api_key.html请求提供 您可以使用

检索您要求的excel描述性数据
get.FRSeriesTags <- function(seriesNam)
{
#     seriesNam = character string containing the ID identifying the FRED series to be retrieved    
#
library("httr")
library("jsonlite")
# dummy FRED api key; request valid key from http://api.stlouisfed.org/api_key.html
apiKey <- "&api_key=abcdefghijklmnopqrstuvwxyz123456"      
base  <- "http://api.stlouisfed.org/fred/"
seriesID <- paste("series_id=", seriesNam,sep="")
fileType <- "&file_type=json"
# 
# get series descriptive data
#
datType <- "series?"
url <- paste(base, datType, seriesID, apiKey, fileType, sep="")
series <- fromJSON(url)$seriess
# 
# get series tag data
#
datType <- "series/tags?"
url <- paste(base, datType, seriesID, apiKey, fileType, sep="")
tags <- fromJSON(url)$tags
#
# format as excel descriptive rows
#
description <- data.frame(Title=series$title[1], 
                      Series_ID = series$id[1], 
                      Source = tags$notes[tags$group_id=="src"][1],
                      Release = tags$notes[tags$group_id=="gen"][1],
                      Frequency = series$frequency[1],
                      Units = series$units[1],
                      Date_Range = paste(series[1, c("observation_start","observation_end")], collapse=" to "),
                      Last_Updated = series$last_updated[1],
                      Notes = series$notes[1],
                      row.names=series$id[1])
return(t(description))
}

检索实际的时间序列数据将以类似的方式完成。有几个json包可供R使用,但jsonlite特别适合这个应用程序。 设置这个比上一个答案要多一些,但如果你对FRED数据做了很多的话,也许值得。