nhanesA survey weights

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

library("nhanesA")
Warning: package 'nhanesA' was built under R version 4.4.3
demoj = nhanes("DEMO_J")
dim(demoj)
[1] 9254   46
bpxj = nhanes("BPX_J")
data = merge(demoj, bpxj, by="SEQN")
dim(data)
[1] 8704   66

You can add options to executable code like this

[1] 8704   66

The echo: false option disables the printing of code (only output is displayed).

library("survey")
Warning: package 'survey' was built under R version 4.4.3
Loading required package: grid
Loading required package: Matrix
Loading required package: survival

Attaching package: 'survey'
The following object is masked from 'package:graphics':

    dotchart
nhanesDesign <- svydesign(id = ~SDMVPSU,  # Primary Sampling Units (PSU)
                          strata  = ~SDMVSTRA, # Stratification used in the survey
                          weights = ~WTMEC2YR,   # Survey weights
                          nest    = TRUE,      # Whether PSUs are nested within strata
                          data    = data)

# subset survey design object
dfsub = subset(nhanesDesign, data$RIDAGEYR>=40)

# subset the original dataset
datasub = data[data$RIDAGEYR>=40,]
## mean on total data set

mean(datasub$BPXDI1, na.rm = TRUE)
[1] 73.04455
##split the data by ethnicity and calculate mean of the unweighted data

unweighted_means <- sapply(split(datasub$BPXDI1, datasub$RIDRETH1), mean, na.rm=TRUE)
unweighted_means 
                   Mexican American                      Other Hispanic 
                           72.41000                            72.97611 
                 Non-Hispanic White                  Non-Hispanic Black 
                           70.84130                            75.71466 
Other Race - Including Multi-Racial 
                           74.41311 
adjmns = svymean(~BPXDI1, dfsub, na.rm=TRUE)
adjmns
         mean     SE
BPXDI1 73.237 0.5597
# By ethnicity
adjmnsbyEth = svyby(~BPXDI1, ~RIDRETH1, dfsub, svymean, na.rm=TRUE)

weighted_means <- as.numeric(adjmnsbyEth$BPXDI1)

combined_data <- cbind(unweighted_means, weighted_means)
colnames(combined_data) <- c("Unweighted", "Weighted")
combined_data
                                    Unweighted Weighted
Mexican American                      72.41000 74.03194
Other Hispanic                        72.97611 74.73756
Non-Hispanic White                    70.84130 72.45422
Non-Hispanic Black                    75.71466 75.71874
Other Race - Including Multi-Racial   74.41311 74.60215
# By Gender 
mns = sapply(split(datasub$BPXDI1, datasub$RIAGENDR), mean, na.rm=TRUE)

adjmnsbyGen = svyby(~BPXDI1, ~RIAGENDR, dfsub, svymean, na.rm=TRUE)

combined_data <- cbind(mns, adjmnsbyGen$BPXDI1)
colnames(combined_data) <- c("Unweighted", "Weighted")
combined_data
       Unweighted Weighted
Male     74.67330 75.31134
Female   71.43385 71.31453
# For the unweighted data

quantile(datasub$BPXDI1, c(0.25,0.5,.75), na.rm = TRUE)
25% 50% 75% 
 66  74  82 
# For the survey weighted data
svyquantile(~BPXDI1, dfsub, quantiles = c(0.25,0.5,0.75), na.rm=TRUE)
$BPXDI1
     quantile ci.2.5 ci.97.5        se
0.25       66     66      68 0.4691643
0.5        74     74      76 0.4691643
0.75       80     80      82 0.4691643

attr(,"hasci")
[1] TRUE
attr(,"class")
[1] "newsvyquantile"
# By Gender 
svyby(~BPXDI1, ~RIAGENDR, dfsub, svyquantile, quantiles = c(0.5), na.rm=TRUE)
       RIAGENDR BPXDI1 se.BPXDI1
Male       Male     76 0.9383286
Female   Female     72 0.4691643