You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用svyciprop分析事后分层子组时出现NaN/0值问题求助

关于survey包事后分层后svyciprop计算异常的问题

我是R语言survey包及事后分层(post-stratification)的新手,若有疏漏还请见谅,非常感谢任何关于问题原因或解决方向的建议!

遗憾的是最初无法提供最小可复现示例,因为我还没能在自有数据外重现问题(数据无法共享)。

问题描述

我在R的survey包(v4.1-1)中,对事后分层的数据运行svyciprop函数,通过svyby或subset获取子组的结果比例,示例代码如下(dat为样本数据,dat_pop为总体数据):

svydat <- svydesign(~1, data = dat, weights = ~1)
svydat <- postStratify(svydat, strata = ~sex, population = table(sex = dat_pop$sex))
svyciprop(~outcome, design = subset(svydat, group == "a"))

有时我会得到估计值及其置信区间全为NaN,或全为0的结果(全0结果明显与数据不符),且无明显规律。
同时会重复两次出现如下警告(部分正常输出的场景也会出现此警告):

1: In summary.glm(g) :
  observations with zero weight not used for calculating dispersion

补充细节

在以下几种相关场景中未出现该问题:

  • 先将数据限制为目标子组,再进行事后分层;
  • 在全量数据上运行svyciprop;
  • 使用method = "beta"、"asin"、"mean"或"xlogit"计算置信区间(这些方法返回结果看起来合理);

反之,使用部分方法时会出现异常:

  • 使用method = "likelihood"时完全无法得到结果,报错如下:
Error in seq.int(xmin, xmax, length.out = n) : 
  'from' must be a finite number

并附加警告:

3: In regularize.values(x, y, ties, missing(ties)) :
  collapsing to unique 'x' values
  • 不使用事后分层,而是显式指定权重参数运行svyciprop(包括使用含所有交叉项的逻辑回归逆概率近似事后分层场景)。

编辑:可复现示例(2023-04-11)

以下是可重现问题的模拟数据:

library(survey)
# Make data -------------------------------------- 

# I'm making three separate datasets then combining them
# For the simulated group on which data is available, making "a" and "b" groups separately 
# Then making the group of people who do not have data, who can belong to both "a" and "b" group
dat_a <- 
  data.frame(
    group = "a",
    sex = c(rep("M", 3000), rep("F", 4000), rep("M", 9000), rep("F", 8000)),
    outcome = c(rep(F, 3000 + 4000), rep(T, 9000 + 8000)),
    inc = 1
  )
dat_b <- 
  data.frame(
    group = "b",
    sex = c(rep("M", 30), rep("F", 50), rep("M", 120), rep("F", 160)),
    outcome = c(rep(F, 30 + 50), rep(T, 120 + 160)),
    inc = 1
  )
dat_out <- 
  data.frame(
    group = c(rep("a", 20 + 30), rep("b", 2000 + 2500)),
    sex = c(rep("M", 20), rep("F", 30), rep("M", 2000), rep("F", 2500)),
    outcome = NA,
    inc = 0
    )


dat_all <- rbind(dat_a, dat_b, dat_out)
dat_inc <- dat_all[dat_all$inc == 1, ]           

# Perform post stratification ------------------------
svydat <- svydesign(ids = ~1, data = dat_inc, weights = ~1)
svydat_ps <- postStratify(svydat, strata = ~sex, population = table(sex = dat_all$sex))


# Demonstrate failure of certain type of CI (but not others) when using svyciprop ----------------------------- 
for (method in  c( "asin", "beta",
                  "mean","xlogit", "logit",  "likelihood")){
  print(method)
  print(svyby(~ outcome, ~ group, svydat_ps, svyciprop, vartype = "ci", method = method))
}


# This problem in a couple of methods for CIs can be traced to svyglm ------------------------------------------
mygroup <- "b"
design <- subset(svydat_ps, group == mygroup)
svyglm(outcome ~ 1, design, 
       family = quasibinomial)$coef

# Interestingly, it doesn't seem to have a problem if I force the start (suggesting it's a convergence issue?):
svyglm(outcome ~ 1, design, 
       family = quasibinomial, start = 0)$coef

进一步排查发现,部分置信区间方法的问题可追溯至svyglm函数;若强制指定start参数,问题似乎得以解决,推测可能是收敛问题。


内容的提问来源于stack exchange,提问作者r_epi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 07:44:58