如何利用R中的热图可视化logistic回归

如何利用R中的热图可视化logistic回归,r,visualization,logistic-regression,R,Visualization,Logistic Regression,我有二元观测y(0,1)和两个自变量(x1和x2)的逻辑回归。我想通过热图(2D预测值矩阵)可视化模型预测。我可以部分得到我想要的(见下图),但如何添加: 预测值的色标 适当的轴(水平和垂直) 垂直)对于x1和x2 如何知道矩阵的适当旋转?x1(或x2)位于水平轴还是垂直轴上 数据模拟 种子(16) x_sample.lr使用matrix()时,它按列填充矩阵,因此检查前199个值,所有值均为x2==1 all(lr.pred$predicted[1:199] == pl.pred.mtr

我有二元观测y(0,1)和两个自变量(x1和x2)的逻辑回归。我想通过热图(2D预测值矩阵)可视化模型预测。我可以部分得到我想要的(见下图),但如何添加:

  • 预测值的色标
  • 适当的轴(水平和垂直) 垂直)对于x1和x2
  • 如何知道矩阵的适当旋转?x1(或x2)位于水平轴还是垂直轴上

数据模拟 种子(16) x_sample.lr使用matrix()时,它按列填充矩阵,因此检查前199个值,所有值均为x2==1

all(lr.pred$predicted[1:199] == pl.pred.mtrx[,1])
当您使用image()绘制此矩阵时,实际上是转置矩阵并绘制颜色,您可以尝试使用以下方法:

image(matrix(1:18,ncol=2))
所以在你的图中,X轴是x1,Y轴是x2,我们可以添加轴标签,抑制记号

# we place it at 1,10,20..100
TICKS = c(1,10*(1:10))

image(pl.pred.mtrx,xlab="x1",ylab="x2",xaxt="n",yaxt="n")
# position of your ticks is the num over the length
axis(side = 1, at = which(x_sample.lr %in% TICKS)/nrow(pl.pred.mtrx),labels = TICKS)
axis(side = 2, at = which(x_sample.lr %in% TICKS)/ncol(pl.pred.mtrx),labels = TICKS)

我不知道添加颜色图例的简单方法。因此,我的建议是使用字段:

library(fields)
# in this case we know how x and y will run.. with respect to matrix z
# in other situations, this will depend on how you construct z
DA = list(x=x_sample.lr,y=x_sample.lr,z=pl.pred.mtrx)
image.plot(DA,col=hcl.colors(12, "YlOrRd", rev = TRUE))

library(fields)
# in this case we know how x and y will run.. with respect to matrix z
# in other situations, this will depend on how you construct z
DA = list(x=x_sample.lr,y=x_sample.lr,z=pl.pred.mtrx)
image.plot(DA,col=hcl.colors(12, "YlOrRd", rev = TRUE))