2014-07-27 2 views
3
library(quantmod) 

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

FRED에서 수치 경제 재무 데이터뿐만 아니라 메타 데이터도 추출하려고합니다. CPI를 차트로 작성하고 메타 데이터를 레이블/각주로 사용하려고합니다. quantmod 패키지를 사용하여이 데이터를 추출 할 수있는 방법이 있습니까?Quantmod FRED 메타 데이터 in R

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) 
+0

단어 : 아니요. 'getSymbols.FRED'는 메타 데이터가 포함 된 파일을 다운로드하지 않습니다. –

답변

5

당신은 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. 
4

FRED는 경제 시리즈의 모든 모두 메타 데이터 및 시계열 데이터를 제공하는 간단한, 잘 문서 JSON 인터페이스 http://api.stlouisfed.org/docs/fred/ 있습니다. 액세스하려면 FRED 계정과 API 키가 필요하지만이 요청은 http://api.stlouisfed.org/api_key.html에서 가능합니다.
당신이 실제 시계열 데이터를 가져 오는

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

사용하여 검색 할 수 있습니다 요청 엑셀 설명 데이터는 비슷한 방법으로 수행 할 수있다. R에 사용할 수있는 몇 가지 json 패키지가 있지만 jsonlite는이 애플리케이션에서 특히 잘 작동합니다. FRED 데이터를 많이 사용하면 이전 답변보다 설정하는 것이 더 있지만 가치가있을 것입니다.