我正在尝试使用 MASS 和 ggplot2 包绘制 Iris 数据集二次判别分析 (QDA) 的结果。脚本的第一部分显示了线性判别分析 (LDA),但我不知道要继续为 QDA 执行此操作。“qda”类的对象与“lda”类对象有点不同,例如:我找不到解释的组间方差/判别分量的迹线比例/X%,无法将它们添加到图中轴。任何帮助或想法如何使用 ggplot2 编码此图?
代码:
require(MASS)
require(ggplot2)
require(scales)
irislda <- lda(Species ~ ., iris)
prop.lda = irislda$svd^2/sum(irislda$svd^2)
plda <- predict(irislda, iris)
datasetLDA = data.frame(species = iris[,"Species"], irislda = plda$x)
ggplot(datasetLDA) + 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=""))
irisqda <- qda(Species ~ ., iris)
pqda <- predict(irisqda, iris)
datasetQDA = data.frame(species = iris[,"Species"], irisqda = pqda$posterior)
ggplot(datasetQDA) + geom_point(???, ???, colour = species, shape = species), size = 2.5)
Run Code Online (Sandbox Code Playgroud)
Oli*_*ver 10
按照 Ducks 的评论,如果您只有 2 个维度,我们可以使用decisionplot链接中提供的函数来可视化这些维度。对于更多的变量,它必须稍微改变。
library(MASS)
model <- qda(Species ~ Sepal.Length + Sepal.Width, iris)
decisionplot(model, iris, class = "Species")
Run Code Online (Sandbox Code Playgroud)
的decisionplot功能如下所示。
decisionplot <- function(model, data, class = NULL, predict_type = "class",
resolution = 100, showgrid = TRUE, ...) {
if(!is.null(class)) cl <- data[,class] else cl <- 1
data <- data[,1:2]
k <- length(unique(cl))
plot(data, col = as.integer(cl)+1L, pch = as.integer(cl)+1L, ...)
# make grid
r <- sapply(data, range, na.rm = TRUE)
xs <- seq(r[1,1], r[2,1], length.out = resolution)
ys <- seq(r[1,2], r[2,2], length.out = resolution)
g <- cbind(rep(xs, each=resolution), rep(ys, time = resolution))
colnames(g) <- colnames(r)
g <- as.data.frame(g)
### guess how to get class labels from predict
### (unfortunately not very consistent between models)
p <- predict(model, g, type = predict_type)
if(is.list(p)) p <- p$class
p <- as.factor(p)
if(showgrid) points(g, col = as.integer(p)+1L, pch = ".")
z <- matrix(as.integer(p), nrow = resolution, byrow = TRUE)
contour(xs, ys, z, add = TRUE, drawlabels = FALSE,
lwd = 2, levels = (1:(k-1))+.5)
invisible(z)
}
Run Code Online (Sandbox Code Playgroud)
如果我们想重新创建它,ggplot2我们只需要更改函数以使用ggplot2函数而不是基本图。这需要将数据更改为data.frames 并在此过程中构建绘图。
decisionplot_ggplot <- function(model, data, class = NULL, predict_type = "class",
resolution = 100, showgrid = TRUE, ...) {
if(!is.null(class)) cl <- data[,class] else cl <- 1
data <- data[,1:2]
cn <- colnames(data)
k <- length(unique(cl))
data$pch <- data$col <- as.integer(cl) + 1L
gg <- ggplot(aes_string(cn[1], cn[2]), data = data) +
geom_point(aes_string(col = 'as.factor(col)', shape = 'as.factor(col)'), size = 3)
# make grid
r <- sapply(data[, 1:2], range, na.rm = TRUE)
xs <- seq(r[1, 1], r[2, 1], length.out = resolution)
ys <- seq(r[1, 2], r[2, 2], length.out = resolution)
g <- cbind(rep(xs, each = resolution),
rep(ys, time = resolution))
colnames(g) <- colnames(r)
g <- as.data.frame(g)
### guess how to get class labels from predict
### (unfortunately not very consistent between models)
p <- predict(model, g, type = predict_type)
if(is.list(p)) p <- p$class
g$col <- g$pch <- as.integer(as.factor(p)) + 1L
if(showgrid)
gg <- gg + geom_point(aes_string(x = cn[1], y = cn[2], col = 'as.factor(col)'), data = g, shape = 20, size = 1)
gg + geom_contour(aes_string(x = cn[1], y = cn[2], z = 'col'), data = g, inherit.aes = FALSE)
}
Run Code Online (Sandbox Code Playgroud)
用法:
decisionplot_ggplot(model, iris, class = "Species")
Run Code Online (Sandbox Code Playgroud)
请注意,它现在返回 ggplot 本身,因此可以使用标准函数来更改标题、主题等。此外,这只是一种直接翻译。使用geom_polygon一个有效的alpha将可能是更多的视觉愉悦。可以使用 的替代选择来制作类似的更好的轮廓geom_*。

| 归档时间: |
|
| 查看次数: |
990 次 |
| 最近记录: |