Tag Archives: R

tsbox 0.1: class-agnostic time series

The R ecosystem knows a vast number of time series classes: ts, xts, zoo, tsibble, tibbletime or timeSeries. The plethora of standards causes confusion. As different packages rely on different classes, it is hard to use them in the same analysis. tsbox provides a set of tools that make it easy to switch between these classes. It also allows the user to treat time series as plain data frames, facilitating the use with tools that assume rectangular data.

comic by xkcd

The tsbox package is built around a set of functions that convert time series of different classes to each other. They are frequency-agnostic, and allow the user to combine multiple non-standard and irregular frequencies. Because coercion works reliably, it is easy to write functions that work identically for all classes. So whether we want to smooth, scale, differentiate, chain-link, forecast, regularize or seasonally adjust a time series, we can use the same tsbox-command for any time series class.

This blog gives a short overview of the changes introduced in 0.1. A detailed overview of the package functionality is given in the documentation page (or in a previous blog-post).

Keeping explicit missing values

Version 0.1, now on CRAN, brings a large number of bug fixes and improvements. A substantial change involves the treatment of NA values in data frames. Previously, all NAs in data frames were treated as implicit, and were only made explicit by a call to ts_regular.

This has changed now. If you convert a ts object to a data frame, all NA values will be preserved. To replicate previous behavior, apply the ts_na_omit function:

x.ts <- ts_c(mdeaths, austres)

ts_span extends outside of series span

This lays the groundwork for ts_span to be extensible. With extend = TRUEts_span extends a regular series with NA values, up to the specified limits, similar to base window. Like all functions in tsbox, this is frequency-agnostic. For example, in the following, the monthly series mdeaths is extended by monthly NA values, while the quarterly series austres is extended by quarterly NA values.

x.df <- ts_df(ts_c(mdeaths, austres))
ts_span(x.df, end = "1999-12-01", extend = TRUE)

ts_default standardizes column names in a data frame

In rectangular data structures, i.e., in a data.frame, a data.table, or a tibble, tsbox stores one or multiple time series in the ‘long’ format. By default, tsbox detects a value, a time and zero, one or several id columns. Alternatively, the time column and the value column can be explicitly named time and value. If explicit names are used, the column order will be ignored.

While automatic column name detection is useful in interactive mode, it produces unnecessary overhead in longer workflows. The helper function ts_default detects and renames the time and the value column, so that auto-detection will be turned off in subsequent steps (note that the names of the id columns are not affected):

x.df <- ts_df(ts_c(mdeaths, austres))
names(x.df) <- c("a fancy id name", "date", "count")
ts_plot(x.df)  # tsbox is fine with that

ts_summary summarizes time series

ts_summary provides a frequency agnostic summary of a ts-boxable object:

ts_summary(ts_c(mdeaths, austres))
#>        id obs    diff freq      start        end
#> 1 mdeaths  72 1 month   12 1974-01-01 1979-12-01
#> 2 austres  89 3 month    4 1971-04-01 1993-04-01

ts_summary returns a plain data frame that can be used for any purpose. It is also recommended for the extraction of various time series properties, such as start, freq or id:

#> [1] "austres"
#> [1] "1971-04-01"

And a cheatsheet!

Finally, we fabricated a tsbox cheat sheet that summarizes most functionality. Print and enjoy working with time series.

Time Series of the World, Unite!

The R ecosystem knows a ridiculous number of time series classes. So, I decided to create a new universal standard that finally covers everyone’s use case… Ok, just kidding!

tsbox, now freshly on CRAN, provides a set of tools that are agnostic towards existing time series classes. It is built around a set of converters, which convert time series stored as ts, xts, data.frame, data.table, tibble, zoo, tsibble or timeSeries to each other.

To install the stable version from CRAN:


To get an idea how easy it is to switch from one class to another, consider this:

x.ts <- ts_c(mdeaths, fdeaths)
x.xts <- ts_xts(x.ts)
x.df <- ts_df(x.xts)
x.tbl <- ts_tbl(x.df)
x.dt <- ts_tbl(x.tbl)
x.zoo <- ts_zoo(x.dt)
x.tsibble <- ts_tsibble(x.zoo)
x.timeSeries <- ts_timeSeries(x.tsibble)

We jump form good old ts objects toxts, store our time series in various data frames and convert them to some highly specialized time series formats.

tsbox is class-agnostic

Because these converters work nicely, we can use them to make functions class-agnostic. If a class-agnostic function works for one class, it works for all:


ts_scale normalizes one or multiple series, by subtracting the mean and dividing by the standard deviation. It works like a ‘generic’ function: You can apply it on any time series object, and it will return an object of the same class as its input.

So, whether we want to smooth, scale, differentiate, chain-link, forecast, regularize or seasonally adjust a series, we can use the same commands to whatever time series at hand. tsbox offers a comprehensive toolkit for the basics of time series manipulation. Here are some additional operations:

ts_pc(x.ts)                 # percentage change rates 
ts_forecast(x.xts)          # forecast, by exponential smoothing
ts_seas(x.df)               # seasonal adjustment, by X-13
ts_frequency(x.dt, "year")  # convert to annual frequency
ts_span(x.tbl, "-1 year")   # limit time span to final year

tsbox is frequency-agnostic

There are many more. Because they all start with ts_, you can use auto-complete to see what’s around. Most conveniently, there is a time series plot function that works for all classes and frequencies:

  `Airline Passengers` = AirPassengers, 
  `Lynx trappings` = ts_df(lynx), 
  `Deaths from Lung Diseases` = ts_xts(fdeaths),
  title = "Airlines, trappings, and deaths",
  subtitle = "Monthly passengers, annual trappings, monthly deaths"


There is also a version that uses ggplot2 and has the same syntax.

Time series in data frames

You may have wondered why we treated data frames as a time series class. The spread of dplyr and data.table has given data frames a boost and made them one of the most popular data structures in R. So, storing time series in a data frame is an obvious consequence. And even if you don’t intend to keep time series in data frames, this is still the format in which you import and export your data. tsbox makes it easy to switch from data frames to time series and back.

Make existing functions class-agnostic

tsbox includes tools to make existing functions class-agnostic. To do so, the ts_ function can be used to wrap any function that works with time series. For a function that works on "ts" objects, this is as simple as that:

ts_rowsums <- ts_(rowSums)
ts_rowsums(ts_c(mdeaths, fdeaths))

Note that ts_ returns a function, which can be used with or without a name.

In case you are wondering, tsbox uses data.table as a backend, and makes use of its incredibly efficient reshaping facilities, its joins and rolling joins. And thanks to anytime, tsbox will be able to recongnize almost any date format without manual intervention.

So, enjoy some relieve in R’s time series class struggle.

Website: www.tsbox.help

screenshot of www.dataseries.org

Forecasting GDP with R and dataseries.org

The website dataseries.org aims to be Switzerland’s FRED – a free comprehensive database of Swiss time series. Powered by R and written in Shiny (also using a bit of JavaScript) it allows you to quickly search and explore a large number of data series.

Switzerland’s time series in one place

Similarly to the United States, public data in Switzerland is produced by a large number of different offices, which makes it hard to find any particular series. dataseries.org provides a structured and automatically updated collection of most of these series. We are still working on the data input, but are pretty much complete in the field of Economics.

You can download the data as spreadsheets or graphs, or embed interactive widgets in your website. Alternatively, you can import the data directly into R, using the dataseries package. Install the package from CRAN,


and run the ds function with the id argument that you find on the website:

plot(dataseries::ds("GDP.PBRTT.A.R", "ts"), 
     ylab = "mio CHF, at 2010 prices, s. adj.", 
     main = "Gross Domestic Product")


This will give you an R plot of Switzerland’s GDP. (The data is cached, so calling the function again will not re-download until you restart the R session.)

Live Import of Series to R

In the following, I will use data from dataseries.org to produce a live forecast of Switzerland’s GDP. Each day the model is run, it will be ensured that the latest data is used. That way it is possible to produce a transparent and up-to-date forecast. For the following exercise, I will only use tools from R base, but it is of course possible to use the same data in a more advanced modeling framework.

In order to produce a reasonable forecast, we want to track early information on the business cycle, which is mostly survey data. We will use a question from the SECO Consumer Confidence Survey on current economic performance, the Credit Suisse / Procure Purchasing Managers’ Index and the ETHZ KOF Barometer.

Transforming the data

Getting these indicators from dataseries.org directly into R is easy. Because these data are measured at different frequencies, we need to convert them to the same quarterly frequency as GDP. There are many packages that offer functions for that (e.g., the tempdisagg package has functions to move both to higher or lower frequencies), but I will stick to basic R here:

# Aggregating months to quarters (post updated on May 6, 2017)
to_quarterly <- function(x){
 aggregate(x, nfrequency = 4, FUN = mean)

pmi <- to_quarterly(dataseries::ds("PMI.SA.PM", "ts"))
kof <- to_quarterly(dataseries::ds("KOF.KFBR", "ts"))
csent <- dataseries::ds("CCI.GEPC", "ts")

A plot of these series shows the common trend in these variables and gives you an indication of the business cycle, which may have turned upward in recent months.

plot(cbind(pmi, kof, csent), main = "Business Cycle Indicators")


Since these series are stationary, our left hand side variable should be stationary as well. This is accomplished by calculating percentage change rates of GDP:

gdp.level <- dataseries::ds("GDP.PBRTT.A.R", "ts")
gdp <- (gdp.level / lag(gdp.level, -1)) - 1

ARIMA modelling

R’s workhorse for time series modeling is the arima function, which allows you to construct a univariate or multivariate model of GDP growth. Since the data is seasonally adjusted, a simple autoregressive process (AR1) offers a good benchmark:

# AR1
m0 <- arima(gdp, order = c(1, 0, 0))
fct0 <- predict(m0, n.ahead = 1)$pred
# GDP Growth Q1: +0.3 

If you need advice on which ARIMA model to choose, the information criterions, accessed by the R functions AIC or BIC, can help you to choose a model. Simply take the model with lowest information criterion. The auto.arima function from the forecast package also allows you to do the selection automatically.

We can include our series individually or jointly and estimate a range of different models. A good model (in terms of the AIC information criterion) is the following, which uses PMI and KOF data (but not consumer sentiment data):

dta <- window(cbind(pmi, kof), start = start(pmi), end = end(pmi))
m1 <- arima(window(gdp, start = start(dta)), 
            xreg = window(dta, end = end(gdp)))
fct1 <- predict(m1, n.ahead = 1, 
                newxreg = window(dta, start = tsp(gdp)[2] + 0.25,
                                 end = tsp(gdp)[2] + 0.25)
# GDP Growth Q1: +0.7

The model’s forecast for the first quarter of 2017 is 0.7 – a value that hasn’t been reached for more than two years.

A factor model

If you have multiple indicators at hand, a common problem is multicollinearity, the fact that indicators are correlated, and therefore too many indicators deteriorate the quality of the model estimation.

An easy fix is to use a factor model, where the indicators are summarized in a few factors, which can be calculated by principal components (see Stock and Watson 2002):

# PMI, KOF, Consumer Sentiment, first Principal Component
pca <- prcomp(window(cbind(pmi, kof, csent), start = start(pmi), 
                     end = tsp(gdp)[2] + 0.25),
             scale. = TRUE)
dta.pca <- ts(pca$x[, 'PC1'], start = start(pmi), frequency = 4)

m2 <- arima(window(gdp, start = start(dta)), 
            xreg = window(dta.pca, end = end(gdp)))
fct2 <- predict(m2, n.ahead = 1, 
                newxreg = window(dta.pca, start = tsp(gdp)[2] + 0.25)
# GDP Growth Q1: +0.7

Again, we get a forecast value of 0.7. Overall, survey data indicates that the economy is well on track. Let’s do a graphical comparison of our forecasts:

# skeletons to include forecasts 
gdp.fct0 <- window(gdp, extend = TRUE, end = tsp(gdp)[2] + 0.25)
gdp.fct1 <- gdp.fct2 <- gdp.fct0

# plug forecasts into skeletons
window(gdp.fct0, start = end(gdp.fct0)) <- fct0
window(gdp.fct1, start = end(gdp.fct1)) <- fct1
window(gdp.fct2, start = end(gdp.fct2)) <- fct2

ts.plot(window(cbind(gdp, gdp.fct0, gdp.fct1, gdp.fct2), 
               start = 2010), 
        col = 1:4, ylab = "quarterly growth rates, s. adj.", 
        main = "GDP Forecasts")
legend("topright", legend = c("GDP Growth Rate", "AR 1 Forecast", 
                              "PMI, KOF", "Principal Component"), 
       lty = 1, col = 1:4, bty = "n")


Publication of first quarter GDP is on June 1, 2017. See you in a month!


Shiny-based Online Tool for X-13 Seasonal Adjustment: New Features

The R package seasonal makes it easy to use X-13ARIMA-SEATS, the seasonal adjustment software by the U.S. Census Bureau. In a previous post, I wrote about www.seasonal.website, a Shiny-based website showcasing the use of seasonal. Even if you are not using R, the website allows you to upload and adjust your own series, without the need for any software installation.

The latest version of www.seasonal.website comes with several new features:

Live Parsing of X-13 spc Files

The main new feature is a live parser of X-13 spc files. Changes in the Options, triggered by the pull-down menus, or changes in the R Call, are reflected in an updated X-13 Call. On the other hand, changes in the X-13 Call will be reflected in updates in the Options and the R Call.

manipulate the X-13 spec file

Interactively manipulate the X-13 spec file or the R call

This brings interesting new possibilities:

  • Non-R-users may use the website to generate spc files, which they can use in any software that includes X-13ARIMA-SEATS.
  • People familiar with X-13 may use the spc syntax to learn about the syntax of the R-package seasonal.
  • People familiar with the R-package seasonal may use it learn about the spc syntax.

New Upload/Download Dialog

The upload/download feature has been reworked. A button on the top-right corner opens a new upload and download dialog.

New upload/download dialog

New upload/download dialog

Both XLSX and CSV formats are supported. You can upload and adjust your own monthly or quarterly time series. All data will be permanently deleted after your session.

Nice Summary

The summary, previously just the printed output of the R-function summary, has been overhauled. Colored flags indicate the significance level of the coefficients, reddish colors indicate warning signs from the tests.

New Summary

New Summary

New Online Tool for Seasonal Adjustment

Seasonal adjustment of time series can be a hassle. The softwares used by statistical agencies (X-13, X-12, TRAMO-SEATS) have tons of fantastic options, but the steep learning curve prevents users from taking advantage of the functionality of these packages, or from using them at all.

The R package seasonal simplifies the task by providing an interface to X-13, the newest seasonal adjustment software by the US Census Bureau. It combines and extends the capabilities of the older X-12ARIMA and TRAMO-SEATS software packages. The most simple use of seasonal requires the application of the main function to a time series, which invokes automated procedures that work well in many circumstances:


A new shiny based website is showcasing the use of seasonal and allows for online adjustment of time series, without the need to download and install seasonal. The AirPassengers series is set as the default series, but can be replaced by any uploaded series. There are other demo series that show the use of the software to adjust Indian Diwali or Chinese New Year effects.

The site allows to adjust most parameters of X-13, and to view and download a substantial part of its output. Frequently used options can be chosen from a drag and drop menu, while less often used options can be chosen by manipulating the R-Call itself.

Here are some of the most interesting features of the website:

Frequently Used Options

Frequently used options of X-13 can be modified using the drop down selectors. Each change will result in a re-estimation of the seasonal adjustment model. The R-call, the output and the summary are updated accordingly.

Frequently Used Options

Choosing the Output

A substantial part of the output of X-13ARIMA-SEATS can be shown on the website. Click and drag to zoom into the graph. Double click to restore the original view.

A substantial part of the output of X-13ARIMA-SEATS can be shown on the website.

Manipulating the R-Call

The R-Call to seasonal can be modified and run online. In the picture below, the ARIMA model has been adjusted to include an autoregressive parameter of order 2. Press the button to execute the modified call.

Manipulating the R Call

Upload and Download

User defined series can be uploaded, importing from Excel or CSV. Also, all viewable series can be downloaded as Excel or CSV.

Upload and Download

Chinese New Year, Indian Diwali

Chinese New Year or Indian Diwali support is included out of the box and can be selected from the drop down menu. Adjustment for these holidays is as easy as adjusting Easter effects.

Adjusting Chinese New Year or Indian Diwali Effects

Running X-13 Examples Online

The examples from the official manual of X-13 can be run directly on the website. The collection of examples can found here.

Examples of X 13ARIMA SEATS in R

Try it out!

Adjusting Chinese New Year Effects in R is Easy

The Spring Festival is the most important holiday in China and many other Asian countries. Traditionally, the holiday starts on Chinese New Year’s Eve, and lasts to the Lantern Festival on the 15th day of the first month of the lunisolar calendar. The Chinese New Year is celebrated either in January or in February of the Gregorian calendar.

Because of its importance, Chinese New Year seriously distorts monthly time series, which are usually reported according to the Gregorian calendar. Unlike Easter, Chinese New Year does not affect quarterly time series, as it always falls in the first quarter.

The standard software packages for seasonal adjustment, X-12-ARIMA and X-13-ARIMA-SEATS (developed by the U.S. Census Bureau) or Tramo Seats (developed by the Bank of Spain) have a built-in adjustment procedure for Easter holiday, but not for Chinese New Year. However, all packages allow for the inclusion of user defined variables, and the Chinese New Year can be modeled as such.

The R package seasonal

With the R package seasonal, generating and including such a series is easy. We will use it in the following to seasonally adjust and remove Chinese New Year effects from the nominal dollar value of imports to China. seasonal is an interface to X-13ARIMA-SEATS; for more information and installation details, see here.

Chinese imports are included as an example series in seasonal. As the series has a very different seasonal pattern before 2000, we focus on the later period. (Adjusting the whole series in one step is possible, but for good results one should manually model the seasonal break.)

data(cntrade)  # contains imports ('imp') and exports ('exp') of China
imp <- window(imp, start = 2000)  # this shortens the series

seasonal includes the genhol() function, a R version of the equally named software utility by the U.S. Census Bureau. Using the dates of the Chinese New Year as an input, it produces a time series with the deviations from the monthly means. Here we are assuming that the holiday starts on New Year’s Eve and lasts for one week.

data(holiday)  # dates of Chinese New Year and Easter, included in seasonal
cny.ts <- genhol(cny, start = -1, end = 6, center = "calendar")

In 2014, only two days in January were affected by the holiday (New Year’s Eve and New Year’s Day). 75% of the holiday fell into February. Thus, January was affected slightly less than average, February slightly more. This is very different from 2012, when the holiday completely fell into January.

       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
2011 -0.26  0.26  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
2012  0.74 -0.74  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
2013 -0.26  0.26  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
2014 -0.01  0.01  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

Including user defined regressors

The time series cny.ts can be included in the main seasonal adjustment. The automated procedures of X-13ARIMA-SEATS can be applied to the imp series in the following way:

m1 <- seas(imp, xreg = cny.ts, regression.usertype = "holiday", x11 = list())

## Call:
## seas(x = imp, xreg = cny.ts, regression.usertype = "holiday", 
##     x11 = list())
## Coefficients:
##                   Estimate Std. Error z value Pr(>|z|)    
## cny.ts            -0.18104    0.01548  -11.70  < 2e-16 ***
## Weekday            0.00514    0.00104    4.94  7.8e-07 ***
## LS2008.Nov        -0.37584    0.04745   -7.92  2.3e-15 ***
## MA-Nonseasonal-01  0.39776    0.07202    5.52  3.3e-08 ***
## MA-Seasonal-12     0.72749    0.06428   11.32  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## ARIMA structure: (0 1 1)(0 1 1)   Number of obs.: 169   Transform: log
## AICc: 1.6e+03, BIC: 1.62e+03   QS seas. test (adj. series):   0  
## Box-Ljung (no autocorr.): 33.6 .  Shapiro (normality): 0.978 **

With xreg, arbitrary user defined regressors can be included, regression.usertype = "holiday" ensures that the final series does not include the regression effect. We also have chosen X11 as the decomposition method.

Unsurprisingly, the summary reveals a highly significant Chinese New Year effect. As the automatic model has been estimated on the logarithmic series, the coefficient of -0.18 indicates that New Year in 2012 has lowered imports by approximately 0.74 * 18 = 13%. The automatic procedure has also detected weekday effects and a level shift during the financial crisis.

Multiple regressors

We can do even better by using more than one user defined regressors, one for the pre-New-Year period and one for the post-New-Year period (thanks, Freya Beamish):

pre_cny <- genhol(cny, start = -6, end = -1, frequency = 12, center = "calendar")
post_cny <- genhol(cny, start = 0, end = 6, frequency = 12, center = "calendar")
m2 <- seas(x = imp, xreg = cbind(pre_cny, post_cny), regression.usertype = "holiday", 
           x11 = list())

## Call:
## seas(x = imp, xreg = cbind(pre_cny, post_cny), regression.usertype = "holiday", 
##     x11 = list())
## Coefficients:
##                    Estimate Std. Error z value Pr(>|z|)    
## pre_cny            0.070843   0.019199    3.69  0.00022 ***
## post_cny          -0.241043   0.020816  -11.58  < 2e-16 ***
## Weekday            0.005233   0.000943    5.55  2.9e-08 ***
## LS2008.Nov        -0.357887   0.045790   -7.82  5.5e-15 ***
## MA-Nonseasonal-01  0.331626   0.073967    4.48  7.3e-06 ***
## MA-Seasonal-12     0.687479   0.065740   10.46  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## ARIMA structure: (0 1 1)(0 1 1)   Number of obs.: 169   Transform: log
## AICc: 1.59e+03, BIC: 1.61e+03   QS seas. test (adj. series):0.75  
## Box-Ljung (no autocorr.): 37.6 *  Shapiro (normality): 0.984 *

adjusted and unadjusted series

Chinese imports: adjusted and unadjusted series

There are actually two kind of New Year effects: Until New Year’s Eve, import activity is higher than usual. During the holiday, it is lower. By including two regressors, these opposite effects can be modeled. Note that the negative effect is more pronounced than the positive one.

Manual refinements

The model could be further refined. With the static() function, a non-automatic version of the previous call can be extracted. It can be copy-pasted and used for further manipulations.


## seas(x = imp, xreg = cbind(pre_cny, post_cny), regression.usertype = "holiday", 
##     x11 = list(), regression.variables = c("td1coef", "ls2008.Nov"
##     ), arima.model = "(0 1 1)(0 1 1)", regression.aictest = NULL, 
##     outlier = NULL, transform.function = "log")

The inspect() function opens an interactive window that allows for the manipulation of a number of arguments. With each change, the adjustment process and the visualizations are recalculated. (This only works with R Studio.)


After some playing around, we would probably stay with the two regressor adjustment model from above:

m2 <- seas(x = imp, xreg = cbind(pre_cny, post_cny), regression.usertype = "holiday", 
           x11 = list(), regression.variables = c("td1coef", "ls2008.Nov"), 
           arima.model = "(0 1 1)(0 1 1)", regression.aictest = NULL, 
           outlier = NULL, transform.function = "log")

It’s far form perfect. Normality statistics are bad, and there may be some traces of autocorrelation. On the other hand, the seasonal component is stable and revisions are small.

Comparing the series

Was it worth the pain? The following graph shows the same seasonal adjustment with and without the Chinese New Year adjustment:

m3 <- seas(x = imp, x11 = list(), regression.variables = c("td1coef", "ls2008.Nov"), 
           arima.model = "(0 1 1)(0 1 1)", regression.aictest = NULL, outlier = NULL, 
           transform.function = "log")

ts.plot(diff(log(cbind(final(m2), final(m3)))), col = c("red", "blue"), 
        lwd = c(2, 1))
Comparison of adjusted and unadjusted time series

Not adjusting Chinese New Year seriously distorts the time series

In 2012, we would have concluded that imports have plumped in January, soared in February and plumped again in March (blue line). With the adjustment, we rightly conclude that there was no such craziness (red line).

ts.plot(final(m2), final(m1), col = c("red", "blue"), lwd = c(2, 1))
Stagnating Imports

Chinese imports have stagnated this January

How useful is the two regressor model? Most of the time, the single regressor model performs reasonably well and leads to results similar to the two regressors model. This year, however, the Lunar New Year fell on January 31. As people were importing more in the pre-New-Year period, January imports were actually affected by a positive New Year effect. The right adjustment would be to correct the numbers downward! With the one regressor model, we would wrongly conclude that imports have soared (blue line). In fact, they have actually stagnated (red line).

Quickly Explore the Penn World Tables in R

The Penn World Tables are one of the greatest source of worldwide macroeconomic data, but dealing with its web interface is somewhat cumbersome. Fortunately, the data is also available as a R package on CRAN. Having some tools at hand to quickly draw some nice graphs from the data is useful in many circumstances. Here are three exciting packages that help you to quickly explore the PWT data in R. All of them are on CRAN.

First, get the data (in PWT 8, you need to calculate per capita measures yourself):

pwt8.0$cap <- pwt8.0$rgdpe / pwt8.0$pop

1. ggplot2

Use ggplot2 to draw time series for a variable. Exchange the isocodes in the list or the variable name to alter the graph.

dta <- subset(pwt8.0, isocode %in% c("USA", "JPN", "KOR", "CHN"))

      dta[, 'cap'], 
      geom = "line", 
      group = dta$isocode,
      color = as.factor(dta$isocode)
     ) + 
theme(legend.title = element_blank()) +

and that’s what you will get:


2. manipulate

Use ggplot2 and manipulate to be even more flexible. (You need RStudio for that, but you should have it anyway!)

dta <- subset(pwt8.0, isocode %in% c("USA", "JPN", "KOR", "CHN"))

        dta[, vars], 
        geom = "line", 
        group = dta$isocode,
        color = as.factor(dta$isocode)
       ) + theme(legend.title = element_blank()),
 vars = picker(as.list(colnames(pwt8.0)[-(1:3)]))

you will get a nice menu to choose the variable of your interest.


3. googleVis

googleVis let’s you use the Google Visualisation API.


First, get some pretty colors from colorbrewer2.org:

colorbrewer <- "{maxValue: 40000, colors:['#F7FCF0', '#E0F3DB', '#CCEBC5', '#A8DDB5', '#7BCCC4', '#4EB3D3', '#2B8CBE', '#0868AC', '#084081']}"

And now draw a map with your data (make a screenshot if you want to use it)

plot(gvisGeoChart(subset(pwt8.0, year == 2011), "country", "cap",
                   options=list(colorAxis = colorbrewer)))

That’s the result:


Or simply show all data, in a very similar way as the Google Public Data Explorer does.

plot(gvisMotionChart(pwt8.0, idvar="country", timevar="year")))