library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
mydata <- read_delim("C:\\Users\\abhir\\OneDrive\\Documents\\luddy\\H510-Stats,R\\adult.csv", delim = ",")
## Rows: 32561 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Work-Class, Education, Marital Status, Occupation, Relationship, Ra...
## dbl (6): Age, Fnl_Wgt, Education_Level, Capital_Gain, Capital_Loss, hrs_week
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mydata
## # A tibble: 32,561 × 15
## Age `Work-Class` Fnl_Wgt Education Education_Level `Marital Status`
## <dbl> <chr> <dbl> <chr> <dbl> <chr>
## 1 39 " State-gov" 77516 " Bachelo… 13 " Never-married"
## 2 50 " Self-emp-not-inc" 83311 " Bachelo… 13 " Married-civ-s…
## 3 38 " Private" 215646 " HS-grad" 9 " Divorced"
## 4 53 " Private" 234721 " 11th" 7 " Married-civ-s…
## 5 28 " Private" 338409 " Bachelo… 13 " Married-civ-s…
## 6 37 " Private" 284582 " Masters" 14 " Married-civ-s…
## 7 49 " Private" 160187 " 9th" 5 " Married-spous…
## 8 52 " Self-emp-not-inc" 209642 " HS-grad" 9 " Married-civ-s…
## 9 31 " Private" 45781 " Masters" 14 " Never-married"
## 10 42 " Private" 159449 " Bachelo… 13 " Married-civ-s…
## # ℹ 32,551 more rows
## # ℹ 9 more variables: Occupation <chr>, Relationship <chr>, Race <chr>,
## # Sex <chr>, Capital_Gain <dbl>, Capital_Loss <dbl>, hrs_week <dbl>,
## # Country <chr>, Annual_pay <chr>
#FIRST HYPOTHESIS TEST H0 (Null Hypthesis): The mean Education Level is less than or equal to 10.05 Ha: The mean Education Level is greater than 10.05
null_mean <- 10.05
t_test_result <- t.test(mydata$Education_Level, mu = null_mean, alternative = "greater")
p_value <- t_test_result$p.value
alpha <- 0.05
if (p_value < alpha) {
cat("Rejecting the null hypothesis. The mean of
Education is greater than 10.05.\n")
} else {
cat("Fail to reject the null hypothesis. There is no sufficient evidence that the mean of Education Level is greater than 10.0.5.\n")
}
## Rejecting the null hypothesis. The mean of
## Education is greater than 10.05.
#Neyman Pearson Test H0 (Null Hypthesis): The mean Education Level is less than or equal to 10.05 Ha: The mean Education level is greater than 10.05
EL <- mydata$Education_Level
null_variance <- 10.05
alpha <- 0.05
# Calculate the sample variance
sample_variance <- var(EL)
# Calculate the chi-squared test statistic
test_statistic <- ((length(EL) - 1) * sample_variance) / null_variance
# Calculate the p-value
p_value <- pchisq(test_statistic, df = length(EL) - 1, lower.tail = TRUE)
# Compare the p-value to alpha to make a decision
if (p_value < alpha) {
cat("Reject the null hypothesis. The population variance is less than the specified value.\n")
} else {
cat("Fail to reject the null hypothesis. There is no sufficient evidence that the population variance is less than the specified value.\n")
}
## Reject the null hypothesis. The population variance is less than the specified value.
cat("Test Statistic:", test_statistic, "\n")
## Test Statistic: 21443.89
cat("Degrees of Freedom:", length(EL) - 1, "\n")
## Degrees of Freedom: 32560
cat("P-Value:", p_value, "\n")
## P-Value: 0
#Second Hypothesis Test
women_EL <- mydata$Education_Level[mydata$Sex == "Female"]
men_EL <- mydata$Education_Level[mydata$Sex == "Male"]
t_test_result <- t.test(women_EL,men_EL)
p_value <- t_test_result$p.value
alpha <- 0.05
if (p_value < alpha) { cat(“Reject the null hypothesis. Women education level is significantly less than men.”) } else { cat(“Fail to reject the null hypothesis. There is no sufficient evidence that women Education Level is less compared to men.”) }
#2nd Neyman Pearson test(f_test) Null Hypothesis (H0): Women Education Level is less compared to men.
Alternative Hypothesis (H1): Women Education Level is not less compared to men.
women_EL <- mydata$Education_Level[mydata$Sex == "Female"]
men_EL <- mydata$Education_Level[mydata$Sex == "Male"]
# Perform a two-sample t-test to compare means
anova_result <- aov(Education_Level ~ Sex, data = mydata)
# Get the p-value
p_value <- summary(anova_result)[[1]][["Pr(>F)"]][1]
# Set your alpha level (significance level)
alpha <- 0.05
# Compare the p-value to alpha to make a decision
if (p_value < alpha) {
cat("Reject the null hypothesis. Women Education Level is less compared to men.\n")
} else {
cat("Fail to reject the null hypothesis. There is no sufficient evidence that women Education Level is less compared to men.\n")
}
## Reject the null hypothesis. Women Education Level is less compared to men.
print(anova_result)
## Call:
## aov(formula = Education_Level ~ Sex, data = mydata)
##
## Terms:
## Sex Residuals
## Sum of Squares 32.5 215478.6
## Deg. of Freedom 1 32559
##
## Residual standard error: 2.572566
## Estimated effects may be unbalanced
boxplot(mydata$Education_Level, horizontal = TRUE, col = "lightblue", main = "Education Level of Adults")
# Add a horizontal line for the null hypothesis mean
abline(h = 140, col = "red", lwd = 2)
mean_men <- mean(men_EL) mean_women <- mean(women_EL)
barplot(c(men_EL,women_EL), names.arg = c(“men”, “women”), col = c(“lightblue”, “lightgreen”), main = “Average Education Level by Gender”, ylab = “Average Education Level”)
```
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.