in the following function I have to use the bquote function when fitting the gam model to avoid the error
Error in eval(expr, envir, enclos) : object 'x' not found
when plot.gam is called. The error occurs due to the factor variable I want to plot. However I don't really understand what the bquote does here and why I need it.
library(mgcv)
plot_model <- function(x){
# agam <- gam(mean ~ s(bla) + bla2, data=x)
agam <- eval(bquote(mgcv::gam(mean ~ s(bla) + bla2, data=.(x))))
plot(agam, pages=1, all.terms = TRUE)
}
bla <- data.frame(bla=rnorm(20), bla2=sample(letters[1:4], size=20, replace=T),
mean=sample(20))
plot_model(bla)
The R-help says "bquote quotes its argument except that terms wrapped in .() are evaluated in the specified where environment. Usage bquote(expr, where = parent.frame())." What is the where environment here (parent.frame = the plot_model environment?) and what environment would it be evaluated in without the bquote (the environment created by the call to gam?)?
bquotesubstitutes the expression.(X)with the value ofx. Therefore what is actually evaluated is:This error pops up because
plot.gamasks to look for the symbolxin the global environment rather than the environment ofplot_model.When debugged with
recover: