Search code examples
roptimizationmodelinglda

R Passing parameters to lda() or qda()


I'm trying to optimize my code by saving parameters in vector form and pass it to lda() for modeling. The following method works fine for lm, but not for qda or lda. The error message I received is highlighted in yellow.

intvars <- c("x*y","y*t","z*w")
intfm <- paste("clickthrough", "~", paste(intvars, collapse = " + "))
lda_model_int <- lda(intfm, data = s_train)

Error in lda.default(intfm, data = s_train) : 'x' is not a matrix


Solution

  • You will have to change your string to formula or you can reformulate

    intvars <- c("x*y","y*t","z*w")
    intfm <- reformulate(intvars,"clickthrough")
    lda_model_int <- lda(intfm, data = s_train)
    

    If you wanted to do it your way you will have to do

    intvars <- c("x*y","y*t","z*w")
    intfm <- as. formula(paste("clickthrough", "~", paste(intvars, collapse = " + ")))
    lda_model_int <- lda(intfm, data = s_train)