---
title: Sedentary Behavior in Adults
subtitle: Using R with NHANES
author: Cubicle 3253
format:
html:
code-tools: true
echo: false
embed-resources: true
fig-asp: 0.6
fig-dpi: 300
grid:
sidebar-width: 200px
body-width: 1500px
margin-width: 100px
linestretch: 1.1
toc: true
toc-location: left
---
## Introduction
<p>
Evidence suggests a positive relationship between sedentary behavior and
all-cause mortality
(<a href="https://health.gov/sites/default/files/2019-09/08_F-2_Sedentary_Behavior.pdf">1</a>).
NHANES first started measuring sedentary behavior in the 2007-2008 survey
(<a href="https://wwwn.cdc.gov/nchs/data/nhanes/2007-2008/questionnaires/paq07_08_eng.pdf">2</a>),
using the question:
*"The following question is about sitting or reclining at work, at home, or at school.
Include time spent sitting at a desk, sitting with friends, traveling in a car, bus,
or train, reading, playing cards, watching television, or using a computer.
Do not include time spent sleeping. How much time {do you/does SP} usually spend
sitting or reclining on a typical day?"*
</p><p>
For the for the 2009-2010 survey
(<a href="https://wwwn.cdc.gov/nchs/data/nhanes/2009-2010/questionnaires/paq_f.pdf">3</a>),
the question was changed to:
*"The following question is about sitting at work, at home, getting to and from places,
or with friends, including time spent sitting at a desk, traveling in a car or bus,
reading, playing cards, watching television, or using a computer.
Do not include time spent sleeping. How much time {do you/does SP} usually spend
sitting on a typical day?"*
and has remained the same since.
</p><p>
This document examines sedentary behavior, measured as time spent sitting,
in adults from NHANES 2007-08 to NHANES 2021-23.
Also of interest was the possible effect of a probe querying times
of less than 8 hours introduced midway through the 2011-2012 survey
(<a href="https://wwwn.cdc.gov/Nchs/Nhanes/2011-2012/PAQ_G.htm">4</a>).
This probe remained during the 2013-2014 survey,
was changed to less than 3 hours for the 2015-2016 survey,
and was dropped afterwards.
</p>
```{r}
#| message: false
library(dplyr)
library(ggplot2)
library(gt)
library(haven)
library(survey)
library(srvyr)
library(tidyr)
read_nhanes <- function(xpt_name) {
xpt_name <- toupper(xpt_name)
if(substr(xpt_name, 1, 2) == "P_") {
begin_year = "2017"
} else {
begin_year = switch(substr(xpt_name, nchar(xpt_name) - 1, nchar(xpt_name)),
"_L" = "2021",
"_J" = "2017",
"_I" = "2015",
"_H" = "2013",
"_G" = "2011",
"_F" = "2009",
"_E" = "2007",
"_D" = "2005",
"_C" = "2003",
"_B" = "2001",
"1999")
}
xpt_url <- paste0("https://wwwn.cdc.gov/nchs/data/nhanes/public/", begin_year, "/datafiles/", xpt_name, ".xpt")
try(read_xpt(xpt_url), silent = TRUE)
}
do_graph <- function() {
One |>
filter(!is.na(RIDAGEYR), !is.na(PAD680)) |>
ggplot(aes(x = PAD680, y = SDDSRVYR, fill = SDDSRVYR, weight = survey_wt)) +
geom_boxplot() +
scale_fill_viridis_d(alpha = 0.6, direction = -1) +
labs(
x = "Minutes of Sedentary Behavior",
y = "Survey Period") +
coord_cartesian(xlim = c(0, 1080)) +
scale_x_continuous(
breaks = seq(0, 1080, 60),
expand = expansion(mult = 0.01)) +
scale_y_discrete(limits = rev) +
theme_bw() +
theme(
text = element_text(size = 8),
legend.position = "none")
}
```
```{r}
demo_vars <- c("SEQN", "SDDSRVYR", "RIAGENDR", "RIDAGEYR", "INDFMPIR",
"SDMVSTRA", "SDMVPSU", "WTINT2YR", "WTINTPRP")
DEMO_E <- read_nhanes("DEMO_E") |> select(any_of(demo_vars))
DEMO_F <- read_nhanes("DEMO_F") |> select(any_of(demo_vars))
DEMO_G <- read_nhanes("DEMO_G") |> select(any_of(demo_vars))
DEMO_H <- read_nhanes("DEMO_H") |> select(any_of(demo_vars))
DEMO_I <- read_nhanes("DEMO_I") |> select(any_of(demo_vars))
P_DEMO <- read_nhanes("P_DEMO") |> select(any_of(demo_vars))
DEMO_L <- read_nhanes("DEMO_L") |> select(any_of(demo_vars))
DEMO <- bind_rows(DEMO_E, DEMO_F, DEMO_G, DEMO_H, DEMO_I, P_DEMO, DEMO_L)
```
```{r}
PAQ_E <- read_nhanes("PAQ_E") |> select(SEQN, PAD680)
PAQ_F <- read_nhanes("PAQ_F") |> select(SEQN, PAD680)
PAQ_G <- read_nhanes("PAQ_G") |> select(SEQN, PAD680)
PAQ_H <- read_nhanes("PAQ_H") |> select(SEQN, PAD680)
PAQ_I <- read_nhanes("PAQ_I") |> select(SEQN, PAD680)
P_PAQ <- read_nhanes("P_PAQ") |> select(SEQN, PAD680)
PAQ_L <- read_nhanes("PAQ_L") |> select(SEQN, PAD680)
PAQ <- bind_rows(PAQ_E, PAQ_F, PAQ_G, PAQ_H, PAQ_I, P_PAQ, PAQ_L)
```
```{r}
One <- left_join(DEMO, PAQ, by = "SEQN") |>
mutate(
SDDSRVYR = case_match(SDDSRVYR,
5 ~ "2007-2008",
6 ~ "2009-2010",
7 ~ "2011-2012",
8 ~ "2013-2014",
9 ~ "2015-2016",
66 ~ "2017-Mar 2020",
12 ~ "Aug 2021-Aug 2023"),
RIAGENDR = if_else(RIAGENDR == 1, "Men", "Women"),
RIDAGEYR = case_match(RIDAGEYR,
18:34 ~ "18 to 34",
35:49 ~ "35 to 49",
50:64 ~ "50 to 64",
65:80 ~ "65 or more"),
survey_wt = coalesce(WTINTPRP, WTINT2YR),
PAD680 = if_else(PAD680 >= 7777, NA_real_, PAD680))
```
```{r}
NHANES <- One |>
filter(!is.na(RIDAGEYR), !is.na(PAD680)) |>
as_survey_design(id = SDMVPSU, strata = SDMVSTRA, nest = TRUE, weights = survey_wt)
```
## Overall
The distribution of sedentary behavior appeared to move higher from
2007-2008 to 2013-2014 and then back lower.
```{r}
do_graph()
```
<details>
<summary>
Data Table
</summary>
```{r}
NHANES |>
group_by(SDDSRVYR) |>
summarize(
n = n(),
m = survey_mean(PAD680, vartype = "ci"),
q = survey_quantile(PAD680, quantiles = c(0.25, 0.50, 0.75), vartype = NULL)) |>
mutate(m = sprintf("%.0f (%.0f-%.0f)", m, m_low, m_upp)) |>
select(!(m_low:m_upp)) |>
gt() |>
cols_label(
SDDSRVYR = "Survey Period",
m = "Mean (95% CI)",
q_q25 = md("25^th^"),
q_q50 = md("50^th^"),
q_q75 = md("75^th^")) |>
tab_spanner(columns = 4:6, label = "Percentile") |>
fmt_integer(columns = 2) |>
cols_align(columns = 1, align = "left") |>
cols_align(columns = 2:6, align = "center") |>
tab_options(table.align = "left",
data_row.padding = px(2),
row_group.padding = px(2),
table.font.size = 16)
```
</details>
<details>
<summary>
Statistical Testing
</summary>
```{r}
calc_pairwise <- function(surv1, surv2) {
if (surv1 != surv2) {
p <- svyranktest(PAD680~SDDSRVYR, subset(NHANES, SDDSRVYR %in% c(surv1, surv2)))$p.value
} else {
p <- NA
}
return(p)
}
overall_p <- svyranktest(PAD680~SDDSRVYR, NHANES)$p.value
if (overall_p < 0.0001) {
table_footnote <- "P-value from overall test <0.0001"
} else {
table_footnote <- sprintf("P-value from overall test %.4f", overall_p)
}
surveys <- c("2007-2008", "2009-2010", "2011-2012", "2013-2014", "2015-2016", "2017-Mar 2020", "Aug 2021-Aug 2023")
x <- tibble(s1 = surveys, s2 = surveys) |>
expand(s1, s2) |>
rowwise() |>
mutate(p = calc_pairwise(s1, s2)) |>
pivot_wider(names_from = s2, values_from = p)
x |>
gt() |>
cols_label(s1 = "Survey Period") |>
tab_spanner(columns = 2:7, label = "P-values from Pairwise Comparisons") |>
fmt_number(decimals = 4) |>
sub_small_vals(
threshold = 0.0001,
small_pattern = "<0.0001") |>
sub_missing(missing_text = "-") |>
tab_footnote(table_footnote) |>
tab_options(table.align = "left",
data_row.padding = px(2),
row_group.padding = px(2),
table.font.size = 14)
```
</details>
## By Sex
For both males and females,
the distribution of sedentary behavior also appeared to move higher from
2007-2008 to 2013-2014 and then back lower.
```{r}
do_graph() +
theme(strip.background = element_rect(fill = "#fde72522")) +
facet_wrap(vars(RIAGENDR), nrow = 2)
```
## By Age Group
For all age groups,
the distribution of sedentary behavior also appeared to move higher from
2007-2008 to 2013-2014 and then back lower.
```{r}
do_graph() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
strip.background = element_rect(fill = "#fde72522")) +
facet_wrap(vars(RIDAGEYR), nrow = 2)
```