Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(broom)
- library(tidyverse)
- library(janitor)
- healthdata <- read.spss("Health_LISS_Core_Study_Wave_12_2020_data_plus_background.sav", to.data.frame = TRUE)
- # Create healthdata_s
- healthdata_s <- healthdata %>%
- select(ch19l016, ch19l017, ch19l004, ch19l001, ch19l002, nettoink)
- summary(healthdata_s)
- healthdata_s %>% View()
- # add a new category called BMI
- healthdata_s <- healthdata_s %>%
- mutate(BMI = (ch19l017 / (ch19l016 / 100)^2))
- # create histogram
- healthdata_s %>%
- ggplot() +
- geom_histogram(aes(x = BMI))
- # only show people with a BMI of 14 or higher and 50 or lower
- healthdata_s <- healthdata_s %>%
- filter(BMI >= 14 & BMI <= 50)
- # show mean and sd and total amount
- healthdata_s %>%
- summarise(mean = mean(BMI), sd = sd(BMI))
- mean(healthdata_s$BMI, na.rm = TRUE)
- sd(healthdata_s$BMI, na.rm = TRUE)
- nrow(filter(healthdata_s, BMI != "NA"))
- # mutate health variable to numbers
- healthdata_s <- healthdata_s %>%
- mutate(generalhealth = case_when(
- ch19l004 == "poor" ~ 1,
- ch19l004 == "moderate" ~ 2,
- ch19l004 == "good" ~ 3,
- ch19l004 == "very good" ~ 4,
- ch19l004 == "excellent" ~ 5))
- # histogram variable health
- healthdata_s %>%
- ggplot() +
- geom_histogram(aes(x = generalhealth))
- # scatterplot
- healthdata_s %>%
- ggplot(aes(x = BMI, y = generalhealth)) +
- geom_point() +
- geom_smooth(method = "lm", se= FALSE)
- # OLS model
- model <- healthdata_s %>%
- lm(generalhealth ~ BMI, data = .)
- model
- summary(model)
- # standardizing part
- model_2 <- healthdata_s %>%
- lm(scale(generalhealth) ~ scale(BMI), .)
- summary(model_2)
- # standardized values scatterplot
- healthdata_s %>%
- ggplot(aes(x = BMI, y = generalhealth)) +
- geom_point() +
- geom_smooth(method = "lm", se= FALSE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement