Simple Linear Regression: # Loading Data
Simple Linear Regression: # Loading Data
Soumya Roy
# Loading Data
d<-read.csv("C:/Users/Soumya-
Roy/Desktop/DAR_20/Session_4/Advertising.csv",header=T)
# Scatter Plots
# For having three scatter plots in a single row
par(mfrow=c(1,3))
# Sales vs TV
plot(TV,Sales,col="red",lwd=2)
# Sales vs Radio
plot(Radio,Sales,col="red",lwd=2)
# Sales vs Newspaper
plot(Newspaper,Sales,col="red",lwd=2)
# Fitting the Simple Linear Regression Models
# Model 1: Sales on TV
mod_1=lm(Sales~TV,data=d)
summary(mod_1)
##
## Call:
## lm(formula = Sales ~ TV, data = d)
##
## 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
# Model 2: Sales on Radio
mod_2=lm(Sales~Radio,data=d)
summary(mod_2)
##
## Call:
## lm(formula = Sales ~ Radio, data = d)
##
## 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
##
## Call:
## lm(formula = Sales ~ Newspaper, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.2272 -3.3873 -0.8392 3.5059 12.7751
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.35141 0.62142 19.88 < 2e-16 ***
## Newspaper 0.05469 0.01658 3.30 0.00115 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.092 on 198 degrees of freedom
## Multiple R-squared: 0.05212, Adjusted R-squared: 0.04733
## F-statistic: 10.89 on 1 and 198 DF, p-value: 0.001148
confint(mod_2)
## 2.5 % 97.5 %
## (Intercept) 8.2015885 10.4216877
## Radio 0.1622443 0.2427472
confint(mod_3)
## 2.5 % 97.5 %
## (Intercept) 11.12595560 13.57685854
## Newspaper 0.02200549 0.08738071
# Sales vs Radio
plot(Radio,Sales,col="red",lwd=2)
abline(mod_2,lwd=3,col="blue")
# Sales vs Newspaper
plot(Newspaper,Sales,col="red",lwd=2)
abline(mod_3,lwd=3,col="blue")