R中的Quantmod FRED元数据

jes*_*ica 4 r quantmod

library(quantmod)

getSymbols("GDPC1",src = "FRED")
Run Code Online (Sandbox Code Playgroud)

我试图提取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)
Run Code Online (Sandbox Code Playgroud)

GSe*_*See 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)
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)


Wal*_*ltS 5

FRED有一个简单,文档齐全的json界面http://api.stlouisfed.org/docs/fred/,它提供了所有经济系列的元数据和时间序列数据.Access需要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))
}
Run Code Online (Sandbox Code Playgroud)

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