hemoglobin <- read.table("data/hemoglobin.csv",
header = TRUE, sep = ",", stringsAsFactors = TRUE
)Descriptive analysis of the hemoglobin data
Programming and Data Analysis with R
Homepage
Topics covered
- Variance and standard deviation
- Other measures of variability (range, interquartile range)
Description of the problem
We are interested in comparing the effectiveness of two different methods, called A and B, for the measurement of haemoglobin in blood.
Artificial blood containing 15 grams of haemoglobin every 100\,\text{cm}^3 has been created in the laboratory.
A total of n = 360 samples were drawn from the compound.
Of these, in n_A = 180 samples the haemoglobin was measured using method A, whereas for the remaining n_B = 180 samples method B was used.
The differences between the various measurements are largely to be attributed to the measurement errors of the two different methods.
Importing the hemoglobin data
As done previously, first of all you need to download the file hemoglobin.csv and save it on your computer. Link to the file
Alternatively, we can simply obtain them using the link:
path <- "https://tommasorigon.github.io/EnvEcoStat/data/hemoglobin.csv"
hemoglobin <- read.table(path, header = TRUE, sep = ",", stringsAsFactors = TRUE)str(hemoglobin)'data.frame': 360 obs. of 2 variables:
$ hemoglobin: num 15 15.1 15.2 14.8 15 ...
$ method : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
The option stringsAsFactors = TRUE implies that the variable method is coded as a factor and not as a character.
Preliminary operations
In this format the data are hard to analyse, at least without using the advanced *apply functions or the tools of the so-called tidyverse.
Therefore, we create the two variables hemo_A and hemo_B, containing the haemoglobin values for the two methods.
hemo_A <- hemoglobin$hemoglobin[hemoglobin$method == "A"] # Group A
hemo_B <- hemoglobin$hemoglobin[hemoglobin$method == "B"] # Group B
summary(hemo_A) Min. 1st Qu. Median Mean 3rd Qu. Max.
14.44 14.87 15.00 15.00 15.14 15.64
summary(hemo_B) Min. 1st Qu. Median Mean 3rd Qu. Max.
13.83 14.83 15.02 15.02 15.22 15.80
boxplot(hemoglobin$hemoglobin ~ hemoglobin$method,
ylab = "Haemoglobin",
xlab = "Method"
)
From these first descriptive analyses it emerges that both methods are, at least on average, well calibrated. Which of the two is preferable?
The variance I
Recall that the variance of the data y_1,\dots,y_n is equal to
\text{var}(y) = \frac{1}{n}\sum_{i=1}^n (y_i - \bar{y})^2 = \left(\frac{1}{n}\sum_{i=1}^n y_i^2\right) - \bar{y}^2.
We can therefore compute the variances of the two groups “manually”:
mean((hemo_A - mean(hemo_A))^2) # Variance of group A[1] 0.0456162
mean((hemo_B - mean(hemo_B))^2) # Variance of group B[1] 0.09901038
mean(hemo_A^2) - mean(hemo_A)^2 # Variance of group A, alternative formula[1] 0.0456162
mean(hemo_B^2) - mean(hemo_B)^2 # Variance of group B, alternative formula[1] 0.09901038
Method B is therefore characterised by a larger variability.
The variance II
For convenience, we create a new function called my_var that computes the variance of a generic vector x. We will therefore have:
my_var <- function(x) {
mean(x^2) - mean(x)^2
}
my_var(hemo_A)[1] 0.0456162
my_var(hemo_B)[1] 0.09901038
In R the function var is available, however this computes a slightly different quantity, that is
s^2 = \frac{1}{n-1}\sum_{i=1}^n (y_i - \bar{y})^2,
that is the unbiased estimator of the variance.
Not clear why? It will be addressed in Unit I, devoted to sampling distributions and estimators.
The variance III
The difference between my_var and var is, in practice, essentially negligible in this case, indeed:
var(hemo_A)[1] 0.04587104
var(hemo_B)[1] 0.09956351
Standard deviation
The standard deviation is the square root of the variance, that is \text{sd}(y) = \sqrt{\text{var}(y)}.
We therefore create a suitable R function called my_sd to compute it.
Also in this case, be careful that the R command sd computes instead the value of
s = \sqrt{\frac{1}{n-1}\sum_{i=1}^n (y_i - \bar{y})^2}.
my_sd <- function(x) {
sqrt(my_var(x))
}
my_sd(hemo_A)[1] 0.2135795
my_sd(hemo_B)[1] 0.3146591
sd(hemo_A)[1] 0.2141753
sd(hemo_B)[1] 0.3155369
Range
The range is the difference between the minimum and the maximum of the distribution, that is (\text{``Range''}) = y_{(n)} - y_{(1)}, where y_{(1)} and y_{(n)} represent respectively the minimum and the maximum of the data.
Although a specific function to compute it does not exist, we can use the following commands
max(hemo_A) - min(hemo_A)[1] 1.20242
max(hemo_B) - min(hemo_B)[1] 1.97508
diff(range(hemo_A))[1] 1.20242
diff(range(hemo_B))[1] 1.97508
Interquartile range
The interquartile range is the difference between the third and the first quartile, that is (\text{``Interquartile range''}) = \mathcal{Q}_{0.75} - \mathcal{Q}_{0.25}.
It is much more resistant than the variance in the presence of a few extreme observations.
Its implementation in R is the following:
interquartile_range <- function(x) {
diff(quantile(x, probs = c(0.25, 0.75)))
}
interquartile_range(hemo_A) 75%
0.2699275
interquartile_range(hemo_B) 75%
0.3929925