r调查分位数:舍入小数位数

y0u0uwnf  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(267)
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中找不到任何内容。

kb5ga3dv

kb5ga3dv1#

svyquantile不进行舍入。
在此示例中,两位数精度是数据的精度:fpl只保留两位小数,默认情况下,svyquantile返回左分位数,该分位数始终是观测值之一。实际上,fpl的大多数不同值会出现多次:有20个观测值等于第10百分位数,29个观测值等于中位数,1220个观测值等于第90百分位数,因此无论您为qrule参数指定什么,分位数都将等于本例中的一个观测值。
如果使fpl的噪声更大,则会得到更多位数

> ageDesign<-update(ageDesign, fpl_noisy=fpl+runif(nrow(ageDesign),0,0.005))
> svyquantile(~fpl_noisy, ageDesign, quantiles=c(0.1, 0.5, 0.9))
$fpl_noisy
     quantile    ci.2.5   ci.97.5         se
0.1 0.8027744 0.7128426 0.8841695 0.04019022
0.5 2.9711470 2.5921659 3.3747105 0.18357099
0.9 5.0031355 5.0027002 5.0035307 0.00019482

attr(,"hasci")
[1] TRUE
attr(,"class")
[1] "newsvyquantile"

相关问题