library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2
df <- data.frame(read_excel("C:\\Users\\Muhammad Bilal\\Desktop\\Project/Sports_People_data.xlsx"))
# 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")
# 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.
# 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
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.
# 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
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]
# 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.
# 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
# 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
# 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