sboysel / fredr

An R client for the Federal Reserve Economic Data (FRED) API
https://sboysel.github.io/fredr/
Other
92 stars 21 forks source link

way to traverse category trees? #85

Closed thistleknot closed 3 years ago

thistleknot commented 3 years ago

similar to traverseCategories found here?

https://github.com/jcizel/FredR/blob/master/R/FredR.R

I'm having a hard time exploring all the categories. I have to sift through them manually and what I'd like to do is start at a base node up top and sort all the way down by popularity but I need to be able to traverse the tree first.

DavisVaughan commented 3 years ago

That function isn't exported, so im not really sure what it actually does.

fredr is really designed for you to use it in conjunction with the FRED site. For example, they have a category section https://fred.stlouisfed.org/categories/

Clicking on Money and Banking -> Exchange Rates -> Daily Rates, I can see all the daily rate series and can use their drop down to sort by popularity.

At the top in the URL, I can see the category id for Daily Rates is 94, which I can use in more fredr functions if I want.

You can incrementally get your way there with fredr_category_children(), but I still needed to reference the website to figure out the path to get to the Daily Series category.

library(fredr)
#> Warning: package 'fredr' was built under R version 4.0.2

fredr_category_children(0)
#> # A tibble: 8 x 3
#>      id name                                    parent_id
#>   <int> <chr>                                       <int>
#> 1 32991 Money, Banking, & Finance                       0
#> 2    10 Population, Employment, & Labor Markets         0
#> 3 32992 National Accounts                               0
#> 4     1 Production & Business Activity                  0
#> 5 32455 Prices                                          0
#> 6 32263 International Data                              0
#> 7  3008 U.S. Regional Data                              0
#> 8 33060 Academic Data                                   0

fredr_category_children(32991)
#> # A tibble: 7 x 4
#>      id name               parent_id notes                                      
#>   <int> <chr>                  <int> <chr>                                      
#> 1    22 Interest Rates         32991  <NA>                                      
#> 2    15 Exchange Rates         32991  <NA>                                      
#> 3    24 Monetary Data          32991  <NA>                                      
#> 4    46 Financial Indicat…     32991  <NA>                                      
#> 5    23 Banking                32991  <NA>                                      
#> 6 32360 Business Lending       32991  <NA>                                      
#> 7 32145 Foreign Exchange …     32991 "In addition to the listed daily intervent…

fredr_category_children(15)
#> # A tibble: 5 x 4
#>      id name            parent_id notes                                         
#>   <int> <chr>               <int> <chr>                                         
#> 1    94 Daily Rates            15 "Effective January 1, 2009, the Federal Reser…
#> 2    95 Monthly Rates          15  <NA>                                         
#> 3 32219 Annual Rates           15  <NA>                                         
#> 4   105 Trade-Weighted…        15  <NA>                                         
#> 5   158 By Country             15  <NA>

fredr_category_series(94, order_by = "popularity")[c(1, 4, 5, 6)]
#> # A tibble: 35 x 4
#>    id       title                               observation_sta… observation_end
#>    <chr>    <chr>                               <chr>            <chr>          
#>  1 TWEXEME… Trade Weighted U.S. Dollar Index: … 2006-01-01       2019-01-01     
#>  2 TWEXAFE… Trade Weighted U.S. Dollar Index: … 2006-01-01       2019-01-01     
#>  3 TWEXBGS… Trade Weighted U.S. Dollar Index: … 2006-01-01       2019-01-01     
#>  4 DEXSLUS  Sri Lanka / U.S. Foreign Exchange … 1973-01-02       2020-11-25     
#>  5 TWEXEME… Trade Weighted U.S. Dollar Index: … 2006-01-01       2020-10-01     
#>  6 DTWEXO   Trade Weighted U.S. Dollar Index: … 1995-01-04       2019-12-31     
#>  7 DEXDNUS  Denmark / U.S. Foreign Exchange Ra… 1971-01-04       2020-11-25     
#>  8 TWEXAFE… Trade Weighted U.S. Dollar Index: … 2006-01-01       2020-10-01     
#>  9 DEXUSNZ  U.S. / New Zealand Foreign Exchang… 1971-01-04       2020-11-25     
#> 10 DEXVZUS  Venezuela / U.S. Foreign Exchange … 1995-01-02       2020-11-25     
#> # … with 25 more rows

I think it would be rather hard to show all recursive children of a node at once if that is what you are asking for

thistleknot commented 3 years ago

image

thistleknot commented 3 years ago

updated script

library(fredr)
fredr_set_key("getyourown")

recursive.categories <- function(x) {

  #parent <- fredr_category(32266)
  Sys.sleep(.5)
  parent <- fredr_category(x)

  Sys.sleep(.5)
  series <- fredr_category_series(x)

  Sys.sleep(.5)
  children <- fredr_category_children(category_id = parent$id)

  print(parent)
  print(series)

  if (length(children) == 0)  {
    p <- cbind(parent$id,parent$name)

    names(p) <- iconv(paste(parent$id,parent$name), from="UTF-8", to="UTF-8", sub="")
    #names(p) <- paste(parent$id,parent$name)
    #return (p)
    if(nrow(series)!=0)
      {
        #set <- list(parent,series)
        #names(set) <- c(iconv(paste(parent$id,parent$name,"info"), from="UTF-8", to="UTF-8", sub=""),iconv(paste(parent$id,parent$name,"series"), from="UTF-8", to="UTF-8", sub=""))
        set <- list(series)
        names(set) <- c(iconv(paste(parent$id,parent$name,"series"), from="UTF-8", to="UTF-8", sub=""))
      } else 
      {
        #set <- parent
      }

    return(set)
  }

  else  {
    c <- (lapply(children[["id"]], function(y)
    {
      Sys.sleep(.5)
      print(y)
      recursive.categories(y)
    }))
  names(c) <- iconv(paste(children$id,children$name), from="UTF-8", to="UTF-8", sub="")
  #names(c) <- paste(children$id,children$name)

  #set <- list(parent,c,series)
  #set <- list(c,series)

  if(nrow(series)!=0)
  {
    #set <- list(parent,c,series)
    #names(set) <- c(iconv(paste(parent$id,parent$name,"info"), from="UTF-8", to="UTF-8", sub=""),iconv(paste(parent$id,parent$name,"children"), from="UTF-8", to="UTF-8", sub=""),iconv(paste(parent$id,parent$name,"series"), from="UTF-8", to="UTF-8", sub=""))
    set <- list(c,series)
    names(set) <- c(iconv(paste(parent$id,parent$name,"children"), from="UTF-8", to="UTF-8", sub=""),iconv(paste(parent$id,parent$name,"series"), from="UTF-8", to="UTF-8", sub=""))
  } else 
  {
    #set <- list(parent,c)
    #names(set) <- c(iconv(paste(parent$id,parent$name,"info"), from="UTF-8", to="UTF-8", sub=""),iconv(paste(parent$id,parent$name,"children"), from="UTF-8", to="UTF-8", sub=""))
    set <- list(c)
    names(set) <- c(iconv(paste(parent$id,parent$name,"children"), from="UTF-8", to="UTF-8", sub=""))
  }

  return(set)
  #return(c)
  }

}

sets <- recursive.categories(0)
#sets <- recursive.categories(32266)
#sets <- recursive.categories(32998)
View(sets)

store <- as.Node(sets)
ToDataFrameTree(store)
DavisVaughan commented 3 years ago

@thistleknot I'm happy to hear that you found a solution that works for you, however, I think that fredr should probably stick to just the API endpoints without too much additional work on top of it, so we probably won't add this. Thanks for detailing your solution though, in case anyone else needs this!