How can we measure the number of additional clicks or sales that an AdWords campaign generated? How can we estimate the impact of a new feature on app downloads? How do we compare the effectiveness of publicity across countries?
In principle, all of these questions can be answered through causal inference.
In practice, estimating a causal effect accurately is hard, especially when a randomised experiment is not available. One approach we've been developing at Google is based on Bayesian structural time-series models. We use these models to construct a synthetic control — what would have happened to our outcome metric in the absence of the intervention. This approach makes it possible to estimate the causal effect that can be attributed to the intervention, as well as its evolution over time.
We've been testing and applying structural time-series models for some time at Google. For example, we've used them to better understand the effectiveness of advertising campaigns and work out their return on investment. We've also applied the models to settings where a randomised experiment was available, to check how similar our effect estimates would have been without an experimental control.
Today, we're excited to announce the release of CausalImpact, an open-source R package that makes causal analyses simple and fast. With its release, all of our advertisers and users will be able to use the same powerful methods for estimating causal effects that we've been using ourselves.
Our main motivation behind creating the package has been to find a better way of measuring the impact of ad campaigns on outcomes. However, the CausalImpact package could be used for many other applications involving causal inference. Examples include problems found in economics, epidemiology, or the political and social sciences.
How the package works
The CausalImpact R package implements a Bayesian approach to estimating the causal effect of a designed intervention on a time series. Given a response time series (e.g., clicks) and a set of control time series (e.g., clicks in non-affected markets, clicks on other sites, or Google Trends data), the package constructs a Bayesian structural time-series model with a built-in spike-and-slab prior for automatic variable selection. This model is then used to predict the counterfactual, i.e., how the response metric would have evolved after the intervention if the intervention had not occurred.
As with all methods in causal inference, valid conclusions require us to check for any given situation whether key model assumptions are fulfilled. In the case of CausalImpact, we are looking for a set of control time series which are predictive of the outcome time series in the pre-intervention period. In addition, the control time series must not themselves have been affected by the intervention. For details, see Brodersen et al. (2014).
A simple example
The figure below shows an application of the R package. Based on the observed data before the intervention (black) and a control time series (not shown), the model has computed what would have happened after the intervention at time point 70 in the absence of the intervention (blue).
The difference between the actual observed data and the prediction during the post-intervention period is an estimate of the causal effect of the intervention. The first panel shows the observed and predicted response on the original scale. The second panel shows the difference between the two, i.e., the causal effect for each point in time. The third panel shows the individual causal effects added up in time.
The script used to create the above figure is shown in the left part of the window below. Using package defaults means our analysis boils down to just a single line of code: a call to the function CausalImpact() in line 10. The right-hand side of the window shows the resulting numeric output. For details on how to customize the model, see the documentation.
How to get started
The best place to start is the package documentation. The package is hosted on Github and can be installed using:
install.packages("devtools")
library(devtools)
devtools::install_github("google/CausalImpact")
library(CausalImpact)
By Kay H. Brodersen, Google
Showing posts with label R. Show all posts
Showing posts with label R. Show all posts
CausalImpact: A new open-source package for estimating causal effects in time series
Wednesday, September 10, 2014
RProtoBuf & HistogramTools: Statistical Analysis Tools for Large Data Sets
Thursday, October 10, 2013
At Google, building, managing and safely securing some of the world’s largest storage systems requires complex analysis of filesystem metadata. This is an important part of making sure that the information stored within those systems is quickly accessible and always secure. We're always looking for ways to make our data storage systems more efficient, and often times, this requires understanding the age, size and access patterns of the data stored, the failure rates of servers and disks, and more. You can imagine how complex this becomes with each new data center added.
Given the number of files and servers that are relevant for this performance analysis, we bin the metadata into a compact histogram form. We use these output histograms for many purposes, such as (i) building Markov models of data availability, (ii) statistical forecasting of resource usage, and (iii) formulating and solving optimization problems to determine optimal allocation of flash devices.
We rely on several open source tools to make our work easier. The most common tool we use for statistical analysis of the performance, availability, and resource needs of our internal systems is the R programming language. We’ve released two package updates that make R particularly suitable for interacting with other distributed systems.
If you're interested to learn more, we have shared some of our research findings at conferences such as OSDI, USENIX ATC, and JSM.
By Murray Stokely, Storage Analytics Team Lead
Given the number of files and servers that are relevant for this performance analysis, we bin the metadata into a compact histogram form. We use these output histograms for many purposes, such as (i) building Markov models of data availability, (ii) statistical forecasting of resource usage, and (iii) formulating and solving optimization problems to determine optimal allocation of flash devices.
We rely on several open source tools to make our work easier. The most common tool we use for statistical analysis of the performance, availability, and resource needs of our internal systems is the R programming language. We’ve released two package updates that make R particularly suitable for interacting with other distributed systems.
- RProtoBuf is an R package for Google’s Protocol Buffer library that allows one to define simple data structures with intuitive getter and setter methods. These data structures can be serialized into an extremely compact format for sending to other distributed systems. Recent releases include improved support for 64-bit integers, protocol buffer extensions, and more.
- HistogramTools is a new R package I have released that uses RProtoBuf to read in a compact protocol buffer representation of binned data and includes a number of helpful functions for manipulating, plotting, and measuring the statistical information loss due to the binning. In addition to protocol buffers, it also supports importing aggregate performance data directly from DTrace output.
If you're interested to learn more, we have shared some of our research findings at conferences such as OSDI, USENIX ATC, and JSM.
By Murray Stokely, Storage Analytics Team Lead
BARUG Meetup at the Googleplex
Thursday, September 20, 2012
What is R? R is an open-source, statistical programming language that is increasingly becoming the lingua franca of modern data analysis. R developers have created over 4,000 packages to run in the R environment, including several dozen developed by Googlers. We continue to work with the R community through conferences, meetups like this one, Faculty Research Awards, and the annual Google Summer of Code program, which funded 16 students to work with the R Project this summer.
Last week, Google hosted the Bay Area useR Group (BARUG) at our Mountain View HQ. Over 130 R users and developers met for pizza, networking, and presentations by Googlers.
This was our third time hosting a BARUG Meetup at Google. Last year we hosted meetings in both Mountain View and San Francisco. Last week’s meetup topics included an overview of the integration of R into Google’s engineering systems by Sundar Dorai-Raj (above), exciting new work serializing R to run over Flume by Karl Millar (below left), and tips for better R code by Olivia Lau (below right).
By Olivia Lau and Murray Stokely, Google’s R Intergrouplet
Bringing 64-bit data to R
Thursday, November 24, 2011

The R programming language has become one of the standard tools for statistical data analysis and visualization, and is widely used by Google and many others. The language includes extensive support for working with vectors of integers, numerics (doubles), and many other types, but has lacked support for 64-bit integers. Romain Francois has recently uploaded the int64 package to CRAN as well as updated versions of the Rcpp and RProtobuf packages to make use of this package. Inside Google, this is important when interacting with other engineering systems such as Dremel and Protocol Buffers, where our engineers and quantitative analysts often need to read in 64-bit quantities from a datastore and perform statistical analysis inside R.
Romain has taken the approach of storing int64 vectors as S4 objects with a pair of R’s default 32-bit integers to store the high and low-order bits. Almost all of the standard arithmetic operations built into the R language have been extended to work with this new class. The design is such that the necessary bit-artihmetic is done behind the scenes in high-performance C++ code, but the higher-level R functions work transparently. This means, for example, that you can:
• Perform arithmetic operations between 64-bit operands or between int64 objects and integer or numeric types in R.
• Read and write CSV files including 64-bit values by specifying int64 as a colClasses argument to read.csv and write.csv (with int64 version 1.1).
• Load and save 64-bit types with the built-in serialization methods of R.
• Compute summary statistics of int64 vectors, such as max, min, range, sum, and the other standard R functions in the Summary Group Generic.
For even higher levels of precision, there is also the venerable and powerful GNU Multiple Precision Arithmetic Library and the R GMP package on CRAN, although Romain’s new int64 package is a better fit for the 64-bit case.
We’ve had to work around the lack of 64-bit integers in R for several years at Google. And after several discussions with Romain, we were very happy to be able to fund his development of this package to solve the problem not just for us, but for the broader open-source community as well. Enjoy!
By Murray Stokely, Software Engineer, Infrastructure Quantitative Team
Visualization Meetup at the Googleplex
Monday, June 20, 2011

Earlier this month we were excited to host a joint meetup with the Bay Area visualization group and the Bay Area R user group at Google’s headquarters in Mountain View.
Hadley Wickham from Rice University came and gave a talk on Interactive Graphics in R. Earlier in the day we were excited to host him for a Google Tech Talk on Engineering Data Analysis for the many internal Google teams using his ggplot2 software for visualization with R. The videos for both talks are available from YouTube.
You may remember our previous post about Hadley’s visualization work when his Google Summer of Code student was awarded a prestigious statistical computing award for his work with ggplot2.
Thanks to the Open Source Programs Office at Google for hosting this event, the meetup organizers, Zhou Yu, and of course Hadley Wickham.
By Murray Stokely and Zhou Yu, Google Engineers and Analysts
Google Summer of Code Student Project Wins Statistical Software Award
Thursday, April 28, 2011
We are happy to report that one of the Google Summer of Code students from last year, Ian Fellows, has been awarded the John M. Chambers Statistical Software Award for his work with the R Project. This award will be presented at the annual Joint Statistical Meeting in August.
Ian’s project involved extending the scope of the Deducer cross-platform GUI interface to R by adding an intuitive plot builder for building up complex statistical graphics in a user friendly environment. Ian has described the plot builder as “a piece of software that simply wouldn't have been built except for the funding from Google.” In addition to the online documentation, Ian has put together a series of video tutorials on YouTube introducing the new software:
Deducer Video - Part 1
Console Video - Part 2
We’re also happy to report that once again Ian will be participating in the Google Summer of Code, this year as a mentor with the R Project.
I personally use ggplot2 frequently for visualizing complex data sets at Google, and I’m looking forward to utilizing the new GUI PlotBuilder for my work. Congratulations again, Ian.
By Murray Stokely, Software Engineering Team
UseR Meetup at Google San Francisco
Friday, February 11, 2011
Earlier this week, Google hosted the Bay Area useR Group at our San Francisco office. Over 40 attendees showed up to hear Dylan Beaudette from UC Davis give a presentation about investigating soil genesis and geography with R (PDF). Dylan has been using R to study and visualize large amounts of soil data and has made his routines available in the aqp: Algorithms for Quantitative Pedology package on CRAN.Dylan's research also utilizes a number of Google technologies, such as Google Earth KMZ overlays of soil data and the Google Ngram Viewer for tracking Temporal Trends in Soil Science Jargon. In addition to open source and soil science, there were lively discussions at the meetup about reproducible research and the data sharing problem.
I’d like to thank our speaker, Dylan, all of the attendees, the Bay Area useR organizers who continue to put together interesting talks each month, and the Google Open Source Program Office for hosting the event. We’re looking forward to the R User Conference in England in August and more local Bay Area Meetups in the interim.
By Murray Stokely, Software Engineer, Infrastructure Quantitative Team
Integrating R with C++: Rcpp, RInside, and RProtobuf
Thursday, October 28, 2010
Last week the R “intergrouplet” at Google invited Dirk Eddelbuettel and Romain Francois to come and give a Tech Talk about their joint work on Rcpp, RInside, and RProtoBuf. Dirk and Romain agreed and also let us share the video of their talk:
We were also happy to have John Chambers, creator of the S programming language and member of the R Core team, join us for the talk and discussions afterward.
At Google we use a lot of C++ and an increasing amount of R for data analysis, so the Rcpp package is very interesting to us as a modern type-safe way to write R packages that interface with other Google technologies written in C++. Likewise, the RProtoBuf package makes it easy for analysts and engineers to use R with Protocol Buffers read from Bigtable or other data stores.
By Murray Stokely, Quantitative Engineering Team
We were also happy to have John Chambers, creator of the S programming language and member of the R Core team, join us for the talk and discussions afterward.
At Google we use a lot of C++ and an increasing amount of R for data analysis, so the Rcpp package is very interesting to us as a modern type-safe way to write R packages that interface with other Google technologies written in C++. Likewise, the RProtoBuf package makes it easy for analysts and engineers to use R with Protocol Buffers read from Bigtable or other data stores.
By Murray Stokely, Quantitative Engineering Team
Notes from useR! 2010
Thursday, July 29, 2010
I've just returned from the annual useR! conference for the open source R programming language. This year the conference attracted nearly 500 individuals to the NIST campus outside Washington D.C.
The conference provided a great opportunity to meet with some of the package authors that are working on third-party extensions, including Romain Francois and Dirk Eddelbuettel who jointly gave a pair of well-attended talks on their RProtoBuf and Rcpp packages.
In addition to the 3 days of tutorials, panels, and presentations, Google sponsored a dinner for conference attendees at the National Zoo in Washington D.C. to facilitate the "hallway track" of informal discussions outside of the official conference program.
Thanks to all those presenters, sponsors, and organizers involved in putting together a successful conference. For those who weren’t able to attend, the abstracts and slides from the 168 presentations and a more limited number of videos are available from the technical sessions. Hope to see you next year...
By Murray Stokely, Software Engineering Team
Highlights from useR!
Thursday, September 11, 2008
By Pete Meyer, Ads Quality Statistics Team
Google was a sponsor of this year's international user conference in Dortmund, Germany for the R statistical programming environment. In addition to sessions focusing on statistical modeling in various areas of applications, the conference included a number of sessions focusing on user interfaces for R and producing high quality output for reports and browsers. There were sessions on high performance computing, including parallelized approaches to R, and new routines for handling massive objects in response to the increasing size and complexity of datasets. Google was among a number of companies who showed examples of how R can be used in production environments as a statistical tool to better understand collected data. We presented some examples (PDF, 348 KB) of how HTML and email output and database access can be integrated into R's statistical environment. The goal is to provide new insights in a familiar format for those who oversee the human evaluation component of Google's advertising quality efforts, without having to generate intermediate datasets.
We're a member of the R community as both users and contributors via Google Summer of Code™. We see the R community as an ally in our mission to organize the world's information and make it universally accessible and useful, and we are glad to be able to support their efforts as a sponsor.
Google was a sponsor of this year's international user conference in Dortmund, Germany for the R statistical programming environment. In addition to sessions focusing on statistical modeling in various areas of applications, the conference included a number of sessions focusing on user interfaces for R and producing high quality output for reports and browsers. There were sessions on high performance computing, including parallelized approaches to R, and new routines for handling massive objects in response to the increasing size and complexity of datasets. Google was among a number of companies who showed examples of how R can be used in production environments as a statistical tool to better understand collected data. We presented some examples (PDF, 348 KB) of how HTML and email output and database access can be integrated into R's statistical environment. The goal is to provide new insights in a familiar format for those who oversee the human evaluation component of Google's advertising quality efforts, without having to generate intermediate datasets.
We're a member of the R community as both users and contributors via Google Summer of Code™. We see the R community as an ally in our mission to organize the world's information and make it universally accessible and useful, and we are glad to be able to support their efforts as a sponsor.




