install packages and import libraries

library(sandwich) 
library(lmtest) 
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(ggplot2) 
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

1. Data Collection and Dataset Structure

#Upload database 
dati<-read.csv("neonati.csv")

attach(dati) 
summary(dati) 
##    Anni.madre     N.gravidanze       Fumatrici        Gestazione   
##  Min.   : 0.00   Min.   : 0.0000   Min.   :0.0000   Min.   :25.00  
##  1st Qu.:25.00   1st Qu.: 0.0000   1st Qu.:0.0000   1st Qu.:38.00  
##  Median :28.00   Median : 1.0000   Median :0.0000   Median :39.00  
##  Mean   :28.16   Mean   : 0.9812   Mean   :0.0416   Mean   :38.98  
##  3rd Qu.:32.00   3rd Qu.: 1.0000   3rd Qu.:0.0000   3rd Qu.:40.00  
##  Max.   :46.00   Max.   :12.0000   Max.   :1.0000   Max.   :43.00  
##       Peso        Lunghezza         Cranio     Tipo.parto       
##  Min.   : 830   Min.   :310.0   Min.   :235   Length:2500       
##  1st Qu.:2990   1st Qu.:480.0   1st Qu.:330   Class :character  
##  Median :3300   Median :500.0   Median :340   Mode  :character  
##  Mean   :3284   Mean   :494.7   Mean   :340                     
##  3rd Qu.:3620   3rd Qu.:510.0   3rd Qu.:350                     
##  Max.   :4930   Max.   :565.0   Max.   :390                     
##    Ospedale            Sesso          
##  Length:2500        Length:2500       
##  Class :character   Class :character  
##  Mode  :character   Mode  :character  
##                                       
##                                       
## 
n<-nrow(dati)

moments::skewness(Peso) #negatively skewed distribution: higher frequency of values below the average compared to a symmetrical #distribution
## [1] -0.6470308
moments::kurtosis(Peso)-3 #leptokurtic distribution: dataset may contain more outliers or extreme values than a normal distribution.
## [1] 2.031532
shapiro.test(Peso )#the data do not follow a normal distribution.
## 
##  Shapiro-Wilk normality test
## 
## data:  Peso
## W = 0.97066, p-value < 2.2e-16
#Relationship between the weight of newborns and variables of the dataset 

panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...) {
  usr <- par("usr")
  on.exit(par(usr = usr))
  plot.window(xlim = c(0,1), ylim = c(0,1))
  r <- cor(x, y, use = "complete.obs")
  txt <- format(c(r, 1), digits = digits)[1]
  txt <- paste0(prefix, txt)
  if(missing(cex.cor)) 
    cex.cor <- 0.8 / strwidth(txt)
  text(0.5, 0.5, txt, cex = cex.cor)
}

pairs(dati[sapply(dati, is.numeric)], lower.panel=panel.cor, upper.panel=panel.smooth)

#moderate positive correlation between "Gestazione" e "Peso". A scatterplot that is similar to a logarithmic function
#relationship with control variables
par(mfrow=c(1,2)) 
boxplot(Peso) 
boxplot(Peso~Sesso) 

boxplot(Peso~Fumatrici)

#verify whether the observed average difference is statistically significant.
wilcox.test(Peso ~ Fumatrici, data = dati) 
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Peso by Fumatrici
## W = 138162, p-value = 0.05971
## alternative hypothesis: true location shift is not equal to 0
#There is a tendency towards a difference, but it is not strong enough to be confirmed with the sample available.
#Use the Wilcoxon test instead of the t-test because the variable 'peso' does not follow a normal distribution.

mean(Peso[Fumatrici==0]) 
## [1] 3286.153
mean(Peso[Fumatrici==1]) 
## [1] 3236.346
#The average weight for children of non-smoking mothers is slightly higher (3286 g) than for children of smoking mothers (3236 g), indicating a possible difference, albeit small.

#The presence of numerous outliers could indicate significant weight variability within the groups.

2. Analysis and Modelling

2.1 Preliminary Analysis

2.1.1 Analysis of the relationship between ‘peso’ and variables (“Lungghezza”, “Peso” and “Cranio”).

par(mfrow=c(1,3))
boxplot(Peso~Sesso) 
boxplot(Lunghezza~Sesso) 
boxplot(Cranio~Sesso)

wilcox.test(Peso ~ Sesso, data = dati) 
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Peso by Sesso
## W = 538641, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
wilcox.test(Lunghezza ~ Sesso, data = dati) 
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Lunghezza by Sesso
## W = 594455, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
wilcox.test(Cranio ~ Sesso, data = dati)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Cranio by Sesso
## W = 641638, p-value = 9.633e-15
## alternative hypothesis: true location shift is not equal to 0
#The difference in the weight, length and dimensions of the skulls of males and females is statistically significant.

2.1.2 In some hospitals, more caesarean sections are performed.

#H0: the proportion of caesarean sections is the same in all hospitals

tabella <- table(Ospedale, Tipo.parto) 
print(tabella)
##         Tipo.parto
## Ospedale Ces Nat
##     osp1 242 574
##     osp2 254 595
##     osp3 232 603
test_chi <- chisq.test(tabella) 

print(test_chi) #There is insufficient statistical evidence to claim that the proportion of caesarean sections differs between the three hospitals #in the sample.
## 
##  Pearson's Chi-squared test
## 
## data:  tabella
## X-squared = 1.0972, df = 2, p-value = 0.5778

2.1.3 The average weight and length of the sample of newborns are significantly equal to those of the population.

#From OMS(Organizzazione Mondiale della Sanità),mean weight population newborns 
mu_m_weight <- 3.3464 #kg
mu_f_weight <- 3.2322 #Kg 
mu_m_length <- 498.842 #millimeters 
mu_f_length <- 491.477 #millimeters

dati_m <- subset(dati, Sesso == "M") 
dati_f <- subset(dati, Sesso == "F")

weight_male_newborns_sample <- mean(dati_m$Peso)
weight_female_newborns_sample <- mean(dati_f$Peso)

#Normal test for variable 'peso' divided by variable 'sesso'. 
shapiro.test(dati_m$Peso)
## 
##  Shapiro-Wilk normality test
## 
## data:  dati_m$Peso
## W = 0.96647, p-value = 2.321e-16
shapiro.test(dati_f$Peso) 
## 
##  Shapiro-Wilk normality test
## 
## data:  dati_f$Peso
## W = 0.96285, p-value < 2.2e-16
#Datas do not follow a normal distribution.

#H0: the median weight of males in the sample is equal to 3.3464 kg
wilcox.test(dati_m$Peso, mu_m_weight) #p_value>0.05: I can't refuse H0
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  dati_m$Peso and mu_m_weight
## W = 1244, p-value = 0.08374
## alternative hypothesis: true location shift is not equal to 0
#H0: the median weight of females in the sample is equal to 3.2322 kg
wilcox.test(dati_f$Peso, mu_f_weight) #p_value>0.05: I can't refuse H0
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  dati_f$Peso and mu_f_weight
## W = 1256, p-value = 0.08374
## alternative hypothesis: true location shift is not equal to 0
#H0: the median length of males in the sample is equal to 498.842 mm
wilcox.test(dati_f$Lunghezza, mu_m_length) 
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  dati_f$Lunghezza and mu_m_length
## W = 537, p-value = 0.8024
## alternative hypothesis: true location shift is not equal to 0
#p_value>0.05: I can't refuse H0

#H0: the median weight of females in the sample is equal to 491.477 mm
wilcox.test(dati_f$Lunghezza, mu_f_length) 
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  dati_f$Lunghezza and mu_f_length
## W = 622, p-value = 0.9879
## alternative hypothesis: true location shift is not equal to 0
#p_value>0.05: I can't refuse H0

#In light of the results obtained: the average weight and length of this sample of newborns are significantly equal to those of the population.

2.2 Creation of the Regression Model

mod<-lm(Peso~.,data=dati) 

summary(mod)
## 
## Call:
## lm(formula = Peso ~ ., data = dati)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1124.40  -181.66   -14.42   160.91  2611.89 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -6738.4762   141.3087 -47.686  < 2e-16 ***
## Anni.madre        0.8921     1.1323   0.788   0.4308    
## N.gravidanze     11.2665     4.6608   2.417   0.0157 *  
## Fumatrici       -30.1631    27.5386  -1.095   0.2735    
## Gestazione       32.5696     3.8187   8.529  < 2e-16 ***
## Lunghezza        10.2945     0.3007  34.236  < 2e-16 ***
## Cranio           10.4707     0.4260  24.578  < 2e-16 ***
## Tipo.partoNat    29.5254    12.0844   2.443   0.0146 *  
## Ospedaleosp2    -11.2095    13.4379  -0.834   0.4043    
## Ospedaleosp3     28.0958    13.4957   2.082   0.0375 *  
## SessoM           77.5409    11.1776   6.937 5.08e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 273.9 on 2489 degrees of freedom
## Multiple R-squared:  0.7289, Adjusted R-squared:  0.7278 
## F-statistic: 669.2 on 10 and 2489 DF,  p-value: < 2.2e-16
mod1 <- lm(Peso ~ . - Anni.madre - N.gravidanze, data = dati) 

summary(mod1)
## 
## Call:
## lm(formula = Peso ~ . - Anni.madre - N.gravidanze, data = dati)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1102.46  -185.30   -16.65   159.62  2604.52 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -6676.7697   135.7136 -49.198  < 2e-16 ***
## Fumatrici       -26.1769    27.5402  -0.950   0.3420    
## Gestazione       31.1436     3.7833   8.232 2.94e-16 ***
## Lunghezza        10.2526     0.3008  34.088  < 2e-16 ***
## Cranio           10.6198     0.4236  25.067  < 2e-16 ***
## Tipo.partoNat    28.5670    12.0969   2.362   0.0183 *  
## Ospedaleosp2    -11.8642    13.4532  -0.882   0.3779    
## Ospedaleosp3     28.8518    13.5094   2.136   0.0328 *  
## SessoM           78.7002    11.1868   7.035 2.56e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 274.3 on 2491 degrees of freedom
## Multiple R-squared:  0.7279, Adjusted R-squared:  0.727 
## F-statistic:   833 on 8 and 2491 DF,  p-value: < 2.2e-16
mod2 <- lm(Peso ~ Gestazione + Lunghezza + Cranio + Sesso, data = dati) 

summary(mod2)
## 
## Call:
## lm(formula = Peso ~ Gestazione + Lunghezza + Cranio + Sesso, 
##     data = dati)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1138.2  -184.3   -17.6   163.3  2627.3 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -6651.1188   135.5172 -49.080  < 2e-16 ***
## Gestazione     31.2737     3.7856   8.261 2.31e-16 ***
## Lunghezza      10.2054     0.3007  33.939  < 2e-16 ***
## Cranio         10.6704     0.4245  25.139  < 2e-16 ***
## SessoM         79.1049    11.2117   7.056 2.22e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 275 on 2495 degrees of freedom
## Multiple R-squared:  0.7261, Adjusted R-squared:  0.7257 
## F-statistic:  1654 on 4 and 2495 DF,  p-value: < 2.2e-16
full_model <- lm(Peso ~ (Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + Tipo.parto)^2, data = dati) 

mod3 <- step(full_model, direction = "both") 
## Start:  AIC=28060.18
## Peso ~ (Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto)^2
## 
##                         Df Sum of Sq       RSS   AIC
## - Gestazione:Sesso       1      6934 184020284 28058
## - Sesso:Tipo.parto       1      7720 184021070 28058
## - Gestazione:Lunghezza   1     18916 184032266 28058
## - Cranio:Tipo.parto      1     31582 184044932 28059
## - Lunghezza:Cranio       1     37341 184050691 28059
## - Cranio:Sesso           1     38298 184051648 28059
## - Cranio:Fumatrici       1     44022 184057372 28059
## - Fumatrici:Tipo.parto   1     92241 184105591 28059
## - Sesso:Fumatrici        1     94911 184108261 28060
## - Lunghezza:Tipo.parto   1    103018 184116368 28060
## <none>                               184013350 28060
## - Lunghezza:Sesso        1    167813 184181163 28061
## - Gestazione:Tipo.parto  1    292982 184306333 28062
## - Gestazione:Fumatrici   1    399475 184412825 28064
## - Gestazione:Cranio      1    572836 184586186 28066
## - Lunghezza:Fumatrici    1    765539 184778889 28069
## 
## Step:  AIC=28058.28
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Sesso + 
##     Lunghezza:Fumatrici + Lunghezza:Tipo.parto + Cranio:Sesso + 
##     Cranio:Fumatrici + Cranio:Tipo.parto + Sesso:Fumatrici + 
##     Sesso:Tipo.parto + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Sesso:Tipo.parto       1      7736 184028021 28056
## - Gestazione:Lunghezza   1     16398 184036682 28057
## - Cranio:Tipo.parto      1     31477 184051761 28057
## - Lunghezza:Cranio       1     34015 184054299 28057
## - Cranio:Fumatrici       1     43455 184063739 28057
## - Cranio:Sesso           1     44415 184064699 28057
## - Fumatrici:Tipo.parto   1     93738 184114022 28058
## - Sesso:Fumatrici        1     97854 184118139 28058
## - Lunghezza:Tipo.parto   1    103214 184123498 28058
## <none>                               184020284 28058
## - Lunghezza:Sesso        1    174712 184194996 28059
## + Gestazione:Sesso       1      6934 184013350 28060
## - Gestazione:Tipo.parto  1    291612 184311896 28060
## - Gestazione:Fumatrici   1    402547 184422832 28062
## - Gestazione:Cranio      1    569094 184589378 28064
## - Lunghezza:Fumatrici    1    767398 184787682 28067
## 
## Step:  AIC=28056.38
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Sesso + 
##     Lunghezza:Fumatrici + Lunghezza:Tipo.parto + Cranio:Sesso + 
##     Cranio:Fumatrici + Cranio:Tipo.parto + Sesso:Fumatrici + 
##     Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Gestazione:Lunghezza   1     16112 184044133 28055
## - Cranio:Tipo.parto      1     31192 184059213 28055
## - Lunghezza:Cranio       1     34189 184062210 28055
## - Cranio:Fumatrici       1     43689 184071710 28055
## - Cranio:Sesso           1     45668 184073689 28055
## - Fumatrici:Tipo.parto   1     95742 184123763 28056
## - Sesso:Fumatrici        1     96962 184124982 28056
## - Lunghezza:Tipo.parto   1    112365 184140386 28056
## <none>                               184028021 28056
## - Lunghezza:Sesso        1    179542 184207563 28057
## + Sesso:Tipo.parto       1      7736 184020284 28058
## - Gestazione:Tipo.parto  1    287632 184315652 28058
## + Gestazione:Sesso       1      6950 184021070 28058
## - Gestazione:Fumatrici   1    408392 184436412 28060
## - Gestazione:Cranio      1    570723 184598744 28062
## - Lunghezza:Fumatrici    1    763754 184791775 28065
## 
## Step:  AIC=28054.6
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Cranio + Lunghezza:Sesso + Lunghezza:Fumatrici + 
##     Lunghezza:Tipo.parto + Cranio:Sesso + Cranio:Fumatrici + 
##     Cranio:Tipo.parto + Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Lunghezza:Cranio       1     22284 184066417 28053
## - Cranio:Tipo.parto      1     34158 184078291 28053
## - Cranio:Fumatrici       1     43670 184087803 28053
## - Cranio:Sesso           1     57147 184101280 28053
## - Sesso:Fumatrici        1     95011 184139144 28054
## - Fumatrici:Tipo.parto   1     97204 184141337 28054
## - Lunghezza:Tipo.parto   1    116188 184160321 28054
## <none>                               184044133 28055
## - Lunghezza:Sesso        1    204336 184248469 28055
## - Gestazione:Tipo.parto  1    276518 184320651 28056
## + Gestazione:Lunghezza   1     16112 184028021 28056
## + Sesso:Tipo.parto       1      7451 184036682 28057
## + Gestazione:Sesso       1      4447 184039686 28057
## - Gestazione:Fumatrici   1    407787 184451920 28058
## - Lunghezza:Fumatrici    1    760766 184804899 28063
## - Gestazione:Cranio      1    850467 184894600 28064
## 
## Step:  AIC=28052.9
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Lunghezza:Tipo.parto + 
##     Cranio:Sesso + Cranio:Fumatrici + Cranio:Tipo.parto + Sesso:Fumatrici + 
##     Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Cranio:Tipo.parto      1     36620 184103038 28051
## - Cranio:Fumatrici       1     40568 184106986 28052
## - Cranio:Sesso           1     62611 184129028 28052
## - Sesso:Fumatrici        1     93936 184160353 28052
## - Fumatrici:Tipo.parto   1     96550 184162967 28052
## - Lunghezza:Tipo.parto   1    117653 184184070 28053
## <none>                               184066417 28053
## - Lunghezza:Sesso        1    200382 184266799 28054
## + Lunghezza:Cranio       1     22284 184044133 28055
## - Gestazione:Tipo.parto  1    283705 184350122 28055
## + Sesso:Tipo.parto       1      7727 184058690 28055
## + Gestazione:Lunghezza   1      4208 184062210 28055
## + Gestazione:Sesso       1      2935 184063482 28055
## - Gestazione:Fumatrici   1    407436 184473853 28056
## - Lunghezza:Fumatrici    1    754270 184820687 28061
## - Gestazione:Cranio      1   2369492 186435910 28083
## 
## Step:  AIC=28051.4
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Lunghezza:Tipo.parto + 
##     Cranio:Sesso + Cranio:Fumatrici + Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Cranio:Fumatrici       1     42104 184145141 28050
## - Cranio:Sesso           1     66022 184169060 28050
## - Lunghezza:Tipo.parto   1     82986 184186023 28051
## - Sesso:Fumatrici        1     93151 184196188 28051
## - Fumatrici:Tipo.parto   1     95981 184199018 28051
## <none>                               184103038 28051
## - Lunghezza:Sesso        1    205013 184308050 28052
## + Cranio:Tipo.parto      1     36620 184066417 28053
## + Lunghezza:Cranio       1     24746 184078291 28053
## + Sesso:Tipo.parto       1      7407 184095631 28053
## + Gestazione:Lunghezza   1      5321 184097717 28053
## + Gestazione:Sesso       1      2635 184100403 28053
## - Gestazione:Tipo.parto  1    316193 184419231 28054
## - Gestazione:Fumatrici   1    409870 184512908 28055
## - Lunghezza:Fumatrici    1    759244 184862281 28060
## - Gestazione:Cranio      1   2358588 186461625 28081
## 
## Step:  AIC=28049.97
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Lunghezza:Tipo.parto + 
##     Cranio:Sesso + Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Cranio:Sesso           1     72209 184217350 28049
## - Lunghezza:Tipo.parto   1     83671 184228812 28049
## - Fumatrici:Tipo.parto   1     98240 184243381 28049
## - Sesso:Fumatrici        1    105322 184250464 28049
## <none>                               184145141 28050
## - Lunghezza:Sesso        1    214865 184360006 28051
## + Cranio:Fumatrici       1     42104 184103038 28051
## + Cranio:Tipo.parto      1     38156 184106986 28052
## + Lunghezza:Cranio       1     21464 184123677 28052
## + Sesso:Tipo.parto       1      7606 184137535 28052
## + Gestazione:Lunghezza   1      5914 184139227 28052
## + Gestazione:Sesso       1      2380 184142761 28052
## - Gestazione:Tipo.parto  1    319370 184464512 28052
## - Gestazione:Fumatrici   1    428749 184573891 28054
## - Lunghezza:Fumatrici    1    758055 184903196 28058
## - Gestazione:Cranio      1   2333915 186479056 28080
## 
## Step:  AIC=28048.95
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Lunghezza:Tipo.parto + 
##     Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Lunghezza:Tipo.parto   1     84597 184301947 28048
## - Fumatrici:Tipo.parto   1     99895 184317246 28048
## - Sesso:Fumatrici        1    107146 184324496 28048
## - Lunghezza:Sesso        1    142659 184360009 28049
## <none>                               184217350 28049
## + Cranio:Sesso           1     72209 184145141 28050
## + Cranio:Fumatrici       1     48291 184169060 28050
## + Cranio:Tipo.parto      1     41922 184175429 28050
## + Lunghezza:Cranio       1     27145 184190205 28051
## + Gestazione:Lunghezza   1     12885 184204466 28051
## + Sesso:Tipo.parto       1      9142 184208209 28051
## + Gestazione:Sesso       1      6706 184210645 28051
## - Gestazione:Tipo.parto  1    316118 184533469 28051
## - Gestazione:Fumatrici   1    426550 184643901 28053
## - Lunghezza:Fumatrici    1    765675 184983025 28057
## - Gestazione:Cranio      1   2265671 186483021 28078
## 
## Step:  AIC=28048.1
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Sesso:Fumatrici + 
##     Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq       RSS   AIC
## - Fumatrici:Tipo.parto   1     95096 184397044 28047
## - Sesso:Fumatrici        1    114424 184416371 28048
## - Lunghezza:Sesso        1    140892 184442839 28048
## <none>                               184301947 28048
## + Lunghezza:Tipo.parto   1     84597 184217350 28049
## + Cranio:Sesso           1     73135 184228812 28049
## - Gestazione:Tipo.parto  1    234240 184536187 28049
## + Cranio:Fumatrici       1     49069 184252879 28049
## + Lunghezza:Cranio       1     26650 184275297 28050
## + Sesso:Tipo.parto       1     18800 184283148 28050
## + Gestazione:Lunghezza   1     14097 184287850 28050
## + Gestazione:Sesso       1      6943 184295005 28050
## + Cranio:Tipo.parto      1      3043 184298904 28050
## - Gestazione:Fumatrici   1    411525 184713472 28052
## - Lunghezza:Fumatrici    1    775433 185077380 28057
## - Gestazione:Cranio      1   2296311 186598258 28077
## 
## Step:  AIC=28047.39
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Sesso:Fumatrici
## 
##                         Df Sum of Sq       RSS   AIC
## - Sesso:Fumatrici        1     96176 184493220 28047
## - Lunghezza:Sesso        1    132784 184529828 28047
## <none>                               184397044 28047
## + Fumatrici:Tipo.parto   1     95096 184301947 28048
## + Lunghezza:Tipo.parto   1     79798 184317246 28048
## + Cranio:Sesso           1     74734 184322310 28048
## - Gestazione:Tipo.parto  1    228181 184625225 28049
## + Cranio:Fumatrici       1     51513 184345530 28049
## + Lunghezza:Cranio       1     25913 184371130 28049
## + Sesso:Tipo.parto       1     21469 184375575 28049
## + Gestazione:Lunghezza   1     15615 184381429 28049
## + Gestazione:Sesso       1      8384 184388660 28049
## + Cranio:Tipo.parto      1      3326 184393717 28049
## - Gestazione:Fumatrici   1    472726 184869770 28052
## - Lunghezza:Fumatrici    1    834012 185231056 28057
## - Gestazione:Cranio      1   2320339 186717383 28077
## 
## Step:  AIC=28046.69
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici
## 
##                         Df Sum of Sq       RSS   AIC
## - Lunghezza:Sesso        1    134319 184627539 28047
## <none>                               184493220 28047
## + Sesso:Fumatrici        1     96176 184397044 28047
## - Gestazione:Tipo.parto  1    208421 184701641 28048
## + Lunghezza:Tipo.parto   1     86746 184406474 28048
## + Fumatrici:Tipo.parto   1     76849 184416371 28048
## + Cranio:Sesso           1     76381 184416839 28048
## + Cranio:Fumatrici       1     64121 184429099 28048
## + Lunghezza:Cranio       1     24378 184468842 28048
## + Sesso:Tipo.parto       1     20283 184472937 28048
## + Gestazione:Lunghezza   1     14245 184478975 28049
## + Gestazione:Sesso       1     11665 184481555 28049
## + Cranio:Tipo.parto      1      2605 184490614 28049
## - Gestazione:Fumatrici   1    561031 185054251 28052
## - Lunghezza:Fumatrici    1    744334 185237554 28055
## - Gestazione:Cranio      1   2330058 186823278 28076
## 
## Step:  AIC=28046.51
## Peso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + Gestazione:Tipo.parto + 
##     Lunghezza:Fumatrici
## 
##                         Df Sum of Sq       RSS   AIC
## <none>                               184627539 28047
## + Lunghezza:Sesso        1    134319 184493220 28047
## + Sesso:Fumatrici        1     97711 184529828 28047
## + Lunghezza:Tipo.parto   1     85266 184542273 28047
## - Gestazione:Tipo.parto  1    211856 184839395 28047
## + Fumatrici:Tipo.parto   1     69454 184558085 28048
## + Cranio:Fumatrici       1     68081 184559458 28048
## + Gestazione:Lunghezza   1     25481 184602058 28048
## + Sesso:Tipo.parto       1     25006 184602533 28048
## + Gestazione:Sesso       1     17699 184609840 28048
## + Lunghezza:Cranio       1     15122 184612417 28048
## + Cranio:Tipo.parto      1      3001 184624538 28049
## + Cranio:Sesso           1       117 184627422 28049
## - Gestazione:Fumatrici   1    574710 185202249 28052
## - Lunghezza:Fumatrici    1    729714 185357253 28054
## - Gestazione:Cranio      1   2514148 187141687 28078
## - Sesso                  1   3263203 187890742 28088
summary(mod3)
## 
## Call:
## lm(formula = Peso ~ Gestazione + Lunghezza + Cranio + Sesso + 
##     Fumatrici + Tipo.parto + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Fumatrici, data = dati)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1107.90  -181.77   -13.66   164.91  2653.22 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                72.83720 1134.35041   0.064  0.94881    
## Gestazione               -145.86227   30.20656  -4.829 1.46e-06 ***
## Lunghezza                  10.27319    0.30490  33.694  < 2e-16 ***
## Cranio                     -9.50339    3.48179  -2.729  0.00639 ** 
## SessoM                     74.07656   11.16849   6.633 4.03e-11 ***
## Fumatrici                 125.00911  809.64559   0.154  0.87731    
## Tipo.partoNat            -415.92567  263.49929  -1.578  0.11458    
## Gestazione:Cranio           0.52684    0.09049   5.822 6.57e-09 ***
## Gestazione:Fumatrici      -61.30076   22.02304  -2.783  0.00542 ** 
## Gestazione:Tipo.partoNat   11.40337    6.74759   1.690  0.09115 .  
## Lunghezza:Fumatrici         4.58444    1.46166   3.136  0.00173 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 272.4 on 2489 degrees of freedom
## Multiple R-squared:  0.732,  Adjusted R-squared:  0.7309 
## F-statistic: 679.8 on 10 and 2489 DF,  p-value: < 2.2e-16

2.3 Selection of the Optimal Model

BIC(mod,mod2,mod1,mod3) #step_model could be the best model
##      df      BIC
## mod  12 35241.84
## mod2  6 35220.54
## mod1 10 35235.28
## mod3 12 35213.09

2.4 Model Quality Analysis

par(mfrow=c(2,2)) 
plot(mod3)

#1) It seems that the homoscedasticity assumption is being respected. Observations are distributed in a random way around the #line located at value 0.
#2) Most of the points follow the diagonal line in the Q-Q plot, it means that the model residuals are approximately normal.
#3) Even with the Scale-Location, the model seems to respect the homoscedasticity assumption. 
#4) Most of the residuals are randomly distributed around zero, suggesting a good overall fit.

lmtest::bptest(mod3) #p-value<0.5: the model exhibits heteroscedasticity 
## 
##  studentized Breusch-Pagan test
## 
## data:  mod3
## BP = 93.588, df = 10, p-value = 1.038e-15
lmtest::dwtest(mod3) #there is no evidence of positive autocorrelation in the residuals. 
## 
##  Durbin-Watson test
## 
## data:  mod3
## DW = 1.9643, p-value = 0.1854
## alternative hypothesis: true autocorrelation is greater than 0
shapiro.test(mod3$residuals) #the residues do not follow a normal distribution. 
## 
##  Shapiro-Wilk normality test
## 
## data:  mod3$residuals
## W = 0.97403, p-value < 2.2e-16
plot(density(residuals(mod3)))

#condition of residuals aren't respected. 
#log trasformation 
dati$logPeso <- log(dati$Peso) 
full_model_log <- lm(logPeso ~ (Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + Tipo.parto)^2, data = dati) 
mod4 <- step(full_model_log, direction = "both") 
## Start:  AIC=-12377.8
## logPeso ~ (Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto)^2
## 
##                         Df Sum of Sq    RSS    AIC
## - Sesso:Tipo.parto       1  0.000031 17.380 -12380
## - Cranio:Tipo.parto      1  0.000194 17.380 -12380
## - Gestazione:Sesso       1  0.001637 17.382 -12380
## - Cranio:Sesso           1  0.001974 17.382 -12380
## - Lunghezza:Tipo.parto   1  0.003274 17.383 -12379
## - Cranio:Fumatrici       1  0.004308 17.384 -12379
## - Sesso:Fumatrici        1  0.005011 17.385 -12379
## - Fumatrici:Tipo.parto   1  0.005242 17.385 -12379
## - Lunghezza:Sesso        1  0.005537 17.386 -12379
## <none>                               17.380 -12378
## - Gestazione:Tipo.parto  1  0.016689 17.397 -12377
## - Gestazione:Fumatrici   1  0.040023 17.420 -12374
## - Lunghezza:Fumatrici    1  0.052136 17.432 -12372
## - Gestazione:Cranio      1  0.125834 17.506 -12362
## - Gestazione:Lunghezza   1  0.158441 17.538 -12357
## - Lunghezza:Cranio       1  0.225786 17.606 -12348
## 
## Step:  AIC=-12379.8
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Sesso + 
##     Gestazione:Fumatrici + Gestazione:Tipo.parto + Lunghezza:Cranio + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Lunghezza:Tipo.parto + 
##     Cranio:Sesso + Cranio:Fumatrici + Cranio:Tipo.parto + Sesso:Fumatrici + 
##     Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Cranio:Tipo.parto      1  0.000193 17.380 -12382
## - Gestazione:Sesso       1  0.001638 17.382 -12382
## - Cranio:Sesso           1  0.001991 17.382 -12382
## - Lunghezza:Tipo.parto   1  0.003411 17.384 -12381
## - Cranio:Fumatrici       1  0.004312 17.384 -12381
## - Sesso:Fumatrici        1  0.004999 17.385 -12381
## - Fumatrici:Tipo.parto   1  0.005276 17.385 -12381
## - Lunghezza:Sesso        1  0.005594 17.386 -12381
## <none>                               17.380 -12380
## - Gestazione:Tipo.parto  1  0.016659 17.397 -12379
## + Sesso:Tipo.parto       1  0.000031 17.380 -12378
## - Gestazione:Fumatrici   1  0.040203 17.420 -12376
## - Lunghezza:Fumatrici    1  0.052106 17.432 -12374
## - Gestazione:Cranio      1  0.125897 17.506 -12364
## - Gestazione:Lunghezza   1  0.158522 17.539 -12359
## - Lunghezza:Cranio       1  0.225821 17.606 -12350
## 
## Step:  AIC=-12381.77
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Sesso + 
##     Gestazione:Fumatrici + Gestazione:Tipo.parto + Lunghezza:Cranio + 
##     Lunghezza:Sesso + Lunghezza:Fumatrici + Lunghezza:Tipo.parto + 
##     Cranio:Sesso + Cranio:Fumatrici + Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Gestazione:Sesso       1  0.001634 17.382 -12384
## - Cranio:Sesso           1  0.002018 17.382 -12384
## - Lunghezza:Tipo.parto   1  0.003399 17.384 -12383
## - Cranio:Fumatrici       1  0.004354 17.385 -12383
## - Sesso:Fumatrici        1  0.004991 17.385 -12383
## - Fumatrici:Tipo.parto   1  0.005265 17.386 -12383
## - Lunghezza:Sesso        1  0.005622 17.386 -12383
## <none>                               17.380 -12382
## - Gestazione:Tipo.parto  1  0.017452 17.398 -12381
## + Cranio:Tipo.parto      1  0.000193 17.380 -12380
## + Sesso:Tipo.parto       1  0.000029 17.380 -12380
## - Gestazione:Fumatrici   1  0.040264 17.421 -12378
## - Lunghezza:Fumatrici    1  0.052224 17.433 -12376
## - Gestazione:Cranio      1  0.125842 17.506 -12366
## - Gestazione:Lunghezza   1  0.158437 17.539 -12361
## - Lunghezza:Cranio       1  0.227476 17.608 -12351
## 
## Step:  AIC=-12383.53
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Sesso + 
##     Lunghezza:Fumatrici + Lunghezza:Tipo.parto + Cranio:Sesso + 
##     Cranio:Fumatrici + Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Cranio:Sesso           1  0.002667 17.385 -12385
## - Lunghezza:Tipo.parto   1  0.003427 17.385 -12385
## - Lunghezza:Sesso        1  0.004034 17.386 -12385
## - Cranio:Fumatrici       1  0.004267 17.386 -12385
## - Sesso:Fumatrici        1  0.005306 17.387 -12385
## - Fumatrici:Tipo.parto   1  0.005436 17.387 -12385
## <none>                               17.382 -12384
## - Gestazione:Tipo.parto  1  0.017279 17.399 -12383
## + Gestazione:Sesso       1  0.001634 17.380 -12382
## + Cranio:Tipo.parto      1  0.000189 17.382 -12382
## + Sesso:Tipo.parto       1  0.000030 17.382 -12382
## - Gestazione:Fumatrici   1  0.040724 17.423 -12380
## - Lunghezza:Fumatrici    1  0.052452 17.434 -12378
## - Gestazione:Cranio      1  0.124986 17.507 -12368
## - Gestazione:Lunghezza   1  0.165188 17.547 -12362
## - Lunghezza:Cranio       1  0.226178 17.608 -12353
## 
## Step:  AIC=-12385.15
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Sesso + 
##     Lunghezza:Fumatrici + Lunghezza:Tipo.parto + Cranio:Fumatrici + 
##     Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Lunghezza:Sesso        1  0.001669 17.386 -12387
## - Lunghezza:Tipo.parto   1  0.003445 17.388 -12387
## - Cranio:Fumatrici       1  0.004681 17.389 -12386
## - Sesso:Fumatrici        1  0.005387 17.390 -12386
## - Fumatrici:Tipo.parto   1  0.005482 17.390 -12386
## <none>                               17.385 -12385
## - Gestazione:Tipo.parto  1  0.017311 17.402 -12385
## + Cranio:Sesso           1  0.002667 17.382 -12384
## + Gestazione:Sesso       1  0.002283 17.382 -12384
## + Cranio:Tipo.parto      1  0.000218 17.384 -12383
## + Sesso:Tipo.parto       1  0.000051 17.385 -12383
## - Gestazione:Fumatrici   1  0.040539 17.425 -12381
## - Lunghezza:Fumatrici    1  0.053550 17.438 -12380
## - Gestazione:Cranio      1  0.122763 17.507 -12370
## - Gestazione:Lunghezza   1  0.162886 17.547 -12364
## - Lunghezza:Cranio       1  0.236395 17.621 -12353
## 
## Step:  AIC=-12386.91
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Fumatrici + 
##     Lunghezza:Tipo.parto + Cranio:Fumatrici + Sesso:Fumatrici + 
##     Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Lunghezza:Tipo.parto   1  0.003397 17.390 -12388
## - Cranio:Fumatrici       1  0.004772 17.391 -12388
## - Fumatrici:Tipo.parto   1  0.005257 17.392 -12388
## - Sesso:Fumatrici        1  0.005403 17.392 -12388
## <none>                               17.386 -12387
## - Gestazione:Tipo.parto  1  0.017484 17.404 -12386
## + Lunghezza:Sesso        1  0.001669 17.385 -12385
## + Cranio:Sesso           1  0.000303 17.386 -12385
## + Cranio:Tipo.parto      1  0.000225 17.386 -12385
## + Gestazione:Sesso       1  0.000193 17.386 -12385
## + Sesso:Tipo.parto       1  0.000082 17.386 -12385
## - Gestazione:Fumatrici   1  0.040986 17.427 -12383
## - Lunghezza:Fumatrici    1  0.053403 17.440 -12381
## - Gestazione:Cranio      1  0.121649 17.508 -12372
## - Gestazione:Lunghezza   1  0.161248 17.547 -12366
## - Lunghezza:Cranio       1  0.234962 17.621 -12355
## 
## Step:  AIC=-12388.42
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Fumatrici + 
##     Cranio:Fumatrici + Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Cranio:Fumatrici       1  0.004817 17.395 -12390
## - Fumatrici:Tipo.parto   1  0.005036 17.395 -12390
## - Sesso:Fumatrici        1  0.005728 17.395 -12390
## <none>                               17.390 -12388
## - Gestazione:Tipo.parto  1  0.014653 17.404 -12388
## + Lunghezza:Tipo.parto   1  0.003397 17.386 -12387
## + Lunghezza:Sesso        1  0.001621 17.388 -12387
## + Cranio:Sesso           1  0.000320 17.389 -12386
## + Sesso:Tipo.parto       1  0.000304 17.389 -12386
## + Gestazione:Sesso       1  0.000211 17.390 -12386
## + Cranio:Tipo.parto      1  0.000155 17.390 -12386
## - Gestazione:Fumatrici   1  0.040065 17.430 -12385
## - Lunghezza:Fumatrici    1  0.053947 17.444 -12383
## - Gestazione:Cranio      1  0.121794 17.512 -12373
## - Gestazione:Lunghezza   1  0.160524 17.550 -12368
## - Lunghezza:Cranio       1  0.235066 17.625 -12357
## 
## Step:  AIC=-12389.73
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Fumatrici + 
##     Sesso:Fumatrici + Fumatrici:Tipo.parto
## 
##                         Df Sum of Sq    RSS    AIC
## - Fumatrici:Tipo.parto   1  0.005203 17.400 -12391
## - Sesso:Fumatrici        1  0.006746 17.401 -12391
## <none>                               17.395 -12390
## - Gestazione:Tipo.parto  1  0.014932 17.409 -12390
## + Cranio:Fumatrici       1  0.004817 17.390 -12388
## + Lunghezza:Tipo.parto   1  0.003442 17.391 -12388
## + Lunghezza:Sesso        1  0.001711 17.393 -12388
## + Cranio:Sesso           1  0.000418 17.394 -12388
## + Sesso:Tipo.parto       1  0.000327 17.394 -12388
## + Gestazione:Sesso       1  0.000181 17.394 -12388
## + Cranio:Tipo.parto      1  0.000125 17.394 -12388
## - Gestazione:Fumatrici   1  0.042039 17.436 -12386
## - Lunghezza:Fumatrici    1  0.050526 17.445 -12384
## - Gestazione:Cranio      1  0.119089 17.514 -12375
## - Gestazione:Lunghezza   1  0.159843 17.554 -12369
## - Lunghezza:Cranio       1  0.232669 17.627 -12358
## 
## Step:  AIC=-12390.98
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Fumatrici + 
##     Sesso:Fumatrici
## 
##                         Df Sum of Sq    RSS    AIC
## - Sesso:Fumatrici        1  0.005712 17.405 -12392
## <none>                               17.400 -12391
## - Gestazione:Tipo.parto  1  0.014611 17.414 -12391
## + Fumatrici:Tipo.parto   1  0.005203 17.395 -12390
## + Cranio:Fumatrici       1  0.004983 17.395 -12390
## + Lunghezza:Tipo.parto   1  0.003216 17.396 -12389
## + Lunghezza:Sesso        1  0.001488 17.398 -12389
## + Cranio:Sesso           1  0.000505 17.399 -12389
## + Sesso:Tipo.parto       1  0.000407 17.399 -12389
## + Gestazione:Sesso       1  0.000280 17.399 -12389
## + Cranio:Tipo.parto      1  0.000114 17.400 -12389
## - Gestazione:Fumatrici   1  0.046738 17.446 -12386
## - Lunghezza:Fumatrici    1  0.054097 17.454 -12385
## - Gestazione:Cranio      1  0.119018 17.519 -12376
## - Gestazione:Lunghezza   1  0.158983 17.559 -12370
## - Lunghezza:Cranio       1  0.232782 17.633 -12360
## 
## Step:  AIC=-12392.16
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Gestazione:Tipo.parto + Lunghezza:Cranio + Lunghezza:Fumatrici
## 
##                         Df Sum of Sq    RSS    AIC
## - Gestazione:Tipo.parto  1  0.013372 17.419 -12392
## <none>                               17.405 -12392
## + Cranio:Fumatrici       1  0.005916 17.399 -12391
## + Sesso:Fumatrici        1  0.005712 17.400 -12391
## + Fumatrici:Tipo.parto   1  0.004168 17.401 -12391
## + Lunghezza:Tipo.parto   1  0.003561 17.402 -12391
## + Lunghezza:Sesso        1  0.001532 17.404 -12390
## + Cranio:Sesso           1  0.000533 17.405 -12390
## + Gestazione:Sesso       1  0.000377 17.405 -12390
## + Sesso:Tipo.parto       1  0.000369 17.405 -12390
## + Cranio:Tipo.parto      1  0.000147 17.405 -12390
## - Lunghezza:Fumatrici    1  0.048671 17.454 -12387
## - Gestazione:Fumatrici   1  0.053752 17.459 -12386
## - Gestazione:Cranio      1  0.119220 17.525 -12377
## - Gestazione:Lunghezza   1  0.160545 17.566 -12371
## - Lunghezza:Cranio       1  0.231195 17.637 -12361
## - Sesso                  1  0.306477 17.712 -12350
## 
## Step:  AIC=-12392.24
## logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Lunghezza:Cranio + Lunghezza:Fumatrici
## 
##                         Df Sum of Sq    RSS    AIC
## <none>                               17.419 -12392
## + Gestazione:Tipo.parto  1  0.013372 17.405 -12392
## + Cranio:Fumatrici       1  0.006085 17.413 -12391
## + Sesso:Fumatrici        1  0.004473 17.414 -12391
## + Fumatrici:Tipo.parto   1  0.003998 17.415 -12391
## + Lunghezza:Sesso        1  0.001786 17.417 -12390
## + Cranio:Tipo.parto      1  0.001487 17.417 -12390
## + Cranio:Sesso           1  0.000452 17.418 -12390
## + Lunghezza:Tipo.parto   1  0.000393 17.418 -12390
## + Gestazione:Sesso       1  0.000220 17.419 -12390
## + Sesso:Tipo.parto       1  0.000000 17.419 -12390
## - Tipo.parto             1  0.033926 17.453 -12389
## - Lunghezza:Fumatrici    1  0.046352 17.465 -12388
## - Gestazione:Fumatrici   1  0.054804 17.474 -12386
## - Gestazione:Cranio      1  0.127496 17.546 -12376
## - Gestazione:Lunghezza   1  0.176124 17.595 -12369
## - Lunghezza:Cranio       1  0.232474 17.651 -12361
## - Sesso                  1  0.303214 17.722 -12351
summary(mod4)
## 
## Call:
## lm(formula = logPeso ~ Gestazione + Lunghezza + Cranio + Sesso + 
##     Fumatrici + Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + 
##     Gestazione:Fumatrici + Lunghezza:Cranio + Lunghezza:Fumatrici, 
##     data = dati)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.42182 -0.05199 -0.00030  0.05192  0.67658 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           1.080e+00  3.428e-01   3.151  0.00165 ** 
## Gestazione            3.545e-03  1.937e-02   0.183  0.85477    
## Lunghezza             1.778e-02  1.524e-03  11.672  < 2e-16 ***
## Cranio                5.175e-03  2.111e-03   2.452  0.01429 *  
## SessoM                2.259e-02  3.432e-03   6.581 5.68e-11 ***
## Fumatrici             1.677e-01  2.483e-01   0.675  0.49942    
## Tipo.partoNat         8.126e-03  3.692e-03   2.201  0.02781 *  
## Gestazione:Lunghezza -1.602e-04  3.195e-05  -5.016 5.66e-07 ***
## Gestazione:Cranio     2.623e-04  6.147e-05   4.267 2.05e-05 ***
## Gestazione:Fumatrici -1.893e-02  6.765e-03  -2.798  0.00518 ** 
## Lunghezza:Cranio     -2.431e-05  4.219e-06  -5.762 9.32e-09 ***
## Lunghezza:Fumatrici   1.154e-03  4.485e-04   2.573  0.01014 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08367 on 2488 degrees of freedom
## Multiple R-squared:  0.7924, Adjusted R-squared:  0.7915 
## F-statistic: 863.5 on 11 and 2488 DF,  p-value: < 2.2e-16
#residue analysis 
lmtest::bptest(mod4) #p-value<0.5: the model exhibits heteroscedasticity 
## 
##  studentized Breusch-Pagan test
## 
## data:  mod4
## BP = 258.91, df = 11, p-value < 2.2e-16
lmtest::dwtest(mod4)#there is no evidence of positive autocorrelation in the residuals. 
## 
##  Durbin-Watson test
## 
## data:  mod4
## DW = 1.954, p-value = 0.1251
## alternative hypothesis: true autocorrelation is greater than 0
shapiro.test(mod4$residuals)#the residues do not follow a normal distribution. 
## 
##  Shapiro-Wilk normality test
## 
## data:  mod4$residuals
## W = 0.98246, p-value < 2.2e-16
plot(density(residuals(mod4)))

#Calculation of robust standard errors of the "HC1" type 
robust_vcov <- vcovHC(mod4, type = "HC1") 
coeftest(mod4, vcov = robust_vcov)
## 
## t test of coefficients:
## 
##                         Estimate  Std. Error t value  Pr(>|t|)    
## (Intercept)           1.0800e+00  5.4750e-01  1.9726 0.0486557 *  
## Gestazione            3.5452e-03  4.7941e-02  0.0739 0.9410568    
## Lunghezza             1.7783e-02  3.6838e-03  4.8274 1.467e-06 ***
## Cranio                5.1753e-03  6.1969e-03  0.8352 0.4037134    
## SessoM                2.2589e-02  3.4036e-03  6.6369 3.918e-11 ***
## Fumatrici             1.6770e-01  1.9257e-01  0.8708 0.3839256    
## Tipo.partoNat         8.1262e-03  3.6337e-03  2.2364 0.0254159 *  
## Gestazione:Lunghezza -1.6024e-04  9.0104e-05 -1.7784 0.0754668 .  
## Gestazione:Cranio     2.6232e-04  1.4743e-04  1.7792 0.0753256 .  
## Gestazione:Fumatrici -1.8928e-02  5.7199e-03 -3.3091 0.0009493 ***
## Lunghezza:Cranio     -2.4312e-05  1.2224e-05 -1.9889 0.0468266 *  
## Lunghezza:Fumatrici   1.1541e-03  4.7003e-04  2.4554 0.0141392 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#new model without the variability "cranio" with a p-value= 0.94 
mod5 <- update(mod4, . ~ . - Cranio) 
summary(mod5) 
## 
## Call:
## lm(formula = logPeso ~ Gestazione + Lunghezza + Sesso + Fumatrici + 
##     Tipo.parto + Gestazione:Lunghezza + Gestazione:Cranio + Gestazione:Fumatrici + 
##     Lunghezza:Cranio + Lunghezza:Fumatrici, data = dati)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.44203 -0.05213 -0.00005  0.05212  0.71050 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           1.549e+00  2.848e-01   5.439 5.87e-08 ***
## Gestazione            7.153e-03  1.933e-02   0.370  0.71139    
## Lunghezza             1.916e-02  1.418e-03  13.509  < 2e-16 ***
## SessoM                2.252e-02  3.436e-03   6.555 6.75e-11 ***
## Fumatrici             1.869e-01  2.484e-01   0.752  0.45183    
## Tipo.partoNat         8.011e-03  3.695e-03   2.168  0.03025 *  
## Gestazione:Lunghezza -2.277e-04  1.627e-05 -13.989  < 2e-16 ***
## Gestazione:Cranio     3.495e-04  5.018e-05   6.966 4.15e-12 ***
## Gestazione:Fumatrici -1.918e-02  6.771e-03  -2.833  0.00464 ** 
## Lunghezza:Cranio     -2.066e-05  3.951e-06  -5.229 1.85e-07 ***
## Lunghezza:Fumatrici   1.135e-03  4.489e-04   2.529  0.01149 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08376 on 2489 degrees of freedom
## Multiple R-squared:  0.7919, Adjusted R-squared:  0.7911 
## F-statistic: 947.3 on 10 and 2489 DF,  p-value: < 2.2e-16
lmtest::bptest(mod5) 
## 
##  studentized Breusch-Pagan test
## 
## data:  mod5
## BP = 189.29, df = 10, p-value < 2.2e-16
lmtest::dwtest(mod5) 
## 
##  Durbin-Watson test
## 
## data:  mod5
## DW = 1.9576, p-value = 0.1445
## alternative hypothesis: true autocorrelation is greater than 0
shapiro.test(mod5$residuals) 
## 
##  Shapiro-Wilk normality test
## 
## data:  mod5$residuals
## W = 0.98122, p-value < 2.2e-16
robust_vcov <- vcovHC(mod5, type = "HC1") 
coeftest(mod5, vcov = robust_vcov)
## 
## t test of coefficients:
## 
##                         Estimate  Std. Error  t value  Pr(>|t|)    
## (Intercept)           1.5488e+00  4.0003e-01   3.8719 0.0001108 ***
## Gestazione            7.1526e-03  4.7988e-02   0.1490 0.8815274    
## Lunghezza             1.9158e-02  3.6357e-03   5.2693 1.487e-07 ***
## SessoM                2.2521e-02  3.4052e-03   6.6138 4.570e-11 ***
## Fumatrici             1.8691e-01  1.9401e-01   0.9634 0.3354393    
## Tipo.partoNat         8.0108e-03  3.6327e-03   2.2052 0.0275328 *  
## Gestazione:Lunghezza -2.2766e-04  2.1933e-05 -10.3797 < 2.2e-16 ***
## Gestazione:Cranio     3.4954e-04  1.3960e-04   2.5039 0.0123462 *  
## Gestazione:Fumatrici -1.9184e-02  5.7575e-03  -3.3321 0.0008747 ***
## Lunghezza:Cranio     -2.0659e-05  1.0761e-05  -1.9198 0.0549998 .  
## Lunghezza:Fumatrici   1.1355e-03  4.6908e-04   2.4206 0.0155663 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
BIC(mod,mod1,mod2,mod3,mod4,mod5)# mod4 has a lower BIC
##      df       BIC
## mod  12 35241.837
## mod1 10 35235.280
## mod2  6 35220.545
## mod3 12 35213.094
## mod4 13 -5219.837
## mod5 12 -5221.629
#I have chosen model 4 because it applies a log transformation, addressing the issue that the residual assumptions of the first four models were not met. Although heteroscedasticity and non-normality of residuals remain issues in model 4, it yields better #overall performance, particularly evidenced by a higher adjusted R-squared and a lower BIC.

3. Forecasts and Results

#Since the number of pregnancies was not significant in the model, our prediction of neonatal weight is based on other relevant variables included in the model, such as length, sex and gestation duration.

new_case <- data.frame( 
  Gestazione = 39, 
  Lunghezza = mean(dati_f$Lunghezza),
  Cranio = mean(dati_f$Cranio), 
  Sesso = "F",
  Fumatrici = 0,
  Tipo.parto = "Nat" )

predicted_logPeso <- predict(mod4, newdata = new_case) 
predicted_Peso <- exp(predicted_logPeso) 
predicted_Peso #the prediction is 3154.35 g
##       1 
## 3154.35

4. Views

#dataframe with the varabiles: "Gestazione" in range (37,42) and "Fumatrici" (0,1) 
data <- expand.grid( 
  Gestazione = seq(37, 42, by = 1), 
  Fumatrici = c(0, 1), 
  Sesso = c("F","M") ) 

#costant variables 
data$Lunghezza <- mean(dati_f$Lunghezza)
data$Cranio <- mean(dati_f$Cranio) 
data$Tipo.parto <- "Nat"

predicted_logPeso_view <- predict(mod4, newdata = data)
predicted_Peso_view <- exp(predicted_logPeso_view) 
data$Peso_predetto <- predicted_Peso_view 
predicted_Peso_view
##        1        2        3        4        5        6        7        8 
## 3069.501 3111.636 3154.350 3197.649 3241.543 3286.040 3171.269 3154.524 
##        9       10       11       12       13       14       15       16 
## 3137.868 3121.300 3104.819 3088.425 3139.628 3182.725 3226.415 3270.704 
##       17       18       19       20       21       22       23       24 
## 3315.600 3361.114 3243.720 3226.593 3209.556 3192.609 3175.752 3158.984
#Convert smokers into a factor

data$Fumatrici <- factor(data$Fumatrici, levels = c(0,1), labels = c("Non Fumatore", "Fumatore"))

#Create the line graph

ggplot(data, aes(x = Gestazione, y = Peso_predetto, color = Fumatrici, linetype = Sesso)) + geom_line(size = 1) + labs( title = "Impatto di Gestazione, Fumo e Sesso sul Peso Predetto", x = "Settimane di Gestazione", y = "Peso Predetto (grammi)", color = "Fumatrici", linetype = "Sesso" ) + theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#The results show how the Weight variable is influenced by Gender and Smoking. The graphic representation is a line showing the #predicted weight as a function of weeks of gestation.

#It can be seen that the predicted weight for male newborns is generally higher than that for females, whether the mother is a #smoker or not.

#An interesting aspect is the different trend in predicted weight in relation to maternal smoking. If the mother is a non-smoker, #weight increases as the weeks of gestation progress. Conversely, if the mother is a smoker, the model shows a tendency for #weight to decrease as gestation progresses.

#These results suggest an interaction between maternal smoking and gestation in determining birth weight, with a negative effect #of smoking that could attenuate or reverse the expected weight gain as gestation progresses.

#Dataset and Model Limitations #1) The dataset used is limited in size, which may reduce the statistical power of some tests and limit the generalisability of the #results.

#2) Preliminary analyses showed that the response variable Weight does not follow a normal distribution and exhibits outliers. #Although a logarithmic transformation was adopted to mitigate this effect, the normality and heteroscedasticity of the residuals #were not completely resolved, which may affect the validity of the confidence intervals and statistical tests associated with #the model.