library(haven)
library(survey)
library(dplyr)
nhanesDemo <- read_xpt(url("https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT"))
# Rename variables into something more readable
nhanesDemo$fpl <- nhanesDemo$INDFMPIR
nhanesDemo$age <- nhanesDemo$RIDAGEYR
nhanesDemo$gender <- nhanesDemo$RIAGENDR
nhanesDemo$persWeight <- nhanesDemo$WTINT2YR
nhanesDemo$psu <- nhanesDemo$SDMVPSU
nhanesDemo$strata <- nhanesDemo$SDMVSTRA
# Select the necessary columns
nhanesAnalysis <- nhanesDemo %>%
select(fpl, age, gender, persWeight, psu, strata)
# Set up the design
nhanesDesign <- svydesign(id = ~psu,
strata = ~strata,
weights = ~persWeight,
nest = TRUE,
data = nhanesAnalysis)
# Select those between the agest of 18 and 79
ageDesign <- subset(nhanesDesign, age > 17 & age < 80 & !is.na(fpl))
quantile_results <- svyquantile(~fpl, ageDesign, quantiles=c(0.1, 0.5, 0.9))
print(quantile_results)
svyquantile
的默认四舍五入是小数点后两位。我如何更改它?我在documentation中找不到任何内容。
1条答案
按热度按时间kb5ga3dv1#
svyquantile
不进行舍入。在此示例中,两位数精度是数据的精度:
fpl
只保留两位小数,默认情况下,svyquantile
返回左分位数,该分位数始终是观测值之一。实际上,fpl
的大多数不同值会出现多次:有20个观测值等于第10百分位数,29个观测值等于中位数,1220个观测值等于第90百分位数,因此无论您为qrule
参数指定什么,分位数都将等于本例中的一个观测值。如果使
fpl
的噪声更大,则会得到更多位数