R - 负二项式预测中带有gam的函数图中的误差

时间:2018-04-30 16:06:40

标签: r gam

我构建了一个带有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 

1 个答案:

答案 0 :(得分:1)

你安装了一个没有任何光滑的模型,所以plot.gam()无需绘制。您可以在all.terms = TRUE调用中添加plot(),也可以绘制线性/参数字词。

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)
制造

enter image description here

使用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

enter image description here