Reputation: 2409
I'm doing some regression analysis and I've come across some strange behavior from the lda
function in the MASS
library. Specifically, it seems to be unable to accept a string as it's formula
argument. This doesn't appear to be a problem for the base glm
functions. I've constructed a small example using iris
to illustrate the point.
library(MASS)
myForm<-"Species~Petal.Length"
# Disregard the warnings from this line, they're an artifact of the example. It works.
lgrIris<-glm(formula=myForm, data=iris, family="binomial")
# Breaks.
ldaIris<-lda(formula=myForm, data=iris)
The final line above throws:
Error in lda.default(formula = myForm, data = iris) :
argument "x" is missing, with no default
Which, judging from the documentation, seems to indicate that lda
doesn't think it's been provided a formula
argument. Does anyone know why this is happening, or how to fix it?
Upvotes: 1
Views: 310
Reputation:
You can turn "myForm" into a formula using as.formula()
:
myForm <- "Species~Petal.Length"
class(myForm)
# [1] "character"
myForm <- as.formula(myForm)
class(myForm)
# [1] "formula"
myForm
# Species ~ Petal.Length
lda(formula=myForm, data=iris)
# Call:
# lda(myForm, data = iris)
# Prior probabilities of groups:
# setosa versicolor virginica
# 0.3333333 0.3333333 0.3333333
# Group means:
# Petal.Length
# setosa 1.462
# versicolor 4.260
# virginica 5.552
# Coefficients of linear discriminants:
# LD1
# Petal.Length 2.323774
Upvotes: 3
Reputation: 15458
myForm<-as.formula(paste("Species","Petal.Length",sep="~"))
lgrIris<-glm(formula=myForm, data=iris, family="binomial")
Upvotes: 1