For your assignment you may be using different dataset than what is included here.
Always read carefully the instructions on Sakai.
Tasks/questions to be completed/answered are highlighted in larger bolded fonts and numbered according to their section.
In a given year, if it rains more, we may see that there might be an increase in crop production. This is because more water may lead to more plants.
This is a direct relationship; the number of fruits may be able to be predicted by amount of waterfall in a certain year.
This example represents simple linear regression, which is an extremely useful concept that allows us to predict values of a certain variable based off another variable.
This lab will explore the concepts of simple linear regression, multiple linear regression, and watson analytics.
We are going to use tidyverse a collection of R packages designed for data science.
## Loading required package: tidyverse
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1 ✔ purrr 0.2.4
## ✔ tibble 1.4.2 ✔ dplyr 0.7.4
## ✔ tidyr 0.7.2 ✔ stringr 1.2.0
## ✔ readr 1.1.1 ✔ forcats 0.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## Loading required package: 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
Name your dataset ‘mydata’ so it easy to work with.
Commands: read_csv() rename() head()
mydata = read.csv(file="data/Advertising.csv")
head(mydata)
## 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
mydata <- rename(mydata, "case_number" = "X")
sales <- mydata$sales
newspaper <- mydata$newspaper
radio <- mydata$radio
TV <- mydata$TV
case_number <- mydata$case_number
head(mydata)
## case_number 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
Sales is the dependent variable. Where as TV Radio and Newspaper are independent.
#corr = cor(mydata)
#corr
corr = cor(mydata[ -c(1,1)])
corr
## TV radio newspaper sales
## TV 1.00000000 0.05480866 0.05664787 0.7822244
## radio 0.05480866 1.00000000 0.35410375 0.5762226
## newspaper 0.05664787 0.35410375 1.00000000 0.2282990
## sales 0.78222442 0.57622257 0.22829903 1.0000000
The value 1.0 goes down the matrix diagonally because it is the correlation between the same variable. The strongest correlation is between sales and TV at 78%.
qplot( x = radio, y = sales, data = mydata)
Two variables that have a relationship between .2 and .6 are radio and sales. Their correlation is 57.6%
#Simple Linear Regression Model
#reg <- lm( DEPENDENT_VARIABLE ~ INDEPENDENT_VARIABLE )
reg <- lm( sales ~ radio, data = mydata)
reg
##
## Call:
## lm(formula = sales ~ radio, data = mydata)
##
## Coefficients:
## (Intercept) radio
## 9.3116 0.2025
#summary(MODEL)
summary(reg)
##
## Call:
## lm(formula = sales ~ radio, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.7305 -2.1324 0.7707 2.7775 8.1810
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.31164 0.56290 16.542 <2e-16 ***
## radio 0.20250 0.02041 9.921 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.275 on 198 degrees of freedom
## Multiple R-squared: 0.332, Adjusted R-squared: 0.3287
## F-statistic: 98.42 on 1 and 198 DF, p-value: < 2.2e-16
The r-squared is .332 and adjusted is .329 These values are both below .5 so they are not significant to analyze. This, however, does not indicate that this line isn’t a great fit. Anything above .75 would be better. The r-squared and the adjusted are fairly equal. 33% of the time you can predict the impact of radio ads to sales. R squareds below .5 indicate a line is a bad fit.
#p <- qplot( x = INDEPENDENT_VARIABLE, y = DEPENDENT_VARIABLE, data = mydata) + geom_point()
p <- qplot( x = mydata$radio, y = mydata$sales, data = mydata) + geom_point()
p + geom_smooth(method = "lm", formula = y ~ x)
The pattern is that the more radio advertising there is, the higher the sales amount. This line has a positive slope.
The r squared and the adjusted r squared are .897 and .896. This is a great fit for our data because it is over .75. This is a better fit than the last model.
mlrm_sales2 <- lm( mydata$sales ~ mydata$radio + mydata$TV + mydata$newspaper)
mlrm_sales2
##
## Call:
## lm(formula = mydata$sales ~ mydata$radio + mydata$TV + mydata$newspaper)
##
## Coefficients:
## (Intercept) mydata$radio mydata$TV mydata$newspaper
## 2.938889 0.188530 0.045765 -0.001037
summary(mlrm_sales2)
##
## Call:
## lm(formula = mydata$sales ~ mydata$radio + mydata$TV + mydata$newspaper)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.8277 -0.8908 0.2418 1.1893 2.8292
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.938889 0.311908 9.422 <2e-16 ***
## mydata$radio 0.188530 0.008611 21.893 <2e-16 ***
## mydata$TV 0.045765 0.001395 32.809 <2e-16 ***
## mydata$newspaper -0.001037 0.005871 -0.177 0.86
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.686 on 196 degrees of freedom
## Multiple R-squared: 0.8972, Adjusted R-squared: 0.8956
## F-statistic: 570.3 on 3 and 196 DF, p-value: < 2.2e-16
The r squared is .897 and the adjusted r squared is .895. Both are great fits for the model with only slight variation.
MODEL 1
# lm( sales ~ radio )
radio = 69
predicted_sales_radio = 9.3116 + (0.20250) * (radio)
predicted_sales_radio
## [1] 23.2841
Through this model, 23.28 is the predicted sales value when using the radio data point of 69.
MODEL 2
# lm( sales ~ radio + TV )
radio = 69
TV = 255
predicted_sales_TVradio = 2.92110 + 0.18799 * (radio) + 0.04575 * (TV)
predicted_sales_TVradio
## [1] 27.55866
Through model 2, 27.56 is the predicted sales value when using both the TV and radio data to do a multiple linier regression.
MODEL 3
#lm( sales ~ radio + TV + newspaper)
radio = 69
TV = 255
newspaper = 75
predicted_sales_all = 2.938889 + 0.188530 * (radio) + 0.045765 * (TV) - 0.001037 * (newspaper)
predicted_sales_all
## [1] 27.53976
Through model 3’s analysis, the predicted sales utilizing newspaper, TV, and radio is 27.54.
To complete the last task, follow the directions found below. Make sure to screenshot and attach any pictures of the results obtained or any questions asked.
The advertising data has 91% quality according to Watson.
knitr::include_graphics('images/lab 05 watson 1.png')
The predictive strength of the variables according to Watson is 72%. TV is the strongest predictor, but both radio and TV combined are the best predictors for sales!
knitr::include_graphics('images/lab 05 watson 2.png')
Above is the decision tree that shows the same results as the decision rules.
The predicted value strength of TV and Radio is 20.75.
knitr::include_graphics('images/spiral.png')
Above is a spiral visualization with radio as the farthest predictor and TV and Radio combined as the best. This shows that sales do not just happen because you do one thing. It shows that the amount of sales driven by advertising is much more complicated than this = this. Consumer sales is complex and companies should use many forms of advertising to drive sales. Each customer and consumer is different and therefore there needs to be variety in how companies advertise in order to maximize sales.
The Watson analytics results 100% reconciles our findings based in the R regression analysis. Our analysis showed that TV was the strongest predictor. But we used radio to analyze in task 2. Radio’s analysis in Watson is seen below.
knitr::include_graphics('images/radio.png')
The predictive strength of radio is 32%.
Above, I wrote a quick analysis on why sales is not driven by just one variable. Sales is best driven by multiple advertising mediums because all consumers are different.