如何仅从gam.check中获取地块 [英] How to get only the plots from gam.check
问题描述
在mgcv
包中应用gam.check
时,R
会生成一些残差图和基本尺寸输出.有没有办法仅生成图而不是打印输出?
When applying gam.check
in the mgcv
package, R
produces some residual plots and basis dimension output. Is there a way to only produce the plots and not the printed output?
library(mgcv)
set.seed(0)
dat <- gamSim(1,n=200)
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3), data=dat)
plot(b, pages=1)
gam.check(b, pch=19, cex=.3)
推荐答案
有四个图,从左上方开始,向下移动并穿过,我们有:
There are four plots, from top left, moving down and across we have:
- 残差的QQ图
- 残差的直方图
- 残差与线性预测变量的关系图
- 观察值与拟合值的关系图.
在下面的代码中,根据您的示例,我假定b
包含您的拟合模型.首先,我们需要一些东西
In the code below, I assume b
contains your fitted model, as per your example. First some things we need
type <- "deviance" ## "pearson" & "response" are other valid choices
resid <- residuals(b, type = type)
linpred <- napredict(b$na.action, b$linear.predictors)
observed.y <- napredict(b$na.action, b$y)
请注意,最后两行采用的是NA
处理方法,该方法适用于将模型拟合到linear.predictors
和y
(响应数据的存储副本)上的信息.
Note the last two lines are applying the NA
handling method used when the model was fitted to the information on the linear.predictors
and y
, the stored copy of the response data.
上面的代码和下面显示的代码均在gam.check()
源代码的前10行左右给出.要查看此内容,只需输入
The above code and that shown below is all given in the first 10 or so lines of the gam.check()
source. To view this, just enter
gam.check
在R提示符下.
每个图的产生方式如下:
Each plot is produced as follows:
这是通过qq.gam()
产生的:
qq.gam(b, rep = 0, level = 0.9, type = type, rl.col = 2,
rep.col = "gray80")
残差直方图
这是使用
Histogram of residuals
This is produced using
hist(resid, xlab = "Residuals", main = "Histogram of residuals")
残差与线性预测变量
这是使用
Residuals vs linear predictor
This is produced using
plot(linpred, resid, main = "Resids vs. linear pred.",
xlab = "linear predictor", ylab = "residuals")
观测值与拟合值
这是使用
Observed vs fitted values
This is produced using
plot(fitted(b), observed.y, xlab = "Fitted Values",
ylab = "Response", main = "Response vs. Fitted Values")
这篇关于如何仅从gam.check中获取地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!