library(datarium)
View(marketing)
x<-marketing[,c("youtube","sales")]
View(x)
One of the variable is independent and other is dependent variable.So Check whether the data is linear or not # Check whether the data is linear or not
library(ggplot2)
ggplot(x,aes(youtube,sales))+geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess'
As you can see approximately variables are linearly related, So we can use Simple Linear Regression model.
model<-lm(sales ~ youtube,data = x)
model
##
## Call:
## lm(formula = sales ~ youtube, data = x)
##
## Coefficients:
## (Intercept) youtube
## 8.43911 0.04754
y<-model$fitted.values
errors<-model$residuals
summary(model)
##
## Call:
## lm(formula = sales ~ youtube, data = x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.0632 -2.3454 -0.2295 2.4805 8.6548
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.439112 0.549412 15.36 <2e-16 ***
## youtube 0.047537 0.002691 17.67 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.91 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
Coefficients of this model are significant and so you can use the model
R square is the value that tells us ,how much varience of dependent variable is being explained by independent variable. If R SQUARE is close to 1,that tells us model is better one. From above summary we can say R Square value=0.6 which is near to 1 and hence this model is better.
Accuracy is How well the model is predicting the Outcome.
library(DMwR)
## Loading required package: lattice
## Loading required package: grid
regr.eval(x$sales,model$fitted.values)
## mae mse rmse mape
## 3.059767 15.138220 3.890787 0.205766
For lesser data we can use “mape” as accuracy measure.Here it represents 20.5% as errors
Hence,Accuracy of this model is 79.5%
plot(model)
In Residuals vs Fitted plot ,red line is drawn along the dotted line and all fitted values scattered around it without any systematic relationship then linearity assumption is met on the residuals.
Statistical tests are used to check the normality of residuals Shapiro wilk Test,Anderson Darling Test
NUll Hypothesis :data is normally distributed Alternate Hypothesis :data is not normally distributed
shapiro.test(model$residuals)
##
## Shapiro-Wilk normality test
##
## data: model$residuals
## W = 0.99053, p-value = 0.2133
As p-value is > 0.05 we can accept null hypothesis.Hence Residual data is normally distributed.
Similarly Anderson Darling Test
library(nortest)
ad.test(model$residuals)
##
## Anderson-Darling normality test
##
## data: model$residuals
## A = 0.49121, p-value = 0.217
Checking for constant error rate
plot(model)
To check the correlation between errors we use Durbin Watson Test Null Hypothesis : No correlation between errors Alternate Hypothesis: correlation between errors
library(car)
## Loading required package: carData
durbinWatsonTest(model)
## lag Autocorrelation D-W Statistic p-value
## 1 0.02342385 1.934689 0.612
## Alternative hypothesis: rho != 0
As p-value >0.05 we can accept the null hypothesis,so there is no correlation between errors
boxplot(model$residuals)
youtube=2000
new_data<- data.frame(youtube)
pred_sales<-predict(model,newdata = new_data)
pred_sales
## 1
## 103.5124