Advertisement
Davejee

Untitled

Jan 18th, 2024
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.75 KB | None | 0 0
  1. library(broom)
  2. library(tidyverse)
  3. library(janitor)
  4. healthdata <- read.spss("Health_LISS_Core_Study_Wave_12_2020_data_plus_background.sav", to.data.frame = TRUE)
  5.  
  6. # Create healthdata_s
  7. healthdata_s <- healthdata %>%
  8.     select(ch19l016, ch19l017, ch19l004, ch19l001, ch19l002, nettoink)
  9. summary(healthdata_s)
  10. healthdata_s %>% View()
  11.  
  12. # add a new category called BMI
  13. healthdata_s <- healthdata_s %>%
  14.   mutate(BMI = (ch19l017 / (ch19l016 / 100)^2))
  15.  
  16. # create histogram
  17. healthdata_s %>%
  18.   ggplot() +
  19.   geom_histogram(aes(x = BMI))
  20.  
  21. # only show people with a BMI of 14 or higher and 50 or lower
  22. healthdata_s <- healthdata_s %>%
  23.   filter(BMI >= 14 & BMI <= 50)
  24.  
  25. # show mean and sd and total amount
  26. healthdata_s %>%
  27. summarise(mean = mean(BMI), sd = sd(BMI))
  28.  
  29. mean(healthdata_s$BMI, na.rm = TRUE)
  30. sd(healthdata_s$BMI, na.rm = TRUE)
  31. nrow(filter(healthdata_s, BMI != "NA"))
  32.  
  33. # mutate health variable to numbers
  34. healthdata_s <- healthdata_s %>%
  35.   mutate(generalhealth = case_when(
  36.     ch19l004 == "poor" ~ 1,
  37.     ch19l004 == "moderate" ~ 2,
  38.     ch19l004 == "good" ~ 3,
  39.     ch19l004 == "very good" ~ 4,
  40.     ch19l004 == "excellent" ~ 5))
  41.  
  42. # histogram variable health
  43. healthdata_s %>%
  44.   ggplot() +
  45.   geom_histogram(aes(x = generalhealth))
  46.  
  47. # scatterplot
  48. healthdata_s %>%
  49.   ggplot(aes(x = BMI, y = generalhealth)) +
  50.   geom_point() +
  51.   geom_smooth(method = "lm", se= FALSE)
  52.  
  53. # OLS model
  54. model <- healthdata_s %>%
  55.   lm(generalhealth ~ BMI, data = .)
  56. model
  57. summary(model)
  58.  
  59. # standardizing part
  60. model_2 <- healthdata_s %>%
  61.   lm(scale(generalhealth) ~ scale(BMI), .)
  62.  
  63. summary(model_2)
  64.  
  65. # standardized values scatterplot
  66. healthdata_s %>%
  67.   ggplot(aes(x = BMI, y = generalhealth)) +
  68.   geom_point() +
  69.   geom_smooth(method = "lm", se= FALSE)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement