is.data.frame(数据) 中的错误:找不到对象 'data=''

Error in is.data.frame(data) : object 'data='' not found

我是 r 的新手,正在努力编写一个函数:我正在尝试制作一个适合 GAM 的函数。我找到了一个线性模型的例子,它工作得很好。但是,尝试对 GAM 使用相同的结构会引发错误。

线性模型:

library(mgcv)
library(ggplot2)
theme_set(theme_bw())
set.seed(100)
dd <- data.frame(x=1:100,y=round(rnorm(100,mean=100),1), z=round(runif(100,1,4),1))
lm(y~x, weights=z, data=dd)$call
lm(formula = y ~ x, data = dd, weights = z)

f2 <- function(f,w,d){
   do.call("lm", list(formula=as.formula(f), weights=as.name(w), data=as.name(d)))
 }
 f2("y~x", "z", "dd")$call
lm(formula = y ~ x, data = dd, weights = z)

和 GAM 版本

set.seed(100)
dd <- data.frame(x=1:100,y=round(rnorm(100,mean=100),1))
gam(y ~ s(x, k=64, bs='ad'), data = dd, method = 'REML', select = 'TRUE')$call
gam(formula = y ~ s(x, k=64, bs='ad'), data = dd, method = 'REML', select = 'TRUE')

 f2 <- function(f,m,s,d){
    do.call("gam", list(formula=as.formula(f),method=as.name(m),select=as.name(s),data=as.name(d)))
  }

f2("y ~ s(x, k=64, bs='ad')", "method = 'REML'", "select = 'TRUE'", "data = dd")$call

# Error in is.data.frame(data) : object 'data = dd' not found occurs

gam(formula = y ~ s(x, k = 64, bs = "ad"), data = dd)

通话:

f2 <- function(f, m, s, d){
    do.call("gam", list(formula=as.formula(f),method = "REML",select = s, data = as.name(d)))
}
f2("y ~ s(x, k=64, bs='ad')", "REML", TRUE, d = "dd")$call

结果:

gam(formula = y ~ s(x, k = 64, bs = "ad"), data = dd, method = "REML", 
    select = TRUE)