我想将公式的右侧传递给 R 函数,然后“添加”公式的左侧并调用gam()
. 我想在没有丑陋的 as.formula() 结构等的情况下实现这一点。
我陷入了以下最小示例,您知道出了什么问题吗?
require(mgcv)
set.seed(0) ## set.seed(1)
gamEx1 <- gamSim(1, n=400, dist="normal", scale=2) ## simulate some data
str(gamEx1) ## display structure
## calling gam() and passing the right-hand side of a formula
gamFitter <- function(formula.RHS, data, ...){
z <- 2*data$y + data$f0 # some given values
gam(z ~ formula.RHS, data=data, ...) # call gam()
}
## call the function with the right-hand side of a formula
gamFitter(formula.RHS=~s(x0)+s(x1)+s(x2)+s(x3), data=gamEx1)
Error in model.frame.default(formula = z ~ formula.RHS, data = data,
drop.unused.levels = TRUE) :
invalid type (language) for variable 'formula.RHS'