I am trying to programmatically create the formula for a gamm
estimation, which I want to use later in a Shiny app. My goal is to create a formula of this kind:
y ~ s(x, bs = "cs")
Im stuck because I dont know how to paste the part after the comma: bs="cs"
.
input<-list()
input$x<-"mpg"
input$y<- "hp"
formula<-as.formula(paste(input$y_variable," ~ ",paste(paste("s(",paste(input$x_variable,collapse="+")),', bs="cs")')))
z <- mgcv::gamm(formula, data=mtcars)
In cases where multiple commands entered in a single line start messing with your understanding of the code, it usually makes sense to break them up:
dep_vars <- paste0(input$x, collapse = "+")
after_tilde <- paste0("s(", dep_vars, ", bs = 'cs')")
dyn_string <- paste0(input$y, " ~ ", after_tilde)
mgcv::gamm(as.formula(dyn_string), data=mtcars)
However, it turns out that the actual problem is that you defined input$x
and input$y
, but you were using input$x_variable
and input_y_variable
in the formula creation instead