簡體   English   中英

R-負二項式預測中帶有gam的函數圖中的誤差

[英]error in function plot with gam in R - negative binomial prediction

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

您擬合的模型沒有任何平滑,因此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)

生產

在此處輸入圖片說明

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM