qq.gam
位於 mgcv
包(package)。 說明
采用 gam()
生成的擬合 gam
對象並生成其殘差的 QQ 圖(以擬合模型係數和尺度參數為條件)。如果滿足模型分布假設,那麽通常這些圖應該接近一條直線(盡管離散數據可能會產生與該線明顯的隨機偏離)。
用法
qq.gam(object, rep=0, level=.9,s.rep=10,
type=c("deviance","pearson","response"),
pch=".", rl.col=2, rep.col="gray80", ...)
參數
object |
由 |
rep |
要生成多少個複製數據集來模擬殘差分布的分位數。 |
level |
如果對分位數使用模擬,則可以為 QQ-plot 提供參考區間,這指定了級別。 0 或更少表示沒有間隔,1 或更多則簡單地為生成的每個重複繪製 QQ 圖。 |
s.rep |
在直接計算下將均勻分位數隨機化到數據的次數。 |
type |
應該繪製什麽樣的殘差?請參閱 |
pch |
要使用的情節字符。 19 很好。 |
rl.col |
繪圖上參考線的顏色。 |
rep.col |
參考帶或複製參考圖的顏色。 |
... |
傳遞給繪圖函數的額外圖形參數。 |
細節
模型殘差的 QQ-plots 可以通過兩種方式之一生成。最便宜的方法通過將均勻分布的分位數與每個數據相關聯,並將這些均勻分位數輸入與每個數據相關的分位數函數來生成參考分位數。然後使用所得分位數代替每個數據來生成殘差的近似分位數。殘餘分位數是對數據的均勻分位數的 s.rep
隨機化進行平均的。
第二種方法是使用直接模擬。對於每個重複,根據擬合模型模擬數據,並計算相應的殘差。這會重複 rep
次。分位數很容易從如此獲得的殘差的經驗分布中獲得。通過該方法也可以計算參考帶。
即使rep
設置為零,如果該族沒有可用的分位數函數,例程也會嘗試模擬分位數。如果沒有可用的隨機偏差生成函數族(例如,對於擬族),則生成正常的QQ-plot。擬合模型係數和尺度參數估計的常規條件。
這些圖與 Ben 和 Yohai (2004) 中提出的圖非常相似,但製作起來要便宜得多(不建議對 Ben 和 Yohai 中的二進製數據殘差進行解釋)。
請注意,從擬合到二進製數據的原始殘差圖幾乎不包含有關模型擬合的有用信息。殘差是負還是正取決於響應是零還是一。給定符號後,殘差的大小完全由擬合值決定。因此,從二進製數據的殘差 QQ-plots 中隻能檢測到最嚴重的模型違規。要真正檢查二進製數據殘差的分布假設,您必須能夠以某種方式對數據進行分組。除二元以外的二項式模型都可以。
例子
library(mgcv)
## simulate binomial data...
set.seed(0)
n.samp <- 400
dat <- gamSim(1,n=n.samp,dist="binary",scale=.33)
p <- binomial()$linkinv(dat$f) ## binomial p
n <- sample(c(1,3),n.samp,replace=TRUE) ## binomial n
dat$y <- rbinom(n,n,p)
dat$n <- n
lr.fit <- gam(y/n~s(x0)+s(x1)+s(x2)+s(x3)
,family=binomial,data=dat,weights=n,method="REML")
par(mfrow=c(2,2))
## normal QQ-plot of deviance residuals
qqnorm(residuals(lr.fit),pch=19,cex=.3)
## Quick QQ-plot of deviance residuals
qq.gam(lr.fit,pch=19,cex=.3)
## Simulation based QQ-plot with reference bands
qq.gam(lr.fit,rep=100,level=.9)
## Simulation based QQ-plot, Pearson resids, all
## simulated reference plots shown...
qq.gam(lr.fit,rep=100,level=1,type="pearson",pch=19,cex=.2)
## Now fit the wrong model and check....
pif <- gam(y~s(x0)+s(x1)+s(x2)+s(x3)
,family=poisson,data=dat,method="REML")
par(mfrow=c(2,2))
qqnorm(residuals(pif),pch=19,cex=.3)
qq.gam(pif,pch=19,cex=.3)
qq.gam(pif,rep=100,level=.9)
qq.gam(pif,rep=100,level=1,type="pearson",pch=19,cex=.2)
## Example of binary data model violation so gross that you see a problem
## on the QQ plot...
y <- c(rep(1,10),rep(0,20),rep(1,40),rep(0,10),rep(1,40),rep(0,40))
x <- 1:160
b <- glm(y~x,family=binomial)
par(mfrow=c(2,2))
## Note that the next two are not necessarily similar under gross
## model violation...
qq.gam(b)
qq.gam(b,rep=50,level=1)
## and a much better plot for detecting the problem
plot(x,residuals(b),pch=19,cex=.3)
plot(x,y);lines(x,fitted(b))
## alternative model
b <- gam(y~s(x,k=5),family=binomial,method="ML")
qq.gam(b)
qq.gam(b,rep=50,level=1)
plot(x,residuals(b),pch=19,cex=.3)
plot(b,residuals=TRUE,pch=19,cex=.3)
作者
Simon N. Wood simon.wood@r-project.org
參考
N.H. Augustin, E-A Sauleaub, S.N. Wood (2012) On quantile quantile plots for generalized linear models Computational Statistics & Data Analysis. 56(8), 2404-2409.
M.G. Ben and V.J. Yohai (2004) JCGS 13(1), 36-47.
https://www.maths.ed.ac.uk/~swood34/
也可以看看
相關用法
- R vcov.gam 從 GAM 擬合中提取參數(估計器)協方差矩陣
- R gam.check 擬合 gam 模型的一些診斷
- R null.space.dimension TPRS 未懲罰函數空間的基礎
- R gam.reparam 尋找平方根懲罰的穩定正交重新參數化。
- R extract.lme.cov 從 lme 對象中提取數據協方差矩陣
- R scat 用於重尾數據的 GAM 縮放 t 係列
- R choldrop 刪除並排名第一 Cholesky 因子更新
- R smooth.construct.cr.smooth.spec GAM 中的懲罰三次回歸樣條
- R bandchol 帶對角矩陣的 Choleski 分解
- R gam.side GAM 的可識別性邊條件
- R cox.ph 附加 Cox 比例風險模型
- R mgcv.parallel mgcv 中的並行計算。
- R gamm 廣義加性混合模型
- R pdTens 實現張量積平滑的 pdMat 類的函數
- R Predict.matrix GAM 中平滑項的預測方法
- R Predict.matrix.soap.film 皂膜光滑度預測矩陣
- R smooth.construct.bs.smooth.spec GAM 中的懲罰 B 樣條
- R gamlss.gH 計算回歸係數的對數似然導數
- R plot.gam 默認 GAM 繪圖
- R mvn 多元正態加性模型
- R gfam 分組家庭
- R smooth.construct GAM 中平滑項的構造函數
- R pcls 懲罰約束最小二乘擬合
- R gam.fit3 使用 GCV、UBRE/AIC 或 RE/ML 導數計算進行 P-IRLS GAM 估計
- R rTweedie 生成 Tweedie 隨機偏差
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 QQ plots for gam model residuals。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。