R中调用draw函数可视化bam模型报错:无匹配继承方法
解决bam模型调用gratia::draw()时的方法匹配错误
问题描述
运行bam模型后调用gratia包的draw()函数时反复报错,已加载gratia和mgcv包,尝试简化模型复杂度、更换拟合方法均未解决。
代码示例
library("gratia") library("mgcv") m3 <- bam(occur ~ s(temp) + # sal + s(DO) + sed_depth + # water_depth + ave_sav + s(prop_sg) + # prop_ma + Season + TimeOfDay + s(ToD) + s(DoY, bs = 'cc') + CYR.std + # Spatial variation s(Longitude, Latitude, bs = 'tp') + s(fSite, bs = "re"), data = toad, method = 'fREML', # knots = knots, # nthreads = 4, discrete = TRUE, family = binomial(link = "logit"), select = TRUE, gamma = 1.5) draw(m3, select = c(1,2), scale = "fixed")
报错信息
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘draw’ for signature ‘"bam"’
解决方案
- 核心原因:gratia的
draw()函数仅支持gam类对象,未针对bam类实现对应方法。 - 方法一:转换bam对象为gam类
bam与gam模型结构高度兼容,手动修改对象类后即可调用draw():class(m3) <- "gam" draw(m3, select = c(1,2), scale = "fixed") - 方法二:使用mgcv原生绘图函数
直接调用mgcv包的plot.gam()实现可视化,无需转换对象:plot(m3, select = c(1,2), scale = "fixed")
内容的提问来源于stack exchange,提问作者Nate
相关产品推荐
相关产品推荐

