簡體   English   中英

R中的Quantmod FRED元數據

[英]Quantmod FRED Metadata in R

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)

您可以使用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)

您可以使用grep搜索具有數據頭部的行,而使用子集僅包含之前的行,而不是使用nrows=17進行硬編碼。

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.

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))
}

檢索實際時間序列數據將以類似的方式完成。 有幾個json包可供R使用,但jsonlite特別適合這個應用程序。 設置它比上一個答案要多一些,但如果你對FRED數據做了很多的話,也許值得。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM