Classification functions in linear discriminant analysis in R

前端 未结 3 1520
陌清茗
陌清茗 2020-12-19 03:30

After completing a linear discriminant analysis in R using lda(), is there a convenient way to extract the classification functions for each group?

Fro

3条回答
  •  情书的邮戳
    2020-12-19 03:52

    Suppose x is your LDA object:

    x$terms
    

    You can have a peak at the object by looking at it's structure:

    str(x)
    

    Update:

    Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),Sp = rep(c("s","c","v"), rep(50,3)))
    train <- sample(1:150, 75)
    table(Iris$Sp[train])
    z <- lda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train)
    predict(z, Iris[-train, ])$class
    str(z)
    List of 10
     $ prior  : Named num [1:3] 0.333 0.333 0.333
      ..- attr(*, "names")= chr [1:3] "c" "s" "v"
     $ counts : Named int [1:3] 30 25 20
      ..- attr(*, "names")= chr [1:3] "c" "s" "v"
     $ means  : num [1:3, 1:4] 6.03 5.02 6.72 2.81 3.43 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:3] "c" "s" "v"
      .. ..$ : chr [1:4] "Sepal.L." "Sepal.W." "Petal.L." "Petal.W."
     $ scaling: num [1:4, 1:2] 0.545 1.655 -1.609 -3.682 -0.443 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:4] "Sepal.L." "Sepal.W." "Petal.L." "Petal.W."
      .. ..$ : chr [1:2] "LD1" "LD2"
     $ lev    : chr [1:3] "c" "s" "v"
     $ svd    : num [1:2] 33.66 2.93
     $ N      : int 75
     $ call   : language lda(formula = Sp ~ ., data = Iris, prior = c(1, 1, 1)/3, subset = train)
     $ terms  :Classes 'terms', 'formula' length 3 Sp ~ Sepal.L. + Sepal.W. + Petal.L. + Petal.W.
      .. ..- attr(*, "variables")= language list(Sp, Sepal.L., Sepal.W., Petal.L., Petal.W.)
      .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
      .. .. ..- attr(*, "dimnames")=List of 2
      .. .. .. ..$ : chr [1:5] "Sp" "Sepal.L." "Sepal.W." "Petal.L." ...
      .. .. .. ..$ : chr [1:4] "Sepal.L." "Sepal.W." "Petal.L." "Petal.W."
      .. ..- attr(*, "term.labels")= chr [1:4] "Sepal.L." "Sepal.W." "Petal.L." "Petal.W."
      .. ..- attr(*, "order")= int [1:4] 1 1 1 1
      .. ..- attr(*, "intercept")= int 1
      .. ..- attr(*, "response")= int 1
      .. ..- attr(*, ".Environment")=

提交回复
热议问题