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

前端 未结 2 926
小鲜肉
小鲜肉 2020-12-24 08:37

Using ggord one can make nice linear discriminant analysis ggplot2 biplots (cf chapter 11, Fig 11.5 in \"Biplots in practice\" by M. Greenacre), as

相关标签:
2条回答
  • 2020-12-24 08:45

    Also just came up with the following easy solution: just make a column in df where class predictions are made stochastically, according to the posterior probabilities, which then results in dithering in uncertain regions, e.g. as in

    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)
    

    rest as above, and inserting

    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))
    

    gives me

    A lot easier and clearer than starting to mix colours in some additive or subtractive fashion anyway (which is the part where I still had trouble, and which apparently is not so trivial to do well).

    0 讨论(0)
  • 2020-12-24 08:53

    I suppose the easiest way will be to show the posterior probabilities. It is pretty straightforward for your case:

    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)
    

    You can see the points blend in at blue-green border.

    0 讨论(0)
提交回复
热议问题