To complete this question, follow these steps:
  1. Replace the “Your Name Here” text in the author: field with your own name.

  2. Supply your solutions to the questions by editing EXAM I.Rmd.

  3. When you have completed the homework and have checked that your code both runs in the Console and knits correctly when you click Knit HTML, rename the R Markdown file to EXAM I_YourNameHere.Rmd, and submit on course site. (YourNameHere should be changed to your own name.)

Question 1 Detect missing values

Step 1: upload the packages and import the data using the read_excel function

# Your code here 
library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
library(openxlsx)

Fitness <- read_excel("Fitness.xlsx")

Part I: Subset

Q1: (1.5 points)

# Your code here 

#Q1
filtered_data <- Fitness %>%
  filter(Married == "Yes" & Exercise == "Never" & Income > 80000)

Part II: Use the omission to handle missing values

Q2: (1.5 points)

# Your code here 


rows_with_missing_values <- which(!complete.cases(Fitness))
print(rows_with_missing_values)
##  [1]  14  21  48  55  62  86 111 143 175 181
num_individuals_filtered <- nrow(filtered_data)

print(num_individuals_filtered)
## [1] 22

Q3: (1.5 points)

# Your code here 


Fitness_omit <- na.omit(Fitness)
num_individuals_omit <- nrow(Fitness_omit)

print(num_individuals_omit)
## [1] 407

Part III: Handle the missing values using the simple imputation strategy.

Q4: (1.5 points)

# Your code here 

missing_values <- colSums(is.na(Fitness))
print(missing_values)
##       ID Exercise  Married   Income 
##        0        5        2        3

Q5 : (3 points)

Step 1: Use the code in the exam to create a new dataframe for imputation.

# Insert the code from Question Q5 step 1 to create a new dataframe for imputation

fitness_impute <- Fitness

Step 2: use the imputation strategy to replace NA values from each column using the new dataframe you created above

# Your code here imputation

table(fitness_impute$Married)
## 
##  No Yes 
## 134 281
fitness_impute$Married[is.na(fitness_impute$Married)] <- "Yes"

table(fitness_impute$Exercise)
## 
##    Always     Never     Often Sometimes 
##        81       128       104        99
fitness_impute$Exercise[is.na(fitness_impute$Exercise)] <- "Never"

mean(fitness_impute$Income,na.rm=TRUE)
## [1] 81727.41
fitness_impute$Income[is.na(fitness_impute$Income)] <- 81727.41

colSums(is.na(fitness_impute))
##       ID Exercise  Married   Income 
##        0        0        0        0

Q6: (1 point)

# Your code here 

filtered_data_impute <- fitness_impute %>%
  filter(Married == "Yes" & Exercise == "Never" & Income > 80000)

num_individuals_impute <- nrow(filtered_data_impute)

print(num_individuals_impute)
## [1] 24

Part IV: Visualization

Q7: (1.5 points)

# Your code here 

library(ggplot2)

ggplot(fitness_impute, aes(x = Exercise, y = Income, fill = Married)) +
  geom_bar(stat = "summary", fun = "mean", position = "dodge") +
  labs(title = "Average Income Based on Exercise Habits",
       x = "Exercise Habits",
       y = "Average Income") +
  scale_fill_manual(values = c("No" = "blue", "Yes" = "red")) +
  theme_minimal()

# People who "Always" exercise are majoritly not married. 

Q8: (1.5 points)

# Your code here 

library(dplyr)

average_income <- fitness_impute %>%
  group_by(Exercise, Married) %>%
  summarize(Average_Income = mean(Income))
## `summarise()` has grouped output by 'Exercise'. You can override using the
## `.groups` argument.
print(average_income)
## # A tibble: 8 × 3
## # Groups:   Exercise [4]
##   Exercise  Married Average_Income
##   <chr>     <chr>            <dbl>
## 1 Always    No              88090.
## 2 Always    Yes             81853.
## 3 Never     No              79991.
## 4 Never     Yes             77582.
## 5 Often     No              82212.
## 6 Often     Yes             82404.
## 7 Sometimes No              81413.
## 8 Sometimes Yes             85031.

Question 2 RFM

Step 1: Upload the necessary packages and import the data file using the read_csv function.

# Your code here 

library(readr)

patients_data <- read_csv("Patient.csv")
## Rows: 400 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Race, Education
## dbl (5): Weight, Height, Exercise, BMI, Age
## 
## ℹ 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.

Q1: (1.5 points)

# Your code here 

patients_data$BMI_group <- cut(patients_data$BMI, breaks = 4, labels = c(1, 2, 3, 4), include.lowest = TRUE)

head(patients_data$BMI_group)
## [1] 1 2 2 3 1 2
## Levels: 1 2 3 4

Q2: (1.5 points)

# Your code here 


patients_data$exercise_group <- cut(patients_data$Exercise, breaks = 4, labels = c(1, 2, 3, 4), include.lowest = TRUE)
head(patients_data$exercise_group)
## [1] 1 2 3 2 2 3
## Levels: 1 2 3 4

Q3: (1.5 points)

# Your code here 

patients_data$age_group <- cut(patients_data$Age, breaks = 4, labels = c(1, 2, 3, 4), include.lowest = TRUE)
head(patients_data$age_group)
## [1] 2 3 4 1 3 1
## Levels: 1 2 3 4

Q4: (1 point)

# Your code here 

patients_at_risk <- patients_data %>%
  filter(BMI_group == 4 & exercise_group == 4 & age_group == 4)

num_patients_at_risk <- nrow(patients_at_risk)
print(num_patients_at_risk)
## [1] 4

Q5 : (1.5 points)

# Your code here 

patients_at_risk_111 <- patients_data %>%
  filter(BMI_group == 1 & exercise_group == 1 & age_group == 1)

num_patients_at_risk_111 <- nrow(patients_at_risk_111)
print(num_patients_at_risk_111)
## [1] 5
# The major difference between these people is their age, people at higher risk are much older than the people at lower risk
#Let's see that below

print(mean(patients_at_risk$Age))
## [1] 69
print(mean(patients_at_risk_111$Age))
## [1] 29
#The average age of patients in group 444 is 69 whereas the average age for people in 111 is 29