STATISTICAL LEARNING

Welcome to my project!I am exploring three fascinating data analysis methods: multiple linear regression, clustering, and classification. Our goal is to comprehend the relationships between various variables, spot hidden patterns in the data, and create models to forecast and organize data points. To get insightful information and make wise choices, we’ll employ statistical techniques and algorithms. Join me on this adventure where we turn data into knowledge. It’s going to be fun and rewarding! The dataset chosen is the “Concrete_data”, it concerns a series of factors that influence the strength of the concrete. Among the variables we have Cement, Blast Furnace Slag, Fly_Ash, Water, Superplasticizer, Coarse_Aggregate and Fine_Aggregate; these are all expressed per kg in m^3. Then we have the age of the concrete expressed in days and finally the strength expressed in Megapascals.

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## âś” ggplot2 3.4.2     âś” purrr   1.0.1
## âś” tibble  3.2.1     âś” dplyr   1.1.2
## âś” tidyr   1.3.0     âś” stringr 1.5.0
## âś” readr   2.1.3     âś” forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag()    masks stats::lag()
library(caret)
## Caricamento del pacchetto richiesto: lattice
## 
## Caricamento pacchetto: 'caret'
## 
## Il seguente oggetto è mascherato da 'package:purrr':
## 
##     lift
library(datarium)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(car)
## Caricamento del pacchetto richiesto: carData
## 
## Caricamento pacchetto: 'car'
## 
## Il seguente oggetto è mascherato da 'package:dplyr':
## 
##     recode
## 
## Il seguente oggetto è mascherato da 'package:purrr':
## 
##     some
library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(MASS)
## 
## Caricamento pacchetto: 'MASS'
## 
## Il seguente oggetto è mascherato da 'package:dplyr':
## 
##     select
library(nortest)
library(lmtest)
## Caricamento del pacchetto richiesto: zoo
## 
## Caricamento pacchetto: 'zoo'
## 
## I seguenti oggetti sono mascherati da 'package:base':
## 
##     as.Date, as.Date.numeric
library(readxl)


data <- read_excel("~/Downloads/Concrete_Data.xls")

Once the dataset is loaded, I’m going to remove any missing data and rename the variables by simplifying the names. Then I summarize each variable and immediately remove any outliers in order to make my data more manageable and precise. For data variable I create many levels dividing they by trimester.

data <- na.omit(data)
title <- c("Cement", "Blast_Furnace_Slag","Fly_Ash","Water","Superplasticizer","Coarse_Aggregate","Fine_Aggregate","Day","Concrete_compressive_strength")
colnames(data) <- title

attach(data)

s <- summary(Cement)
iqr <- s[5] - s[2]
data <- subset(data, Cement > (s[2]-1.5*iqr) & Cement < (s[5]+1.5*iqr))

s <- summary(Blast_Furnace_Slag)
iqr <- s[5] - s[2]
data <- subset(data, Blast_Furnace_Slag > (s[2]-1.5*iqr) & Blast_Furnace_Slag < (s[5]+1.5*iqr))

s <- summary(Fly_Ash)
iqr <- s[5] - s[2]
data <- subset(data, Fly_Ash > (s[2]-1.5*iqr) & Fly_Ash < (s[5]+1.5*iqr))

s <- summary(Water)
iqr <- s[5] - s[2]
data <- subset(data, Water > (s[2]-1.5*iqr) & Water < (s[5]+1.5*iqr))

s <- summary(Superplasticizer)
iqr <- s[5] - s[2]
data <- subset(data, Superplasticizer > (s[2]-1.5*iqr) & Superplasticizer < (s[5]+1.5*iqr))

s <- summary(Coarse_Aggregate)
iqr <- s[5] - s[2]
data <- subset(data, Coarse_Aggregate > (s[2]-1.5*iqr) & Coarse_Aggregate < (s[5]+1.5*iqr))

s <- summary(Fine_Aggregate)
iqr <- s[5] - s[2]
data <- subset(data, Fine_Aggregate > (s[2]-1.5*iqr) & Fine_Aggregate < (s[5]+1.5*iqr))

s <- summary(Concrete_compressive_strength)
iqr <- s[5] - s[2]
data <- subset(data, Concrete_compressive_strength> (s[2]-1.5*iqr) & Concrete_compressive_strength< (s[5]+1.5*iqr))

data$Day <- cut(data$Day, breaks = c(0, 92 , 183, 274,366), labels = c("1st tm", "2nd tm","3dh tm", "4th tm"))

print(summary(data))
##      Cement      Blast_Furnace_Slag    Fly_Ash          Water      
##  Min.   :102.0   Min.   :  0.00     Min.   :  0.0   Min.   :127.0  
##  1st Qu.:192.0   1st Qu.:  0.00     1st Qu.:  0.0   1st Qu.:166.6  
##  Median :269.4   Median : 20.00     Median :  0.0   Median :185.7  
##  Mean   :279.1   Mean   : 73.01     Mean   : 54.6   Mean   :182.4  
##  3rd Qu.:349.0   3rd Qu.:143.25     3rd Qu.:118.3   3rd Qu.:192.0  
##  Max.   :540.0   Max.   :342.10     Max.   :200.1   Max.   :228.0  
##  Superplasticizer Coarse_Aggregate Fine_Aggregate      Day     
##  Min.   : 0.000   Min.   : 801.0   Min.   :594.0   1st tm:887  
##  1st Qu.: 0.000   1st Qu.: 932.0   1st Qu.:732.0   2nd tm: 80  
##  Median : 6.350   Median : 968.0   Median :778.9   3dh tm: 13  
##  Mean   : 5.877   Mean   : 974.6   Mean   :772.4   4th tm: 20  
##  3rd Qu.:10.100   3rd Qu.:1029.4   3rd Qu.:822.0               
##  Max.   :22.100   Max.   :1145.0   Max.   :945.0               
##  Concrete_compressive_strength
##  Min.   : 2.332               
##  1st Qu.:23.522               
##  Median :33.871               
##  Mean   :35.435               
##  3rd Qu.:45.320               
##  Max.   :79.400

Now I can draw my first model composed by all the variable! After using the function step I remove the less significative predictors

mod <- lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
    Fly_Ash + Water + Superplasticizer +(Coarse_Aggregate)+(Fine_Aggregate)+ Day, data = data)
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + (Coarse_Aggregate) + 
##     (Fine_Aggregate) + Day, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -32.039  -7.722   0.248   7.093  30.527 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        47.287041  30.370684   1.557 0.119791    
## Cement              0.101874   0.009385  10.855  < 2e-16 ***
## Blast_Furnace_Slag  0.077845   0.011222   6.937 7.24e-12 ***
## Fly_Ash             0.042121   0.013948   3.020 0.002594 ** 
## Water              -0.206206   0.047721  -4.321 1.71e-05 ***
## Superplasticizer    0.388366   0.117129   3.316 0.000947 ***
## Coarse_Aggregate   -0.010188   0.010538  -0.967 0.333855    
## Fine_Aggregate     -0.006457   0.012031  -0.537 0.591618    
## Day2nd tm          17.300611   1.326283  13.044  < 2e-16 ***
## Day3dh tm          19.040241   3.220700   5.912 4.65e-09 ***
## Day4th tm          17.394575   2.581012   6.739 2.70e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.09 on 989 degrees of freedom
## Multiple R-squared:   0.55,  Adjusted R-squared:  0.5454 
## F-statistic: 120.9 on 10 and 989 DF,  p-value: < 2.2e-16
mod <- step(mod, direction = "back", trace = FALSE) 
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -32.210  -7.690   0.382   7.222  30.365 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        23.711210   4.896404   4.843 1.49e-06 ***
## Cement              0.107307   0.004599  23.333  < 2e-16 ***
## Blast_Furnace_Slag  0.084589   0.005567  15.195  < 2e-16 ***
## Fly_Ash             0.049080   0.008870   5.533 4.02e-08 ***
## Water              -0.173367   0.024591  -7.050 3.35e-12 ***
## Superplasticizer    0.438566   0.107631   4.075 4.98e-05 ***
## Day2nd tm          17.122950   1.315530  13.016  < 2e-16 ***
## Day3dh tm          18.900041   3.194811   5.916 4.54e-09 ***
## Day4th tm          17.477844   2.575442   6.786 1.98e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.08 on 991 degrees of freedom
## Multiple R-squared:  0.5494, Adjusted R-squared:  0.5458 
## F-statistic:   151 on 8 and 991 DF,  p-value: < 2.2e-16

Since our Adjusted R^2 is not bad, we are going to check the assumption for MLR!

##1ST ASSUMPTION: model is linear in parameters

crPlots(mod)

We can accept first assumption and go on!

##2ND ASSUMPTION: mean of res =0 and res normally distribuited

mean(mod$residuals)
## [1] -7.935666e-17
shapiro.test(mod$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  mod$residuals
## W = 0.99759, p-value = 0.1493
plot(mod)

Also the second assumption is respected. Let’s continue with third!

##3RD ASSUMPTION: homoscedasticity of residuals

ncvTest(mod)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 46.91898, Df = 1, p = 7.3983e-12
plot(mod)

Looking to Scale-Location plot and computing the test we see that this assumption is not respected and so we have two possible solution: 1) to transform the response y in log(y) 2) to use WLS giving more weight to high-quality data.

Let’s try with first solution

mod <- lm (log(Concrete_compressive_strength) ~ Cement + Blast_Furnace_Slag + 
    Fly_Ash + Water + Superplasticizer +(Coarse_Aggregate)+(Fine_Aggregate)+ Day, data = data)
summary(mod)
## 
## Call:
## lm(formula = log(Concrete_compressive_strength) ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + (Coarse_Aggregate) + 
##     (Fine_Aggregate) + Day, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.86922 -0.24550  0.06362  0.26676  0.79980 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         4.1920653  1.0819926   3.874 0.000114 ***
## Cement              0.0031060  0.0003344   9.290  < 2e-16 ***
## Blast_Furnace_Slag  0.0021517  0.0003998   5.382 9.20e-08 ***
## Fly_Ash             0.0015683  0.0004969   3.156 0.001648 ** 
## Water              -0.0060945  0.0017001  -3.585 0.000354 ***
## Superplasticizer    0.0129495  0.0041729   3.103 0.001968 ** 
## Coarse_Aggregate   -0.0004519  0.0003754  -1.204 0.228947    
## Fine_Aggregate     -0.0005863  0.0004286  -1.368 0.171653    
## Day2nd tm           0.5773685  0.0472504  12.219  < 2e-16 ***
## Day3dh tm           0.5416256  0.1147414   4.720 2.69e-06 ***
## Day4th tm           0.6070287  0.0919517   6.602 6.62e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.395 on 989 degrees of freedom
## Multiple R-squared:  0.4883, Adjusted R-squared:  0.4831 
## F-statistic: 94.38 on 10 and 989 DF,  p-value: < 2.2e-16
mod <- step(mod, direction = "back", trace = FALSE) 
summary(mod)
## 
## Call:
## lm(formula = log(Concrete_compressive_strength) ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.87606 -0.23825  0.05967  0.27725  0.82173 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         2.7620161  0.1745008  15.828  < 2e-16 ***
## Cement              0.0035048  0.0001639  21.384  < 2e-16 ***
## Blast_Furnace_Slag  0.0026254  0.0001984  13.233  < 2e-16 ***
## Fly_Ash             0.0020936  0.0003161   6.623 5.75e-11 ***
## Water              -0.0041535  0.0008764  -4.739 2.46e-06 ***
## Superplasticizer    0.0143541  0.0038358   3.742 0.000193 ***
## Day2nd tm           0.5719942  0.0468836  12.200  < 2e-16 ***
## Day3dh tm           0.5534689  0.1138585   4.861 1.36e-06 ***
## Day4th tm           0.6144766  0.0917850   6.695 3.61e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3949 on 991 degrees of freedom
## Multiple R-squared:  0.4873, Adjusted R-squared:  0.4832 
## F-statistic: 117.8 on 8 and 991 DF,  p-value: < 2.2e-16
shapiro.test(mod$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  mod$residuals
## W = 0.96463, p-value = 7.395e-15
ncvTest(mod)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 90.43301, Df = 1, p = < 2.22e-16

In this case Adjusted R^2 decrease, the second assumption of normality is not respected and also the heteroscedasticity problem is not solved.

mod <- lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
    Fly_Ash + Water + Superplasticizer +(Coarse_Aggregate)+(Fine_Aggregate)+ Day, data = data)
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + (Coarse_Aggregate) + 
##     (Fine_Aggregate) + Day, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -32.039  -7.722   0.248   7.093  30.527 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        47.287041  30.370684   1.557 0.119791    
## Cement              0.101874   0.009385  10.855  < 2e-16 ***
## Blast_Furnace_Slag  0.077845   0.011222   6.937 7.24e-12 ***
## Fly_Ash             0.042121   0.013948   3.020 0.002594 ** 
## Water              -0.206206   0.047721  -4.321 1.71e-05 ***
## Superplasticizer    0.388366   0.117129   3.316 0.000947 ***
## Coarse_Aggregate   -0.010188   0.010538  -0.967 0.333855    
## Fine_Aggregate     -0.006457   0.012031  -0.537 0.591618    
## Day2nd tm          17.300611   1.326283  13.044  < 2e-16 ***
## Day3dh tm          19.040241   3.220700   5.912 4.65e-09 ***
## Day4th tm          17.394575   2.581012   6.739 2.70e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.09 on 989 degrees of freedom
## Multiple R-squared:   0.55,  Adjusted R-squared:  0.5454 
## F-statistic: 120.9 on 10 and 989 DF,  p-value: < 2.2e-16
mod <- step(mod, direction = "back", trace = FALSE) 
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -32.210  -7.690   0.382   7.222  30.365 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        23.711210   4.896404   4.843 1.49e-06 ***
## Cement              0.107307   0.004599  23.333  < 2e-16 ***
## Blast_Furnace_Slag  0.084589   0.005567  15.195  < 2e-16 ***
## Fly_Ash             0.049080   0.008870   5.533 4.02e-08 ***
## Water              -0.173367   0.024591  -7.050 3.35e-12 ***
## Superplasticizer    0.438566   0.107631   4.075 4.98e-05 ***
## Day2nd tm          17.122950   1.315530  13.016  < 2e-16 ***
## Day3dh tm          18.900041   3.194811   5.916 4.54e-09 ***
## Day4th tm          17.477844   2.575442   6.786 1.98e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.08 on 991 degrees of freedom
## Multiple R-squared:  0.5494, Adjusted R-squared:  0.5458 
## F-statistic:   151 on 8 and 991 DF,  p-value: < 2.2e-16
var_res <- var(mod$residuals)

vaaaar <- mod$residuals^2
wei <- 1 / vaaaar


mod <- lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
    Fly_Ash + Water + Superplasticizer +(Coarse_Aggregate)+(Fine_Aggregate)+ Day , data= data,weights = wei )
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + (Coarse_Aggregate) + 
##     (Fine_Aggregate) + Day, data = data, weights = wei)
## 
## Weighted Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3770 -1.0034  0.7660  0.9959  1.5557 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        21.5312410  1.9580765  10.996   <2e-16 ***
## Cement              0.1084350  0.0006957 155.863   <2e-16 ***
## Blast_Furnace_Slag  0.0854773  0.0009167  93.248   <2e-16 ***
## Fly_Ash             0.0506910  0.0010333  49.059   <2e-16 ***
## Water              -0.1724649  0.0029707 -58.055   <2e-16 ***
## Superplasticizer    0.4268479  0.0079130  53.943   <2e-16 ***
## Coarse_Aggregate    0.0009451  0.0006976   1.355    0.176    
## Fine_Aggregate      0.0009494  0.0008351   1.137    0.256    
## Day2nd tm          17.1174366  0.0775949 220.600   <2e-16 ***
## Day3dh tm          19.5720740  0.8683476  22.539   <2e-16 ***
## Day4th tm          15.2766135  1.0690367  14.290   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.997 on 989 degrees of freedom
## Multiple R-squared:  0.998,  Adjusted R-squared:  0.998 
## F-statistic: 4.932e+04 on 10 and 989 DF,  p-value: < 2.2e-16
mod <- step(mod, direction = "back", trace = FALSE) 
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day, data = data, weights = wei)
## 
## Weighted Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.2578 -1.0023  0.8486  0.9971  1.5620 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        23.9839343  0.3019447   79.43   <2e-16 ***
## Cement              0.1076676  0.0003625  296.98   <2e-16 ***
## Blast_Furnace_Slag  0.0845008  0.0005357  157.73   <2e-16 ***
## Fly_Ash             0.0495434  0.0005612   88.28   <2e-16 ***
## Water              -0.1750144  0.0019273  -90.81   <2e-16 ***
## Superplasticizer    0.4268452  0.0072297   59.04   <2e-16 ***
## Day2nd tm          17.1641551  0.0677252  253.44   <2e-16 ***
## Day3dh tm          19.6400433  0.8645617   22.72   <2e-16 ***
## Day4th tm          15.2792149  1.0689409   14.29   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9969 on 991 degrees of freedom
## Multiple R-squared:  0.998,  Adjusted R-squared:  0.998 
## F-statistic: 6.165e+04 on 8 and 991 DF,  p-value: < 2.2e-16
shapiro.test(mod$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  mod$residuals
## W = 0.99751, p-value = 0.1327
ncvTest(mod)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.003391049, Df = 1, p = 0.95356
bptest(mod)
## 
##  studentized Breusch-Pagan test
## 
## data:  mod
## BP = 0.17428, df = 8, p-value = 1

Doing WLS I completely solve my problem of heteroscedasticity!

##4TH ASSUMPTION: no autocorrelation of res

acf(mod$residuals)

pacf(mod$residuals)

Since the assumption is not met, I will try to use a lag1 var.

lag_var <- lag(mod$residuals, 1)

lag_var <- c(lag_var)


mod<- lm(Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
    Fly_Ash + Water + Superplasticizer +(Coarse_Aggregate)+(Fine_Aggregate)+ Day +lag_var,data = data)

summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + (Coarse_Aggregate) + 
##     (Fine_Aggregate) + Day + lag_var, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -30.0980  -6.3084   0.5064   6.9420  30.0573 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        26.992790  28.175076   0.958 0.338278    
## Cement              0.106846   0.008694  12.289  < 2e-16 ***
## Blast_Furnace_Slag  0.083508   0.010398   8.031 2.73e-15 ***
## Fly_Ash             0.048639   0.012920   3.765 0.000177 ***
## Water              -0.178612   0.044294  -4.032 5.95e-05 ***
## Superplasticizer    0.358189   0.108618   3.298 0.001010 ** 
## Coarse_Aggregate   -0.003248   0.009770  -0.332 0.739606    
## Fine_Aggregate      0.002402   0.011165   0.215 0.829723    
## Day2nd tm          13.693552   1.258863  10.878  < 2e-16 ***
## Day3dh tm          16.712954   2.986452   5.596 2.84e-08 ***
## Day4th tm          16.597471   2.389528   6.946 6.81e-12 ***
## lag_var             0.391566   0.030285  12.929  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.26 on 987 degrees of freedom
##   (1 osservazione eliminata a causa di un valore mancante)
## Multiple R-squared:  0.6143, Adjusted R-squared:   0.61 
## F-statistic: 142.9 on 11 and 987 DF,  p-value: < 2.2e-16
mod <- step(mod, direction = "back", trace = FALSE) 
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day + lag_var, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -30.8261  -6.3801   0.5369   6.8164  29.9216 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        25.195095   4.535999   5.554 3.58e-08 ***
## Cement              0.106240   0.004269  24.889  < 2e-16 ***
## Blast_Furnace_Slag  0.083066   0.005154  16.118  < 2e-16 ***
## Fly_Ash             0.047642   0.008211   5.802 8.82e-09 ***
## Water              -0.175379   0.022794  -7.694 3.44e-14 ***
## Superplasticizer    0.386294   0.099864   3.868 0.000117 ***
## Day2nd tm          13.601022   1.247486  10.903  < 2e-16 ***
## Day3dh tm          16.400306   2.962798   5.535 3.98e-08 ***
## Day4th tm          16.568511   2.384285   6.949 6.66e-12 ***
## lag_var             0.391386   0.030216  12.953  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.25 on 989 degrees of freedom
##   (1 osservazione eliminata a causa di un valore mancante)
## Multiple R-squared:  0.6139, Adjusted R-squared:  0.6104 
## F-statistic: 174.7 on 9 and 989 DF,  p-value: < 2.2e-16
Box.test(mod$residuals)
## 
##  Box-Pierce test
## 
## data:  mod$residuals
## X-squared = 0.62339, df = 1, p-value = 0.4298

Lagging our residuals I solve my problem as we can se by box.test that is an alternative iv dwtest. I can not use dwtest since I used WLS.

##5TH ASSUMPTION: predictors and residuals are uncorrelated

I can check this by plotting every scatter plot composed by each predictors and residuals.

residuals<- c(mod$residuals,0)
plot(data$Cement,residuals)

plot(data$Blast_Furnace_Slag,residuals)

plot(data$Fly_Ash,residuals)

plot(data$Water,residuals)

plot(data$Superplasticizer,residuals)

Since there are not correlation and strange pattern we can accept this assumption

##6TH ASSUMPTION: p>n It is verify since I have 1000 units and 9 predictors.

##7TH ASSUMPTION: absence of multicollinearity

library(corrplot)
## corrplot 0.92 loaded
corrplot(cor(data[,1:6]))

vif(mod)
##                        GVIF Df GVIF^(1/(2*Df))
## Cement             1.808814  1        1.344921
## Blast_Furnace_Slag 1.859940  1        1.363796
## Fly_Ash            2.614668  1        1.616994
## Water              1.976334  1        1.405821
## Superplasticizer   2.746763  1        1.657336
## Day                1.220118  3        1.033714
## lag_var            1.057494  1        1.028345

Since the vif is always less than 5 I can accept this assumption. So this is my final model.

mod<- lm(Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
    Fly_Ash + Water + Superplasticizer + Day +lag_var,data = data)

summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day + lag_var, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -30.8261  -6.3801   0.5369   6.8164  29.9216 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        25.195095   4.535999   5.554 3.58e-08 ***
## Cement              0.106240   0.004269  24.889  < 2e-16 ***
## Blast_Furnace_Slag  0.083066   0.005154  16.118  < 2e-16 ***
## Fly_Ash             0.047642   0.008211   5.802 8.82e-09 ***
## Water              -0.175379   0.022794  -7.694 3.44e-14 ***
## Superplasticizer    0.386294   0.099864   3.868 0.000117 ***
## Day2nd tm          13.601022   1.247486  10.903  < 2e-16 ***
## Day3dh tm          16.400306   2.962798   5.535 3.98e-08 ***
## Day4th tm          16.568511   2.384285   6.949 6.66e-12 ***
## lag_var             0.391386   0.030216  12.953  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.25 on 989 degrees of freedom
##   (1 osservazione eliminata a causa di un valore mancante)
## Multiple R-squared:  0.6139, Adjusted R-squared:  0.6104 
## F-statistic: 174.7 on 9 and 989 DF,  p-value: < 2.2e-16
mod <- step(mod, direction = "back", trace = FALSE) 
summary(mod)
## 
## Call:
## lm(formula = Concrete_compressive_strength ~ Cement + Blast_Furnace_Slag + 
##     Fly_Ash + Water + Superplasticizer + Day + lag_var, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -30.8261  -6.3801   0.5369   6.8164  29.9216 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        25.195095   4.535999   5.554 3.58e-08 ***
## Cement              0.106240   0.004269  24.889  < 2e-16 ***
## Blast_Furnace_Slag  0.083066   0.005154  16.118  < 2e-16 ***
## Fly_Ash             0.047642   0.008211   5.802 8.82e-09 ***
## Water              -0.175379   0.022794  -7.694 3.44e-14 ***
## Superplasticizer    0.386294   0.099864   3.868 0.000117 ***
## Day2nd tm          13.601022   1.247486  10.903  < 2e-16 ***
## Day3dh tm          16.400306   2.962798   5.535 3.98e-08 ***
## Day4th tm          16.568511   2.384285   6.949 6.66e-12 ***
## lag_var             0.391386   0.030216  12.953  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.25 on 989 degrees of freedom
##   (1 osservazione eliminata a causa di un valore mancante)
## Multiple R-squared:  0.6139, Adjusted R-squared:  0.6104 
## F-statistic: 174.7 on 9 and 989 DF,  p-value: < 2.2e-16