簡體   English   中英

R:在ggplot2中繪制線性判別分析的后驗分類概率

[英]R: plotting posterior classification probabilities of a linear discriminant analysis in ggplot2

使用ggord可以做出很好的線性判別分析ggplot2 (參見M. Greenacre的“實踐中的ggplot2第11章,圖11.5),如

library(MASS)
install.packages("devtools")
library(devtools)
install_github("fawda123/ggord")
library(ggord)
data(iris)
ord <- lda(Species ~ ., iris, prior = rep(1, 3)/3)
ggord(ord, iris$Species)

在此輸入圖像描述

我還想添加分類區域(顯示為與其各自組相同顏色的實心區域,例如α= 0.5)或類別隸屬度的后驗概率(隨后alpha根據此后驗概率和相同顏色變化用於每個組)(可以在BiplotGUI完成,但我正在尋找一個ggplot2解決方案)。 有誰知道如何使用ggplot2 ,也許使用geom_tile

編輯:下面有人詢問如何計算后驗分類概率和預測類別。 這是這樣的:

library(MASS)
library(ggplot2)
library(scales)
fit <- lda(Species ~ ., data = iris, prior = rep(1, 3)/3)
datPred <- data.frame(Species=predict(fit)$class,predict(fit)$x)
#Create decision boundaries
fit2 <- lda(Species ~ LD1 + LD2, data=datPred, prior = rep(1, 3)/3)
ld1lim <- expand_range(c(min(datPred$LD1),max(datPred$LD1)),mul=0.05)
ld2lim <- expand_range(c(min(datPred$LD2),max(datPred$LD2)),mul=0.05)
ld1 <- seq(ld1lim[[1]], ld1lim[[2]], length.out=300)
ld2 <- seq(ld2lim[[1]], ld1lim[[2]], length.out=300)
newdat <- expand.grid(list(LD1=ld1,LD2=ld2))
preds <-predict(fit2,newdata=newdat)
predclass <- preds$class
postprob <- preds$posterior
df <- data.frame(x=newdat$LD1, y=newdat$LD2, class=predclass)
df$classnum <- as.numeric(df$class)
df <- cbind(df,postprob)
head(df)

           x        y     class classnum       setosa   versicolor virginica
1 -10.122541 -2.91246 virginica        3 5.417906e-66 1.805470e-10         1
2 -10.052563 -2.91246 virginica        3 1.428691e-65 2.418658e-10         1
3  -9.982585 -2.91246 virginica        3 3.767428e-65 3.240102e-10         1
4  -9.912606 -2.91246 virginica        3 9.934630e-65 4.340531e-10         1
5  -9.842628 -2.91246 virginica        3 2.619741e-64 5.814697e-10         1
6  -9.772650 -2.91246 virginica        3 6.908204e-64 7.789531e-10         1

colorfun <- function(n,l=65,c=100) { hues = seq(15, 375, length=n+1); hcl(h=hues, l=l, c=c)[1:n] } # default ggplot2 colours
colors <- colorfun(3)
colorslight <- colorfun(3,l=90,c=50)
ggplot(datPred, aes(x=LD1, y=LD2) ) +
    geom_raster(data=df, aes(x=x, y=y, fill = factor(class)),alpha=0.7,show_guide=FALSE) +
    geom_contour(data=df, aes(x=x, y=y, z=classnum), colour="red2", alpha=0.5, breaks=c(1.5,2.5)) +
    geom_point(data = datPred, size = 3, aes(pch = Species,  colour=Species)) +
    scale_x_continuous(limits = ld1lim, expand=c(0,0)) +
    scale_y_continuous(limits = ld2lim, expand=c(0,0)) +
    scale_fill_manual(values=colorslight,guide=F)

在此輸入圖像描述

(並不完全確定這種使用1.5和2.5的輪廓/間隔顯示分類邊界的方法總是正確的 - 對於物種1和2以及物種2和3之間的邊界是正確的,但如果物種1的區域是在物種3旁邊,因為那時我會得到兩個邊界 - 也許我將不得不使用這里使用的方法其中每個物種對之間的每個邊界被單獨考慮)

這使得我可以繪制分類區域。 我正在尋找一種解決方案,同時也繪制每個物種在每個坐標處的實際后驗分類概率,使用與每個物種的后驗分類概率成比例的α(不透明度)和物種特定的顏色。 換句話說,疊加三個圖像的堆疊。 由於已知ggplot2中的alpha混合是依賴順序的 ,我認為此堆棧的顏色必須事先計算,並使用類似的東西繪制

qplot(x, y, data=mydata, fill=rgb, geom="raster") + scale_fill_identity() 

這是我所追求的SAS示例

在此輸入圖像描述

也許有人知道怎么做嗎? 或者是否有人對如何最好地表示這些后驗分類概率有任何想法?

請注意,該方法應適用於任意數量的組,而不僅僅適用於此特定示例。

我想最簡單的方法是顯示后驗概率。 對你的案子來說非常簡單:

datPred$maxProb <- apply(predict(fit)$posterior, 1, max)
ggplot(datPred, aes(x=LD1, y=LD2) ) +
  geom_raster(data=df, aes(x=x, y=y, fill = factor(class)),alpha=0.7,show_guide=FALSE) +
  geom_contour(data=df, aes(x=x, y=y, z=classnum), colour="red2", alpha=0.5, breaks=c(1.5,2.5)) +
  geom_point(data = datPred, size = 3, aes(pch = Species,  colour=Species, alpha = maxProb)) +
  scale_x_continuous(limits = ld1lim, expand=c(0,0)) +
  scale_y_continuous(limits = ld2lim, expand=c(0,0)) +
  scale_fill_manual(values=colorslight, guide=F)

在此輸入圖像描述

您可以看到這些點以藍綠色邊框混合。

還提出了以下簡單的解決方案:只需在df中創建一個列,其中隨機地進行類預測,根據后驗概率,然后導致不確定區域中的抖動,例如

fit = lda(Species ~ Sepal.Length + Sepal.Width, data = iris, prior = rep(1, 3)/3)
ld1lim <- expand_range(c(min(datPred$LD1),max(datPred$LD1)),mul=0.5)
ld2lim <- expand_range(c(min(datPred$LD2),max(datPred$LD2)),mul=0.5)

如上所述,插入

lvls=unique(df$class)
df$classpprob=apply(df[,as.character(lvls)],1,function(row) sample(lvls,1,prob=row))

p=ggplot(datPred, aes(x=LD1, y=LD2) ) +
  geom_raster(data=df, aes(x=x, y=y, fill = factor(classpprob)),hpad=0, vpad=0, alpha=0.7,show_guide=FALSE) +
  geom_point(data = datPred, size = 3, aes(pch = Group,  colour=Group)) +
  scale_fill_manual(values=colorslight,guide=F) +
  scale_x_continuous(limits=rngs[[1]], expand=c(0,0)) +
  scale_y_continuous(limits=rngs[[2]], expand=c(0,0))

給我 在此輸入圖像描述

比起以某種加成或減少方式混合顏色要容易和清晰得多(這是我仍然遇到麻煩的部分,而且顯然不是那么容易做得好)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM