# Polynomial Regression for Digital Ads with R

Polynomial Regression for Digital Ads with R

This post discusses how to use polynomial regression for digital advertising data. Polynomial regression can help us better understand the relationship between spend and impressions (or spend and clicks). This method can be particularly useful when looking at daily data with variability in daily spends. Models can be used to analyze, estimate, and benchmark performance of future campaigns. The full code can be found on GitHub.

The code used here uses a second order polynomial function to allow for diminishing marginal returns. For impressions the function takes the form of: $\widehat{impressions} =\widehat{B}_{0} + \widehat{B}_{1}spend + \widehat{B}_{2}spend^2$

Or in the case of clicks: $\widehat{clicks} =\widehat{B}_{0} + \widehat{B}_{1}spend + \widehat{B}_{2}spend^2$

To run this code begin by importing the ggplot2, scales, and rio packages.

First we define a function to fit a second order polynomial regression given two variables. This function also creates a ggplot object that maps a scatter plot of actual observations along with a regression line of predicted values.

Next we define an objective function. We will pass estimated coefficients from our model to this function and then optimize the function to get a sense for where optimal spend levels may be when assuming diminishing marginal returns.

Similarly we define a function to estimate the predicted values of our dependent variable (impressions or clicks) given a spend level.

The above code can be edited to fit other functional forms however, in many cases the second order polynomial will suffice.

Finally, we perform regression, estimation, and optimization. The code below uses Impressions and Revenue (Adv Currency) as input data. This is how columns would be named if using daily data from a Google DV360 Report. This method can also be applied to Facebook Ads data and Google Ads data, however the target column names should be changed to reflect the names used in those data sets.