我正在努力编写一个调用变量的 R 函数get()
假设我有这个数据框:
mydat = data.frame(y = rnorm(100),
x = rnorm(100),
day = sample(90:260, 100, replace = T),
r1 = sample(seq(2008,2015,1), 100, replace = T),
r2 = sample(letters, 100, replace = T),
r3 = sample(letters, 100, replace = T))
我想编写一个返回 gamm 模型摘要的函数,例如像这样
gamm_summary = function(data, response = "y"){
require(mgcv)
gamm_model = gamm(get(response) ~ s(day),
random = list(r1=~1, r2=~1, r3 =~1), data = data, method = "REML")
summary(gamm_model$gam)
}
gamm_summary(mydat)
为什么这会给我错误:
获取(响应)中的错误:找不到对象“响应”
但以下工作:
lm_summary = function(data, response = "y") {
lm_model = lm(get(response) ~ x, data = data)
summary(lm_model)
}
lm_summary(mydat)
问:为什么 get 无法在我的 gamm 函数中工作,我该如何重写该函数以使其工作?