- We analyze the impact of advertising budgets on sales.
- Data includes advertising spending on TV, Radio, and Newspaper.
2024-06-05
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 6)
# load libraries library(knitr) library(ggplot2) library(plotly)
## ## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2': ## ## last_plot
## The following object is masked from 'package:stats': ## ## filter
## The following object is masked from 'package:graphics': ## ## layout
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 6, out.width = "100%", out.height = "100%")
# load dataset
data <- read.csv('https://www.statlearning.com/s/Advertising.csv')
head(data)
## X TV radio newspaper sales ## 1 1 230.1 37.8 69.2 22.1 ## 2 2 44.5 39.3 45.1 10.4 ## 3 3 17.2 45.9 69.3 9.3 ## 4 4 151.5 41.3 58.5 18.5 ## 5 5 180.8 10.8 58.4 12.9 ## 6 6 8.7 48.9 75.0 7.2
# scatter plots
ggplot(data, aes(x=TV, y=sales)) + geom_point() + ggtitle("sales vs TV advertising") + theme_minimal()
ggplot(data, aes(x=radio, y=sales)) + geom_point() + ggtitle("sales vs radio Advertising") + theme_minimal()
ggplot(data, aes(x=newspaper, y=sales)) + geom_point() + ggtitle("sales vs newspaper Advertising") + theme_minimal()
# linear model model <- lm(sales ~ TV, data=data) summary(model)
## ## Call: ## lm(formula = sales ~ TV, data = data) ## ## Residuals: ## Min 1Q Median 3Q Max ## -8.3860 -1.9545 -0.1913 2.0671 7.2124 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 7.032594 0.457843 15.36 <2e-16 *** ## TV 0.047537 0.002691 17.67 <2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 3.259 on 198 degrees of freedom ## Multiple R-squared: 0.6119, Adjusted R-squared: 0.6099 ## F-statistic: 312.1 on 1 and 198 DF, p-value: < 2.2e-16
# regression line
p <- ggplot(data, aes(x=TV, y=sales)) + geom_point() + geom_smooth(method="lm", col="blue") + ggtitle("Linear Regression: sales vs TV") +theme_minimal()
ggplotly(p) %>% layout(autosize = TRUE)
## `geom_smooth()` using formula = 'y ~ x'
\[ \hat{Y} = \beta_0 + \beta_1 \cdot X \] - Intercept (\(\beta_0\)): 7.0326 - Slope (\(\beta_1\)): 0.0475
p <- ggplot(data, aes(x=TV, y=sales)) + geom_point() + geom_smooth(method="lm", col="blue") + ggtitle("linear regression: sales vs TV") + theme_minimal()
ggplotly(p) %>% layout(autosize = TRUE)
## `geom_smooth()` using formula = 'y ~ x'