r调查包:几何平均值及置信区间

wpx232ag  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(224)

我大致复制了this代码:

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)

他们计算简单的算术平均值如下:

# Arithmetic mean
svymean(~age, ageDesign, na.rm = TRUE)

我想计算(1)使用svymean或一些相关方法计算几何平均值(2)几何平均值的95%置信区间,如果可能的话,无需手动使用SE。

svymean(~log(age), log(ageDesign), na.rm = TRUE)

但这会引发一个错误,我该怎么做

7dl7o3gd

7dl7o3gd1#

您希望取变量的对数,而不是整个调查设计的对数(这没有任何意义)
下面介绍如何计算log(年龄)的平均值,获得置信区间,然后将其取幂到几何平均值尺度

> svymean(~log(age), ageDesign, na.rm = TRUE)
           mean     SE
log(age) 3.7489 0.0115
> meanlog<-svymean(~log(age), ageDesign, na.rm = TRUE)
> confint(meanlog)
            2.5 %   97.5 %
log(age) 3.726351 3.771372
> exp(confint(meanlog))
            2.5 %   97.5 %
log(age) 41.52729 43.43963

或者,您可以先求幂,然后构造置信区间:

> (geomean<-svycontrast(meanlog, quote(exp(`log(age)`))))
          nlcon     SE
contrast 42.473 0.4878
> confint(geomean)
            2.5 %   97.5 %
contrast 41.51661 43.42879

我希望第一种方法一般能给予更好的置信区间,但在这里几乎没有什么区别。

相关问题