Descriptive analysis of the forbes data

Programming and Data Analysis with R

Author
Affiliation

Tommaso Rigon

Università degli Studi di Milano-Bicocca

Homepage

Topics covered

  • Covariance and correlation
  • The simple linear regression model
  • Least squares and goodness of fit
NoteNote

The exercises associated with this unit are available at this link.

Description of the problem

For n = 17 locations in the Alps, the atmospheric pressure (inHg, that is “inches of mercury”) and the boiling temperature of water (in degrees Fahrenheit) are measured.

The data come from an experiment carried out by the Scottish physicist Forbes in 1857.

Forbes was interested in estimating the altitude through the pressure. However, at the time the barometer was a heavy and expensive instrument.

In the mountains water boils at a different temperature, so that one can try to estimate the pressure starting from the boiling temperature.

NoteResearch question

Can we predict the atmospheric pressure from the boiling temperature of water?

Importing the forbes data

As done previously, first of all you need to download the file forbes.csv and save it on your computer. Link to the file

forbes <- read.table("data/forbes.csv", header = TRUE, sep = ",")

Alternatively, we can simply obtain them using the link:

path <- "https://tommasorigon.github.io/EnvEcoStat/data/forbes.csv"
forbes <- read.table(path, header = TRUE, sep = ",")
str(forbes)
'data.frame':   17 obs. of  2 variables:
 $ bp  : num  194 194 198 198 199 ...
 $ pres: num  20.8 20.8 22.4 22.7 23.1 ...

Preliminary operations

For interpretative reasons, we convert the temperature from degrees Fahrenheit to degrees Celsius, recalling that

(\text{``Fahrenheit''}) = 32 + \frac{9}{5}(\text{``Celsius''}).

colnames(forbes) <- c("TempF", "Pressure") # Change the names of the variables

forbes$TempC <- round((forbes$TempF - 32) * 5 / 9, 2) # From Fahrenheit to Celsius
summary(forbes)
     TempF          Pressure         TempC       
 Min.   :194.3   Min.   :20.79   Min.   : 90.17  
 1st Qu.:199.4   1st Qu.:23.15   1st Qu.: 93.00  
 Median :201.3   Median :24.01   Median : 94.06  
 Mean   :203.0   Mean   :25.06   Mean   : 94.97  
 3rd Qu.:208.6   3rd Qu.:27.76   3rd Qu.: 98.11  
 Max.   :212.2   Max.   :30.06   Max.   :100.11  

Why do we round the values using round? For aesthetic reasons only.

Histogram

Let us first look at the distribution of the boiling temperature. We can decide to specify the intervals of the classes ourselves, or to leave this choice to R.

par(mfrow = c(1, 2)) # Split the graphical window into 2 parts

# Option 1, for a total of 6 equally spaced classes
hist(forbes$TempC) # Equivalent to: hist(forbes$TempC, breaks = "sturges")

# Option 2, we define manually 5 equally spaced classes
breaks <- c(90, 92.5, 95, 97.5, 100, 102.5)
hist(forbes$TempC, breaks = breaks)

The histogram on the left uses 6 classes. The one on the right uses 5 classes.

Location indices

We have already computed the means through the command summary; for completeness:

mean(forbes$TempC)
[1] 94.97353
mean(forbes$Pressure)
[1] 25.05882

What is the average boiling temperature in degrees Fahrenheit? We do not need the values of TempF: it is enough to use the properties of the mean.

32 + 9 / 5 * mean(forbes$TempC) # Using the properties of the mean
[1] 202.9524
mean(forbes$TempF) # Computes the mean from the original data
[1] 202.9529

Variability indices

Since it will turn out to be useful later, we define the function my_var that computes the variance, as in Unit E.

# Yes, the function is defined on a single line and there is nothing wrong with it
my_var <- function(x) mean(x^2) - mean(x)^2

# Computation of the two variances
my_var(forbes$TempC)
[1] 9.630811
my_var(forbes$Pressure)
[1] 8.584575

The scatterplot

To understand the relationship between the two variables, we need the new command plot, which can be used (among other things!) to build a scatterplot.

We provide two versions of the same plot; the second one contains aesthetic improvements.

par(mfrow = c(1, 1)) # We want to show one plot at a time
plot(forbes$TempC, forbes$Pressure)

plot(forbes$TempC, forbes$Pressure,
  pch = 16,
  xlab = "Temperature", ylab = "Pressure"
)

It is therefore evident that the data are approximately (although not perfectly) aligned.

Covariance

To measure the strength of this relationship, we first have to obtain the covariance between two variables.

The covariance between two sets of data x_1,\dots,x_n and y_1,\dots,y_n is defined as

\text{cov}(x,y) = \frac{1}{n}\sum_{i=1}^n(x_i - \bar{x})(y_i - \bar{y}) = \frac{1}{n}\sum_{i=1}^nx_i y_i - \bar{x}\bar{y}.

We therefore define the function my_cov, which computes precisely the covariance:

my_cov <- function(x, y) mean(x * y) - mean(x) * mean(y)
my_cov(forbes$TempC, forbes$Pressure) # = my_cov(forbes$Pressure, forbes$TempC)
[1] 9.067404

In R there is also the command cov which, as in the case of the variance, divides the sum by (n - 1) and not by n, for reasons related to statistical inference:

cov(forbes$Pressure, forbes$TempC) # = 17 / 16 * my_cov(forbes$TempC, forbes$Pressure)
[1] 9.634117

Correlation

The correlation index is defined as: \text{cor}(x,y) = \frac{\text{cov}(x,y)}{\sqrt{\text{var}(x) \text{var}(y)}}, where \text{var}(x) and \text{var}(y) are the variances of Unit E, computed by my_var.

Therefore, we can compute the correlation as follows:

my_cov(forbes$TempC, forbes$Pressure) /
  sqrt(my_var(forbes$TempC) * my_var(forbes$Pressure))
[1] 0.9972227
cov(forbes$TempC, forbes$Pressure) /
  sqrt(var(forbes$TempC) * var(forbes$Pressure))
[1] 0.9972227

In R there is also the command cor, which returns the correlation

correlation <- cor(forbes$TempC, forbes$Pressure)
correlation
[1] 0.9972227

The least squares line I

In a linear model of the form y_i = \alpha + \beta x_i + \epsilon_i, the least squares estimates are equal to \hat{\alpha} = \bar{y} - \hat{\beta}\:\bar{x}, \qquad \hat{\beta} = \frac{\text{cov}(x,y)}{\text{var}(x)}.

Therefore, we can compute them as follows:

# Slope
beta_hat <- my_cov(forbes$TempC, forbes$Pressure) / my_var(forbes$TempC)
# Intercept
alpha_hat <- mean(forbes$Pressure) - mean(forbes$TempC) * beta_hat

c(alpha_hat, beta_hat)
[1] -64.3587103   0.9414995

The least squares line II

We can now draw the estimated line on the scatterplot:

plot(forbes$TempC, forbes$Pressure,
  pch = 16,
  xlab = "Temperature", ylab = "Pressure"
)
abline(a = alpha_hat, b = beta_hat)

NoteNote

The same estimates are obtained with the command lm, which we will meet again in the part of the course devoted to linear models:

lm(Pressure ~ TempC, data = forbes)

Call:
lm(formula = Pressure ~ TempC, data = forbes)

Coefficients:
(Intercept)        TempC  
   -64.3587       0.9415  

Prediction

Using the estimates obtained, we can quickly compute the predicted pressure for any boiling temperature:

x <- seq(from = 90, to = 100, length = 20)
alpha_hat + beta_hat * x
 [1] 20.37625 20.87177 21.36730 21.86283 22.35835 22.85388 23.34940 23.84493
 [9] 24.34046 24.83598 25.33151 25.82703 26.32256 26.81809 27.31361 27.80914
[17] 28.30467 28.80019 29.29572 29.79124

In particular, if water boils at 97 degrees Celsius, the estimated pressure is:

alpha_hat + beta_hat * 97
[1] 26.96674

Goodness of fit

How well does the line fit the data? The coefficient R^2 for a simple linear regression model is defined as: R^2 = 1 - \frac{\text{var}(e)}{\text{var}(y)} = \text{cor}(x,y)^2, where e_1,\dots,e_n are the residuals.

First of all we therefore compute the residuals:

residuals <- forbes$Pressure - (alpha_hat + beta_hat * forbes$TempC)

The coefficient R^2 can then be obtained in two different ways:

correlation^2
[1] 0.994453
1 - my_var(residuals) / my_var(forbes$Pressure)
[1] 0.994453