# Which is the dependent variable?

I was looking at this Data Science question on TestDome.

The problems is stated as the following:

Implement the desired_marketing_expenditure function, which returns the required amount of money that needs to be invested in a new marketing campaign to sell the desired number of units.

Use the data from previous marketing campaigns to evaluate how the number of units sold grows linearly as the amount of money invested increases.

For example, for the desired number of 60,000 units sold and previous campaign data from the table below, the function should return the float 250,000.

Approaching this with linear regression I see this as:

marketing_expenditure = coeff * units_sold + intercept + error

because what I'm trying to find is the marketing expenditure given a number of units sold.

However the author of this test seems it has seen the marketing expenditure as the independent variable, in other words:

units_sold = coeff * marketing_expenditure + intercept + error

from which then it calculates the marketing_expenditure by rearranging the equation.

The two approaches are not equivalent and give different results as depending on what is the dependent / independent variable the linear regression algorithm tries to minimise different square distances to different regression lines.

Which approach is correct and why?

• In a cause-and-effect relationship, units sold will not directly cause marketing expenditures. That is, marketing expenditures are not directly dependent on units sold. – James Phillips Nov 16 '18 at 17:36
• Thanks @JamesPhillips, I see your point but for what I read about Linear Regression, LR is not trying to explain causality but mainly correlation. In fact one could find a correlation between chocolate consumption and Nobel prizes but that does not mean that there is a causality. In that light, one would fit a Linear Regression based on what one wants to predict not what causes what. Am I wrong? – Giuseppe Romagnuolo Nov 19 '18 at 10:17
• Absent its context, this question is difficult to parse. It sounds like it might be asking you to implement an inverse regression but without the author recognizing that to be the case. – whuber Nov 19 '18 at 15:11
• However the author of this test seems it has seen the marketing expenditure as the independent variable, in other words: units_sold = coeff * marketing_expenditure + intercept + error from which then it calculates the marketing_expenditure by rearranging the equation. – Subhash C. Davar Nov 22 '18 at 1:24
• However the author of this test seems it has seen the marketing expenditure as the independent variable, in other words: units_sold = coeff * marketing_expenditure + intercept + error from which then it calculates the marketing_expenditure. This is self-propagated idea without reasoning . Moreover no assumptions that invoke statistics are pointed. No definition of terms including error term – Subhash C. Davar Nov 22 '18 at 1:36

Implement the desired_marketing_expenditure function, which returns the required amount of money that needs to be invested in a new marketing campaign to sell the desired number of units.

You know what? Let's implement that function. I will use Python.

Import libraries

import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model


Use the data

mark_exp = np.array([300000, 200000, 400000, 300000, 100000]).reshape(-1, 1)
units_s = np.array([60000, 50000, 90000, 80000, 30000]).reshape(-1, 1)


First, a linear regression like this: units_sold = coeff * marketing_expenditure + intercept

lin_regr = linear_model.LinearRegression()
lin_regr.fit(mark_exp, units_s)

print(lin_regr.score(mark_exp, units_s)) # 0.91
print(lin_regr.coef_) # 0.2
print(lin_regr.intercept_) # 10_000
print(lin_regr.predict(np.array(60_000).reshape(-1, 1))) # 22_000


Well, that number is wrong, just what I expected. The input number is $$60\,000$$ and the function returns $$22\,000$$.

Second, a linear regression like this: marketing_expenditure = coeff * units_sold + intercept

lin_regr2 = linear_model.LinearRegression()
lin_regr2.fit(units_s, mark_exp)

print(lin_regr2.score(units_s, mark_exp)) # 0.91
print(lin_regr2.coef_) # 4.56
print(lin_regr2.intercept_) # -22_807.01
print(lin_regr2.predict(np.array(60_000).reshape(-1, 1)) # 250_877.19


I like this. The input number is $$60\,000$$ and the function returns $$250\,877$$. What did I assume? That marketing_expenditure is the dependent variable and units_sold is the independent variable, just what you said in your question. What do I want to predict? $$Z$$. So let $$Z$$ be the dependent variable.

However, the confusion... the damn confusion is present because the guy does something neither you or I would do. Let me give an example.

Suppose that I give two lists of numbers, one labeled time with $$t_1, t_2, \dots, t_{10}$$ as the numbers and the other labeled velocity with $$v_1, v_2, \dots, v_{10}$$ as the numbers. Each pair $$(t_i, v_i)$$ is measured in an experiment and I tell you that the relationship between both variables is

$$v = a\cdot t + b. \tag{1}$$

The goal is to find $$a$$ and $$b$$ with linear regression. You and I naturally assume that $$t$$ is the independent variable and $$v$$ is the dependent variable. Say you find $$a = 2.5$$ and $$b = 1.3$$ (with the correct units, but we don't care for the moment). Then I tell you that find $$t$$ given $$v = 4.8$$, what would you do? Rearrange the equation?

$$\dfrac{1}{a} v - \dfrac{b}{a} = t \tag{2}$$ $$c \cdot v + d = t, \quad \text{with} \quad c=\dfrac{1}{a}, d = \dfrac{-b}{a} \tag{3}$$

Do a linear regression to find $$c$$ and $$d$$? Did I tell you that if you want to predict $$t$$, $$t$$ is the dependent variable? I will not tell you why $$(1)$$ feels more natural, maybe because there are historical reasons [citation needed] or maybe time is viewed as the independent variable when you include distance, or speed, or acceleration, or force, etc... [citation needed]. By the way, in $$(1)$$, $$a$$ is acceleration and $$b$$ is the initial speed; in $$(3)$$, $$c$$ and $$d$$ are...

Which approach is correct and why?

I am with you, if you want to predict marketing_expenditure, marketing_expenditure is the dependent variable (besides, with Python it's easier than the other option). There must be strong reasons (other than casuality) to change the order of the variables, I don't see those in this example.

The questions in the first highlighted section call for you to do both - and also maybe neither!

Implement the desired_marketing_expenditure function, which returns the required amount of money that needs to be invested in a new marketing campaign to sell the desired number of units.

This is very poorly worded. To me, "implement a function" doesn't call for regression at all. It sounds like there is an already existing function, and you should just do something with it. However, if regression is called for, then it seems like number of units is the DV. That is, to me, also strange. You can't manipulate the number of units sold, you can manipulate the amount spent, but the way it's worded, it looks like they want you to do it that way.

Use the data from previous marketing campaigns to evaluate how the number of units sold grows linearly as the amount of money invested increases.

This clearly does call for linear regression, but here the DV is clearly number of units.

So, what to do? Can you contact the people who wrote TestDome? Can you ignore this?