It is first necessary to upload the txt files to R and extract the data, which we are going to do by using the \(read.table\) function:
men = read.table("~/Downloads/data9b_m.txt", header = TRUE, sep = '\t')
women = read.table("~/Downloads/data9b_w.txt", header = TRUE, sep = '\t')
Please note the files are fully extracted. Let’s use the \(str\) function to show that. \(str\) stands for structure and show how many columns, how many rows, and what are the names and types of the variables.
str(men)
## 'data.frame': 865 obs. of 3 variables:
## $ ID : int 1 2 6 7 8 10 11 13 17 18 ...
## $ steps: int 15000 15000 14861 14861 14699 14560 14560 14560 14560 14560 ...
## $ bmi : num 16.9 16.9 16.8 16.8 17.3 20.5 20.6 20.5 20.4 20.4 ...
str(women)
## 'data.frame': 921 obs. of 3 variables:
## $ ID : int 3 4 5 9 12 14 15 16 21 23 ...
## $ steps: int 15000 14861 14861 14699 14560 14560 14560 14560 14560 14560 ...
## $ bmi : num 17 17.2 17.2 16.8 20.4 20.5 20.5 20.5 19.7 19.6 ...
Now, let’s calculate the mean for the steps of both men and women, per prediction (a):
mean(men$steps)
## [1] 8013.908
mean(women$steps)
## [1] 6891.237
Prediction (a) is, indeed, true. As it is possible to see from the calculations, men average more than 1000 steps more than women.
Now, let’s move forward to predictions (b) and (c). To verify these, we must calculate the Pearson correlation coefficient between the number of steps and the bmi for men and women separately.
coefficient_correlation_men <- (cov(men$steps, men$bmi, method = "pearson"))/(sd(men$steps)*sd(men$bmi))
coefficient_correlation_women <- (cov(women$steps, women$bmi, method = "pearson"))/(sd(women$steps)*sd(women$bmi))
Now that we created the variables with each correspondent value, let’s print them to discover the value. First, for prediction (b):
coefficient_correlation_women
## [1] -0.3123607
The result is, indeed, negative. The prediction holds, i.e. it is true.
Now, let’s proceed to prediction (c):
coefficient_correlation_men
## [1] -0.2303534
The resut is negative. Therefore, this prediction is false.
We know that a perfect -1 negative correlation coefficient means an inverse relationship between the values of the two variables, i.e. as one variable increases, the other decreases and vice-versa.
Also, a perfect +1 positive correlation means a direct relationship between the two variables.
Finally, a 0 correlation means that there is no correlation between the two variables.
We see that both values, despite negative, are close to 0. Therefore, we can conclude that, even though the number of steps is somewhat inversely proportional to a person’s BMI, this is not a very clear/strong correlation.