Nguồn Lớp học tại VLU * “R and machine learning” *.
Giới thiệu: Đây là phưphương phân tích mô hình phân định tuyến tính
Tải thư viện
library(datasets)
library(MASS)
library(ggplot2)
data(iris)
data(iris)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
fit <- lda(Species~Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data=iris)
fit
## Call:
## lda(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
## data = iris)
##
## Prior probabilities of groups:
## setosa versicolor virginica
## 0.3333333 0.3333333 0.3333333
##
## Group means:
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## setosa 5.006 3.428 1.462 0.246
## versicolor 5.936 2.770 4.260 1.326
## virginica 6.588 2.974 5.552 2.026
##
## Coefficients of linear discriminants:
## LD1 LD2
## Sepal.Length 0.8293776 0.02410215
## Sepal.Width 1.5344731 2.16452123
## Petal.Length -2.2012117 -0.93192121
## Petal.Width -2.8104603 2.83918785
##
## Proportion of trace:
## LD1 LD2
## 0.9912 0.0088
predicted <- predict(fit, iris)
plot.data <- cbind(iris, predict(fit)$x)
head(plot.data)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species LD1 LD2
## 1 5.1 3.5 1.4 0.2 setosa 8.061800 0.3004206
## 2 4.9 3.0 1.4 0.2 setosa 7.128688 -0.7866604
## 3 4.7 3.2 1.3 0.2 setosa 7.489828 -0.2653845
## 4 4.6 3.1 1.5 0.2 setosa 6.813201 -0.6706311
## 5 5.0 3.6 1.4 0.2 setosa 8.132309 0.5144625
## 6 5.4 3.9 1.7 0.4 setosa 7.701947 1.4617210
ggplot()
ggplot(data=plot.data, aes(x=LD1, y=LD2, col=Species))+geom_point()