Factor names were originally assigned to the first row, so had to fix it.
nsch_2022_topical <- read.delim("~/Downloads/nsch_2022_topical.csv", header=FALSE)
bmidata <- data.frame(nsch_2022_topical)
new_factor_names <- as.character(bmidata[1, ])
colnames(bmidata) <- new_factor_names
bmidata <- bmidata[-1, , drop = FALSE]
bmidata <- bmidata[bmidata$sc_age_years >= 10 & bmidata$sc_age_years <= 17, ]
New bmidata now only filters people aged 10-17.
Constructs a new column that says “Yes” if the bmiclass says, “greater than the 95th percentile” and “No” otherwise.
characters_to_check <- c("greater than the 95th percentile")
check_characters <- function(row) {
if (any(grepl(paste(characters_to_check, collapse = "|"), row))) {
return("Yes")
} else {
return("No")
}
}
bmidata$obesitycheck <- apply(bmidata, 1, check_characters)
head(bmidata[, c("bmiclass", "obesitycheck")])
## bmiclass obesitycheck
## 6 5th percentile to less than the 85th percentile No
## 11 Equal to or greater than the 95th percentile Yes
## 12 85th percentile to less than the 95th percentile No
## 17 Less than the 5th percentile No
## 18 5th percentile to less than the 85th percentile No
## 19 85th percentile to less than the 95th percentile No
Last column of obesitycheck is now added.
obesity <- bmidata$obesitycheck
sc_age_years <- bmidata$sc_age_years
sc_age_years <- as.numeric(as.character(sc_age_years))
new_obesity <- ifelse(obesity == "Yes", 1, 0)
obesity_rates<- as.numeric(as.character(new_obesity))
weighted.mean(obesity_rates)
## [1] 0.1382534
weighted.mean(obesity_rates[bmidata$higrade == "More than high school"])
## [1] 0.1209284
weighted.mean(obesity_rates[bmidata$higrade != "More than high school"])
## [1] 0.2180974
filtered_fpl_i1_greater_than_2 <- bmidata$fpl_i1[nchar(as.character(bmidata$fpl_i1)) > 2]
filtered_fpl_i1_equal_or_less_than_2 <- bmidata$fpl_i1[nchar(as.character(bmidata$fpl_i1)) <= 2]
weighted.mean(obesity_rates[bmidata$fpl_i1 %in% filtered_fpl_i1_greater_than_2])
## [1] 0.1338233
weighted.mean(obesity_rates[bmidata$fpl_i1 %in% filtered_fpl_i1_equal_or_less_than_2])
## [1] 0.2077295
Row 1: More than high school education. Row 2: High school or less. Row 3: Above federal poverty level. Row 4: Below federal poverty level. Based on the results, people who graduate from more than highschool (college) have around a .1 lower obesity rate than people who do not attend college. Additionally, people who come from families above the poverty level have a .07 lower obesity rate than people that come from families under the poverty level. As a result, people who cannot attend more than high school are most affected by childhood obesity, followed by people with families under the poverty threshold.
aggregate(obesity_rates ~ screentime, data = bmidata, weighted.mean)
## screentime obesity_rates
## 1 1 hour 0.09601677
## 2 2 hours 0.11694548
## 3 3 hours 0.13761777
## 4 4 or more hours 0.17548196
## 5 Less than 1 hour 0.08287293
## 6 No valid response 0.11508951
aggregate(obesity_rates ~ hoursleep, data = bmidata, weighted.mean)
## hoursleep obesity_rates
## 1 10 hours 0.1163708
## 2 11 or more hours 0.1277174
## 3 6 hours 0.1829163
## 4 7 hours 0.1569608
## 5 8 hours 0.1421584
## 6 9 hours 0.1160564
## 7 Less than 6 hours 0.2450000
## 8 No valid response 0.0968661
aggregate(obesity_rates ~ physactiv, data = bmidata, weighted.mean)
## physactiv obesity_rates
## 1 0 days 0.21370421
## 2 1 - 3 days 0.16454564
## 3 4 - 6 days 0.09353812
## 4 Every day 0.09559435
## 5 No valid response 0.10876133
For screentime, there definetly seems to be an increase in obesity rates as people spend more time on screen, the highest being more than four hours.
For hours of sleep, people who sleep less than 6 hours have easily the highest obesity rates at 0.245, and there is a noticeable increase in obesity rates as hours of sleep decreases. For physical activity, people who never exercise have the highest obesity rates, and those that exercise at least 4 times a day have their obesity rates halved.
Hours of sleep seems to have the highesy impact on obesity rates, as the max to min margin is around .13. Screentime, while it has an impact on obesity rates, seems to have the lowest impact, where only those who spend 4 hours or more have significantly higher obesity rates.