I am struggling with writing an R function that calls a variable using get()
Suppose I have this data frame:
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))
and I would like to write a function that returns the summary of a gamm model, e.g like this
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)
Why does this give me the error:
Error in get(response) : object 'response' not found
yet the following works:
lm_summary = function(data, response = "y") {
lm_model = lm(get(response) ~ x, data = data)
summary(lm_model)
}
lm_summary(mydat)
Q: Why does get fail to work in my gamm function and how could I rewrite the function so that it works?