# how to deal with independent variable of value 0 when applying log-log model?

I am trying to apply log-log in a marketing mix model. The dependent variable is sales, among the independent variables, one is holiday, it is dummy variable, the value is either 1(yes), or 0 (no). Another variable is competitor spend, sometimes, the value is 0.

I want to use a log-log model in R. But how can deal with variables like holiday and competitor's spend which have value of 0? I cannot remove these two variables from the model.

I already did a linear regression model in R, can anyone help me with the log-log model? (As suggested by a comment in below, I used log(x+1) in the log-log model, is it correct?)

model <- lm(SALES ~ HOLIDAY + AVERAGE_PRICE + COMPETITOR_MEDIA_SPEND
+ IMP_TV + IMP_EMAIL + IMP_PAID_SEARCH + IMP_ONLINE_DISPLAY
+ IMP_PRODUCT_SEARCH, data = mmm)

model2 <- lm(log(SALES) ~ log(HOLIDAY+1) + log(AVERAGE_PRICE)
+ log(COMPETITOR_MEDIA_SPEND+1)+ log(IMP_TV) + log(IMP_EMAIL)
+ log(IMP_PAID_SEARCH) + log(IMP_ONLINE_DISPLAY)
+ log(IMP_PRODUCT_SEARCH), data = mmm)

• Some people use log(x+1) in situations like this. – David Lane Mar 3 '17 at 4:49
• Another possible road to go down is a zero inflated model. – Matthew Gunn Mar 3 '17 at 20:56
• See 80% of missing data in a single variable & How small a quantity should be added to x to avoid taking the log of zero?. And, as @Carl explains, transforming indicator variables really makes no difference - you could just as well set HOLIDAY to $\pi$ for 'yes' & $7$ for 'no'. – Scortchi - Reinstate Monica Mar 7 '17 at 12:48
• @Scortchi Correct. The only advantage of using $x=\{1(no), 2(yes)\}$ is that $c_x\ln x\rightarrow x^{c_x}\rightarrow\{1^{c_x}=1,2^{c_x}\}$. That is, that $2^{c_x}$ is simply the adjustment factor for a yes answer. In other words, if we have one answer as the multiplicative identity ($=1$), the scaling is easier to understand. – Carl Mar 8 '17 at 16:44
• @MatthewGunn How using a Poisson model would you adjust for the disagreement between the square root makes normal residuals of a Poisson model and the logarithm makes normal residuals for a proportional error (log) model? Or, perhaps that is besides the point and you were just giving a general indication of the direction in which another solution may lie without specifying explicitly how to do that? – Carl Mar 8 '17 at 17:02

If $\mathit{HOLIDAY}_t$ is a binary, indicator variable, then there's absolutely no reason to compute $X_t = \log(1 + \mathit{HOLIDAY}_t)$. Just stick the indicator $\mathit{HOLIDAY}_t$ on the right hand side of the regression.

### Table of values:

$$\begin{array}{ccc} \text{Is day t a holiday?} & \mathit{HOLIDAY}_t & \log(1 + \mathit{HOLIDAY}_t) \\ \text{no} & 0 & 0 \\ \text{yes} & 1 & \log(2) \end{array}$$

You're basically creating an indicator variable where it takes the value $\log(2)$ (which is $\approx .6931$) if the condition is true. IMHO, this is bizarre.

### Two equivalent regressions:

A regression of: $$y_t = a + b_1 \mathit{HOLIDAY}_t + \epsilon_t$$

is equivalent to a regression of: $$y_t = a + b_2 \log(1+\mathit{HOLIDAY}_t) + \epsilon_t$$

in the sense that your estimate $\hat{b}_2 = \frac{\hat{b}_1}{\log(2)}$ since $\log(1 + \mathit{HOLIDAY}_t) = \log(2)\mathit{HOLIDAY}_t$.

### Conclusion (weirdo transforms hurt interpretability)

If you ran the regression $\log(Sales_t) = a + b \, \mathit{HOLIDAY}_t + \epsilon_t$ and got an estimate for $b$ of .02, you would basically conclude that sales are 2 percent higher on holidays.

If you ran the regression $\log(Sales_t) = a + b \log(1 + HOLIDAY_t) + \epsilon_t$, you would then get an estimate of $.02 / \log(2)$ = .0289, which has absolutely no meaningful interpretation.

Actually, that is much simpler than one might fear. There are many solutions, and here is the one I use. I set 1(no) and 2(yes). Now what that does is create Log(no)=0 and Log(yes)=Log(2). Now, one could set $e$(yes) so that Log(yes)=1, but that actually makes no difference to the regression significance.

Here is a link to a paper using 1=male, and 2=female, for ordinary least squares log-log (i.e., power function) modelling.

The other question is for the value 0 occurring in a real variable. One can, for example do as @DavidLane suggests and use Log(1+x). However, that can alter the regression. Another possibility is to use 1(no spending) and x(spending) to yield Log(no spending)=0 and Log(spending)=Log(x). There are many other solutions and approaches. For example, if zero is within the range of a continuous variable, it may be that $\sqrt x$ is a better transform than $\ln x$. What is best depends on what the best transformation is for that portion of the data, and, it is possible, and sometimes necessary to transform different variables differently. See power transforms. This link includes (thank-you @horaceT) the Box-Cox transform, which is a log transform that you might like to use.

• So log(x+1) can be used for the dummy variables but not for continuous variables, right?@Carl – Zoey Mar 3 '17 at 5:15
• What you asked was about binary variables, for which I answered 1, 2. If the data is truly proportional type, for a continuous variable Log(1+x) would not be exactly proportional modelling. Which is why I suggested to using Log(x) for all spending except no spending, which would then be Log(1)=0. What that does after detransforming the additive term in the logarithm $+k\ln x$ is make a multiplicative term $x^k$, where $x$ is spending, and $k$ is determined by regression. – Carl Mar 3 '17 at 5:39
• @Carl Can you cite a reference for the log(no spending) method? – horaceT Mar 3 '17 at 20:36
• @horaceT No, nor am I suggesting it as other than a possibility. It is a counter argument to using $\ln (x+1)$ rather than a recommendation. I am using it to demonstrate what $\ln (x+1)$ is not. I can try to think of another work around, but in practice if I have zeros, my reflex reaction would be to test if a power function or other transform is not preferable to a logarithm, and typically I find one, so I have not had the problem occur in practice. – Carl Mar 3 '17 at 21:05
• @Carl The two-parameter Box-Cox transform could handle x=zero, that may be one solution. See this – horaceT Mar 3 '17 at 21:41