-
Notifications
You must be signed in to change notification settings - Fork 2
xtdata specification
This draft is about the xtdata pseudo-class that is defined within the qogdata package. The pseudo-class is defined by an attribute carried by data frames. If that convention proves useful through enough methods, it could be turned into a genuine class.
The current xtdata specification is an object attribute that has been tested on data frames and might also work on data tables and objects of other classes. It aims at providing the user with some of the characteristics of the xt Stata commands.
The xtdata specification should serve users with panel data, typically cross-sectional time series of unique (un)balanced country-year observations. More complex time series are better handled by the zoo or xts packages, and other options exist for survey data.
At this early stage of development, xtdata is coded as a data frame attribute, not as a proper S3 or S4 class. Please feel free to suggest making it so in the Issues section, and I'' consider it the main goal of version 1.0 before submitting it to CRAN.
The xtdata attribute passed to data frames in the qogdata package contains a list of panel data attributes described in the documentation of the xtset function (required parameters are in bold):
-
data, a character vector of identifier variable names, in the following order:- unique identifier
- time period identifier
- alternative identifier (short)
- alternative identifier (long)
- any other identifier
- ...
-
spec, a character vector that matches variable names with their formats- format of unique identifier
- format of time period
- format of alternative identifier (short)
- format of alternative identifier (long)
- any other format for other identifier
- ...
-
type, a character vector that defines the type of panel data- primary type of panel data
- secondary type of panel data
- ...
-
name, a character vector that defines a short description of the panel data- primary name
- secondary name
- ...
-
url, a character vector that defines a URI locator (a website address) to the panel- primary URI
- secondary URI
- ...
The specification is internally set with xtset, accessed with xt and tested with xtdata (returns TRUE if required elements exist). The name and url are data cosmetics. To check for the presence of xtdata attributes, use !is.null(xt(data)$type).
In the qogdata package, this specification is used on QOG datasets to declare the type of data as country and maps the ccode, ccodealp and cname variables to their countrycode formats, iso3n, iso3c and country.name. It also cites the data source in name and url.
Similar specifications are passed to all datasets downloaded by get methods (currently: get_wdi, get_uds and get_gwpt). This allows to pass any of them to xtmerge to perform a safe merge of country-year observations. Any other dataset can carry an xtdata attribute.
As an example of how that specification can further be put to use, the xtmerge function calls the xtcountry function to use countrycode against unique and alternative identifiers of a data set, in order to find the largest number of ISO-3N country code matches in a country-level dataset.
The xtsubset and xtsample functions subset or sample from a data frame while preserving its xtdata attribute. The xtdes function returns its characteristics and T distribution.
The time period stored in xt(data)$data[2] is currently used only to check that both datasets passed to xtmerge have identical time formats in xt(data)$spec[2], but only because testing the package with country-year observations provided little reason to use it any other way.
The xtdata specification should be extensible enough to deal with, for instance, quarterly data for FIPS units, or time series of NUTS-2 regional data. Feel free to declare one or more types of xtdata for a dataset: country and its helper functions for countrycode are only one example of what should be doable.
The next extension will use region and nuts attributes with the xtregion function to prepare data out of the QOG EU Regions and Eurostat datasets, which sometimes include both country and region data and use other time periods than year (most often quarter).
-
xtmissing,xtlineandxtmapare companion plot functions:-
xtmissingplots panel data missing values -
xtmapplots one or more years of country data on world maps (see below) -
xtplotplots one or more years of data in other ways
-
The xtmap function uses the t argument to apply panel data transformations:
- if
tisNULL, no time period is used - if
tis equal to single number like1945, a single time period is used - if
tisNULLandxt(data$)spec[2]exists, the maximum time period is used - if
tis a range like1945:1950, a time period method is used
The xtplot method supports similar transformations:
- if
tisNULL, dot or bar plot of values - if
tis1945, dot or bar plot of values for period - if
tisNULLandxt(data$)spec[2]exists, the entire time range is used - if
tis1945:1955, plot line/point/step/area/smooth/bar geoms for period
DRAFT NOTES:
- code
tforxtmap:-
min,max,mean,median,mode,sd,range,std,std01 -
quant.cut(externalize to utils and compare to questionr:quant.cut) -
diff(average change),percent-diff(average rate of change)
-
- code
tforxtplot: