如何在R中通过热图可视化逻辑回归

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

  • 色标到预测值
  • 合适的轴(水平和垂直) 垂直)用于x1和x2
  • 我如何知道矩阵的适当旋转? x1(或x2)在水平或垂直轴上吗?

...

# data simulation
set.seed(16)
x_sample.lr <- seq(1,100,by = 0.5)
# data.frame creation
lr.df <- data.frame(y = sample(c(0,1),50,replace = TRUE),x1 = sample(x_sample.lr,x2 = sample(x_sample.lr,replace = TRUE))

# model creation
lr.mod <- glm(y ~ x1*x2,data = lr.df,family = "binomial")
anova(lr.mod,test = "Chi")
summary(lr.mod)

# ...calculating prediction
lr.pred <- expand.grid(x1 = x_sample.lr,x2 = x_sample.lr)
lr.pred$predicted <- predict(lr.mod,newdata = lr.pred)
head(lr.pred)
#    x1 x2 predicted
# 1 1.0  1  2.306825
# 2 1.5  1  2.279347
# 3 2.0  1  2.251869

# ...plot visualization
pl.pred.mtrx <- matrix(lr.pred$predicted,ncol = sqrt(nrow(lr.pred)))
image(pl.pred.mtrx)

如何在R中通过热图可视化逻辑回归

chengwei8520 回答:如何在R中通过热图可视化逻辑回归

当您使用matrix()时,它会按列填充矩阵,因此检查您的前199个值,所有值均x2 == 1,

all(lr.pred$predicted[1:199] == pl.pred.mtrx[,1])

使用image()绘制此矩阵时,实际上是转置了矩阵并绘制了颜色,可以尝试以下操作:

image(matrix(1:18,ncol=2))

因此在您的绘图中,x轴是x1,yaxis是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)

enter image description here

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

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

enter image description here

本文链接:https://www.f2er.com/3159400.html

大家都在问