R Markdown

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:

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

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)

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")

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
hist(mydata$StdResid,
     xlab = "Standardized Residuals",
     ylab = "Frequency",
     main = "Histogram of Standardized Residuals",
     col = "yellow")

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

vif(fit)
## SocialMedia          TV  Influencer 
##    1.303863    1.308144    1.005554
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:

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)

#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.

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

**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:

Influencer Type - Macro:

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:

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.

Conclusion