R Markdown

# Load the dataset
df <- read.csv(file.choose(), header = TRUE)
attach(df)
dim(df)
## [1] 200   4
# Summary statistics
summary(df)
##        TV             Radio          Newspaper          Sales      
##  Min.   :  0.70   Min.   : 0.000   Min.   :  0.30   Min.   : 1.60  
##  1st Qu.: 74.38   1st Qu.: 9.975   1st Qu.: 12.75   1st Qu.:11.00  
##  Median :149.75   Median :22.900   Median : 25.75   Median :16.00  
##  Mean   :147.04   Mean   :23.264   Mean   : 30.55   Mean   :15.13  
##  3rd Qu.:218.82   3rd Qu.:36.525   3rd Qu.: 45.10   3rd Qu.:19.05  
##  Max.   :296.40   Max.   :49.600   Max.   :114.00   Max.   :27.00
plot(TV, Sales, col = 'red', pch = 16)

hist(TV, border  = 'black', col= 'white')

hist(Sales, border  = 'black', col = 'white')

boxplot(TV, border = 'black', col='gray')

boxplot(Sales, border = 'black', col ='gray')

model <-lm(Sales~TV)

summary(model)
## 
## Call:
## lm(formula = Sales ~ TV)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.4438 -1.4857  0.0218  1.5042  5.6932 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 6.974821   0.322553   21.62   <2e-16 ***
## TV          0.055465   0.001896   29.26   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.296 on 198 degrees of freedom
## Multiple R-squared:  0.8122, Adjusted R-squared:  0.8112 
## F-statistic: 856.2 on 1 and 198 DF,  p-value: < 2.2e-16
plot(model)

boxplot(residuals(model))

confint(model)
##                  2.5 %     97.5 %
## (Intercept) 6.33874038 7.61090260
## TV          0.05172671 0.05920283
coef(model)
## (Intercept)          TV 
##  6.97482149  0.05546477