Replace the “Your Name Here” text in the author:
field with your own name.
Supply your solutions to the questions by editing
EXAM I.Rmd.
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.)
Step 1: upload the packages and import the data using the
read_excel function
## ── 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
## ID Exercise Married Income
## 0 5 2 3
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 <- FitnessStep 2: use the imputation strategy to replace NA values from each column using the new dataframe you created above
##
## No Yes
## 134 281
##
## 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
## ID Exercise Married Income
## 0 0 0 0
# 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()# 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.
## # 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.
Step 1: Upload the necessary packages and import the data file using the read_csv function.
## 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.
# 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
# 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
# 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
# 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
# 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
## [1] 29