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

GLM模型拟合报错‘NA/NaN/Inf in 'y'’排查求助

Troubleshooting "NA/NaN/Inf in 'y'" Error in GLM Fit

Hey there, let's work through this GLM error you're facing. Even though you confirmed there are no NA values in your dataset, the "NA/NaN/Inf in 'y'" error can pop up from a few hidden issues—let's break them down one by one:

  • Check for Inf/NaN in your response variable
    The table(is.na()) check only catches NA values, not infinite (Inf) or not-a-number (NaN) values. Your response variable rope.directional.change might have these hidden issues. Run these quick checks to confirm:

    # Check for infinite values in the response
    any(is.infinite(rope_complete$rope.directional.change))
    # Check for NaN values in the response
    any(is.nan(rope_complete$rope.directional.change))
    

    If either returns TRUE, you'll need to clean those values (e.g., filter out rows with Inf/NaN or correct the source data).

  • Investigate your offset term
    Your offset uses log(rope_complete$rope.Time.of.the.shark.in.the.video)—and here's a common gotcha: if any value in rope.Time.of.the.shark.in.the.video is 0, log(0) returns -Inf, which will throw off the model. Check for zero values with:

    any(rope_complete$rope.Time.of.the.shark.in.the.video == 0)
    

    If you find zeros, you have a few fixes:

    1. Filter out rows where the time is 0 (if those observations aren't meaningful for your analysis)
    2. Add a tiny constant to the time values before taking the log (e.g., log(rope.Time.of.the.shark.in.the.video + 0.01) to avoid log(0))
  • Verify your code is complete and clean
    The code snippet you shared cuts off mid-offset definition: offset( log(rope_complete$rope.Tim.... Make sure you've written the full offset term (it should reference rope.Time.of.the.shark.in.the.video properly). Also, using the data parameter will make your code cleaner and less prone to typos:

    # Corrected, cleaner code example (with offset adjustment if needed)
    directional_turn_fit <- glm(rope.directional.change ~ rope.X...Sound + offset(log(rope.Time.of.the.shark.in.the.video + 0.01)),
                                data = rope_complete)
    
  • Double-check variable types
    Ensure rope.directional.change is a numeric variable (not a factor or character string). A wrong variable type can sometimes trigger unexpected errors. Confirm with:

    class(rope_complete$rope.directional.change)
    

Start with checking the offset term first—odds are that zero time values are causing the Inf values that break your model.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:45:08