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

使用R中SIMR包绘制聚类观测数相关功效曲线报错求助

解决simr包powerCurve绘图报错:need finite 'xlim' values

问题背景

我正在用R的simr包做两层泊松回归模型的功效分析,想绘制功效曲线观察每个聚类的观测数对功效的影响,但调用powerCurve()函数时触发了绘图错误。

尝试的绘图代码:

power_curve2 <- powerCurve(model, within="x + Z ", breaks=c(20,50,80,100), nsim = 10)
plot(power_curve2)

遇到的错误信息:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

解决方案

1. 修正powerCurve()的参数逻辑

你混淆了within参数的用法:该参数用于指定聚类变量(而非预测变量),以此定义要调整的层级样本量。你的模型中聚类变量是g,目标是调整每个聚类的观测数(L1样本量),因此需要调整参数如下:

修改后的代码:

# 针对L1直接效应x,绘制不同聚类观测数的功效曲线
power_curve2 <- powerCurve(
    model, 
    within = "g",          # 指定聚类变量g,调整其内部观测数
    breaks = c(20,50,80,100), # 要测试的每个聚类的观测数
    nsim = 10,
    fixed("x")             # 明确针对x的效应做功效分析
)
plot(power_curve2)

2. 验证聚类数据结构

确保原始数据中每个聚类的观测数是均匀的(你的代码中已保证每个聚类有100个观测),可以提前验证:

table(X$g) # 查看每个聚类的观测数分布

3. 先检查powerCurve输出再绘图

运行修正后的powerCurve后,先打印输出对象确认存在有效数据:

print(power_curve2)

若输出中包含每个breaks对应的功效值,再调用plot()即可正常生成曲线。

额外提示

  • 若需调整聚类数量(L2样本量),应使用between参数而非within:
    # 绘制不同聚类数量的功效曲线
    power_curve_clusters <- powerCurve(
        model,
        between = "g",
        breaks = c(20,30,40,50),
        nsim = 10,
        fixed("x")
    )
    plot(power_curve_clusters)
    
  • 增加nsim数值(如100)可提升功效估计的稳定性,但会增加计算时间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 04:22:30