[英]plot.gam from mgcv with all.terms = TRUE within a function
I want to write a function that estimates a gam
with factor variables and plots the result of all variables, including the factor variables.我想写一个
gam
来估计一个带有因子变量的游戏并绘制所有变量的结果,包括因子变量。 However, the plot
function from the mgcv-package produces an error.但是,
plot
包中的 plot function 会产生错误。 Why does this error occur and how can I solve it?为什么会出现此错误,我该如何解决?
library(mgcv)
plot_model <- function(x){
agam <- gam(mean ~ s(bla) + bla2, data=x)
plot(agam, pages=1, all.terms = TRUE)
# here Error in eval(expr, envir, enclos) : object 'x' not found
}
bla <- data.frame(bla=rnorm(20), bla2=sample(letters[1:4], size=20, replace=T),
mean=sample(20))
plot_model(bla)
# Error in eval(expr, envir, enclos) : object 'x' not found
Apparently, x
needs to be declared in the local environment so that plot.gam
can use it for the plot. You can make it work as follows:显然,
x
需要在本地环境中声明,以便plot.gam
可以将它用于 plot。您可以按如下方式使其工作:
library(mgcv)
plot_model <- function(y){
data <- y
agam <- mgcv::gam(mean ~ s(bla) + bla2, data=data)
mgcv::plot.gam(x = agam, pages=1, all.terms = TRUE)
# here Error in eval(expr, envir, enclos) : object 'x' not found
}
dat <- data.frame(bla=rnorm(20), bla2=sample(letters[1:4], size=20, replace=T),
mean=sample(20))
plot_model(dat)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.