我想将公式的右侧传递给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'
谢谢,德里克,我不知道这个函数存在。 – 2012-05-04 04:48:31
@MariusHofert在我看来,这个答案比目前接受的更优雅。你能重新考虑接受吗? – Andrie 2013-08-05 13:26:02