-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment 2.qmd
309 lines (264 loc) · 9.88 KB
/
Assignment 2.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
---
title: "Assignment 2"
author: "Jazmin Hernandez"
format: html
editor: visual
embed-resources: true
---
# **Assignment 02 - Data Viz and Wrangling**
## Data Wrangling
```{r}
library(dplyr)
library(readr)
individual_data <- "C:/Users/jhern/Downloads/chs_individual.csv"
regional_data <- "C:/Users/jhern/Downloads/chs_regional.csv"
individual_data <- read_csv(individual_data)
regional_data <- read_csv(regional_data)
```
```{r}
merged_data <- left_join(individual_data, regional_data, by = "townname")
head(merged_data)
```
```{r}
row_count <- nrow(merged_data)
cat("Rows in merged_data:", row_count, "\n")
duplicate_count <- sum(duplicated(merged_data))
cat("Number of duplicate rows in merged_data:", duplicate_count, "\n")
```
```{r}
Mode <- function(x) {
x <- x[!is.na(x)]
uniq_x <- unique(x)
if (length(uniq_x) == 0) return(NA)
uniq_x[which.max(tabulate(match(x, uniq_x)))]
}
```
```{r}
merged_data |>
group_by(male, hispanic) |>
mutate(
across(where(is.numeric),
~ if_else(is.na(.), mean(., na.rm = TRUE), .)),
across(where(is.character),
~ if_else(is.na(.), Mode(.), .))
) |>
ungroup()
```
```{r}
missing_values_count <- colSums(is.na(merged_data))
print(missing_values_count)
```
```{r}
merged_data <- merged_data |>
mutate(obesity_level = case_when(
bmi < 14 ~ "Underweight",
bmi >= 14 & bmi <= 22 ~ "Normal",
bmi > 22 & bmi <= 24 ~ "Overweight",
bmi > 24 ~ "Obese",
TRUE ~ NA_character_
))
```
```{r}
summary_table <- merged_data |>
group_by(obesity_level) |>
summarize(
min_bmi = min(bmi, na.rm = TRUE),
max_bmi = max(bmi, na.rm = TRUE),
total_observations = n(),
.groups = 'drop'
)
print(summary_table)
```
```{r}
merged_data <- merged_data|>
mutate(smoke_gas_exposure = case_when(
smoke == 0 & gasstove == 0 ~ "Non-exposed",
smoke == 1 & gasstove == 0 ~ "Second Hand Smoke Only",
smoke == 0 & gasstove == 1 ~ "Gas Stove Only",
smoke == 1 & gasstove == 1 ~ "Both Exposures",
TRUE ~ NA_character_
))
head(merged_data)
```
```{r}
summary_town <- merged_data |>
group_by(townname) |>
summarize(
average_fev = mean(fev, na.rm = TRUE),
sd_fev = sd(fev, na.rm = TRUE),
.groups = 'drop'
)
print(summary_town)
```
```{r}
summary_sex <- merged_data |>
group_by(male) |>
summarize(
average_fev = mean(fev, na.rm = TRUE),
sd_FEV1 = sd(fev, na.rm = TRUE),
.groups = 'drop'
)
print(summary_sex)
```
```{r}
summary_obesity <- merged_data |>
group_by(obesity_level) |>
summarize(
average_fev = mean(fev, na.rm = TRUE),
sd_fev = sd(fev, na.rm = TRUE),
.groups = 'drop'
)
print(summary_obesity)
```
```{r}
summary_exposure <- merged_data |>
group_by(smoke_gas_exposure) |>
summarize(
average_fev = mean(fev, na.rm = TRUE),
sd_fev = sd(fev, na.rm = TRUE),
.groups = 'drop'
)
print(summary_exposure)
```
## Looking at the Data (EDA)
1. What is the association between BMI and FEV (forced expiratory volume)? From the scatter plot of BMI vs FEV, there does appear to be a positive correlation where as BMI increases, so does forced expiratory volume.
```{r}
colSums(is.na(merged_data|> select(bmi, fev)))
merged_data <- na.omit(merged_data)
```
```{r}
layout(matrix(1:2, nrow=1))
hist(merged_data$bmi)
hist(merged_data$fev)
```
```{r}
library(ggplot2)
ggplot(merged_data, aes(x = bmi, y = fev)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", color = "purple") +
labs(title = "Scatter Plot: BMI vs FEV", x = "BMI", y = "FEV") +
theme_minimal()
```
2. What is the association between smoke and gas exposure and FEV? From the box plot, there does not appear to be an association between smoke and gas exposure and forced expiratory volume. This is because of the considerable overlapping among all four categories smoke and gas exposure.
```{r}
colSums(is.na(merged_data|> select(smoke_gas_exposure, fev)))
merged_data <- na.omit(merged_data)
```
```{r}
ggplot(merged_data, aes(x = factor(smoke_gas_exposure), y = fev)) +
geom_boxplot(fill = "pink") +
labs(title = "Boxplot of FEV by Smoke and Gas Exposure",
x = "Smoke and Gas Exposure (0 = No, 1 = Yes)", y = "FEV") +
theme_minimal()
```
3. What is the association between PM2.5 exposure and FEV? From the scatterplot, there appears to be no association between PM 2.5 exposure and forced expiratory exposure as seen by the linear model only having a very slight decrease from 10 to about 33 pm2.5 exposure.
```{r}
ggplot(merged_data, aes(x = pm2_5_fr, y = fev)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "green") +
labs(title = "Scatter plot of PM2.5 vs FEV", x = "PM2.5 Exposure", y = "FEV") +
theme_minimal()
```
## Visualization
1. Facet plot showing scatterplots with regression lines of BMI vs FEV by “townname”. All scatterplots do show an association between BMI and FEV. We can see that throughout all towns, as BMI increases, so does forced expiratory volume. However, in areas such as Riverside and Alpine, this association seems more gradual.
```{r}
ggplot(merged_data, aes(x = bmi, y = fev)) +
geom_point(alpha = 0.6) +
geom_smooth(method = "lm", se = FALSE, color = "lightblue") +
facet_wrap(~ townname) +
labs(
title = "BMI vs FEV by Town",
x = "BMI",
y = "FEV",
) +
theme_minimal()
```
2. Stacked histograms of FEV by BMI category and FEV by smoke/gas exposure. Use different color schemes than the ggplot default.
The BMI categories seem to fall within similar ranges in this stacked histogram. Being underweight appears to have the lowest counts of FEV and being obese has the highest counts.
```{r}
ggplot(merged_data, aes(x = fev, fill = obesity_level)) +
geom_histogram(position = "stack", bins = 15, alpha = 0.7) +
scale_fill_manual(values = c("Underweight" = "darkred",
"Normal weight" = "lavender",
"Overweight" = "lightgreen",
"Obese" = "darkblue")) +
labs(
title = "Stacked Histogram of FEV by BMI Category",
x = "Forced Expiratory Volume (FEV)",
y = "Count",
fill = "BMI Category"
) +
theme_minimal()
```
The stacked histogram of FEV by smoke/gas exposure shows the category for both exposures has the highest count with second-hand smoke category having the lowest count.
```{r}
ggplot(merged_data, aes(x = fev, fill = smoke_gas_exposure)) +
geom_histogram(position = "stack", bins = 30, alpha = 0.7) +
scale_fill_manual(values = c("purple", "yellow", "blue", "red")) +
labs(
title = "Stacked Histogram of FEV by Smoke/Gas Exposure",
x = "FEV",
y = "Count"
) +
theme_minimal()
```
3. Barchart of BMI by smoke/gas exposure. Taking the average BMI by smoke/gas exposure category we can see that there does not seem to be a significant association between smoke/gas exposure and BMI. Gas stove exposure and non-exposed are the two categories that seem to be slightly associated with a lower BMI while having both exposures and secondhand smoke only seem to be slightly more associated with higher BMI.
```{r}
bmi_summary <- merged_data |>
group_by(smoke_gas_exposure) |>
summarize(avg_bmi = mean(bmi, na.rm = TRUE))
ggplot(bmi_summary, aes(x = smoke_gas_exposure, y = avg_bmi, fill = smoke_gas_exposure)) +
geom_bar(stat = "identity", alpha = 0.7) +
scale_fill_manual(values = c("green", "yellow", "red", "grey"), name = "Smoke/Gas Exposure") +
labs(
title = "Average BMI by Smoke/Gas Exposure",
x = "Smoke/Gas Exposure",
y = "Body Mass Index (BMI)"
) +
theme_minimal() +
theme(
legend.position = "none" # Remove legend if not needed
)
```
4. Statistical summary graphs of FEV by BMI and FEV by smoke/gas exposure category.
From the statistical summary graph of FEV by BMI category, we see that there is overlap between the obese and overweight categories which signifies that there is likely to not be difference between the two groups in their association with FEV. However, there is no overlap between normal and underweight categories which signified there there is likely to be a difference or association between these categories and FEV.
```{r}
ggplot(merged_data, aes(x = obesity_level, y = fev, fill = obesity_level)) +
geom_boxplot() +
scale_fill_manual(values = c("lightyellow", "lightpink", "red", "orange")) +
labs(title = "Boxplot of FEV by BMI Category",
x = "BMI Category",
y = "FEV") +
theme_minimal()
```
This box plot of FEV by smoke/gas exposure shows overlap between all categories of exposure. Because of this, it is likely that there is no difference between these categories in their association between FEV.
```{r}
ggplot(merged_data, aes(x = smoke_gas_exposure, y = fev, fill = smoke_gas_exposure)) +
geom_boxplot() +
scale_fill_manual(values = c("lavender", "lightyellow", "pink", "skyblue")) +
labs(title = "Boxplot of FEV by Smoke/Gas Exposure",
x = "Smoke/Gas Exposure",
y = "FEV") +
theme_minimal()
```
5. A leaflet map showing the concentrations of PM2.5 mass in each of the CHS communities. The map shows greater PM 2.5 concentrations in the Mira Loma area with a concentration of 29.97 µg/m3. Lompoc seems to have the lowest PM 2.5 concentration with 5.96 µg/m3.
```{r}
library(leaflet)
leaflet(merged_data) |>
addTiles() |>
addCircles(lng = ~lon, lat = ~lat, weight = 1,
radius = ~pm25_mass * 100, popup = ~paste(townname, "<br>PM2.5:", pm25_mass),
color = "darkblue", fillOpacity = 0.5) |>
addLegend("bottomright", pal = colorNumeric("Blues", merged_data$pm25_mass),
values = merged_data$pm25_mass, title = "PM2.5 Concentration")
```
6. Choose a visualization to examine whether PM2.5 mass is associated with FEV. The scatterplot shows that there seems to be a slightly negative association between pm2.5 mass and FEV. We can see that as mass increases, FEV slightly decreases.
```{r}
ggplot(merged_data, aes(x = pm25_mass, y = fev)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Relationship between PM2.5 Mass and FEV",
x = "PM2.5 Mass",
y = "FEV") +
theme_minimal()
```