# Fit a V-curve into a model

I have data which consists of number of impressions for an advertisement, position of ad in the break and total number of ads in a break.

Typically, if the channels are not synchronized, people switch to other channels during break and watch the program in that channel. So if I plot Impressions vs position of ad in the break, I get a v-curve with a steep fall and slow rise.

That is because, as soon as the break starts, people switch to other channels and slowly come back to the channel after some time to catchup.

However, this also depends on the total ads in the break since viewership of ad in position 10 is not the same for total number of ads being 11 and total number of ads being 30.

So, the impressions definitely depend on both position as well as total number of ads. I need to model this V-curve (rather a tick mark kind of curve) to predict ad viewership.

I am extremely new to data science and would greatly appreciate some pointers.

NOTE:

I looked at linear modelling but I got a 0.03 R-squared value which I believe is very bad. I am thinking getting 2 linear models for the fall part and rise parts of the V and combining them will be a good thing to do. But I am not sure how I can do it.

There have been a few good piecewise linear regression problems on this site (Stack Overflow):

I have developed a framework on this, which you can read my answers to the above questions. For a piecewise regression with one break point (i.e., two segments), all we need is a parametrization and a function getX to construct design matrix given x (independent variable) and c (break point). Then, we can simply use est, choose.c and pred in my answer for selection of c, model estimation / inference and prediction.

For your problem, we use parametrization:

\begin{equation} f(x;c) = \begin{cases} \beta_0 + \beta_1(c-x), & x < c\\ \beta_0 + \beta_2(x-c), & x\geq c \end{cases} \end{equation}

which can be written into a single line:

\begin{equation} f(x;c) = \beta_0 + \beta_1\max(c-x,\ 0) + \beta_2\max(x-c,\ 0) \end{equation}

We can thus write getX:

getX <- function (x, c) {
cbind("beta_0" = 1, "beta1" = pmax(c - x, 0), "beta2" = pmax(x - c, 0))
}


Now you can just follow the code in example 1 and example 2 to get what you want.

Unlike above linked questions, yours can use R package segmented, which I will not demonstrate.