Simple Linear Regression

library(e1071)
## Warning: package 'e1071' was built under R version 3.5.1
Delivery_time <- read.csv("E:\\Data Science\\data science\\assignments\\Simple Linear Regression\\delivery_time.csv")

colnames(Delivery_time) <- c("DeliveryTime", "SortingTime")
attach(Delivery_time)

#First Moment Business Decision
summary(Delivery_time)
##   DeliveryTime    SortingTime   
##  Min.   : 8.00   Min.   : 2.00  
##  1st Qu.:13.50   1st Qu.: 4.00  
##  Median :17.83   Median : 6.00  
##  Mean   :16.79   Mean   : 6.19  
##  3rd Qu.:19.75   3rd Qu.: 8.00  
##  Max.   :29.00   Max.   :10.00
# Second Moment Business Decision
var(DeliveryTime)
## [1] 25.75462
var(SortingTime)
## [1] 6.461905
sd(DeliveryTime)
## [1] 5.074901
sd(SortingTime)
## [1] 2.542028
#Third Moment Business Decision
skewness(SortingTime)
## [1] 0.04059837
skewness(DeliveryTime)
## [1] 0.3036468
#Foruth Moment BUsiness Decision
kurtosis(SortingTime)
## [1] -1.335955
kurtosis(DeliveryTime)
## [1] -0.3021186
plot(SortingTime, DeliveryTime, col = "Blue")

# Correlation coefficient
cor(SortingTime, DeliveryTime)
## [1] 0.8259973
model1 <- lm(DeliveryTime~SortingTime)

summary(model1)
## 
## Call:
## lm(formula = DeliveryTime ~ SortingTime)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1729 -2.0298 -0.0298  0.8741  6.6722 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   6.5827     1.7217   3.823  0.00115 ** 
## SortingTime   1.6490     0.2582   6.387 3.98e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.935 on 19 degrees of freedom
## Multiple R-squared:  0.6823, Adjusted R-squared:  0.6655 
## F-statistic:  40.8 on 1 and 19 DF,  p-value: 3.983e-06
confint(model1, level = 0.95)
##                2.5 %    97.5 %
## (Intercept) 2.979134 10.186334
## SortingTime 1.108673  2.189367
predict(model1, interval = "predict")
## Warning in predict.lm(model1, interval = "predict"): predictions on current data refer to _future_ responses
##          fit       lwr      upr
## 1  23.072933 16.457161 29.68870
## 2  13.178814  6.780993 19.57663
## 3  16.476853 10.188630 22.76508
## 4  21.423913 14.955850 27.89198
## 5  23.072933 16.457161 29.68870
## 6  16.476853 10.188630 22.76508
## 7  18.125873 11.823294 24.42845
## 8  11.529794  5.010345 18.04924
## 9  23.072933 16.457161 29.68870
## 10 21.423913 14.955850 27.89198
## 11 19.774893 13.411938 26.13785
## 12 13.178814  6.780993 19.57663
## 13 18.125873 11.823294 24.42845
## 14 11.529794  5.010345 18.04924
## 15 11.529794  5.010345 18.04924
## 16 13.178814  6.780993 19.57663
## 17 16.476853 10.188630 22.76508
## 18 18.125873 11.823294 24.42845
## 19  9.880774  3.198090 16.56346
## 20 18.125873 11.823294 24.42845
## 21 14.827833  8.507631 21.14804
# R-squared value for the above model is 0.6823.
# Residual standard error is 2.935 on 19 degrees of freedom.
# Applying different transformations.

model2 <- lm(log(DeliveryTime)~log(SortingTime))
summary(model2)
## 
## Call:
## lm(formula = log(DeliveryTime) ~ log(SortingTime))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.23303 -0.09050 -0.00825  0.08897  0.36439 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       1.74199    0.13312  13.086 5.92e-11 ***
## log(SortingTime)  0.59752    0.07446   8.024 1.60e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1558 on 19 degrees of freedom
## Multiple R-squared:  0.7722, Adjusted R-squared:  0.7602 
## F-statistic: 64.39 on 1 and 19 DF,  p-value: 1.602e-07
confint(model2, level = 0.95)
##                      2.5 %    97.5 %
## (Intercept)      1.4633576 2.0206166
## log(SortingTime) 0.4416707 0.7533739
predict(model2, interval = "predict")
## Warning in predict.lm(model2, interval = "predict"): predictions on current data refer to _future_ responses
##         fit      lwr      upr
## 1  3.117833 2.772199 3.463468
## 2  2.570329 2.232244 2.908413
## 3  2.812603 2.478606 3.146601
## 4  3.054878 2.713126 3.396630
## 5  3.117833 2.772199 3.463468
## 6  2.812603 2.478606 3.146601
## 7  2.904712 2.569144 3.240279
## 8  2.398432 2.050448 2.746417
## 9  3.117833 2.772199 3.463468
## 10 3.054878 2.713126 3.396630
## 11 2.984500 2.646196 3.322803
## 12 2.570329 2.232244 2.908413
## 13 2.904712 2.569144 3.240279
## 14 2.398432 2.050448 2.746417
## 15 2.398432 2.050448 2.746417
## 16 2.570329 2.232244 2.908413
## 17 2.812603 2.478606 3.146601
## 18 2.904712 2.569144 3.240279
## 19 2.156158 1.785357 2.526959
## 20 2.904712 2.569144 3.240279
## 21 2.703662 2.369295 3.038029
# R-squared value for the above model is 0.7722.
# Residual standard error is reduced to 0.1558 on 19 degrees of freedom.