英文:
r survey svyquantile: Rounding the number of decimal places
问题
以下是代码的翻译部分:
library(haven)
library(survey)
library(dplyr)
nhanesDemo <- read_xpt(url("https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT"))
# 为变量重命名,使其更易读
nhanesDemo$fpl <- nhanesDemo$INDFMPIR
nhanesDemo$age <- nhanesDemo$RIDAGEYR
nhanesDemo$gender <- nhanesDemo$RIAGENDR
nhanesDemo$persWeight <- nhanesDemo$WTINT2YR
nhanesDemo$psu <- nhanesDemo$SDMVPSU
nhanesDemo$strata <- nhanesDemo$SDMVSTRA
# 选择必要的列
nhanesAnalysis <- nhanesDemo %>%
select(fpl, age, gender, persWeight, psu, strata)
# 设置设计
nhanesDesign <- svydesign(id = ~psu,
strata = ~strata,
weights = ~persWeight,
nest = TRUE,
data = nhanesAnalysis)
# 选择年龄在18到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
的默认小数位数舍入,我无法在提供的文档中找到相关信息。
英文:
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)
The default rounding of svyquantile
appears to be two digits past the decimal place. How can I change this? I couldn't find anything in the documentation.
答案1
得分: 2
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"
英文:
svyquantile
does no rounding.
In this example, the two digit precision is the precision of the data: fpl
is given to only two decimal places and by default svyquantile
returns the left quantile, which is always one of the observed values. In fact, most of the distinct values of fpl
occur multiple times: there are 20 observations equal to the 10th percentile, 29 equal to the median, and 1220 equal to the 90th percentile, so the quantile will be equal to one of the observed values in this example no matter what you specify for the qrule
argument.
If you make fpl
noisier, you'll get more digits
> 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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论