r survey svyquantile: 舍入小数位数

huangapple go评论57阅读模式
英文:

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 &lt;- read_xpt(url(&quot;https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT&quot;))

# Rename variables into something more readable
nhanesDemo$fpl &lt;- nhanesDemo$INDFMPIR
nhanesDemo$age &lt;- nhanesDemo$RIDAGEYR
nhanesDemo$gender &lt;- nhanesDemo$RIAGENDR
nhanesDemo$persWeight &lt;- nhanesDemo$WTINT2YR
nhanesDemo$psu &lt;- nhanesDemo$SDMVPSU
nhanesDemo$strata &lt;- nhanesDemo$SDMVSTRA

# Select the necessary columns
nhanesAnalysis &lt;- nhanesDemo %&gt;%
  select(fpl, age, gender, persWeight, psu, strata)

# Set up the design
nhanesDesign &lt;- svydesign(id      = ~psu,
                          strata  = ~strata,
                          weights = ~persWeight,
                          nest    = TRUE,
                          data    = nhanesAnalysis)

# Select those between the agest of 18 and 79
ageDesign &lt;- subset(nhanesDesign, age &gt; 17 &amp; age &lt; 80 &amp; !is.na(fpl))

quantile_results &lt;- 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

&gt; ageDesign&lt;-update(ageDesign, fpl_noisy=fpl+runif(nrow(ageDesign),0,0.005))
&gt; 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(,&quot;hasci&quot;)
[1] TRUE
attr(,&quot;class&quot;)
[1] &quot;newsvyquantile&quot;

huangapple
  • 本文由 发表于 2023年2月7日 02:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定