NS<-read.csv("NutritionStudy.csv", header = TRUE)
print("H₀: μ = μ₀ H₁: μ ≠ μ₀")
## [1] "H₀: μ = μ₀ H₁: μ ≠ μ₀"
### This is a one sample t-test because the only data we're interested in is how much fiber people are eating daily
# Calculate the confidence interval
confidence_interval <- t.test(NS$Fiber, conf.level = 0.99)$conf.int
summary(NS$Fiber)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 3.10 9.15 12.10 12.79 15.60 36.80
print("The average amount of fiber people eat each day is 12.79 grams")
## [1] "The average amount of fiber people eat each day is 12.79 grams"
gend_smoke <- subset(NS, select = c(Sex, Smoke))
male_smokers <- sum(gend_smoke$Sex == "Male" & gend_smoke$Smoke == "Yes")
female_smokers <- sum(gend_smoke$Sex == "Female" & gend_smoke$Smoke == "Yes")
total_smokers <- male_smokers + female_smokers
percent_male_smokers <- (male_smokers / total_smokers) * 100
percent_female_smokers <- (female_smokers / total_smokers) * 100
pie(c(percent_male_smokers, percent_female_smokers),
labels = c("Male", "Female"),
main = "Percentage of Smokers by Gender",
col = c("blue", "pink"))
b) Do the sample data provide evidence that the effect of gender on
smoking is statistically significant? In other words, Is there evidence
of a difference in the percentage of current smokers by gender?
cat("There is statistically significant data showing gender affects smoking")
## There is statistically significant data showing gender affects smoking
cat("Percent Male Smokers: 16.28% Percent Female Smokers: 83.72%")
## Percent Male Smokers: 16.28% Percent Female Smokers: 83.72%
NS$Smoke <- as.factor(NS$Smoke)
t_test_result <- t.test(BetaPlasma ~ Smoke, data = NS)
ci <- t_test_result$conf.int
p_value <- t_test_result$p.value
cat("95% Confidence Interval for the difference in mean BetaPlasma levels between smokers and non-smokers:", ci[1], "to", ci[2], "\n")
## 95% Confidence Interval for the difference in mean BetaPlasma levels between smokers and non-smokers: 46.29873 to 112.5133
cat("P-value:", p_value, "\n")
## P-value: 5.174922e-06
cat("There is statistically significant evidence that smoking affects the beta-carotene levels in the blood.\n")
## There is statistically significant evidence that smoking affects the beta-carotene levels in the blood.
library(ggplot2)
ggplot(NS, aes(x = Smoke, y = BetaPlasma, color = Smoke)) + geom_point()
NS$Sex <- as.factor(NS$Sex)
t_test_sex <- t.test(NS$BetaPlasma ~ NS$Sex, conf.level = 0.90)
ci_sex_90 <- t_test_sex$conf.int
cat("90% Confidence Interval for difference in Beta Plasma levels in Males VS Females:", ci_sex_90[1], "to", ci_sex_90[2], "\n")
## 90% Confidence Interval for difference in Beta Plasma levels in Males VS Females: 8.26892 to 86.86661
t_test_result <- t.test(NS$BetaPlasma ~ NS$Sex)
p_value <- t_test_result$p.value
cat("P-value:", p_value, "\n")
## P-value: 0.04747518
cat("Sex has a statistically significant effect on Beta-Carotene levels in the blood. \n")
## Sex has a statistically significant effect on Beta-Carotene levels in the blood.
cor_test_result <- cor.test(NS$Age, NS$Alcohol)
cat("Correlation Coefficient:", cor_test_result$estimate, "\n")
## Correlation Coefficient: 0.05158327
cat("P-value:", cor_test_result$p.value, "\n")
## P-value: 0.361516
cat("There isn't a statistically significance relationship between age and alcohol consumption.\n")
## There isn't a statistically significance relationship between age and alcohol consumption.
library(ggplot2)
ggplot(NS, aes(x=Age, y=Alcohol)) + geom_point()
numerical_vars <- c("Age", "Quetelet", "Calories", "Fat", "Fiber", "Alcohol", "Cholesterol")
for(var in numerical_vars) {
cat("Testing", var, ":\n")
cor_test <- cor.test(NS[[var]], NS$BetaPlasma, method = "pearson")
cat("Correlation Coefficient:", cor_test$estimate, "\n P-value:", cor_test$p.value, "\n\n")
}
## Testing Age :
## Correlation Coefficient: 0.1011276
## P-value: 0.07308498
##
## Testing Quetelet :
## Correlation Coefficient: -0.2293877
## P-value: 3.957005e-05
##
## Testing Calories :
## Correlation Coefficient: -0.02220696
## P-value: 0.6946032
##
## Testing Fat :
## Correlation Coefficient: -0.09164659
## P-value: 0.1044788
##
## Testing Fiber :
## Correlation Coefficient: 0.2359536
## P-value: 2.326513e-05
##
## Testing Alcohol :
## Correlation Coefficient: -0.02221084
## P-value: 0.6945525
##
## Testing Cholesterol :
## Correlation Coefficient: -0.130305
## P-value: 0.02070275
categorical_vars <- c("Smoke", "Sex", "VitaminUse")
for(var in categorical_vars){
cat("Testing", var, ":\n")
if(length(unique(NS[[var]])) == 2) {
t_test_result <- t.test(BetaPlasma ~ get(var), data = NS)
cat("P-value for T-test:", t_test_result$p.value, "\n\n")
} else{
anova_result <- aov(BetaPlasma ~ get(var), data = NS)
summary_anova <- summary(anova_result)
cat("P-value for ANOVA:", summary_anova[[1]][["Pr(>F)"]][1], "\n\n")
}
}
## Testing Smoke :
## P-value for T-test: 5.174922e-06
##
## Testing Sex :
## P-value for T-test: 0.04747518
##
## Testing VitaminUse :
## P-value for ANOVA: 6.376754e-05
NS$PriorSmoke <- as.factor(NS$PriorSmoke)
smoking_status_table <- table(NS$PriorSmoke)
chi_square_test <- chisq.test(smoking_status_table)
cat("P-value:", chi_square_test$p.value, "\n")
## P-value: 1.78434e-14
cat("There is sufficient statistical evidence to reject that the proportions of smoking status are equal. \n")
## There is sufficient statistical evidence to reject that the proportions of smoking status are equal.
model<- lm(Calories ~ Fat + Cholesterol + Age, data = NS)
summary_model <- summary(model)
print(summary_model)
##
## Call:
## lm(formula = Calories ~ Fat + Cholesterol + Age, data = NS)
##
## Residuals:
## Min 1Q Median 3Q Max
## -690.7 -176.6 -47.0 155.8 3315.6
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 512.9462 86.5106 5.929 8.09e-09 ***
## Fat 16.2645 0.7925 20.523 < 2e-16 ***
## Cholesterol 0.4208 0.2015 2.089 0.0375 *
## Age -1.4205 1.3040 -1.089 0.2769
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 331.9 on 311 degrees of freedom
## Multiple R-squared: 0.7643, Adjusted R-squared: 0.762
## F-statistic: 336.1 on 3 and 311 DF, p-value: < 2.2e-16
if(summary_model$coefficients["Fat", "Pr(>|t|)"]< 0.05){
cat("Fat grams consumed daily significantly affect calories consumed.\n")
}
## Fat grams consumed daily significantly affect calories consumed.
if(summary_model$coefficients["Cholesterol", "Pr(>|t|)"]< 0.05){
cat("Mg of cholesterol consumed daily significantly affects calories consumed.\n")
}
## Mg of cholesterol consumed daily significantly affects calories consumed.
if(summary_model$coefficients["Age", "Pr(>|t|)"]< 0.05){
cat("Age significantly affects calories consumed.\n")
}
cat("Age doesn't significantly affects calories consumed.\n")
## Age doesn't significantly affects calories consumed.
NS$Smoke <- as.factor(NS$Smoke)
model <- lm(Cholesterol ~ Smoke, data = NS)
summary(model)
##
## Call:
## lm(formula = Cholesterol ~ Smoke, data = NS)
##
## Residuals:
## Min 1Q Median 3Q Max
## -200.01 -89.22 -35.51 66.18 662.99
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 237.707 7.983 29.777 <2e-16 ***
## SmokeYes 34.826 21.606 1.612 0.108
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 131.7 on 313 degrees of freedom
## Multiple R-squared: 0.008232, Adjusted R-squared: 0.005064
## F-statistic: 2.598 on 1 and 313 DF, p-value: 0.108
ci <- confint(model, level = 0.95)
print(ci)
## 2.5 % 97.5 %
## (Intercept) 221.999728 253.41351
## SmokeYes -7.686032 77.33791
NS$Smoke <- as.factor(NS$Smoke)
model <- lm(NS$Cholesterol ~ NS$Smoke)
summary_model <- summary(model)
print(summary_model)
##
## Call:
## lm(formula = NS$Cholesterol ~ NS$Smoke)
##
## Residuals:
## Min 1Q Median 3Q Max
## -200.01 -89.22 -35.51 66.18 662.99
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 237.707 7.983 29.777 <2e-16 ***
## NS$SmokeYes 34.826 21.606 1.612 0.108
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 131.7 on 313 degrees of freedom
## Multiple R-squared: 0.008232, Adjusted R-squared: 0.005064
## F-statistic: 2.598 on 1 and 313 DF, p-value: 0.108
ci <- confint(model, level = 0.95)
print(ci)
## 2.5 % 97.5 %
## (Intercept) 221.999728 253.41351
## NS$SmokeYes -7.686032 77.33791
coefficients_summary <- summary(model)$coefficients
smoking_effects <- coefficients_summary[coefficients_summary[, 4] < 0.05, ]
NS$VitaminUse <- as.factor(NS$VitaminUse)
anova_result <- aov(RetinolPlasma ~ VitaminUse, data = NS)
summary_anova <- summary(anova_result)
print(summary_anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## VitaminUse 2 21580 10790 0.246 0.782
## Residuals 312 13680538 43848
p_value <- summary(anova_result)[[1]]$`Pr(>F)`[1]
cat("P-value:", p_value, "\n")
## P-value: 0.7820154
cat("There is not statistically significant evidence to show that vitamin use affects retinol plasma levels.\n")
## There is not statistically significant evidence to show that vitamin use affects retinol plasma levels.
library(ggplot2)
ggplot(NS, aes(x = RetinolPlasma, fill = VitaminUse)) + geom_histogram(position = "dodge") + labs(title = "Histogram of Retinol Levels VS Vitamin Use", y = "Vitamin Use")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
NS$VitaminUse <- as.factor(NS$VitaminUse)
NS$Sex <- as.factor(NS$Sex)
table_vitamin_sex <- table(NS$VitaminUse, NS$Sex)
chi_square_test <- chisq.test(table_vitamin_sex)
print(chi_square_test)
##
## Pearson's Chi-squared test
##
## data: table_vitamin_sex
## X-squared = 11.071, df = 2, p-value = 0.003944
cat("There is statistical significance between vitamin use and gender.\n")
## There is statistical significance between vitamin use and gender.
library(ggplot2)
ggplot(NS, aes(x = Sex, fill = VitaminUse)) + geom_bar(position = "fill") + scale_fill_manual(values = c("No" = "red")) + labs(title = "Vitamin Use VS Sex", x = "Sex", y = "Vitamin Use")