Haver_advanced {Haver} | R Documentation

## Advanced Features and Usage of the Haver Package

### Description

This help page provides a realistic and complete work-through example of a real-world data query task, touching upon some advanced concepts of usage. In particular, the following topics are illustrated:

- finding appropriate time series

- renaming of series codes

- using `haver.metadata` to determine necessary series transformations

- conversion to non-Haver objects for data analysis

### An Example Query Task

Let's assume that our task is to perform analysis using the following series:

- US real GDP

- a US interest rate

- real US exports and real US imports of goods and services

- US exchange rates with Canada, the EU, Japan, Switzerland, and the UK

We would like to have quarterly data back to 1970.

In the ‘Examples’ section below, we will use the Haver R package to search Haver databases for series. Please do not forget that Haver's DLXVG3 data browser has powerful search capabilities. However, once you are familiar with the workings of the Haver package, its functions may serve as a complement to the tools in DLXVG3 for searching for series.

### Author(s)

Haver Analytics <tech@haver.com>

Maintainer: Haver Analytics <tech@haver.com>

### See Also

See Haver_package for all help entries related to the Haver package.

### Examples

    haver.path()
    haver.path("examples")

    ## In this example section, we focus on the database 'HAVERMQA'.
    hmd_mqa <- haver.metadata(dat="havermqa")

    ## We have the presumption that our databases include series on US GDP whose
    ## codes contain the string 'gdp'. We look for regular expression matches
    ## in the 'code' field of the metadata:
    hmd_mqa[grep(".*gdp.*", hmd_mqa[["code"]]), ]

    ## We can see that US real GDP has series code 'gdph'.
    ## In order to find interest rates and exports series we apply regular
    ## expressions again, this time to the 'descriptor' metadata field:
    hmd_mqa[ grep(".*Treasury.*", hmd_mqa[["descriptor"]]), ]
    hmd_mqa[ grep(".*Export.*"  , hmd_mqa[["descriptor"]]), ]

    ## To view the full descriptors, you can use 'View'. Here we use 'print':
    print( hmd_mqa[ grep(".*Treasury.*", hmd_mqa[["descriptor"]]), ]
           , fields=c("code", "descriptor"), desc.len=80)
    print( hmd_mqa[ grep(".*Export.*"  , hmd_mqa[["descriptor"]]), ]
           , fields=c("code", "descriptor"), desc.len=80)

    ## We opt for series 'fcm1' and 'xh'. We briefly confirm that the
    ## imports series has an analogous code to the exports series:
    hmd_mqa[hmd_mqa[["code"]]=="mh"]

    ## For exchange rates, we again apply prior knowledge that 'HAVERMQA'
    ## contains many exchange rates series whose codes start with 'fx...':
    hmd_mqa[grep(".*fx.*", hmd_mqa[["code"]]), ]

    ## We pick the series 'fxcan', 'fxeur', 'fxjap', 'fxsw', 'fxuk'

    ## We have now determined the 9 series codes for our analysis:
    ## 'gdph', 'fcm1', 'xh', 'mh', 'fxcan', 'fxeur', 'fxjap', 'fxsw', 'fxuk'
    ## so we can retrieve the data:
    ex_codes <- c("gdph", "fcm1", "xh", "mh", "fxcan",
                  "fxeur", "fxjap", "fxsw", "fxuk")
    hd_adv  <- haver.data(codes=ex_codes, database="havermqa")
    hmd_adv <- haver.datamd(hd_adv)
    print(hmd_adv, fields=c("code", "startdate", "descriptor"), desc.length=NA)

    ## We note that series fxeur starts only in 1999 but keep it in the data set anyway.
    ## From the labels, we can see that the national accounts series do have the same
    ## magnitude (billions of chained dollars). If this had not been the case, we would
    ## have had to harmonize the magnitudes manually.
    ## We also see that exchange rates are not quoted uniformly. We opt to express
    ## all exchange rates in units of foreign currency per US dollar. We note that
    ## later we will have to invert series 'fxeur' and 'fxuk'.
    ## We use the metadata to confirm our conclusions:
    print(hmd_adv, fields=c("code", "magnitude", "datatype"))

    ## As another preliminary QA check, we plot the raw data
    ## using a coercion function:
    plot(as.ts(hd_adv))

    ## At this point we would like to assign series aliases that are more
    ## convenient to work with, possibly because these are standard codes in our
    ## own work flow or in the institution we work for:
    ex_codes   <- c("gdph", "fcm1", "xh", "mh")
    ex_aliases <- c("yr"  , "i"   , "xr", "mr")
    hd_adv     <- haver.setalias(hd_adv, ex_codes, ex_aliases)

    ## You will notice that the series in question have been renamed:
    ## Not run:
    View(hd_adv)

    ## End(Not run)

    ## This information has also been recorded in the metadata:
    hmd_adv <- haver.datamd(hd_adv)
    print(hmd_adv, fields=c("code", "alias", "descriptor"), desc.len=NA)

    ## In order to start data analysis, we convert our HaverData object to an R 'ts'
    ## object. The series aliases that we have assigned will be carried forward.
    ## Our custom series names are the only difference with respect to the
    ## previous plot:
    ex_ts <- as.ts(hd_adv)
    plot(ex_ts)

    ## In the following, we apply a few functions that are outside the Haver package
    ## in order to illustrate that data analysis is done elsewhere.
    ## In particular, we cut off part of the sample, invert some exchange rate series,
    ## generate log-differences and first differences, plot the series,
    ## and look at summary statistics.

    ex_ts                      <- window(ex_ts, start=c(1974,1))
    ex_ts[,c("fxeur", "fxuk")] <- 1 / ex_ts[,c("fxeur", "fxuk")]
    ex_lognames                <- c("yr", "xr", "mr", "fxcan",
                                    "fxeur", "fxjap", "fxsw", "fxuk")
    ex_ts[,ex_lognames]        <- log(ex_ts[,ex_lognames])
    ex_ts                      <- diff(ex_ts)
    plot(ex_ts)
    summary(ex_ts)

    haver.path("restore")

[Package Haver version 3.2 Index]