'Discriminant Analysis (FDA and MDA) plots in R
I am trying to plot the results of Flexible discriminant analysis(FDA) and Mixture discriminant analysis(MDA) using the mda
and ggplot2
package. I did it for Linear discriminant analysis(LDA) but I do not know to continue. Any help or ideas how to code these graphs using ggplot2?
Code:
require(MASS)
require(ggplot2)
require(mda)
require(scales)
irislda <- lda(Species ~ ., iris)
prop.lda = irislda$svd^2/sum(irislda$svd^2)
plda <- predict(irislda, iris)
dataset = data.frame(species = iris[,"Species"], irislda = plda$x)
p1 <- ggplot(dataset) + geom_point(aes(irislda.LD1, irislda.LD2, colour = species, shape = species), size = 2.5) +
labs(x = paste("LD1 (", percent(prop.lda[1]), ")", sep=""),
y = paste("LD2 (", percent(prop.lda[2]), ")", sep=""))
p1
irisfda <- fda(Species ~ ., data = iris, method = mars)
irismda <- mda(Species ~ ., data = iris)
Solution 1:[1]
I believe this gets to what you are after. The fda model only has two dimensions, so it is explaining 100%. The mda model had 5 dimensions, so I am only showing the two that explain the most.
library(dplyr)
irisfda <- fda(Species ~ ., data = iris, method = mars)
irisfda$fit$fitted.values %>%
as_tibble() %>%
bind_cols(species = iris[,"Species"]) %>%
ggplot() +
geom_point(aes(V1, V2, color = species, shape = species), size = 2.5) +
labs(x = paste("FDA1 (", percent(irisfda$percent.explained[1]/100), ")", sep=""),
y = paste("FDA2 (", percent(irisfda$percent.explained[2]/100 - irisfda$percent.explained[1]/100), ")", sep=""))
irismda <- mda(Species ~ ., data = iris)
irismda$fit$fitted.values %>%
as_tibble() %>%
bind_cols(species = iris[,"Species"]) %>%
ggplot() +
geom_point(aes(V1, V2, color = species, shape = species), size = 2.5) +
labs(x = paste("MDA1 (", percent(irismda$percent.explained[1]/100), ")", sep=""),
y = paste("MDA2 (", percent(irismda$percent.explained[2]/100 - irismda$percent.explained[1]/100), ")", sep=""))
EDIT:
To get rid of the warning that you are seeing, we can name the columns of the matrix before passing it to as_tibble
. This edit does not use the %>%
operator.
colnames(irisfda$fit$fitted.values) <- c("V1", "V2")
df1 <- bind_cols(as_tibble(irisfda$fit$fitted.values),
species = iris[,"Species"])
ggplot(df1) +
geom_point(aes(V1, V2, color = species, shape = species), size = 2.5) +
labs(x = paste("FDA1 (", percent(irisfda$percent.explained[1]/100), ")", sep=""),
y = paste("FDA2 (", percent(irisfda$percent.explained[2]/100 - irisfda$percent.explained[1]/100), ")", sep=""))
colnames(irismda$fit$fitted.values) <- c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8")
df2 <- bind_cols(as_tibble(irismda$fit$fitted.values),
species = iris[,"Species"])
ggplot(df2) +
geom_point(aes(V1, V2, color = species, shape = species), size = 2.5) +
labs(x = paste("MDA1 (", percent(irismda$percent.explained[1]/100), ")", sep=""),
y = paste("MDA2 (", percent(irismda$percent.explained[2]/100 - irismda$percent.explained[1]/100), ")", sep=""))
EDIT 2:
It seems that you don't want to use dplyr
so I am including base R functions here with the ggplot
plot.
library(dplyr)
require(MASS)
require(ggplot2)
require(mda)
require(scales)
irisfda <- fda(Species ~ ., data = iris, method = mars)
irismda <- mda(Species ~ ., data = iris)
df1 <- cbind(data.frame(irisfda$fit$fitted.values),
species = iris[,"Species"])
ggplot(df1) +
geom_point(aes(X1, X2, color = species, shape = species), size = 2.5) +
labs(x = paste("FDA1 (", percent(irisfda$percent.explained[1]/100), ")", sep=""),
y = paste("FDA2 (", percent(irisfda$percent.explained[2]/100 - irisfda$percent.explained[1]/100), ")", sep=""))
df2 <- cbind(data.frame(irismda$fit$fitted.values),
species = iris[,"Species"])
ggplot(df2) +
geom_point(aes(X1, X2, color = species, shape = species), size = 2.5) +
labs(x = paste("MDA1 (", percent(irismda$percent.explained[1]/100), ")", sep=""),
y = paste("MDA2 (", percent(irismda$percent.explained[2]/100 - irismda$percent.explained[1]/100), ")", sep=""))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |