A very first example of straight line regression

#install.packages("MASS")
library(MASS)

First of all we have to load the data of the saltBP.dat file.

saltBP <- read.table(file="C:/Users/valer/Downloads/saltBP.dat", header=TRUE)

Now we inspect the data to understand what they are about.

str(saltBP)
## 'data.frame':    25 obs. of  3 variables:
##  $ BP       : num  132 132 134 135 130 ...
##  $ salt     : num  1.55 1.13 4.88 4.11 1.55 4.69 2.16 2.23 2.65 3.68 ...
##  $ saltLevel: int  0 0 0 0 0 0 0 0 0 0 ...
View(saltBP)
plot(saltBP)

table(saltBP)
## , , saltLevel = 0
## 
##         salt
## BP       1.13 1.55 2.16 2.23 2.34 2.65 3.68 4.11 4.69 4.88 4.95 5.21 5.59 5.98
##   128.3     0    0    0    0    0    0    1    0    0    0    0    0    0    0
##   129.29    0    0    0    0    1    0    0    0    0    0    0    0    0    0
##   129.51    0    0    0    0    0    1    0    0    0    0    0    0    0    0
##   129.61    0    0    0    1    0    0    0    0    0    0    0    0    0    0
##   129.85    0    1    0    0    0    0    0    0    0    0    0    0    0    0
##   131.13    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   131.84    1    0    0    0    0    0    0    0    0    0    0    0    0    0
##   132.19    0    1    0    0    0    0    0    0    0    0    0    0    0    0
##   133.86    0    0    0    0    0    0    0    0    0    1    0    0    0    0
##   134.84    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   135.08    0    0    0    0    0    0    0    1    0    0    0    0    0    0
##   135.1     0    0    1    0    0    0    0    0    0    0    0    0    0    0
##   135.65    0    0    0    0    0    0    0    0    0    0    0    1    0    0
##   135.79    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   136.14    0    0    0    0    0    0    0    0    0    0    0    0    0    1
##   136.83    0    0    0    0    0    0    0    0    0    0    1    0    0    0
##   136.84    0    0    0    0    0    0    0    0    1    0    0    0    0    0
##   137.17    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   137.5     0    0    0    0    0    0    0    0    0    0    0    0    1    0
##   138.12    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   140.78    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   142.64    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   144.13    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   144.67    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   145.02    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##         salt
## BP       6.31 7.43 7.98 8.68 9.15 9.24 10.36 11.18 11.84 12.57
##   128.3     0    0    0    0    0    0     0     0     0     0
##   129.29    0    0    0    0    0    0     0     0     0     0
##   129.51    0    0    0    0    0    0     0     0     0     0
##   129.61    0    0    0    0    0    0     0     0     0     0
##   129.85    0    0    0    0    0    0     0     0     0     0
##   131.13    0    0    0    0    0    0     0     0     0     0
##   131.84    0    0    0    0    0    0     0     0     0     0
##   132.19    0    0    0    0    0    0     0     0     0     0
##   133.86    0    0    0    0    0    0     0     0     0     0
##   134.84    0    0    0    0    0    0     0     0     0     0
##   135.08    0    0    0    0    0    0     0     0     0     0
##   135.1     0    0    0    0    0    0     0     0     0     0
##   135.65    0    0    0    0    0    0     0     0     0     0
##   135.79    0    0    0    0    0    0     0     0     0     0
##   136.14    0    0    0    0    0    0     0     0     0     0
##   136.83    0    0    0    0    0    0     0     0     0     0
##   136.84    0    0    0    0    0    0     0     0     0     0
##   137.17    0    0    0    0    0    0     0     0     0     0
##   137.5     0    0    0    0    0    0     0     0     0     0
##   138.12    0    0    0    0    0    0     0     0     0     0
##   140.78    0    0    0    0    0    0     0     0     0     0
##   142.64    0    0    0    0    0    0     0     0     0     0
##   144.13    0    0    0    0    0    0     0     0     0     0
##   144.67    0    0    0    0    0    0     0     0     0     0
##   145.02    0    0    0    0    0    0     0     0     0     0
## 
## , , saltLevel = 1
## 
##         salt
## BP       1.13 1.55 2.16 2.23 2.34 2.65 3.68 4.11 4.69 4.88 4.95 5.21 5.59 5.98
##   128.3     0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   129.29    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   129.51    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   129.61    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   129.85    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   131.13    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   131.84    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   132.19    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   133.86    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   134.84    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   135.08    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   135.1     0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   135.65    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   135.79    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   136.14    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   136.83    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   136.84    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   137.17    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   137.5     0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   138.12    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   140.78    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   142.64    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   144.13    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   144.67    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   145.02    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##         salt
## BP       6.31 7.43 7.98 8.68 9.15 9.24 10.36 11.18 11.84 12.57
##   128.3     0    0    0    0    0    0     0     0     0     0
##   129.29    0    0    0    0    0    0     0     0     0     0
##   129.51    0    0    0    0    0    0     0     0     0     0
##   129.61    0    0    0    0    0    0     0     0     0     0
##   129.85    0    0    0    0    0    0     0     0     0     0
##   131.13    1    0    0    0    0    0     0     0     0     0
##   131.84    0    0    0    0    0    0     0     0     0     0
##   132.19    0    0    0    0    0    0     0     0     0     0
##   133.86    0    0    0    0    0    0     0     0     0     0
##   134.84    0    0    0    1    0    0     0     0     0     0
##   135.08    0    0    0    0    0    0     0     0     0     0
##   135.1     0    0    0    0    0    0     0     0     0     0
##   135.65    0    0    0    0    0    0     0     0     0     0
##   135.79    0    0    0    0    1    0     0     0     0     0
##   136.14    0    0    0    0    0    0     0     0     0     0
##   136.83    0    0    0    0    0    0     0     0     0     0
##   136.84    0    0    0    0    0    0     0     0     0     0
##   137.17    0    0    1    0    0    0     0     0     0     0
##   137.5     0    0    0    0    0    0     0     0     0     0
##   138.12    0    1    0    0    0    0     0     0     0     0
##   140.78    0    0    0    0    0    0     1     0     0     0
##   142.64    0    0    0    0    0    0     0     0     0     1
##   144.13    0    0    0    0    0    0     0     1     0     0
##   144.67    0    0    0    0    0    0     0     0     1     0
##   145.02    0    0    0    0    0    1     0     0     0     0

There are 25 observations on three variables. We will only use blood pressure (BP) and salt consumption (salt). Both are averages taken on 5 consecutive days. We will try and investigate whether salt consumption influences blood pressure. Starting by specifying an equation which allows us to predict blood pressure with salt consumption. Salt consumption is measured in grams and represents the excess with respect of the generally considered correct dose of 6 grams per person. Said that “salt=2” corresponds to a daily consumption of 8 grams of salt.

plot(BP ~ salt, data=saltBP,col="blue")

Throught this scatterplot we could say that there is a linear correlation between the BP (blood preassure) and the amount of salt assumed per person. Increasing the quantity of salt the level of BP, in turn, increases.

Now we calculate the correlation coefficient and verify its significance.

cor.test(saltBP$BP, saltBP$salt, alternative="two.sided",
method="pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  saltBP$BP and saltBP$salt
## t = 7.3888, df = 23, p-value = 1.631e-07
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6636134 0.9267667
## sample estimates:
##       cor 
## 0.8387992

We are able to confirm that there is correlation between the two variables because the p-value is low and we can reject the null hypothesis that the true correlation is equal to 0. The real number/correlation is in the interval (0.66 and 0.93) with 95% of confidence.

Specify the straight line fitted by least squares.

fit.lm <- lm(BP~salt, data=saltBP)
summary(fit.lm)
## 
## Call:
## lm(formula = BP ~ salt, data = saltBP)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.0388 -1.6755  0.3662  1.8824  5.3443 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  128.616      1.102 116.723  < 2e-16 ***
## salt           1.197      0.162   7.389 1.63e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.745 on 23 degrees of freedom
## Multiple R-squared:  0.7036, Adjusted R-squared:  0.6907 
## F-statistic: 54.59 on 1 and 23 DF,  p-value: 1.631e-07

Both intercept and the coefficient for ‘salt’ are statistically significant, it is indicated by the stars and low p-values. Overall, it seems the model shows a significant relationship between salt intake and blood pressure, with higher salt intake associated with higher blood pressure.

At this point we add a straight line (y=mx+q) at the previous scatterplot using the estimate Std calculated earlier to underline the correlation. This combined plot allows a visual understanding of how well the model fits the relationship.

plot(BP ~ salt, data=saltBP,col="blue")
curve(128.61 + 1.197*x, xlim = c(1, 13), lwd = 2, add = TRUE)

The plot function generates a scatter plot with ‘salt’ on the x-axis and ‘BP’ on the y-axis. Then, the curve function adds a line to the plot representing the linear regression model with an intercept of 128.61 and a slope of 1.197 times ‘salt’. This is the best line obtained from the data. We use the linear regression in order to estimate, under some approximation, the relation of salt and BP. If salt amount increases the BP is expected to increase of a given amount. Increasing of one unit and the relation will increase of another 1.197.

Intercept is 128.616, as the average BP, when the conception salt is equal to 0, so when the salt is 6 grams. In other words the intercept=128.61 give us the estimate average of BP when the patient consume 6 gr of salt. If someone assumes more than 6 grams we have to add the slope=1.197. Thanks to this model knowing the salt consumption we are able to predict the most probable value of BP. In addition there is evidence to reject the null hypothesis that the slope of this line is 0. If the slope was equal to 0 there wouldn’t be correlation between the variables.

confint(fit.lm, level = 0.95)
##                   2.5 %     97.5 %
## (Intercept) 126.3369606 130.895834
## salt          0.8617951   1.531993

These values, 2.5% and 97.5%, represent the lower and upper bounds, respectively, of the 95% confidence intervals for the coefficients of the intercept and ‘salt’ in the linear regression model. These intervals provide a range of plausible values for the true population coefficients: - for the intercept, the 95% confidence interval ranges from approximately 126.34 to 130.90, - for the ‘salt’ coefficient, the 95% confidence interval ranges from approximately 0.86 to 1.53. In other words, we are 95% confident that the true value of the intercept=BP lies between the given range, and similarly for the coefficient of ‘salt’.

predict(fit.lm, newdata = data.frame(salt = 40))
##        1 
## 176.4922

The predict function in R allows us to make predictions from a fitted model. In this case, we’re predicting the ‘BP’ using a new value of ‘salt’ (40 in this instance) with the ‘fit.lm’ model we’ve previously created. This is an example with crazy data of the consumption of salt of 40 grams. We are able to predict that the blood pressure supposed to become equal to 176 mmHg.

predict(fit.lm, newdata = data.frame(salt = -6))
##       1 
## 121.435

Example with no consumption of salt. The blood pressure supposed to become 121 mmHg, a value for a “good blood pressure”.

What is the blood pressure for a person which consumes exactly 6 grams of salt per day?

predict(fit.lm, newdata = data.frame(salt = 6))
##        1 
## 135.7978

The blood pressure for a person which consumes exactly 6 grams of salt per day is 136 mmHg.

How much is the blood pressure expected to raise for every extra gram of salt? For every extra gram of salt, the blood pressure is expected to raise of 1.197 mmHg, the value of the slope.

Multiple linear regression

How to predict the volume of wood

We load the data of the cherry.dat file.

cherry <- read.table(file="C:/Users/valer/Downloads/cherry.dat", header = TRUE)

We graphically inspect the data.

str(cherry)
## 'data.frame':    31 obs. of  3 variables:
##  $ Girth : num  8.3 8.6 8.8 10.5 10.7 10.8 11 11 11.1 11.2 ...
##  $ Height: int  70 65 63 72 81 83 66 75 80 75 ...
##  $ Volume: num  10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 ...

We see three variables measured on 31 black cherry trees: the girth (circumference) of the trunk (Girth, in inches), the height (Height,in feet) and the volume of wood obtained from the cut trees (Volume, in cubic feet). We want to use this data set to formulate an equation to predict the volume of wood, which is only known after cutting and machining, from the other two variables, which can be easily measured before cutting. Equations of this kind have various applications.

We explore the relationships among the three variables.

plot(cherry)

We want to use this data set to formulate an equation to predict the volume of wood. To obtain the matrix of correlation coefficients with pairwise p-values:

library(RcmdrMisc)
## Caricamento del pacchetto richiesto: car
## Caricamento del pacchetto richiesto: carData
## Caricamento del pacchetto richiesto: sandwich
rcorr.adjust(cherry, type="pearson")
## 
##  Pearson correlations:
##         Girth Height Volume
## Girth  1.0000 0.5193 0.9671
## Height 0.5193 1.0000 0.5982
## Volume 0.9671 0.5982 1.0000
## 
##  Number of observations: 31 
## 
##  Pairwise two-sided p-values:
##        Girth  Height Volume
## Girth         0.0028 <.0001
## Height 0.0028        0.0004
## Volume <.0001 0.0004       
## 
##  Adjusted p-values (Holm's method)
##        Girth  Height Volume
## Girth         0.0028 <.0001
## Height 0.0028        0.0008
## Volume <.0001 0.0008

All the p-values are low, this suggest strong evidence against the null hypothesis of no correlation, so all the correlations are significantly different from 0. All the three variables are correlated. ‘Girth’ and ‘Volume’ have a very high positive correlation, while ‘Height’ and ‘Volume’ have a moderate positive correlation. ‘Girth’ and ‘Height’ demonstrate a comparatively lower positive correlation.

Multiple linear regression model of the type: Volume = beta0 + beta1Girth + beta2Height + error Using these three variables we want to find the best plane 3d plot.

#volume = b0 + b1*girth + b2*height + error : multiple linear regression model
fit1 <- lm(Volume ~ Girth + Height, data = cherry) #linear model
summary(fit1)
## 
## Call:
## lm(formula = Volume ~ Girth + Height, data = cherry)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.4065 -2.6493 -0.2876  2.2003  8.4847 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -57.9877     8.6382  -6.713 2.75e-07 ***
## Girth         4.7082     0.2643  17.816  < 2e-16 ***
## Height        0.3393     0.1302   2.607   0.0145 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.882 on 28 degrees of freedom
## Multiple R-squared:  0.948,  Adjusted R-squared:  0.9442 
## F-statistic:   255 on 2 and 28 DF,  p-value: < 2.2e-16

All the three p-values are closer to 0 suggesting a significant correlation of the variables. Both Girth and Height have a significant impact on Volume. Girth of 4.7 tells us that this variable has an higher impact in increasing the volume then the height. In other words the girth affects more the volume then the height.Height has a smaller impact on the growth of the volume of the tree. Intercept of -58 is the value at which we consider the girth and the height equal to 0. In this case it doesn’t have sense to consider height and girth equal to 0 for a tree.

The multiple R-squared value measures the goodness of fit of the regression model. It tells us how well the independent variables (Girth and Height) explain the variability observed in the dependent variable (Volume). In this case, an R-squared value of 0.948 means that approximately 94.8% of the variance in the Volume can be explained by the independent variables (Girth and Height) included in the model.

Volume = b0 + b1Girth + b2Height + b3(Girth2 Height) + error The base area in turn is linked to the square of the circumference. Now we create a new variable called GGH (Girth x Girth x Height) and re-fit the model. The model “formula” now must be Volume ∼ Girth + Height + GGH.

cherry$GGH <- cherry$Girth^2 * cherry$Height
fit2 <- lm(Volume ~ Girth + Height + GGH, data = cherry)
summary(fit2)
## 
## Call:
## lm(formula = Volume ~ Girth + Height + GGH, data = cherry)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9338 -1.0950 -0.1576  1.7237  4.2030 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.6533133 10.9385947  -0.151    0.881    
## Girth       -0.0942183  0.8132927  -0.116    0.909    
## Height       0.0299220  0.1003890   0.298    0.768    
## GGH          0.0021474  0.0003551   6.047 1.87e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.576 on 27 degrees of freedom
## Multiple R-squared:  0.9779, Adjusted R-squared:  0.9754 
## F-statistic: 398.1 on 3 and 27 DF,  p-value: < 2.2e-16

GGH has a statistically significant positive impact on Volume. For each unit increase in GGH, Volume increases by approximately 0.0021474 units, holding other variables constant. However, Girth and Height do not appear to have a significant impact on Volume in this model, as indicated by their p-values (p > 0.05). On the other hand the p-value of GGH is low and closer to 0, so it is statistically significant. The high R-squared value (0.9779) suggests that approximately 97.8% of the variability in The volume can be explained by the combination of Girth, Height, and GGH in this model. However, while GGH seems to have a significant impact, Girth and Height, in this specific model, do not show statistically significant effects on Volume.

Was it useful to include the product into the model? Can we simplify the model?

cor(cherry)
##            Girth    Height    Volume       GGH
## Girth  1.0000000 0.5192801 0.9671194 0.9795503
## Height 0.5192801 1.0000000 0.5982497 0.5962755
## Volume 0.9671194 0.5982497 1.0000000 0.9888202
## GGH    0.9795503 0.5962755 0.9888202 1.0000000

GGH is a variable of already known information, we see high correlation between Girth and Height and GGH. GGH is quite predictable from Girth (0.9795503). GGH is quite high correlate to height (0.5962755). It wasn’t useful to include the product into the model it doesn’t add something new to the model.

Temperature, age, heart rate and sex

The temperature.dat data has four variables: temperatura = temperature, anni = age, battito = heart rate, sesso = sex. The purpose of the analysis is to assess whether temperature varies with heart rate, sex and age.

temp_data <- read.table(file="C:/Users/valer/Downloads/temperature.dat", header = TRUE, stringsAsFactors=TRUE)
View(temp_data)
summary(temp_data)
##  sesso       anni          battito       temperatura   
##  F:51   Min.   :21.00   Min.   :61.00   Min.   :36.90  
##  M:49   1st Qu.:33.75   1st Qu.:69.00   1st Qu.:37.50  
##         Median :37.00   Median :73.00   Median :37.90  
##         Mean   :37.62   Mean   :73.66   Mean   :37.97  
##         3rd Qu.:42.00   3rd Qu.:78.00   3rd Qu.:38.40  
##         Max.   :50.00   Max.   :87.00   Max.   :39.20

These observations give an overview of the demographics, heart rate, and body temperature statistics across genders and age groups in the dataset. Our aim is to asses if body temperature is influenced by hearth rate, sex, age. Body temperature is our response variable. Sex is the genre of the individuals and it is a discrete variable. We use stringAsfactor to transform categorical variable n factor type.

plot(temp_data)

To visualize the temperature range we use an histogram of temperature.

hist(temp_data $temperatura, xlab="temperature",
main="Histogram of temperature", breaks=10,xlim = c(35,40))

To assess whether/how each single variable impacts on temperature try the following: − for sex, compare the boxplots of temperatura using sesso as grouping variable.

boxplot(temperatura ~ sesso, data=temp_data,col= c("lightpink","lightblue"))

The statistical significance can be assessed with Student’s t test or with Wilcoxon’s two-sample test.

t.test(temperatura~sesso, alternative="two.sided", conf.level=.95,
var.equal=FALSE, data=temp_data)
## 
##  Welch Two Sample t-test
## 
## data:  temperatura by sesso
## t = -0.041274, df = 96.62, p-value = 0.9672
## alternative hypothesis: true difference in means between group F and group M is not equal to 0
## 95 percent confidence interval:
##  -0.2259003  0.2166966
## sample estimates:
## mean in group F mean in group M 
##        37.96275        37.96735
wilcox.test(temperatura ~ sesso, alternative="two.sided", data=temp_data)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  temperatura by sesso
## W = 1245.5, p-value = 0.9807
## alternative hypothesis: true location shift is not equal to 0

− for the two “numerical” variables, that is, battito and anni, check the pairs plots

pairs(temp_data[,-1],col="blue")

correlation_matrix <- cor(temp_data[, c("anni", "battito", "temperatura")])
summary(correlation_matrix)
##       anni             battito        temperatura      
##  Min.   :-0.08265   Min.   :0.1041   Min.   :-0.08265  
##  1st Qu.: 0.01073   1st Qu.:0.3886   1st Qu.: 0.29522  
##  Median : 0.10410   Median :0.6731   Median : 0.67308  
##  Mean   : 0.34048   Mean   :0.5924   Mean   : 0.53015  
##  3rd Qu.: 0.55205   3rd Qu.:0.8365   3rd Qu.: 0.83654  
##  Max.   : 1.00000   Max.   :1.0000   Max.   : 1.00000
cor(temp_data[, c("anni", "battito", "temperatura")])
##                    anni   battito temperatura
## anni         1.00000000 0.1040982 -0.08264726
## battito      0.10409824 1.0000000  0.67308417
## temperatura -0.08264726 0.6730842  1.00000000

The correlation between age and heart rate is approximately 0.1041, suggesting a weak positive correlation. There is a stronger positive correlation of around 0.6731 between heart rate and temperature.Between age and temperature, there seems to be a weak negative correlation of approximately -0.0826. In conclusion heart rate is more correlated to the temperature then the other so the heart rate is the main variable that affects the temperature.

We try to see what happens if we account for all three variables simultaneously by using a multiple linear regression model of the type. temperatura = β0 + β1battito + β2anni + β3sesso + errore where sesso = 1 for male and sesso = 0 for female subjects.

fit_mul <- lm(temperatura ~ . , data=temp_data)
summary(fit_mul)
## 
## Call:
## lm(formula = temperatura ~ ., data = temp_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.82673 -0.27669 -0.01731  0.26268  0.88376 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.177669   0.596141  55.654  < 2e-16 ***
## sessoM      -0.026742   0.081441  -0.328    0.743    
## anni        -0.013240   0.006391  -2.072    0.041 *  
## battito      0.071932   0.007740   9.293 4.91e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4065 on 96 degrees of freedom
## Multiple R-squared:  0.4772, Adjusted R-squared:  0.4609 
## F-statistic: 29.21 on 3 and 96 DF,  p-value: 1.661e-13

The intercept (33.1777) represents the expected value of the dependent variable (temperature) when all independent variables are zero. Gender (sessoM) and Age (anni) have coefficients with p-values higher than 0.05, suggesting they might not be statistically significant predictors of temperature in this model. However, Heart Rate (battito) appears to have a statistically significant positive impact on temperature. The model as a whole seems to predict around 47.72% of the variability in temperature based on the combination of gender, age, and heart rate. However, the significance of gender and age as predictors appears uncertain in this particular analysis.

confint(fit_mul)
##                   2.5 %        97.5 %
## (Intercept) 31.99433919 34.3609988145
## sessoM      -0.18840232  0.1349175238
## anni        -0.02592530 -0.0005546357
## battito      0.05656804  0.0872962674

When zero is included into the interval it is not significant. The interval of “battito” excludes zero, indicating that heart rate is likely a statistically significant predictor of temperature. Also the variable “anni” does not include 0 i the interval but the range considered is all negative and “negative age” doesn’t exist. Heart rate seems to have a statistically significant impact on temperature as its interval doesn’t include zero. It has significance in predicting temperature in this model.

We provide the average temperature for two different gender groups, females and males, within the dataset.

by(temp_data$temperatura, temp_data$sesso, mean)
## temp_data$sesso: F
## [1] 37.96275
## ------------------------------------------------------------ 
## temp_data$sesso: M
## [1] 37.96735

These values suggest a very slight difference in average temperature between females and males in this dataset. The difference seems marginal, with males having a slightly higher average temperature compared to females.

Olive oil

To sart we load the data of the oliveoil.dat file and we inspect the data.

oliveoil <- read.table("C:/Users/valer/Downloads/oliveoil (1).dat", header = TRUE)
summary(oliveoil)
##     Acidity          Peroxide          K232            K270       
##  Min.   :0.1500   Min.   : 8.14   Min.   :1.331   Min.   :0.0850  
##  1st Qu.:0.1900   1st Qu.:10.95   1st Qu.:1.536   1st Qu.:0.1015  
##  Median :0.2600   Median :12.40   Median :1.653   Median :0.1160  
##  Mean   :0.3119   Mean   :13.25   Mean   :1.708   Mean   :0.1181  
##  3rd Qu.:0.3125   3rd Qu.:15.38   3rd Qu.:1.893   3rd Qu.:0.1285  
##  Max.   :0.7300   Max.   :19.40   Max.   :2.222   Max.   :0.1680  
##        DK          
##  Min.   :-0.00500  
##  1st Qu.:-0.00325  
##  Median :-0.00200  
##  Mean   :-0.00175  
##  3rd Qu.: 0.00000  
##  Max.   : 0.00300

There are 16 samples of olive oil whose acidity was measured together with further 4 physico-chemical variables (peroxide, K232, K270, DK). We want to assess whether these four variables influence acidity.

To graphically representing the data using pairs plots (“scatterplot matrix”), estimate the correlations present among the variables and verify their statistical significance.

str(oliveoil)
## 'data.frame':    16 obs. of  5 variables:
##  $ Acidity : num  0.73 0.19 0.26 0.67 0.52 0.26 0.24 0.3 0.35 0.19 ...
##  $ Peroxide: num  12.7 12.3 10.3 13.7 11.2 18.7 15.3 18.5 15.6 19.4 ...
##  $ K232    : num  1.9 1.68 1.63 1.7 1.54 ...
##  $ K270    : num  0.139 0.116 0.116 0.168 0.119 0.142 0.116 0.125 0.104 0.158 ...
##  $ DK      : num  0.003 -0.004 -0.005 -0.002 -0.001 0.001 0 0.001 0 -0.003 ...
plot(oliveoil)

library(RcmdrMisc)
rcorr.adjust(oliveoil, type="pearson")
## 
##  Pearson correlations:
##          Acidity Peroxide   K232   K270     DK
## Acidity   1.0000   0.0467 0.1070 0.4796 0.5289
## Peroxide  0.0467   1.0000 0.8740 0.5535 0.4865
## K232      0.1070   0.8740 1.0000 0.7104 0.5356
## K270      0.4796   0.5535 0.7104 1.0000 0.3112
## DK        0.5289   0.4865 0.5356 0.3112 1.0000
## 
##  Number of observations: 16 
## 
##  Pairwise two-sided p-values:
##          Acidity Peroxide K232   K270   DK    
## Acidity          0.8637   0.6933 0.0602 0.0351
## Peroxide 0.8637           <.0001 0.0261 0.0560
## K232     0.6933  <.0001          0.0020 0.0325
## K270     0.0602  0.0261   0.0020        0.2408
## DK       0.0351  0.0560   0.0325 0.2408       
## 
##  Adjusted p-values (Holm's method)
##          Acidity Peroxide K232   K270   DK    
## Acidity          1.0000   1.0000 0.2799 0.2276
## Peroxide 1.0000           <.0001 0.2091 0.2799
## K232     1.0000  <.0001          0.0184 0.2276
## K270     0.2799  0.2091   0.0184        0.7223
## DK       0.2276  0.2799   0.2276 0.7223

We perform a multiple linear regression model of the type: Acidity: β0 + β1Peroxide + β2K232 + β3K270 + β4DK + error, in which acidity is the dependent variable.

fit_mul <- lm(Acidity ~ Peroxide + K232 + K270 + DK , data=oliveoil)
summary(fit_mul)
## 
## Call:
## lm(formula = Acidity ~ Peroxide + K232 + K270 + DK, data = oliveoil)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.17705 -0.05037  0.01551  0.05681  0.12362 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.7584992  0.2638900   2.874  0.01512 * 
## Peroxide     0.0003056  0.0171446   0.018  0.98610   
## K232        -0.6772869  0.2837587  -2.387  0.03606 * 
## K270         6.8563054  1.6802045   4.081  0.00182 **
## DK          59.2735054 14.5690910   4.068  0.00186 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1058 on 11 degrees of freedom
## Multiple R-squared:  0.7367, Adjusted R-squared:  0.641 
## F-statistic: 7.695 on 4 and 11 DF,  p-value: 0.00328

The intercept (0.7585) represents the expected value of the dependent variable (temperature) when all independent variables are zero. Only K232, K270, and DK seem to have statistically significant impacts on Acidity due to their low p-values. Peroxide does not appear to have a significant relevance on Acidity as its p-value is considerably higher (p > 0.05). K232 has a negative impact on Acidity. An increase in K232 results in a decrease in Acidity, while K270 and DK have positive impacts on Acidity. An increase in these variables leads to an increase in Acidity.

In conclusion the model suggests that K232, K270, and DK are significant predictors of Acidity in the context of this dataset. However, Peroxide doesn’t appear to have a significant effect on Acidity according to this analysis.

confint(fit_mul)
##                  2.5 %      97.5 %
## (Intercept)  0.1776813  1.33931707
## Peroxide    -0.0374295  0.03804073
## K232        -1.3018356 -0.05273825
## K270         3.1582002 10.55441065
## DK          27.2071523 91.33985842

The intervals of the variables K232, K270 and DK do not include zero so they are statistically significant predictor of acidity of the olive oil. On the contrary peroxide which includes zero is not significant.

The model of the correlation coefficients between variables helps us understanding the linear relationships between the variables.

cor(oliveoil)
##            Acidity  Peroxide      K232      K270        DK
## Acidity  1.0000000 0.0466637 0.1069761 0.4795511 0.5289357
## Peroxide 0.0466637 1.0000000 0.8739770 0.5534638 0.4865444
## K232     0.1069761 0.8739770 1.0000000 0.7104164 0.5355591
## K270     0.4795511 0.5534638 0.7104164 1.0000000 0.3111607
## DK       0.5289357 0.4865444 0.5355591 0.3111607 1.0000000

Thanks to this function we are able to say that Acidity is predictable from K232, K270 and DK with more confidence.

chooseCRANmirror(ind=1)  # This will prompt you to choose a mirror interactively
install.packages("rmarkdown")
## Installazione pacchetto in 'C:/Users/valer/AppData/Local/R/win-library/4.3'
## (perché 'lib' non è specificato)
## pacchetto 'rmarkdown' aperto con successo con controllo somme MD5
## 
## I pacchetti binari scaricati sono in
##  C:\Users\valer\AppData\Local\Temp\RtmpUpgW7O\downloaded_packages