我有对数观测值y(0,1)和两个自变量(x1和x2)的逻辑回归。我想通过热图(2D中的预测值矩阵)可视化模型预测。我可以部分得到我想要的东西(见下图),但是如何添加:
...
# 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)
答案 0 :(得分:1)
当您使用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)
我不知道添加颜色图例的简单方法。所以我的建议是使用字段:
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))