R plot.gam错误"1:object $ nsdf中的错误:长度为0的参数"; [英] R plot.gam Error "Error in 1:object$nsdf : argument of length 0"
问题描述
我试图在R中绘制一个gam对象,该对象是我用gam包制作的.我收到 1:object $ nsdf中的错误:使用plot.gam 时长度为0的错误.但是,在那里找到的解决方案(我认为是更新到最新版本)对我不起作用.我正在运行R 3.3.1,gam 1.12和mgcv 1.8.12(mgcv是plot.gam函数的来源).
I am trying to plot a gam object in R, which I made with the gam package. I receive the same error reported in Error in 1:object$nsdf : argument of length 0 when using plot.gam. However, the solution found there, updating to the latest versions (I think), is not working for me. I am running R 3.3.1, gam 1.12, and mgcv 1.8.12 (mgcv is where the plot.gam function is from).
很遗憾,我无法共享正在使用的数据.但是,以下代码-直接从Intro的p.294中提取. R进行统计学习的方法-为我重现错误:
Unfortunately, I cannot share the data I am working with. However, the following code -- pulled directly from the p.294 of Intro. to Statistical Learning with R -- reproduces the error for me:
library(gam)
library(ISLR) # contains the Wage dataset used here
gam.mod <- gam(wage ~ s(year, 4) + s(age, 5) + education, data = Wage)
plot(gam.mod)
有人知道这里发生了什么或如何解决?
Does anybody know what is going on here or how to fix it?
谢谢.
推荐答案
如果仍然收到此消息,则需要将mgcv
和gam
软件包更新为最新版本. 2018年2月对gam
软件包进行了重大更改:找不到函数plot.gam .这意味着,gam
软件包所适合的GAM现在具有"Gam"类,即使加载了mgcv
软件包,plot
也不会选择mgcv::plot.gam
进行绘制.
If you still get this message, you need to update your mgcv
and gam
package to the latest version. A big change was made to gam
package in Feb, 2018: Could not find function plot.gam. This means, a GAM fitted by gam
package now have "Gam" class, and even if mgcv
package is loaded, plot
will not choose mgcv::plot.gam
to plot it.
但是,在R会话中同时包含两个软件包仍然是不安全的.因此,仍然强烈建议您在2016年提出以下建议.
However, it is still unsafe to have both packages in an R session. So the following suggestion made in 2016 is still highly recommended.
建议
最好具有此玩具功能,以检查R会话是否可以运行GAM分析.
It might be a good idea have this toy function to check whether an R session is OK to run GAM analysis.
GAM_status <- function () {
if (all(c("gam", "mgcv") %in% .packages())) print("Not OK")
else print("OK")
}
nsdf
是严格自由度的数量,这是专门在mgcv
中使用的术语.如您所述:mgcv
是plot.gam
函数的来源.
nsdf
is the the number of strict degree of freedom, a term exclusively used in mgcv
. As you mentioned: mgcv
is where the plot.gam
function is from.
问题是您在R会话中同时具有gam
和mgcv
,这两个不兼容的程序包.您将gam.mod
与gam::gam
拟合,然后使用mgcv::plot.gam
绘制模型.
The problem is that you have gam
and mgcv
, two incompatible packages in your R session at the same time. You fit your gam.mod
with gam::gam
, but then plot the model with mgcv::plot.gam
.
注意,通常使用::
的情况在这里会失效.通常,当两个软件包具有某些相互屏蔽的功能时,使用::
是补救措施.但是,对于mgcv
和gam
,这是完全不可能的.所以我的建议是,如果您使用gam
,请不要在R会话中触摸mgcv
,反之亦然.
Note, what is normally true by using ::
will lose effect here. Normally when two packages have some inter-masked functions, the ::
is the remedy. But, for mgcv
and gam
, this is completely impossible. So my suggestion is, if you use gam
, do not ever touch mgcv
in your R session, and vice versa.
所以,我开始一个新的R会话,然后执行以下操作,一切都很好!
So, I start a fresh R session, and do the following, everything is fine!
library(gam)
library(ISLR) # contains the Wage dataset used here
gam.mod <- gam(wage ~ s(year, 4) + s(age, 5) + education, data = Wage)
par(mfrow = c(2,2)); plot(gam.mod)
谢谢您的回答.我从未真正加载过
mgcv
,我只是假设它是gam
的依赖项.我开始了一个新的R会话,并且您提供的代码起作用了.我发现实际上是导致相同问题的car
库.
Thank you for your answer. I never actually loaded
mgcv
, I just assumed it was a dependency forgam
. I started a fresh R session and the code you provided worked. I found that it is actually thecar
library that is causing the same issue.
mgcv
和gam
彼此不依赖,但是由于mgcv
比gam
更流行,因此许多软件包都依赖mgcv
,例如,
mgcv
and gam
does not depend on each other, but since mgcv
is more popular than gam
, many packages has dependency on mgcv
, for example, car
:
car: Companion to Applied Regression
Functions and Datasets to Accompany J. Fox and S. Weisberg, An R Companion to
Applied Regression, Second Edition, Sage, 2011.
Version: 2.1-3
Depends: R (≥ 3.2.0)
Imports: MASS, mgcv, nnet, pbkrtest (≥ 0.4-4), quantreg, grDevices, utils,
stats, graphics
请注意导入"字段,library(car)
将同时加载这些软件包.
Note the "Imports" field, library(car)
will load these packages at the same time.
这篇关于R plot.gam错误"1:object $ nsdf中的错误:长度为0的参数";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!