为什么 lda() 不接受字符串作为 'formula' 参数?
Why won't lda() accept a string as it's 'formula' argument?
我正在做一些回归分析,我发现 MASS
库中的 lda
函数有一些奇怪的行为。具体来说,它似乎无法接受字符串,因为它是 formula
参数。这似乎不是基本 glm
函数的问题。我使用 iris
构建了一个小示例来说明这一点。
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)
上面最后一行抛出:
Error in lda.default(formula = myForm, data = iris) :
argument "x" is missing, with no default
从 documentation 来看,这似乎表明 lda
认为没有提供 formula
参数。有谁知道为什么会这样,或者如何解决它?
myForm<-as.formula(paste("Species","Petal.Length",sep="~"))
lgrIris<-glm(formula=myForm, data=iris, family="binomial")
您可以使用 as.formula()
将 "myForm" 转换为公式:
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
我正在做一些回归分析,我发现 MASS
库中的 lda
函数有一些奇怪的行为。具体来说,它似乎无法接受字符串,因为它是 formula
参数。这似乎不是基本 glm
函数的问题。我使用 iris
构建了一个小示例来说明这一点。
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)
上面最后一行抛出:
Error in lda.default(formula = myForm, data = iris) :
argument "x" is missing, with no default
从 documentation 来看,这似乎表明 lda
认为没有提供 formula
参数。有谁知道为什么会这样,或者如何解决它?
myForm<-as.formula(paste("Species","Petal.Length",sep="~"))
lgrIris<-glm(formula=myForm, data=iris, family="binomial")
您可以使用 as.formula()
将 "myForm" 转换为公式:
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