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

glmmadmb报错scan() expected 'a real', got 'obs'的原因及解决方法

Fixing the scan() expected 'a real', got 'obs' Error in glmmadmb for Negative Binomial Mixed Models

Let's break down your problem, explain what's going wrong, and walk through how to fix it step by step.

First, here's your code for context:

# Simulate your data
uno<-rnorm(100, 0, 1) 
due<-rpois(100, 2) 
tre<-rnorm(100,0,1) 
randomm<-as.factor(seq(from=1, to=length(uno), by=1)) 
data <- cbind.data.frame(uno=uno, due=due, randomm=randomm, tre=tre)

# Attempt to fit the model
mod <- glmmadmb(due~uno+tre+(1|randomm), data = data, family="nbinom1")

Which throws the error:

Error in scan(text = rr[numLines], quiet = TRUE) : scan() expected 'a real', got 'obs'

What This Error Actually Means

This is a surface-level parsing error: glmmadmb tries to read a temporary output file generated during model fitting, but instead of finding the numerical value (a "real" number) it expects, it encounters the string obs.

Your guess about data types was close, but the root issue is your random effect structure: your randomm factor has 100 unique levels (one for every single observation). The random effect (1|randomm) is trying to estimate a unique intercept for every row in your data—this is statistically meaningless (it's equivalent to overfitting the model completely) and causes the underlying ADMB calculations to produce an abnormal output that glmmadmb can't parse correctly.

How to Fix It

1. Create a Meaningful Random Grouping Variable

The most critical fix is to define a grouping variable with repeated observations per group. For example, split your 100 observations into 10 groups of 10:

# Replace randomm with a grouping variable that has repeated levels
randomm <- as.factor(rep(1:10, each=10))
data <- cbind.data.frame(uno=uno, due=due, randomm=randomm, tre=tre)

2. Verify Distribution Compatibility

Your response variable due is count data generated with rpois(), which works perfectly with nbinom1 (quasi-Poisson/negative binomial type 1). If you still run into issues after fixing the grouping variable, test with a simpler Poisson model first to confirm your model structure works:

# Test with a Poisson mixed model first to validate structure
mod_pois <- glmmadmb(due~uno+tre+(1|randomm), data = data, family="poisson")

# Then switch back to the nbinom1 model
mod_nb1 <- glmmadmb(due~uno+tre+(1|randomm), data = data, family="nbinom1")

3. Update the glmmadmb Package (If Needed)

Occasionally, this error can crop up due to package version bugs. If the above steps don't resolve the issue, update the package and its dependencies:

update.packages("glmmadmb")

Full Working Example

Here's the adjusted code that should run without errors (I added a seed for reproducibility):

set.seed(123) # Ensure results are reproducible
uno<-rnorm(100, 0, 1) 
due<-rpois(100, 2) 
tre<-rnorm(100,0,1) 
randomm<-as.factor(rep(1:10, each=10)) # Meaningful grouping with repeated levels
data <- cbind.data.frame(uno=uno, due=due, randomm=randomm, tre=tre)

# Fit the negative binomial type 1 mixed model
mod <- glmmadmb(due~uno+tre+(1|randomm), data = data, family="nbinom1")
summary(mod)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:02:48