如何使用调查包中的 svyby 计算中位数 svyquantiles?

问题描述 投票:0回答:1

我正在阅读《复杂调查》一书,并试图遵循书中的代码,也许是因为这本书有点旧了,现在代码没有按预期工作,我不确定。

我正在尝试使用

svyby()
中的
survey package
函数计算子群体的中位数。

使用的数据可以在这里找到。

library(survey)
library(haven)
chis_adult <- read_dta("Adult.dta")

以下行不在书中,但出于某种原因,我无法设置设计并将 chis_adult 设置为 data.frame 使 svrepdesign 工作,不确定这是否相关并且与错误有任何关系:

chis_adult <- as.data.frame(chis_adult)

chis <- svrepdesign(variables = chis_adult[,1:418],
                    repweights = chis_adult[,420:499],
                    weights = chis_adult[,419], combined.weights = T,
                    type= "other", scale = 1, rscales =1)

现在,我尝试使用以下代码计算子群体的中位数:

medians <- svyby(~bmi_p, ~srsex+racehpr, svyquantile, design = chis, 
                 quantiles = 0.5, covmat = T)

这给了我错误:

Error in (function (cond)  : 
  error in evaluating the argument 'x' in selecting a method for function 't': Can't merge the outer name `ci` with a vector of length > 1.
Please supply a `.name_spec` specification.

我正在尝试设置诸如

na.rm = T, na.rm.by = T, na.rm.all = T
之类的选项,但它们似乎都不起作用。知道会发生什么吗?

r survey
1个回答
0
投票

我没有明确的答案,但错误似乎是由于 Stata 文件中变量的标签造成的,这导致

svyby
函数出现问题。

尝试使用

foreign::read.dta

再次读取文件
library(foreign)
chis_adult <- read.dta("Adult.dta")

chis <- svrepdesign(variables = chis_adult[,1:418],
                    repweights = chis_adult[,420:499],
                    weights = chis_adult[,419], combined.weights = T,
                    type= "other", scale = 1, rscales =1)

以下命令现在返回不同的错误:

svyby(~bmi_p, ~srsex+racehpr, 
      FUN=svyquantile, 
      design = chis, 
      quantiles = 0.5, 
      covmat = T)
#Error in attr(replicates, "scale") <- design$scale : 
#  attempt to set an attribute on NULL

我通过将

covmat
设置为
FALSE
(默认值)来设法防止这种情况。

svyby(~bmi_p, ~srsex+racehpr, 
       FUN=svyquantile, 
       design = chis, 
       quantiles = 0.5)
                                       srsex                        racehpr bmi_p   se.bmi_p
MALE.LATINO                             MALE                         LATINO 27.44 0.12057562
FEMALE.LATINO                         FEMALE                         LATINO 26.26 0.17583981
MALE.PACIFIC ISLANDER                   MALE               PACIFIC ISLANDER 28.87 0.70448510
FEMALE.PACIFIC ISLANDER               FEMALE               PACIFIC ISLANDER 27.26 0.67903816
MALE.AMERICAN INDIAN/ALASKAN NATIVE     MALE AMERICAN INDIAN/ALASKAN NATIVE 28.37 0.61292679
FEMALE.AMERICAN INDIAN/ALASKAN NATIVE FEMALE AMERICAN INDIAN/ALASKAN NATIVE 25.09 0.49486275
MALE.ASIAN                              MALE                          ASIAN 24.37 0.13564793
FEMALE.ASIAN                          FEMALE                          ASIAN 22.14 0.11806357
MALE.AFRICAN AMERICAN                   MALE               AFRICAN AMERICAN 27.41 0.37428760
FEMALE.AFRICAN AMERICAN               FEMALE               AFRICAN AMERICAN 27.20 0.22356736
MALE.WHITE                              MALE                          WHITE 26.39 0.06028781
FEMALE.WHITE                          FEMALE                          WHITE 24.28 0.04521598
MALE.OTHER SINGLE/MULTIPLE RACE         MALE     OTHER SINGLE/MULTIPLE RACE 26.63 0.40443079
FEMALE.OTHER SINGLE/MULTIPLE RACE     FEMALE     OTHER SINGLE/MULTIPLE RACE 25.69 0.36675145
© www.soinside.com 2019 - 2024. All rights reserved.