-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-supervised_learning.Rmd
More file actions
1331 lines (954 loc) · 81.3 KB
/
Copy path07-supervised_learning.Rmd
File metadata and controls
1331 lines (954 loc) · 81.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
output:
html_document:
toc: yes
pdf_document:
toc: yes
html_notebook: default
---
```{r echo=FALSE, eval=TRUE, message=FALSE, warning=FALSE, purl=FALSE}
library(knitr)
options(scipen = 999)
#This code automatically tidies code so that it does not reach over the page
opts_chunk$set(tidy.opts=list(width.cutoff=50),tidy=TRUE, rownames.print = FALSE, rows.print = 10)
opts_chunk$set(cache=T)
```
```{r message=FALSE, warning=FALSE, echo=F, eval=TRUE,paged.print = FALSE}
options(digits = 8)
```
# Supervised learning
## Linear regression
### Correlation
Before we start with regression analysis, we will review the basic concept of correlation first. Correlation helps us to determine the degree to which the variation in one variable, X, is related to the variation in another variable, Y.
#### Correlation coefficient
The correlation coefficient summarizes the strength of the linear relationship between two metric (interval or ratio scaled) variables. Let's consider a simple example. Say you conduct a survey to investigate the relationship between the attitude towards a shop and the duration of being of its customer. The "Attitude" variable can take values between 1 (very unfavorable) and 12 (very favorable), and the "Duration" is measured in months. Let's further assume for this example that the attitude measurement represents an interval scale (although it is usually not realistic to assume that the scale points on an itemized rating scale have the same distance). To keep it simple, let's further assume that you only asked 12 people. We can create a short data set like this:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE,paged.print = FALSE}
library(psych)
attitude <- c(6,9,8,3,10,4,5,2,11,9,10,2)
duration <- c(10,12,12,4,12,6,8,2,18,9,17,2)
att_data <- data.frame(attitude, duration)
att_data <- att_data[order(-attitude), ]
att_data$respodentID <- c(1:12)
str(att_data)
psych::describe(att_data[, c("attitude","duration")])
att_data
```
Let's look at the data first. The following graph shows the individual data points for the "duration" variable, where the y-axis shows the duration of residency in years and the x-axis shows the respondent ID. The blue horizontal line represents the mean of the variable (`r round(mean(att_data$duration),2)`) and the vertical lines show the distance of the individual data points from the mean.
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Scores for duration variable"}
library(ggplot2)
h <- round(mean(att_data$duration), 2)
ggplot(att_data, aes(x = respodentID, y = duration)) +
geom_point(size = 3, color = "deepskyblue4") +
scale_x_continuous(breaks = 1:12) +
geom_hline(data = att_data, aes(yintercept = mean(duration)), color ="deepskyblue4") +
labs(x = "Observations",y = "Duration", size = 11) +
coord_cartesian(ylim = c(0, 18)) +
geom_segment(aes(x = respodentID,y = duration, xend = respodentID,
yend = mean(duration)), color = "deepskyblue4", size = 1) +
theme(axis.title = element_text(size = 16),
axis.text = element_text(size=16),
strip.text.x = element_text(size = 16),
legend.position="none") +
theme_bw()
```
You can see that there are some respondents that have been the store's customers longer than average and some - shorter than average. Let's do the same for the second variable ("Attitude"). Again, the y-axis shows the observed scores for this variable and the x-axis shows the respondent ID.
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Scores for attitude variable"}
ggplot(att_data, aes(x = respodentID, y = attitude)) +
geom_point(size = 3, color = "#f9756d") +
scale_x_continuous(breaks = 1:12) +
geom_hline(data = att_data, aes(yintercept = mean(attitude)), color = "#f9756d") +
labs(x = "Observations",y = "Attitude", size = 11) +
coord_cartesian(ylim = c(0,18)) +
geom_segment(aes(x = respodentID, y = attitude, xend = respodentID,
yend = mean(attitude)), color = "#f9756d", size = 1) +
theme_bw()
```
Again, we can see that some respondents have an above average attitude towards the store (more favorable) and some respondents have a below average attitude. Let's combine both variables in one graph now to see if there is some co-movement:
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE,fig.align="center", fig.cap = "Scores for attitude and duration variables"}
ggplot(att_data) +
geom_point(size = 3, aes(respodentID, attitude), color = "#f9756d") +
geom_point(size = 3, aes(respodentID, duration), color = "deepskyblue4") +
scale_x_continuous(breaks = 1:12) +
geom_hline(data = att_data, aes(yintercept = mean(duration)), color = "deepskyblue4") +
geom_hline(data = att_data, aes(yintercept = mean(attitude)), color = "#f9756d") +
labs(x = "Observations", y = "Duration/Attitude", size = 11) +
coord_cartesian(ylim = c(0, 18)) +
scale_color_manual(values = c("#f9756d", "deepskyblue4")) +
theme_bw()
```
We can see that there is indeed some co-movement here. The variables <b>covary</b> because respondents who have an above (below) average attitude towards the store also appear to have been its customers for an above (below) average amount of time and vice versa. Correlation helps us to quantify this relationship. Before you proceed to compute the correlation coefficient, you should first look at the data. We usually use a scatterplot to visualize the relationship between two metric variables:
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Scatterplot for durationand attitute variables"}
ggplot(att_data) +
geom_point(size = 3, aes(duration, attitude)) +
labs(x = "Duration", y = "Attitude", size = 11) +
theme_bw()
```
How can we compute the correlation coefficient? Remember that the variance measures the average deviation from the mean of a variable:
\begin{equation}
\begin{split}
s_x^2&=\frac{\sum_{i=1}^{N} (X_i-\overline{X})^2}{N-1} \\
&= \frac{\sum_{i=1}^{N} (X_i-\overline{X})*(X_i-\overline{X})}{N-1}
\end{split}
(\#eq:variance)
\end{equation}
When we consider two variables, we multiply the deviation for one variable by the respective deviation for the second variable:
<p style="text-align:center;">
$(X_i-\overline{X})*(Y_i-\overline{Y})$
</p>
This is called the cross-product deviation. Then we sum the cross-product deviations:
<p style="text-align:center;">
$\sum_{i=1}^{N}(X_i-\overline{X})*(Y_i-\overline{Y})$
</p>
... and compute the average of the sum of all cross-product deviations to get the <b>covariance</b>:
\begin{equation}
Cov(x, y) =\frac{\sum_{i=1}^{N}(X_i-\overline{X})*(Y_i-\overline{Y})}{N-1}
(\#eq:covariance)
\end{equation}
You can easily compute the covariance manually as follows
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
x <- att_data$duration
x_bar <- mean(att_data$duration)
y <- att_data$attitude
y_bar <- mean(att_data$attitude)
N <- nrow(att_data)
cov <- (sum((x - x_bar)*(y - y_bar))) / (N - 1)
cov
```
Or you simply use the built-in ```cov()``` function:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
cov(att_data$duration, att_data$attitude) # apply the cov function
```
A positive covariance indicates that as one variable deviates from the mean, the other variable deviates in the same direction. A negative covariance indicates that as one variable deviates from the mean (e.g., increases), the other variable deviates in the opposite direction (e.g., decreases).
However, the size of the covariance depends on the scale of measurement. Larger scale units will lead to larger covariance. To overcome the problem of dependence on measurement scale, we need to convert the covariance to a standard set of units through standardization by dividing the covariance by the standard deviation (similar to how we compute z-scores).
With two variables, there are two standard deviations. We simply multiply the two standard deviations. We then divide the covariance by the product of the two standard deviations to get the standardized covariance, which is known as a correlation coefficient r:
\begin{equation}
r=\frac{Cov_{xy}}{s_x*s_y}
(\#eq:corcoeff)
\end{equation}
This is known as the product moment correlation (r) and it is straight-forward to compute:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
x_sd <- sd(att_data$duration)
y_sd <- sd(att_data$attitude)
r <- cov/(x_sd*y_sd)
r
```
Or you could just use the ```cor()``` function:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
cor(att_data[, c("attitude", "duration")], method = "pearson", use = "complete")
```
The properties of the correlation coefficient ('r') are:
* ranges from -1 to + 1
* +1 indicates perfect linear relationship
* -1 indicates perfect negative relationship
* 0 indicates no linear relationship
* ± .1 represents small effect
* ± .3 represents medium effect
* ± .5 represents large effect
#### Significance testing
How can we determine if our two variables are significantly related? To test this, we denote the population moment correlation *ρ*. Then we test the null of no relationship between variables:
$$H_0:\rho=0$$
$$H_1:\rho\ne0$$
The test statistic is:
\begin{equation}
t=\frac{r*\sqrt{N-2}}{\sqrt{1-r^2}}
(\#eq:cortest)
\end{equation}
It has a t distribution with n - 2 degrees of freedom. You can simply use the ```cor.test()``` function, which also produces the 95% confidence interval:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
cor.test(att_data$attitude, att_data$duration, alternative = "two.sided", method = "pearson", conf.level = 0.95)
```
To determine the linear relationship between variables, the data only needs to be measured using interval scales. If you want to test the significance of the association, the sampling distribution needs to be normally distributed (we usually assume this when our data are normally distributed or when N is large). If parametric assumptions are violated, you should use non-parametric tests:
* Spearman's correlation coefficient: requires ordinal data and ranks the data before applying Pearson's equation.
* Kendall's tau: use when N is small or the number of tied ranks is large.
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
cor.test(att_data$attitude, att_data$duration, alternative = "two.sided", method = "spearman", conf.level = 0.95)
cor.test(att_data$attitude, att_data$duration, alternative = "two.sided", method = "kendall", conf.level = 0.95)
```
Report the results:
A Pearson product-moment correlation coefficient was computed to assess the relationship between the duration of being a customer of a store and the attitude toward this store. There was a positive correlation between the two variables, r = 0.936, n = 12, p < 0.05. A scatterplot summarizes the results (Figure XY).
**A note on the interpretation of correlation coefficients:**
As we have already seen in Chapter 1, correlation coefficients give no indication of the direction of causality. In our example, we can conclude that the attitude toward the store is more positive as the months of being a customer increase. However, we cannot say that the duration causes the attitudes to be more positive. There are two main reasons for caution when interpreting correlations:
* Third-variable problem: there may be other unobserved factors that affect both the 'attitude' and the 'duration' variables
* Direction of causality: Correlations say nothing about which variable causes the other to change (reverse causality: attitudes may just as well cause the duration variable).
### Regression analysis
Correlations measure relationships between variables (i.e., how much two variables covary). Using regression analysis we can predict the outcome of a dependent variable (Y) from one or more independent variables (X). For example, we could be interested in how many products will we will sell if we increase the advertising expenditures by 1000 Euros? In regression analysis, we fit a model to our data and use it to predict the values of the dependent variable from one predictor variable (bivariate regression) or several predictor variables (multiple regression). The following table shows a comparison of correlation and regression analysis:
<br>
| Correlation | Regression
-------------|-------------------------- | --------------------------
Estimated coefficient | Coefficient of correlation (bounded between -1 and +1) | Regression coefficient (not bounded a priori)
Interpretation | Linear association between two variables; Association is bidirectional | (Linear) relation between one or more independent variables and dependent variable; Relation is directional
Role of theory | Theory neither required nor testable | Theory required and testable
<br>
#### Simple linear regression
In simple linear regression, we assess the relationship between one dependent (regressand) and one independent (regressor) variable. The goal is to fit a line through a scatterplot of observations in order to find the line that best describes the data (scatterplot).
Suppose you are a marketing research analyst at a big retail group and your task is to suggest, on the basis of historical data, a marketing plan for the next year that will maximize product sales. The product in question is beer brand Budweiser. The data set that is available to you includes information on the sales of Budweiser *move_ounce* (in ounces, FYI: 1 oz = 29,57 ml), prices *price_ounce* (in dollars per ounce), and several other variables: bonus buy - a price reduction if customers buy a certain quantity of a product (*sale_B*), price reduction in % (*sale_S*), and others. Let's load and inspect the data first:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
regression <- read.table("https://raw.githubusercontent.com/dariayudaeva/RMA2024/main/data/bud_store102.csv",
sep = ",",
header = TRUE) # read in data
str(regression)
regression$store <- as.factor(regression$store) #convert grouping variable to factor
regression$brand_id <- as.factor(regression$brand_id) #convert grouping variable to factor
head(regression)
```
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE, paged.print = FALSE}
psych::describe(regression) #descriptive statistics using psych
```
As stated above, regression analysis may be used to relate a quantitative response ("dependent variable") to one or more predictor variables ("independent variables"). In a simple linear regression, we have one dependent and one independent variable and we regress the dependent variable on the independent variable.
Here are a few important questions that we might seek to address based on the data:
* Is there a relationship between prices and sales?
* How strong is the relationship between prices and sales?
* Which other variables contribute to sales?
* How accurately can we estimate the effect of each variable on sales?
* How accurately can we predict future sales?
* Is the relationship linear?
* Is there synergy among the advertising activities?
We may use linear regression to answer these questions. We will see later that the interpretation of the results strongly depends on the goal of the analysis - whether you would like to simply predict an outcome variable or you would like to explain the causal effect of the independent variable on the dependent variable (see Chapter 1). Let's start with the first question and investigate the relationship between advertising and sales.
##### Estimating the coefficients
A simple linear regression model only has one predictor and can be written as:
\begin{equation}
Y=\beta_0+\beta_1X+\epsilon
(\#eq:regequ)
\end{equation}
In our specific context, let's consider only the influence of prices on sales for now:
\begin{equation}
Sales=\beta_0+\beta_1*price+\epsilon
(\#eq:regequadv)
\end{equation}
The word "price" represents data on advertising expenditures that we have observed and β<sub>1</sub> (the "slope"") represents the unknown relationship between prices and sales. It tells you by how much sales will increase or decrease for an additional dollar added to price. β<sub>0</sub> (the "intercept") is the number of sales we would expect if the price is set to 0. Note that the last assumption is highly theoretical: in the majority of real world scenarios, we never have such variables as prices, advertising expenditures, kilometers to the nearest store set to 0. Hence, it is incorrect to interpret the intercept like this. Together, β<sub>0</sub> and β<sub>1</sub> represent the model coefficients or *parameters*. The error term (ε) captures everything that we miss by using our model, including, (1) misspecifications (the true relationship might not be linear), (2) omitted variables (other variables might drive sales), and (3) measurement error (our measurement of the variables might be imperfect).
Once we have used our training data to produce estimates for the model coefficients, we can predict future sales on the basis of a particular value of price by computing:
\begin{equation}
\hat{Sales}=\hat{\beta_0}+\hat{\beta_1}*price
(\#eq:predreg)
\end{equation}
We use the hat symbol, <sup>^</sup>, to denote the estimated value for an unknown parameter or coefficient, or to denote the predicted value of the response (sales). In practice, β<sub>0</sub> and β<sub>1</sub> are unknown and must be estimated from the data to make predictions. In the case of our pricing example, the data set consists of the prices and product sales for 220 weeks (n = 220). Our goal is to obtain coefficient estimates such that the linear model fits the available data well. In other words, we fit a line through the scatterplot of observations and try to find the line that best describes the data. The following graph shows the scatterplot for our data, where the black line shows the regression line. The grey vertical lines shows the difference between the predicted values (the regression line) and the observed values. This difference is referred to as the residuals ("e").
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Ordinary least squares (OLS)"}
library(dplyr)
options(scipen = 999)
regression1 <- read.table("https://raw.githubusercontent.com/dariayudaeva/RMA2024/main/data/bud_store102.csv",
sep = ",",
header = TRUE)
lm <- lm(move_ounce ~ price_ounce, data = regression1)
regression1$yhat <- predict(lm)
ggplot(regression1, aes(x = price_ounce,y = move_ounce)) +
geom_point(size = 2, color = "deepskyblue4") +
labs(x = "Advertising expenditure (in Euros)", y = "Sales", size = 11) +
geom_segment(aes(x = price_ounce, y = move_ounce, xend = price_ounce, yend = yhat),
color = "grey",size = 0.5, linetype = "solid", alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
theme(axis.title = element_text(size = 16),
axis.text = element_text(size = 16),
strip.text.x = element_text(size = 16),
legend.position="none") +
theme_minimal()
```
The estimation of the regression function is based on the idea of the method of least squares (OLS = ordinary least squares). The first step is to calculate the residuals by subtracting the observed values from the predicted values.
<p style="text-align:center;">
$e_i = Y_i-(\hat{\beta_0}+\hat{\beta_1}X_i)$
</p>
This difference is then minimized by minimizing the sum of the squared residuals:
\begin{equation}
\sum_{i=1}^{N} e_i^2= \sum_{i=1}^{N} [Y_i-(\hat{\beta_0}+\hat{\beta_1X_i)}]^2\rightarrow min!
(\#eq:rss)
\end{equation}
e<sub>i</sub>: Residuals (i = 1,2,...,N)<br>
Y<sub>i</sub>: Values of the dependent variable (i = 1,2,...,N) <br>
&\hat{beta;<sub>0</sub>}: Intercept<br>
&\hat{beta;<sub>1</sub>}: Regression coefficient / slope parameters<br>
X<sub>ni</sub>: Values of the nth independent variables and the i*th* observation<br>
N: Number of observations<br>
This is also referred to as the <b>residual sum of squares (RSS)</b>. Now we need to choose the values for β<sub>0</sub> and β<sub>1</sub> that minimize RSS. So how can we derive these values for the regression coefficient? The equation for β<sub>1</sub> is given by:
\begin{equation}
\hat{\beta_1}=\frac{COV_{XY}}{s_x^2}
(\#eq:slope)
\end{equation}
The exact mathematical derivation of this formula is beyond the scope of this script, but the intuition is to calculate the first derivative of the squared residuals with respect to β<sub>1</sub> and set it to zero, thereby finding the β<sub>1</sub> that minimizes the term. Using the above formula, you can easily compute β<sub>1</sub> using the following code:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE}
cov_y_x <- cov(regression$price_ounce, regression$move_ounce)
cov_y_x
var_x <- var(regression$price_ounce)
var_x
beta_1 <- cov_y_x/var_x
beta_1
```
The interpretation of β<sub>1</sub> is as follows:
For every extra dollar increase of the price, sales can be expected to decrease by `r round(beta_1,3)` oz, which is around 270 liters.
Using the estimated coefficient for β<sub>1</sub>, it is easy to compute β<sub>0</sub> (the intercept) as follows:
\begin{equation}
\hat{\beta_0}=\overline{Y}-\hat{\beta_1}\overline{X}
(\#eq:intercept)
\end{equation}
The R code for this is:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE}
beta_0 <- mean(regression$move_ounce) - beta_1*mean(regression$price_ounce)
beta_0
```
You may also verify this based on a scatterplot of the data. The following plot shows the scatterplot including the regression line, which is estimated using OLS.
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE, fig.align="center", fig.cap = "Scatterplot"}
ggplot(regression, mapping = aes(price_ounce, move_ounce)) +
geom_point(shape = 1) +
geom_smooth(method = "lm", fill = "blue", alpha = 0.1) +
labs(x = "Price ($ per oz)", y = "Sales (oz)") +
theme_bw()
```
The slope coefficient (β<sub>1</sub>) tells you by how much sales (on the y-axis) would decrease if the price (on the x-axis) is increased by one unit ($).
##### Significance testing
In a next step, we assess if the effect of prices on sales is statistically significant. This means that we test the null hypothesis H<sub>0</sub>: "There is no relationship between prices and sales" versus the alternative hypothesis H<sub>1</sub>: "The is some relationship between prices and sales". Or, to state this formally:
$$H_0:\beta_1=0$$
$$H_1:\beta_1\ne0$$
How can we test if the effect is statistically significant? Recall the generalized equation to derive a test statistic:
\begin{equation}
test\ statistic = \frac{effect}{error}
(\#eq:teststatgeneral)
\end{equation}
The effect is given by the β<sub>1</sub> coefficient in this case. To compute the test statistic, we need to come up with a measure of uncertainty around this estimate (the error). This is because we use information from a sample to estimate the least squares line to make inferences regarding the regression line in the entire population. Since we only have access to one sample, the regression line will be slightly different every time we take a different sample from the population. This is sampling variation and it is perfectly normal! It just means that we need to take into account the uncertainty around the estimate, which is achieved by the standard error. Thus, the test statistic for our hypothesis is given by:
\begin{equation}
t = \frac{\hat{\beta_1}}{SE(\hat{\beta_1})}
(\#eq:teststatreg)
\end{equation}
After calculating the test statistic, we compare its value to the values that we would expect to find if there was no effect based on the t-distribution. In a regression context, the degrees of freedom are given by ```N - p - 1``` where N is the sample size and p is the number of predictors. In our case, we have 220 observations and one predictor. Thus, the degrees of freedom is 220 - 1 - 1 = 218. In the regression output below, R provides the exact probability of observing a t value of this magnitude (or larger) if the null hypothesis was true. This probability is the p-value. A small p-value indicates that it is unlikely to observe such a substantial association between the predictor and the outcome variable due to chance in the absence of any real association between the predictor and the outcome.
To estimate the regression model in R, you can use the ```lm()``` function. Within the function, you first specify the dependent variable ("move_ounce") and independent variable ("price_ounce") separated by a ```~``` (tilde). As mentioned previously, this is known as _formula notation_ in R. The ```data = regression``` argument specifies that the variables come from the data frame named "regression". Strictly speaking, you use the ```lm()``` function to create an object called "sales_reg," which holds the regression output. You can then view the results using the ```summary()``` function:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
sales_reg <- lm(move_ounce ~ price_ounce, data = regression) #estimate linear model
summary(sales_reg) #summary of results
```
Note that the estimated coefficients for β<sub>0</sub> (`r round(summary(sales_reg)$coefficients[1],3)`) and β<sub>1</sub> (`r round(summary(sales_reg)$coefficients[2],3)`) correspond to the results of our manual computation above. The associated t-values and p-values are given in the output. The t-values are larger than the critical t-values for the 95% confidence level, since the associated p-values are smaller than 0.05. In case of the coefficient for β<sub>1</sub>, this means that the probability of an association between the prices and sales of the observed magnitude (or larger) is smaller than 0.05, if the value of β<sub>1</sub> was, in fact, 0. This finding leads us to reject the null hypothesis of no association between prices and sales.
The coefficients associated with the respective variables represent <b>point estimates</b>. To obtain a better understanding of the range of values that the coefficients could take, it is helpful to compute <b>confidence intervals</b>. A 95% confidence interval is defined as a range of values such that with a 95% probability, the range will contain the true unknown value of the parameter. For example, for β<sub>1</sub>, the confidence interval can be computed as.
\begin{equation}
CI = \hat{\beta_1}\pm(t_{1-\frac{\alpha}{2}}*SE(\beta_1))
(\#eq:regCI)
\end{equation}
It is easy to compute confidence intervals in R using the ```confint()``` function. You just have to provide the name of you estimated model as an argument:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
confint(sales_reg)
```
For our model, the 95% confidence interval for β<sub>0</sub> is [`r round(confint(sales_reg)[1,1],2)`,`r round(confint(sales_reg)[1,2],2)`], and the 95% confidence interval for β<sub>1</sub> is [`r round(confint(sales_reg)[2,1],2)`,`r round(confint(sales_reg)[2,2],2)`]. Thus, we can conclude that when we do not spend any money on advertising, sales will be somewhere between `r round(confint(sales_reg)[1,1],0)` and `r round(confint(sales_reg)[1,2],0)` units on average. In addition, for each increase in advertising expenditures by one Euro, there will be an average increase in sales of between `r round(confint(sales_reg)[2,1],2)` and `r round(confint(sales_reg)[2,2],2)`. If you revisit the graphic depiction of the regression model above, the uncertainty regarding the intercept and slope parameters can be seen in the confidence bounds (blue area) around the regression line.
##### Assessing model fit
Once we have rejected the null hypothesis in favor of the alternative hypothesis, the next step is to investigate how well the model represents ("fits") the data. How can we assess the model fit?
* First, we calculate the fit of the most basic model (i.e., the mean)
* Then, we calculate the fit of the best model (i.e., the regression model)
* A good model should fit the data significantly better than the basic model
* R<sup>2</sup>: Represents the percentage of the variation in the outcome that can be explained by the model
* The F-ratio measures how much the model has improved the prediction of the outcome compared to the level of inaccuracy in the model
Similar to ANOVA, the calculation of model fit statistics relies on estimating the different sum of squares values. SS<sub>T</sub> is the difference between the observed data and the mean value of Y (aka. total variation). In the absence of any other information, the mean value of Y ($\overline{Y}$) represents the best guess on where a particular observation $Y_{i}$ at a given level of advertising will fall:
\begin{equation}
SS_T= \sum_{i=1}^{N} (Y_i-\overline{Y})^2
(\#eq:regSST)
\end{equation}
The following graph shows the total sum of squares:
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Total sum of squares"}
library(dplyr)
options(scipen = 999)
ggplot(regression1, aes(x = price_ounce, y = move_ounce)) +
geom_point(size = 2, color = "deepskyblue4") +
labs(x = "Price ($ per oz)", y = "Sales (oz)", size = 11) +
geom_segment(aes(x = price_ounce, y = move_ounce, xend = price_ounce,
yend = mean(move_ounce)), color = "grey",
size = 0.5, linetype = "solid", alpha = 0.8) +
geom_hline(data = regression, aes(yintercept = mean(move_ounce)), color = "black", size = 1) +
theme(axis.title = element_text(size = 16),
axis.text = element_text(size = 16),
strip.text.x = element_text(size = 16),
legend.position="none") +
theme_minimal()
```
Based on our linear model, the best guess about the sales level at a given level of prices is the predicted value $\hat{Y}_i$. The model sum of squares (SS<sub>M</sub>) therefore has the mathematical representation:
\begin{equation}
SS_M= \sum_{i=1}^{N} (\hat{Y}_i-\overline{Y})^2
(\#eq:regSSM)
\end{equation}
The model sum of squares represents the improvement in prediction resulting from using the regression model rather than the mean of the data. The following graph shows the model sum of squares for our example:
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Ordinary least squares (OLS)"}
ggplot(regression1, aes(x = price_ounce, y = move_ounce)) +
geom_point(size = 2, color = "deepskyblue4") +
labs(x = "Price ($ per oz)",y = "Sales (oz)", size = 11) +
geom_segment(aes(x = price_ounce, y = yhat, xend = price_ounce, yend = mean(move_ounce)),
color = "grey", size = 0.5, linetype = "solid", alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
geom_hline(data = regression, aes(yintercept = mean(move_ounce)), color = "black", size = 1) +
theme(axis.title = element_text(size = 16),
axis.text = element_text(size = 16),
strip.text.x = element_text(size = 16),
legend.position = "none") +
theme_minimal()
```
The residual sum of squares (SS<sub>R</sub>) is the difference between the observed data points ($Y_{i}$) and the predicted values along the regression line ($\hat{Y}_{i}$), i.e., the variation *not* explained by the model.
\begin{equation}
SS_R= \sum_{i=1}^{N} ({Y}_{i}-\hat{Y}_{i})^2
(\#eq:regSSR)
\end{equation}
The following graph shows the residual sum of squares for our example:
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.cap = "Ordinary least squares (OLS)"}
ggplot(regression1, aes(x = price_ounce, y = move_ounce)) +
geom_point(size = 2, color = "deepskyblue4") +
labs(x = "Price ($ per oz)",y = "Sales (oz)", size = 11) +
geom_segment(aes(x = price_ounce, y = move_ounce, xend = price_ounce, yend = yhat),
color = "grey", size = 0.5, linetype = "solid", alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
theme(axis.title = element_text(size = 16),
axis.text = element_text(size = 16),
strip.text.x = element_text(size = 16),
legend.position = "none") +
theme_minimal()
```
Based on these statistics, we can determine how well the model fits the data as we will see next.
###### R-squared {-}
The R<sup>2</sup> statistic represents the proportion of variance that is explained by the model and is computed as:
\begin{equation}
R^2= \frac{SS_M}{SS_T}
(\#eq:regSSR)
\end{equation}
It takes values between 0 (very bad fit) and 1 (very good fit). Note that when the goal of your model is to *predict* future outcomes, a "too good" model fit can pose severe challenges. The reason is that the model might fit your specific sample so well, that it will only predict well within the sample but not generalize to other samples. This is called **overfitting** and it shows that there is a trade-off between model fit and out-of-sample predictive ability of the model, if the goal is to predict beyond the sample. We will come back to this point later in this chapter.
You can get a first impression of the fit of the model by inspecting the scatter plot as can be seen in the plot below. If the observations are highly dispersed around the regression line (left plot), the fit will be lower compared to a data set where the values are less dispersed (right plot).
```{r message=FALSE, warning=FALSE, echo=FALSE, eval=TRUE, fig.align="center", fig.height = 4, fig.width = 10, fig.cap="Good vs. bad model fit"}
library(cowplot)
library(gridExtra)
library(grid)
set.seed(44)
#x3 <- rlnorm(250, log(1), log(0.6))
options(scipen = 999)
options(digits = 2)
x1 <- rnorm(200,614.41,485)
x <- as.data.frame(subset(x1,x1>0))
names(x)<-c('adspend')
error <- rnorm(nrow(x))
sales <- round(134 + 0.094*x$adspend + 50*error)
sales_data <- data.frame(sales,x$adspend)
names(sales_data)<-c('sales','adspend')
examplereg <- subset(sales_data,sales>0 & adspend>0)
#summary(examplereg)
lm <- lm(sales ~ adspend, data = examplereg)
#summary(lm)
examplereg$yhat <- predict(lm)
scatter_plot1 <- ggplot(examplereg,aes(adspend,sales)) +
geom_point(size=2,shape=1) + # Use hollow circles
geom_smooth(method="lm") + # Add linear examplereg line (by default includes 95% confidence region);
scale_x_continuous(name="advertising expenditures", limits=c(0, 1800)) +
scale_y_continuous(name="sales", limits=c(0, 400)) +
theme_bw() +
labs(title = paste0("R-squared: ",round(summary(lm)$r.squared,2)))
#x3 <- rlnorm(250, log(1), log(0.6))
options(scipen = 999)
options(digits = 2)
x1 <- rnorm(200,614.41,485)
x <- as.data.frame(subset(x1,x1>0))
names(x)<-c('adspend')
error <- rnorm(nrow(x))
sales <- round(134 + 0.094*x$adspend + 20*error)
sales_data <- data.frame(sales,x$adspend)
names(sales_data)<-c('sales','adspend')
#summary(sales_data)
examplereg <- subset(sales_data,sales>0 & adspend>0)
#summary(examplereg)
lm <- lm(sales ~ adspend, data = examplereg)
examplereg$yhat <- predict(lm)
scatter_plot2 <- ggplot(examplereg,aes(adspend,sales)) +
geom_point(size=2,shape=1) + # Use hollow circles
geom_smooth(method="lm") + # Add linear examplereg line (by default includes 95% confidence region);
scale_x_continuous(name="advertising expenditures", limits=c(0, 1800)) +
scale_y_continuous(name="sales", limits=c(0, 400)) +
theme_bw() +
labs(title = paste0("R-squared: ",round(summary(lm)$r.squared,2)))
#p <- plot_grid(plot1, plot2, ncol = 2)
p <- plot_grid(scatter_plot1,scatter_plot2, ncol = 2)
print(p)
# now add the title
#title <- ggdraw() + draw_label("", fontface='bold')
#plot_full <- plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1)) # rel_heights values control title margins
#print(plot_full)
```
The R<sup>2</sup> statistic is reported in the regression output, so you don't need to compute it manually.
###### Adjusted R-squared {-}
Due to the way the R<sup>2</sup> statistic is calculated, it will never decrease if a new explanatory variable is introduced into the model. This means that every new independent variable either doesn't change the R<sup>2</sup> or increases it, even if there is no real relationship between the new variable and the dependent variable. Hence, one could be tempted to just add as many variables as possible to increase the R<sup>2</sup> and thus obtain a "better" model. However, this actually only leads to more noise and therefore a worse model.
To account for this, there exists a test statistic closely related to the R<sup>2</sup>, the **adjusted R<sup>2</sup>**. It can be calculated as follows:
\begin{equation}
\overline{R^2} = 1 - (1 - R^2)\frac{n-1}{n - k - 1}
(\#eq:adjustedR2)
\end{equation}
where ```n``` is the total number of observations and ```k``` is the total number of explanatory variables. The adjusted R<sup>2</sup> is equal to or less than the regular R<sup>2</sup> and can be negative. It will only increase if the added variable adds more explanatory power than one would expect by pure chance. Essentially, it contains a "penalty" for including unnecessary variables and therefore favors more parsimonious models. As such, it is a measure of suitability, good for comparing different models and is very useful in the model selection stage of a project. In R, the standard ```lm()``` function automatically also reports the adjusted R<sup>2</sup> as you can see above.
###### F-test {-}
Similar to the ANOVA, another significance test is the F-test, which tests the null hypothesis:
$$H_0:R^2=0$$
<br>
Or, to state it slightly differently:
$$H_0:\beta_1=\beta_2=\beta_3=\beta_k=0$$
<br>
This means that we test whether any of the included independent variables has a significant effect on the dependent variable. So far, we have only included one independent variable, but we will extend the set of predictor variables below.
The F-test statistic is calculated as follows:
\begin{equation}
F=\frac{\frac{SS_M}{k}}{\frac{SS_R}{(n-k-1)}}=\frac{MS_M}{MS_R}
(\#eq:regSSR)
\end{equation}
which has a F distribution with k number of predictors and n degrees of freedom. In other words, you divide the systematic ("explained") variation due to the predictor variables by the unsystematic ("unexplained") variation.
The result of the F-test is provided in the regression output as well. However, you might manually compute the F-test using the ANOVA results from the model:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE,paged.print = FALSE}
f_calc <- anova(sales_reg)$'Mean Sq'[1]/anova(sales_reg)$'Mean Sq'[2] #compute F
f_calc
f_crit <- qf(.95, df1 = 1, df2 = 100) #critical value
f_crit
f_calc > f_crit #test if calculated test statistic is larger than critical value
```
##### Using the model
After fitting the model, we can use the estimated coefficients to predict sales of Budweiser for different values of prices. Suppose the store plans to set the price per ounce to 2 dollars. How much will it sell? You can easily compute this either by hand:
$$\hat{sales}=63950.6 + (-9060.5)*2=45,829.6$$
<br>
... or by extracting the estimated coefficients from the model summary:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE}
prediction <- summary(sales_reg)$coefficients[1,1] +
summary(sales_reg)$coefficients[2,1]*2 # the slope * 2 EUR
prediction
```
The predicted value of the dependent variable is 45,829.6 oz, i.e., the store will sell around 45,829.6 oz (~1,355 liters) of Budweiser.
#### Log-Log transformation
*(For more details about log-log transformation, a.k.a. multiplicative model, see chapter 6.1.3.3).* Have a look at the plots above again. You might notice some data specific pattern, making the data points look odd: they are pulled to lower edge of the scatterplot. In this particular case, we're dealing with different measurement scales of our independent and dependent variables. Moreover, you could also notice how odd the interpretation of the regression coefficients sounds.
It is very rare that in the retailing context, the predictions are made as we did before. The concept that is used instead is familiar to you from the microeconomics course - elasticity is a measure that is used by retail managers and researchers much more often than mere unit changes.
Let's have a look at the plot again:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE}
ggplot(regression, mapping = aes(price_ounce, move_ounce)) +
geom_point(shape = 1) +
geom_smooth(method = "lm", color = "lavenderblush4", fill = "red", alpha = 0.1) +
labs(x = "Price (ounce)", y = "Sales (ounce)") +
theme_minimal()
```
The way of obtaining a more reasonable view and interpretation in this case is called "multiplicative modeling", or log-log transformation (you can find additional details about log-log transformations below with a slightly different motivation and example).
The multiplicative model has the following formal representation:
\begin{equation}
Y =\beta_0 *X_1^{\beta_1}*X_2^{\beta_2}*...*X_J^{\beta_J}*\epsilon
(\#eq:multiplicative)
\end{equation}
This functional form can be linearized by taking the logarithm of both sides of the equation:
\begin{equation}
log(Y) =log(\beta_0) + \beta_1*log(X_1) + \beta_2*log(X_2) + ...+ \beta_J*log(X_J) + log(\epsilon)
(\#eq:multiplicativetransformed)
\end{equation}
This means that taking logarithms of both sides of the equation makes linear estimation possible. The above transformation follows from two logarithm rules that we apply here:
1. the product rule states that $log(xy)=log(x)+log(y)$; thus, when taking the logarithm of the right hand side of the multiplicative model, we can write $log(X_1) + log(X_2)... log(X_J)$ instead of $log(X_1*X_2*...X_J)$, and
2. the power rule states that $log(x^y) = ylog(x)$; thus, we can write $\beta*log(X)$ instead of $X^{\beta}$
Let's test how the scatterplot would look like if we use the logarithm of our variables using the ```log()``` function instead of the original values.
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE}
ggplot(regression, mapping = aes(log(price_ounce), log(move_ounce))) +
geom_point(shape = 1) +
geom_smooth(method = "lm", color = "lavenderblush4", fill = "red", alpha = 0.1) +
labs(x = "Price", y = "Sales") +
theme_minimal()
```
You can see how the scales changed, and how the observations got more normally distributed. Hence, we can log-transform our variables and estimate the following equation:
\begin{equation}
log(sales) = log(\beta_0) + \beta_1*log(price) + log(\epsilon)
(\#eq:multiplicativetransformed1)
\end{equation}
Now, let's estimate a new regression by applying ```log()``` function to both sales and prices:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE,paged.print = FALSE}
sales_reg2 <- lm(log(move_ounce) ~ log(price_ounce), data = regression)
summary(sales_reg2) #remember that now the interpretation changed
```
In this example, you would interpret the coefficient as follows: **A 1% increase in price leads to a 2.23% decrease in sales**. Hence, the interpretation is in proportional terms and no longer in units. This means that the coefficients in a log-log model can be directly interpreted as elasticities, which also makes communication easier. We can generally also inspect the R<sup>2</sup> statistic to see that the model fit has increased compared to the linear specification (i.e., R<sup>2</sup> has increased to 0.08 from 0.06). However, please note that the variables are now measured on a different scale, which means that the model fit in theory is not directly comparable.
#### Multiple linear regression
Multiple linear regression is a statistical technique that simultaneously tests the relationships between two or more independent variables and an interval-scaled dependent variable. The general form of the equation is given by:
\begin{equation}
Y=(\beta_0+\beta_1*X_1+\beta_2*X_2+\beta_n*X_n)+\epsilon
(\#eq:regequ)
\end{equation}
Again, we aim to find the combination of predictors that correlate maximally with the outcome variable. Note that if you change the composition of predictors, the partial regression coefficient of an independent variable will be different from that of the bivariate regression coefficient. This is because the regressors are usually correlated, and any variation in Y that was shared by X1 and X2 was attributed to X1. The interpretation of the partial regression coefficients is the expected change in Y when X is changed by one unit and all other predictors are held constant.
Let's extend the previous example. Say, in addition to the influence of price itself, you are interested in estimating the influence of sales promotion on the amount of Budweiser. The corresponding equation, including bonus buy, would then be given by:
$$ Sales=\beta_0+\beta_1*price+\beta_2*bonus\_buy+\epsilon$$
β<sub>1</sub> and β<sub>2</sub> represent the unknown relationship between sales and independent variables (price and bonus buy, respectively). The corresponding coefficients tell you by how much sales will change for an additional dollar increase of price (when the other IVs are held constant) and by how much sales will change for an additional unit of price reduction (when price is held constant), etc. Thus, we can make predictions about sales using all these variables.
With several predictors, the partitioning of sum of squares is the same as in the bivariate model, except that the model is no longer a 2-D straight line. With two predictors, the regression line becomes a 3-D regression plane. While multiple regression models that have more than two predictors are not as easy to visualize, you may apply the same principles when interpreting the model outcome:
* Total sum of squares (SS<sub>T</sub>) is still the difference between the observed data and the mean value of Y (total variation)
* Residual sum of squares (SS<sub>R</sub>) is still the difference between the observed data and the values predicted by the model (unexplained variation)
* Model sum of squares (SS<sub>M</sub>) is still the difference between the values predicted by the model and the mean value of Y (explained variation)
* R measures the multiple correlation between the predictors and the outcome
* R<sup>2</sup> is the amount of variation in the outcome variable explained by the model
Estimating multiple regression models is straightforward using the ```lm()``` function. You just need to separate the individual predictors on the right hand side of the equation using the ```+``` symbol. In addition, as discussed before, we would need to use log-log transformation for our use case, which can be done in multiple regression context as well. Hence, we would specify the model as follows (note that bonus buy is already percentage in our data set, i.e., 0.2 value of price reduction is translated as 20% price decrease; we won't take a logarithm of it but rather interpret the results differently):
$$ log(Sales) =log(\beta_0) + \beta_1*log(Price) + \beta_2*bonus\_buy + log(\epsilon) $$
This regression could be estimated as follows:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
multiple_sales_reg <- lm(log(move_ounce) ~ log(price_ounce) + sale_B, data = regression) # estimate the model
summary(multiple_sales_reg) #summary of results
```
The interpretation of the coefficients is as follows:
* price (β<sub>1</sub>): when price increases by 1%, sales will change by `r round(summary(multiple_sales_reg)$coefficients[2],3)`%
* bonus buy (β<sub>2</sub>): when bonus buy increases by 1% (which is one step on a scale from 0 to 100 for a discount, i.e., this is equal to 1-unit increase), sales will change by `r round(summary(multiple_sales_reg)$coefficients[3],3)*100`%
The associated t-values and p-values are also given in the output. You can see that the p-values are smaller than 0.05 for price, while bonus sale is insignificant. Moreover, the p-value for F-test is smaller than 0.05. This means that if the null hypothesis was true (i.e., there was no effect between the variables and sales), the probability of observing associations of the estimated magnitudes (or larger) is very small (e.g., smaller than 0.05).
Again, to get a better feeling for the range of values that the coefficients could take, it is helpful to compute <b>confidence intervals</b>.
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
confint(multiple_sales_reg)
```
What does this tell you? Recall that a 95% confidence interval is defined as a range of values such that with a 95% probability, the range will contain the true unknown value of the parameter. For example, for β<sub>1</sub>, the confidence interval is [`r confint(multiple_sales_reg)[2,1]`,`r confint(multiple_sales_reg)[2,2]`]. Thus, although we have computed a point estimate of `r round(summary(multiple_sales_reg)$coefficients[2],3)` for the effect of price on sales based on our sample, the effect might actually just as well take any other value within this range, considering the sample size and the variability in our data. You could also visualize the output from your regression model including the confidence intervals using the `ggstatsplot` package as follows:
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE,fig.align="center", fig.cap = "Confidence intervals for regression model"}
library(ggstatsplot)
ggcoefstats(x = multiple_sales_reg,
title = "Sales predicted by price, bonus buy, and price reduction")
```
The output also tells us that `r summary(multiple_sales_reg)$r.squared*100`% of the variation can be explained by our model. You may also visually inspect the fit of the model by plotting the predicted values against the observed values. We can extract the predicted values using the ```predict()``` function. So let's create a new variable ```yhat```, which contains those predicted values.
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE}
regression$logmove_ounce_hat <- fitted(multiple_sales_reg)
```
We can now use this variable to plot the predicted values against the observed values. In the following plot, the model fit would be perfect if all points would fall on the diagonal line. The larger the distance between the points and the line, the worse the model fit. In other words, if all points would fall exactly on the diagonal line, the model would perfectly predict the observed values.
```{r message=FALSE, warning=FALSE, eval=TRUE, echo=TRUE, fig.align="center", fig.cap = "Model fit"}
ggplot(data = regression, aes(week, log(move_ounce))) +
geom_vline(xintercept = regression$promoweek, colour = "lightgrey") +
geom_line(aes(y = log(move_ounce), colour = "logsales"), size = 0.5) +
geom_line(aes(y = (logmove_ounce_hat), colour = "logsales (predicted)"), size = 0.5) +
scale_color_manual(values = c("black", "gold")) +
theme_minimal()
```
**Partial plots**
In the context of a simple linear regression (i.e., with a single independent variable), a scatter plot of the dependent variable against the independent variable provides a good indication of the nature of the relationship. If there is more than one independent variable, however, things become more complicated. The reason is that although the scatter plot still show the relationship between the two variables, it does not take into account the effect of the other independent variables in the model. Partial regression plot show the effect of adding another variable to a model that already controls for the remaining variables in the model. In other words, it is a scatterplot of the residuals of the outcome variable and each predictor when both variables are regressed separately on the remaining predictors. In our example, the partial plot would show the effect of adding price as an explanatory variables while controlling for the variation that is explained by sales promotions in both variables (sales and price). Think of it as the purified relationship between price and sales that remains after controlling for other factors. The partial plots can easily be created using the ```avPlots()``` function from the ```car``` package:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE,fig.align="center", fig.cap = "Partial plots"}
library(car)
avPlots(multiple_sales_reg)
```
### Categorical predictors
#### Two categories
```{r}
categories <- read.table("https://raw.githubusercontent.com/WU-RDS/RMA2024/main/data/beer_categorical",
sep = ",",
header = TRUE)
categories$store <- as.factor(categories$store)
categories$brand <- as.factor(categories$brand)
str(categories)
```
We will use a slightly different data set to explore additional opportunities for regression analysis. Suppose, you wish to investigate the effect of the variable "store" on sales, which is a categorical variable that can only take two levels (i.e., 98 = store with ID 98, and 100 = store with ID 100). Categorical variables with two levels are also called binary predictors; in our example, however, they are not decoded into typical binary view (i.e., they are not 0 and 1). It is straightforward to include these variables in your model as "dummy" variables. Dummy variables are factor variables that can only take two values. For our "store" variable, we can create a new predictor variable that takes the form:
\begin{equation}
x_4 =
\begin{cases}
0 & \quad \text{if } i \text{th observation comes from store 98}\\
1 & \quad \text{if } i \text{th observation comes from store 100}
\end{cases}
(\#eq:dummycoding)
\end{equation}
This new variable is then added to our regression equation from before, so that the equation becomes
\begin{align}
Sales =\beta_0 &+\beta_1*price\\
&+\beta_2*bonus\_buy\\
&+\beta_3*store+\epsilon
\end{align}
where "store" represents the new dummy variable and is the coefficient associated with this variable. Estimating the model is straightforward - you just need to include the variable as an additional predictor variable. Note that the variable needs to be specified as a factor variable before including it in your model. If you haven't converted it to a factor variable before, you could also use the wrapper function ```as.factor()``` within the equation.
First, let's reestimate the regression we had before (note that the result slightly changes because we are using a different data set - you can recall that with different samples, the estimation of true value changes). For the sake of easier interpretation, we use regular regression specification (i.e., not log-log transformed).
```{r}
multiple_regression_new <- lm(move_ounce ~ price_ounce + sale_B, data = categories)
summary(multiple_regression_new)
```
Now, let's add the store variable:
```{r}
multiple_regression_store <- lm(move_ounce ~ price_ounce + sale_B + store, data = categories)
summary(multiple_regression_store)
```
You can see that we now have an additional coefficient in the regression output, which tells us the effect of the dummy predictor. The dummy variable can generally be interpreted as the average difference in the dependent variable between the two groups, conditional on the other variables you have included in your model. In this case, the coefficient tells you the difference in sales between store 98 and 100 artists, and whether this difference is significant. Specifically, it means that sales in store 100 are on average 1,724.92 oz higher than in store 98, and this difference is significant (i.e., p < 0.05).
#### More than two categories
Predictors with more than two categories, like our "brand"" variable, can also be included in your model. However, in this case one dummy variable cannot represent all possible values, since there are many brands (i.e., 1 = Amstel, 2 = Budweiser, 3 = Corona, 4 = Fosters, 5 = Heineken, 6 = Old Milwaukee). Thus, we need to create additional dummy variables. For example, for our "brand" variable, we create five dummy variables as follows:
\begin{equation}
x_5 =
\begin{cases}
1 & \quad \text{if } i \text{th product is Budweiser}\\
0 & \quad \text{if } i \text{th product is Amstel}
\end{cases}
(\#eq:dummycoding1)
\end{equation}
\begin{equation}
x_6 =
\begin{cases}
1 & \quad \text{if } i \text{th product is Corona}\\
0 & \quad \text{if } i \text{th product is Amstel}
\end{cases}
(\#eq:dummycoding2)
\end{equation}
and so on.
We would then add these variables as additional predictors in the regression equation and obtain the following model
\begin{align}
Sales =\beta_0 &+\beta_1*price\\
&+\beta_2*bonus\_buy\\
&+\beta_3*store\\
&+\beta_4*Budweiser\\
&+\beta_5*Corona\\
&+\beta_6*Fosters\\
&+\beta_7*Heineken\\
&+\beta_8*Old\_Milwaukee+\epsilon
\end{align}
where "Budweiser", "Corona", "Fosters", "Heineken", and "Old Milwaukee" represent our new dummy variables, and refer to the associated regression coefficients. You don't have to create the dummy variables manually as R will do this automatically when you add the variable to your equation.
The interpretation of the coefficients is as follows: $\beta_5$ is the difference in average sales between the brands "Amstel" and "Budweiser", $\beta_6$ is the difference in average sales between the brands "Amstel" and "Corona", and so on. Note that the level for which no dummy variable is created is also referred to as the *baseline*. In our case, "Amstel" would be the baseline brand. This means that there will always be one fewer dummy variable than the number of levels.
```{r}
multiple_regression_ext <- lm(move_ounce ~ price_ounce + sale_B + store + brand, data = categories)
summary(multiple_regression_ext)
```
How can we interpret the coefficients? It is estimated based on our model that products from the "Budweiser" brand will on average sell 22,298.63 oz more than products from the "Amstel" brand, and that products from the "Corona" brand will sell on average 2,288.88 oz more than the products from the "Amstel" brand, etc. The p-value of both these and some other brand-variables is smaller than 0.05, suggesting that there is statistical evidence for a real difference in sales between the brands
The level of the baseline category is arbitrary. As you have seen, R simply selects the first level as the baseline. If you would like to use a different baseline category, you can use the ```relevel()``` function and set the reference category using the ```ref``` argument. The following would estimate the same model using the second category as the baseline:
```{r}
multiple_regression_ext <- lm(move_ounce ~ price_ounce + sale_B + store + relevel(brand, ref = 2), data = categories)
summary(multiple_regression_ext)
```
Note that while your choice of the baseline category impacts the coefficients and the significance level, the prediction for each group will be the same regardless of this choice.
#### <span style="color: purple;">Non-linear relationships (extra)</span>
##### Multiplicative model
In many practical applications, linear relationship might not be the case. Let's review the implications of a linear specification again:
* Constant marginal returns (e.g., an increase in ad-spend from 10€ to 11€ yields the same increase in sales as an increase from 100,000€ to 100,001€)
* Elasticities increase with X (e.g., advertising becomes relatively more effective; i.e., a relatively smaller change in advertising expenditure will yield the same return)
In many marketing contexts, these might not be reasonable assumptions. Consider the case of advertising. It is unlikely that the return on advertising will not depend on the level of advertising expenditures. It is rather likely that saturation occurs at some level, meaning that the return from an additional Euro spend on advertising is decreasing with the level of advertising expenditures (i.e., decreasing marginal returns). In other words, at some point the advertising campaign has achieved a certain level of penetration and an additional Euro spend on advertising won't yield the same return as in the beginning.
Let's use an example data set, containing the advertising expenditures of a company and the sales (in thousand units).
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
non_linear_reg <- read.table("https://raw.githubusercontent.com/IMSMWU/Teaching/master/MRDA2017/non_linear.dat",
sep = "\t",
header = TRUE) #read in data
head(non_linear_reg)
```
Now we inspect if a linear specification is appropriate by looking at the scatterplot:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE,fig.align="center", fig.cap = "Non-linear relationship"}
ggplot(data = non_linear_reg, aes(x = advertising, y = sales)) +
geom_point(shape=1) +
geom_smooth(method = "lm", fill = "blue", alpha=0.1) +
theme_bw()
```
It appears that a linear model might **not** represent the data well. It rather appears that the effect of an additional Euro spend on advertising is decreasing with increasing levels of advertising expenditures. Thus, we have decreasing marginal returns. We could put this to a test and estimate a linear model:
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE}
linear_reg <- lm(sales ~ advertising, data = non_linear_reg)
summary(linear_reg)
```
Advertising appears to be positively related to sales with an additional Euro that is spent on advertising resulting in 0.0005 additional sales. The R<sup>2</sup> statistic suggests that approximately 51% of the total variation can be explained by the model
To test if the linear specification is appropriate, let's inspect some of the plots that are generated by R. We start by inspecting the residuals plot.
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE,fig.align="center", fig.cap = "Residuals vs. Fitted"}
plot(linear_reg,1)
```
The plot suggests that the assumption of homoscedasticity is violated (i.e., the spread of values on the y-axis is different for different levels of the fitted values). In addition, the red line deviates from the dashed grey line, suggesting that the relationship might not be linear. Finally, the Q-Q plot of the residuals suggests that the residuals are not normally distributed.
```{r message=FALSE, warning=FALSE, echo=TRUE, eval=TRUE,fig.align="center", fig.cap = "Q-Q plot"}
plot(linear_reg,2)
```
To sum up, a linear specification might not be the best model for this data set.
In this case, a multiplicative model might be a better representation of the data. The multiplicative model has the following formal representation:
\begin{equation}
Y =\beta_0 *X_1^{\beta_1}*X_2^{\beta_2}*...*X_J^{\beta_J}*\epsilon
(\#eq:multiplicative)
\end{equation}
This functional form can be linearized by taking the logarithm of both sides of the equation: