Homework 3 Regression Rok Rojc
Source: https://www.kaggle.com/datasets/harrimansaragih/dummy-advertising-and-sales-data?resource=download
#install.packages("readxl")
library(readxl)
mydata <- read_xlsx("~/Desktop/DummyData .xlsx")
colnames(mydata) <- c("TV","Radio","SocialMedia","Influencer","Sales")
set.seed(1)
mydata <- mydata[sample(nrow(mydata), 300), ] #Random sample of 300 units
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata$SocialMedia <- as.numeric(mydata$SocialMedia)
mydata$Sales <- as.numeric(mydata$Sales)
mydata$TV <- as.numeric(mydata$TV)
mydata$ID <- seq(1,nrow(mydata)) #adding ID
mydata$Radio <- NULL #removing radio
mydata$Influencer <- factor(mydata$Influencer,
levels = c("Mega", "Macro"),
labels = c( "Mega", "Macro"))
mydata$SocialMedia <- round(mydata$SocialMedia, digits = 2)
mydata$Sales <- round(mydata$Sales, digits = 2)
library(tidyr)
mydata <- drop_na(mydata)
head(mydata,8)
## # A tibble: 8 × 5
## TV SocialMedia Influencer Sales ID
## <dbl> <dbl> <fct> <dbl> <int>
## 1 44 4.18 Mega 158. 1
## 2 79 7.24 Macro 285. 2
## 3 18 1.06 Macro 56.8 3
## 4 15 2.61 Macro 61.4 4
## 5 60 0.18 Mega 214. 5
## 6 72 0.24 Mega 254. 6
## 7 40 2.71 Macro 148. 7
## 8 61 4.63 Mega 216. 8
Description of variables:
TV promotion budget (in million)
Social Media promotion budget (in million)
Influencer: Whether the promotion collaborate with Mega or Macro
size Influencer.
Sales (in million)
summary(mydata[,c("Sales","TV","SocialMedia","Influencer")])
## Sales TV SocialMedia Influencer
## Min. : 34.06 Min. : 10.00 Min. : 0.040 Mega :159
## 1st Qu.:116.28 1st Qu.: 33.00 1st Qu.: 1.827 Macro:139
## Median :187.96 Median : 53.00 Median : 3.285
## Mean :192.69 Mean : 54.18 Mean : 3.542
## 3rd Qu.:266.15 3rd Qu.: 74.75 3rd Qu.: 5.138
## Max. :364.08 Max. :100.00 Max. :11.530
- The average TV advertising expenditure is 54.11, with a minimum
spend of 10. Most campaigns (75%) have a budget below 74. In social
media advertising, the average spend is 3.542, with the smallest being
0.040 and the majority spending below 5.138. Regarding influencers, 158
campaigns used Mega influencers, while 136 used Macro influencers. Sales
data shows an average of 192.45, ranging from a minimum of 34.06 to a
maximum of 360.11.
How do TV and social media advertising expenditures, as well as the
size of influencer (Mega or Macro), impact sales performance?
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
scatterplotMatrix(mydata[ , c("Sales", "TV", "SocialMedia")],
smooth = FALSE)

- Based of the scatterplot, there is linearity between Sales, TV
advertising and Social Media.
fit <- lm(Sales ~ SocialMedia + TV + Influencer,
data = mydata)
mydata$StdResid <- round(rstandard(fit), 3)
mydata$CooksD <- round(cooks.distance(fit),3)
hist(mydata$StdResid,
xlab = "Standardized Residuals",
ylab = "Frequency",
main = "Histogram of Standardized Residuals",
col = "yellow")

- I see that there is at least one unit with value below -3, so I will
remove it.
mydata <- mydata %>%
filter(!mydata$ID %in% c(144,145))
head(mydata[order(mydata$StdResid),], 3) #Removing unit ID 144
## # A tibble: 3 × 7
## TV SocialMedia Influencer Sales ID StdResid CooksD
## <dbl> <dbl> <fct> <dbl> <int> <dbl> <dbl>
## 1 49 4.16 Mega 166. 109 -2.86 0.015
## 2 48 1.35 Mega 162. 234 -2.76 0.019
## 3 98 5.48 Mega 342. 61 -2.51 0.025
- I removed units ID 144 and 145, because it had a value below 3.
hist(mydata$StdResid,
xlab = "Standardized Residuals",
ylab = "Frequency",
main = "Histogram of Standardized Residuals",
col = "yellow")

- Histogram to show standradized residuals and their distribution with
removed units that had values below -3.
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.99679, p-value = 0.8202
H0: Normality is not violated
H1: Normality is violated
- Based on the sample data the p value of shapiro wilk test normality
is not violated, so I cannot reject H0 (p=0.6012)
vif(fit)
## SocialMedia TV Influencer
## 1.303863 1.308144 1.005554
- There are no issues with too strong multicolinearity, because VIF is
below 5 and average VIF is around 1.
hist(mydata$CooksD,
xlab = "Cooks Distance",
ylab = "Frequency",
main = "Histogram of Cooks Distance",
col = "purple")

head(mydata[order(-mydata$CooksD),c("ID", "CooksD")])
## # A tibble: 6 × 2
## ID CooksD
## <int> <dbl>
## 1 68 0.044
## 2 10 0.036
## 3 4 0.03
## 4 22 0.029
## 5 61 0.025
## 6 195 0.022
High impact units:
- All the units have Cooks distances below 1, but ID 68 and 10 are HIU
so I will remove them
mydata <- mydata %>%
filter(!mydata$ID %in% c(68,10))
head(mydata[order(-mydata$CooksD),c("ID", "CooksD")]) #removing HIU ID 68 AND ID 10
## # A tibble: 6 × 2
## ID CooksD
## <int> <dbl>
## 1 4 0.03
## 2 22 0.029
## 3 61 0.025
## 4 195 0.022
## 5 234 0.019
## 6 3 0.017
hist(mydata$CooksD,
xlab = "Cooks Distance",
ylab = "Frequency",
main = "Histogram of Cooks Distance",
col = "purple")

fit <- lm(Sales ~ SocialMedia + TV + Influencer,
data = mydata)
mydata$StdResid <- round(rstandard(fit),3)
mydata$StdFitted <- scale(fit$fitted.values)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)

- Based on scatterplot between standradized fitted values and
standradized residuals, homoscedasticity and linearity assumptions are
met.
#install.packages("olsrr")
library(olsrr)
##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
ols_test_breusch_pagan(fit)
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## ---------------------------------
## Response : Sales
## Variables: fitted values of Sales
##
## Test Summary
## ----------------------------
## DF = 1
## Chi2 = 0.0395438
## Prob > Chi2 = 0.8423752
H0: Variance is constant.
H1: Variance is not constant.
- Based on sample data we cannot reject H0 (pvalue = 0.705).
Homoscedasticity is not violated.
fit <- lm(Sales ~ SocialMedia + TV + Influencer,
data = mydata)
summary(fit)
##
## Call:
## lm(formula = Sales ~ SocialMedia + TV + Influencer, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.721 -1.909 0.064 2.094 8.286
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.341948 0.459971 -0.743 0.458
## SocialMedia 0.087976 0.090094 0.976 0.330
## TV 3.558101 0.007772 457.788 <2e-16 ***
## InfluencerMacro -0.104934 0.343068 -0.306 0.760
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.927 on 290 degrees of freedom
## Multiple R-squared: 0.9989, Adjusted R-squared: 0.9989
## F-statistic: 9.145e+04 on 3 and 290 DF, p-value: < 2.2e-16
Explanation of partial regression coefficients
Sales = -0.341948 + 0.087976SocialMedia + 3.558101TV -
0.104934InfluencerMacro
- Social Media and Influencer Macro are statistically insignificant,
because p-value>0.05 so I cannot explain them.
- If TV promotion budget increases by 1 million $, Sales on average
increases by 3.56 million $ (p<0.001), assuming all other variables
remain the same.
**SocialMedia Advertising Spend:*
Based on the statistical data, we cannot reject H0 at the given
p-value (p > 0.05), and I cannot prove that SocialMedia advertising
spend has an effect on sales.
TV Advertising Spend:
H0: β2 = 0 (TV advertising spend has no effect on sales)
H1: β2 ≠ 0 (TV advertising spend does have an effect on sales)
Based on the Standard Error (SE) and -value, we reject H0 at p <
0.001, which indicates that TV advertising spend has an effect on
sales.
Influencer Type - Macro:
H0: β3 = 0 (The type of influencer, Macro, has no effect on sales
compared to the baseline category)
H1: β3 ≠ 0 (The type of influencer, Macro, does have an effect on
sales compared to the baseline category)
Based on the statistical data, we fail to reject H0 at the given
p-value (p > 0.05), indicating that the type of influencer, when
categorized as Macro, does not have an effect on sales.
Coefficient of Determination:
- R-squared value: 99.89% of the variability in sales can be explained
by the linear effects of SocialMedia and TV advertising spend, and the
type of influencer (Macro).
F-statistics:
We reject H0 at p < 0.001, leading to the conclusion that at least
one of the predictor variables (SocialMedia spend, TV advertising spend,
and type of influencer) has an impact on sales. The extremely low
p-value associated with the F-statistic confirms that the model is
statistically significant.
library(car)
scatterplot(mydata$Sales ~ mydata$SocialMedia | mydata$Influencer,
smooth = FALSE,
boxplots = FALSE,
ylab = "Sales in Millions of $",
xlab = "Budget of Social Media in Millions of $")

The scatterplot I’m examining shows a positive correlation between
social media budget and sales, with a trend indicating that higher
social media spending is associated with increased sales. The data
points, differentiated by influencer type, suggest that the type of
influencer might impact this relationship, although the single
regression line does not distinguish between the effects of Mega and
Macro influencers.