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

How to visualize logistic regression by heatmap in R

我有二元观察值 y (0,1) 和两个自变量(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, 50, replace = TRUE),
                    x2 = sample(x_sample.lr, 50, 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)

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

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

当你用 image() 绘制这个矩阵时,你实际上转置了矩阵并绘制了颜色,你可以试试这个:

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

所以在你的图中,x 轴是 x1,ya 轴是 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))