Load Library

library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2

Import Excel Dataset

df <- data.frame(read_excel("C:\\Users\\Muhammad Bilal\\Desktop\\Project/Sports_People_data.xlsx"))
df
##        Sex    LBM    BMI
## 1   female 48.830 22.121
## 2     male 72.391 22.998
## 3   female 47.860 19.870
## 4   female 47.099 21.875
## 5   female 66.205 24.236
## 6     male 64.038 22.078
## 7     male 66.659 22.317
## 8     male 66.184 22.550
## 9     male 65.141 22.036
## 10  female 50.678 20.512
## 11  female 62.925 23.126
## 12    male 84.261 24.435
## 13  female 34.999 16.815
## 14    male 69.406 23.553
## 15    male 49.272 19.160
## 16  female 59.672 22.363
## 17    male 74.548 21.745
## 18  female 55.725 20.502
## 19    male 65.078 20.260
## 20    male 73.768 24.491
## 21  female 59.751 25.742
## 22    male 73.072 22.035
## 23    male 34.759 16.815
## 24    male 67.795 24.504
## 25    male 66.043 20.656
## 26    male 55.362 20.600
## 27    male 62.948 21.860
## 28  female 51.161 21.182
## 29    male 75.682 24.475
## 30  female 51.865 25.386
## 31    male 64.275 20.546
## 32  female 63.488 23.172
## 33    male 49.874 20.156
## 34  female 57.726 17.958
## 35    male 96.364 25.862
## 36  female 58.918 24.245
## 37    male 61.487 20.651
## 38  female 57.799 20.761
## 39  female 49.978 20.459
## 40  female 51.821 21.408
## 41  female 51.998 20.991
## 42    male 71.171 20.809
## 43    male 74.809 24.215
## 44    male 61.410 20.926
## 45    male 79.868 23.350
## 46  female 55.678 20.709
## 47    male 64.334 22.005
## 48    male 62.734 19.810
## 49  female 53.290 20.546
## 50  female 49.466 24.215
## 51    male 67.787 20.709
## 52    male 70.654 23.617
## 53  female 43.589 21.427
## 54    male 67.263 20.447
## 55    male 70.811 20.761
## 56    male 62.934 20.130
## 57  female 59.873 25.715
## 58    male 72.141 20.094
## 59  female 72.229 25.862
## 60  female 58.458 22.035
## 61  female 59.613 21.876
## 62    male 48.422 18.418
## 63    male 70.255 23.188
## 64  female 58.021 20.809
## 65    male 73.416 23.982
## 66    male 51.627 21.427
## 67  female 57.248 22.998
## 68    male 59.549 20.459
## 69  female 58.836 20.807
## 70  female 55.500 20.447
## 71  female 60.515 20.794
## 72    male 62.783 24.215
## 73  female 59.252 20.094
## 74    male 67.796 21.756
## 75  female 50.658 21.107
## 76  female 52.833 19.069
## 77  female 57.812 24.132
## 78  female 55.541 23.617
## 79  female 52.644 20.130
## 80    male 59.690 22.121
## 81  female 52.789 22.036
## 82    male 64.747 20.487
## 83    male 62.124 20.133
## 84    male 75.696 22.630
## 85  female 53.563 20.716
## 86    male 57.010 21.875
## 87  female 53.663 22.317
## 88    male 74.521 21.876
## 89  female 62.327 23.350
## 90    male 66.662 22.034
## 91  female 58.490 24.475
## 92    male 62.023 19.949
## 93  female 52.873 20.248
## 94    male 69.693 20.462
## 95  female 69.155 24.506
## 96    male 62.082 19.069
## 97  female 43.609 19.160
## 98  female 44.827 19.133
## 99  female 53.747 21.346
## 100 female 51.969 22.078
## 101 female 52.392 22.229
## 102 female 52.063 20.133
## 103 female 64.651 24.435
## 104   male 76.039 24.245
## 105   male 70.815 23.053
## 106   male 69.616 20.439
## 107   male 66.100 23.179
## 108 female 52.695 20.085
## 109   male 81.341 25.382
## 110 female 59.731 21.745
## 111   male 63.378 20.248
## 112   male 80.469 23.126
## 113 female 55.578 23.188
## 114 female 52.550 17.222
## 115   male 70.196 20.517
## 116   male 72.310 20.807
## 117   male 66.258 21.151
## 118   male 74.645 20.794
## 119 female 53.148 22.550
## 120   male 66.535 22.812
## 121   male 59.744 17.222
## 122   male 78.778 25.742
## 123 female 52.742 19.810
## 124 female 54.897 21.756
## 125   male 86.226 24.236
## 126 female 58.061 24.215
## 127 female 52.129 19.949
## 128 female 50.228 21.269
## 129 female 49.957 21.012
## 130   male 90.638 24.506
## 131   male 79.730 22.638
## 132   male 64.222 20.056
## 133   male 74.102 23.486
## 134 female 51.218 20.651
## 135   male 67.761 17.958
## 136   male 55.967 19.870
## 137   male 62.957 20.085
## 138   male 73.159 25.426
## 139   male 61.181 21.107
## 140   male 67.370 25.386
## 141 female 54.471 20.656
## 142   male 55.593 17.097
## 143 female 53.878 22.034
## 144   male 60.750 21.269
## 145 female 52.236 22.005
## 146 female 53.672 20.487
## 147 female 53.621 20.056
## 148 female 43.559 18.418
## 149   male 61.965 21.182
## 150 female 57.187 20.439
## 151   male 60.101 21.012
## 152 female 51.354 21.860
## 153   male 64.836 20.716
## 154   male 75.116 22.363
## 155 female 57.225 20.462
## 156 female 52.831 24.504
## 157   male 65.066 21.978
## 158   male 63.125 21.408
## 159   male 71.360 19.131
## 160   male 62.935 20.991
## 161 female 57.110 24.491
## 162   male 57.434 20.420
## 163   male 72.167 20.060
## 164   male 60.585 20.512
## 165   male 70.477 21.274
## 166   male 65.756 21.346
## 167   male 81.305 23.172
## 168 female 56.080 23.053
## 169 female 55.972 25.426
## 170   male 74.372 24.132
## 171   male 67.636 20.502
## 172 female 58.104 23.486
## 173 female 52.779 21.978
## 174   male 78.921 25.715
## 175 female 59.417 19.131
## 176   male 64.789 22.229
## 177 female 49.677 17.097
## 178   male 50.948 19.133

1. Perform an exploratory data analysis, taking care to describe the type of variables in the data set.

# Display the structure of the dataset
str(df)
## 'data.frame':    178 obs. of  3 variables:
##  $ Sex: chr  "female" "male" "female" "female" ...
##  $ LBM: num  48.8 72.4 47.9 47.1 66.2 ...
##  $ BMI: num  22.1 23 19.9 21.9 24.2 ...
# Summary statistics
summary(df)
##      Sex                 LBM             BMI       
##  Length:178         Min.   :34.76   Min.   :16.82  
##  Class :character   1st Qu.:53.63   1st Qu.:20.46  
##  Mode  :character   Median :60.55   Median :21.38  
##                     Mean   :61.60   Mean   :21.68  
##                     3rd Qu.:67.78   3rd Qu.:23.13  
##                     Max.   :96.36   Max.   :25.86
# Check the first few rows of the dataset
head(df)
##      Sex    LBM    BMI
## 1 female 48.830 22.121
## 2   male 72.391 22.998
## 3 female 47.860 19.870
## 4 female 47.099 21.875
## 5 female 66.205 24.236
## 6   male 64.038 22.078
# Descriptive statistics for numeric variables
summary(df$Sex)
##    Length     Class      Mode 
##       178 character character
# Descriptive statistics for categorical variables
table(df$LBM)
## 
## 34.759 34.999 43.559 43.589 43.609 44.827 47.099  47.86 48.422  48.83 49.272 
##      1      1      1      1      1      1      1      1      1      1      1 
## 49.466 49.677 49.874 49.957 49.978 50.228 50.658 50.678 50.948 51.161 51.218 
##      1      1      1      1      1      1      1      1      1      1      1 
## 51.354 51.627 51.821 51.865 51.969 51.998 52.063 52.129 52.236 52.392  52.55 
##      1      1      1      1      1      1      1      1      1      1      1 
## 52.644 52.695 52.742 52.779 52.789 52.831 52.833 52.873 53.148  53.29 53.563 
##      1      1      1      1      1      1      1      1      1      1      1 
## 53.621 53.663 53.672 53.747 53.878 54.471 54.897 55.362   55.5 55.541 55.578 
##      1      1      1      1      1      1      1      1      1      1      1 
## 55.593 55.678 55.725 55.967 55.972  56.08  57.01  57.11 57.187 57.225 57.248 
##      1      1      1      1      1      1      1      1      1      1      1 
## 57.434 57.726 57.799 57.812 58.021 58.061 58.104 58.458  58.49 58.836 58.918 
##      1      1      1      1      1      1      1      1      1      1      1 
## 59.252 59.417 59.549 59.613 59.672  59.69 59.731 59.744 59.751 59.873 60.101 
##      1      1      1      1      1      1      1      1      1      1      1 
## 60.515 60.585  60.75 61.181  61.41 61.487 61.965 62.023 62.082 62.124 62.327 
##      1      1      1      1      1      1      1      1      1      1      1 
## 62.734 62.783 62.925 62.934 62.935 62.948 62.957 63.125 63.378 63.488 64.038 
##      1      1      1      1      1      1      1      1      1      1      1 
## 64.222 64.275 64.334 64.651 64.747 64.789 64.836 65.066 65.078 65.141 65.756 
##      1      1      1      1      1      1      1      1      1      1      1 
## 66.043   66.1 66.184 66.205 66.258 66.535 66.659 66.662 67.263  67.37 67.636 
##      1      1      1      1      1      1      1      1      1      1      1 
## 67.761 67.787 67.795 67.796 69.155 69.406 69.616 69.693 70.196 70.255 70.477 
##      1      1      1      1      1      1      1      1      1      1      1 
## 70.654 70.811 70.815 71.171  71.36 72.141 72.167 72.229  72.31 72.391 73.072 
##      1      1      1      1      1      1      1      1      1      1      1 
## 73.159 73.416 73.768 74.102 74.372 74.521 74.548 74.645 74.809 75.116 75.682 
##      1      1      1      1      1      1      1      1      1      1      1 
## 75.696 76.039 78.778 78.921  79.73 79.868 80.469 81.305 81.341 84.261 86.226 
##      1      1      1      1      1      1      1      1      1      1      1 
## 90.638 96.364 
##      1      1
# Visualize the distribution of numeric variables
hist(df$BMI, main = "Histogram of Numeric Variable")

2. Using an appropriate statistical test, investigate whether there is a difference in mean LBM between males and females.

# Assuming your dataset has a column 'LBM' for Lean Body Mass and 'Gender' for gender
# H0: There is no difference in mean LBM between males and females
# H1: There is a significant difference in mean LBM between males and females

# Assuming 'data' is your dataset
t_test_result <- t.test(LBM ~ Sex, data = df)

# Display the t-test result
print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  LBM by Sex
## t = -11.316, df = 163.41, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
##  -14.95107 -10.50854
## sample estimates:
## mean in group female   mean in group male 
##             54.66394             67.39374
# Extract p-value
p_value <- t_test_result$p.value

# Check the p-value against your significance level (e.g., 0.05)
if (p_value < 0.05) {
  cat("Reject the null hypothesis. There is a significant difference in mean LBM between males and females.\n")
} else {
  cat("Fail to reject the null hypothesis. There is no significant difference in mean LBM between males and females.\n")
}
## Reject the null hypothesis. There is a significant difference in mean LBM between males and females.

3. For male and female sports people separately, calculate the correlation coefficient for LBM and BMI given and comment on the relationship between LBM and BMI.

# Assuming your data is stored in a data frame called 'df'
correlation_male <- cor(df[df$Sex == "male", "LBM"], df[df$Sex == "male", "BMI"])
correlation_female <- cor(df[df$Sex == "female", "LBM"], df[df$Sex == "female", "BMI"])

cat("Correlation coefficient for males:", correlation_male, "\n")
## Correlation coefficient for males: 0.6962944
cat("Correlation coefficient for females:", correlation_female, "\n")
## Correlation coefficient for females: 0.5557179

We would like to investigate a model to test the relationship between LBM and BMI for male sportspeople. You must include output from R to support your findings.Details you should include are:

(a) using your previous results comment on whether there would be any value in including the data for females in this model.

The decision of whether or not to include females in the model can be made with the help of the distinct correlation coefficients that you have computed for males and females. It is possible that there is a gender-dependent association between LBM and BMI if the correlation coefficient for females differs significantly from that for males. The correlation coefficients may indicate that gender is not a major factor in the relationship if they are similar. In order to take possible gender-specific impacts into consideration, you might also think about looking into interaction terms in your model.

(b) A description of the model;

# Fit a linear regression model for males
male_model <- lm(BMI ~ LBM, data = df[df$Sex == "male", ])

####(c) A summary of the fitted model with interpretation of test statistics and parameter estimates;

# Display summary of the model for males
summary(male_model)               
## 
## Call:
## lm(formula = BMI ~ LBM, data = df[df$Sex == "male", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.7861 -0.8917 -0.0947  0.8911  3.7004 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 11.60205    1.07653  10.777  < 2e-16 ***
## LBM          0.14967    0.01583   9.455 2.41e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.421 on 95 degrees of freedom
## Multiple R-squared:  0.4848, Adjusted R-squared:  0.4794 
## F-statistic:  89.4 on 1 and 95 DF,  p-value: 2.412e-15

Interpretation

The model suggests a statistically significant positive relationship between LBM and BMI for male sportspeople. This means that, on average, as LBM increases, BMI also tends to increase. [1] The overall fit of the model is statistically significant, indicating that the model explains a significant amount of variability in BMI. [1]

(d) Assumptions of the Model:

# Diagnostic plots for males
plot(male_model)

A linear regression model assumes a linear relationship between predictor variables and the response, implying that changes in predictors lead to proportional changes in the response. It requires that residuals be independent, ensuring that the error in predicting one observation does not influence another. Homoscedasticity, or constant variance of residuals, is crucial, indicating that prediction errors are consistent across different levels of predictor values. Normality of residuals is assumed, suggesting that the distribution of errors follows a normal distribution. Additionally, there should be no perfect multicollinearity among predictor variables, and for time-series data, residuals should exhibit no autocorrelation, meaning that errors at one point in time are not correlated with errors at nearby points. Ensuring these assumptions are met is essential for the reliability and validity of the linear regression model.

(e) Formal Test for Significant Linear Relationship:

# Test for overall significance of the model for males
anova(male_model)
## Analysis of Variance Table
## 
## Response: BMI
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## LBM        1 180.48 180.477  89.404 2.412e-15 ***
## Residuals 95 191.77   2.019                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

5. Use the model developed in Question 4 to predict the LBM for a male whose BMI is 25.

# Replace 'male_model' and 'df' with your actual variable names

# Create a data frame with the BMI value you want to predict for
main_data <- data.frame(LBM = 25)

# Use the predict() function to get the predicted LBM
predicted_LBM <- predict(male_model, newdata = main_data)

# Print the result
cat("Predicted LBM for a male with BMI 25:", predicted_LBM, "\n")
## Predicted LBM for a male with BMI 25: 15.34391

6. Assess the predictive performance of the model.

# Assuming you have a test set
# Replace 'test_data' with your actual test set
predictions <- predict(male_model, main_data = test_data)
mae <- mean(abs(predictions - main_data$LBM))
mse <- mean((predictions - main_data$LBM)^2)
r_squared <- summary(male_model)$r.squared

cat("Mean Absolute Error (MAE):", mae, "\n")
## Mean Absolute Error (MAE): 3.335434
cat("Mean Squared Error (MSE):", mse, "\n")
## Mean Squared Error (MSE): 12.82215
cat("R-squared:", r_squared, "\n")
## R-squared: 0.4848259