【问题标题】:error in function plot with gam in R - negative binomial predictionR 中带有 gam 的函数图中的错误 - 负二项式预测
【发布时间】:2018-05-01 23:29:22
【问题描述】:

我用gam 构建了一个负二项模型,例如:

> nb.gam <- gam(terms(seizure.rate ~ . * age_cat ,  data = epilepsy_cat),
+                   data = epilepsy_cat , scale=-1, family=nb(link="log"))
> summary(nb.gam)

Family: Negative Binomial(1.495) 
Link function: log 

Formula:
seizure.rate ~ (treatment + age_cat) * age_cat
attr(,"variables")
list(seizure.rate, treatment, age_cat)
attr(,"factors")
             treatment age_cat treatment:age_cat
seizure.rate         0       0                 0
treatment            1       0                 1
age_cat              0       1                 1
attr(,"term.labels")
[1] "treatment"         "age_cat"           "treatment:age_cat"
attr(,"order")
[1] 1 1 2
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>

Parametric coefficients:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)         1.97716    0.29951   6.601 4.08e-11 ***
treatment          -0.46457    0.39763  -1.168   0.2427    
age_cat2            0.06306    0.38880   0.162   0.8712    
age_cat3            0.29152    0.46737   0.624   0.5328    
treatment:age_cat2  0.25385    0.53232   0.477   0.6334    
treatment:age_cat3 -1.58097    0.80874  -1.955   0.0506 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


R-sq.(adj) =  0.0226   Deviance explained = 15.1%
-REML = 164.71  Scale est. = 1         n = 58

但是当我尝试绘制这个时,我得到了一个错误。产生了两个地块,但我预计会出现 4 个地块。 有什么想法吗?

> plot(nb.gam,residuals=TRUE,col="red",shade=TRUE) # cex=1.3,ylim=c(-9,6),
Error in plot.gam(nb.gam, residuals = TRUE, col = "red", shade = TRUE) : 
  No terms to plot - nothing for plot.gam() to do.
> gam.check(nb.gam)

Method: REML   Optimizer: outer newton
full convergence after 2 iterations.
Gradient range [-5.07631e-06,-5.07631e-06]
(score 164.7056 & scale 1).
Hessian positive definite, eigenvalue range [15.68978,15.68978].
Model rank =  6 / 6 

【问题讨论】:

    标签: r gam


    【解决方案1】:

    你拟合了一个没有任何平滑的模型,所以 plot.gam() 没有什么可绘制的。您可以将 all.terms = TRUE 添加到您的 plot() 调用 get it 以绘制线性/参数项。

    library("mgcv")
    set.seed(3)
    n <- 400
    dat <- gamSim(1,n=n)
    g <- exp(dat$f/5)
    
    ## negative binomial data... 
    dat$y <- rnbinom(g,size=3,mu=g)
    
    ## same with theta estimation...
    b <- gam(y ~ x0 + x1 + x2 + x3, family=nb(), data=dat)
    plot(b, pages=1, all.terms = TRUE)
    

    生产

    当使用gam.check() 时,我得到了四个图:

    > gam.check(b)
    
    Method: REML   Optimizer: outer newton
    full convergence after 2 iterations.
    Gradient range [-6.590911e-05,-6.590911e-05]
    (score 1109.152 & scale 1).
    Hessian positive definite, eigenvalue range [113.3885,113.3885].
    Model rank =  5 / 5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-12
      • 2014-12-03
      • 2015-08-16
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多