绘制逻辑回归的三维决策边界

时间:2014-10-02 16:09:13

标签: r plot 3d logistic-regression boundary

我已经安装了一个逻辑回归模型,该模型考虑了3个变量。我想制作数据点的三维图并绘制决策边界(我想这是一个平面)。

我找到了适用于案例的在线示例(以便您可以直接加载数据)

mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")

我正在考虑使用3Dscatterplot包,但我不确定应该用什么方程来绘制边界。有什么想法吗?

非常感谢,

1 个答案:

答案 0 :(得分:1)

决策边界将是一个三维平面,您可以使用R中的任何三维绘图包进行绘图。我将通过定义xy网格然后使用persp计算相应的z值。 outer函数:

# Use iris dataset for example logistic regression
data(iris)
iris$long <- as.numeric(iris$Sepal.Length > 6)
mod <- glm(long~Sepal.Width+Petal.Length+Petal.Width, data=iris, family="binomial")

# Plot 50% decision boundary; another cutoff can be achieved by changing the intercept term
x <- seq(2, 5, by=.1)
y <- seq(1, 7, by=.1)
z <- outer(x, y, function(x, y) (-coef(mod)[1] - coef(mod)[2]*x - coef(mod)[3]*y) /
       coef(mod)[4])
persp(x, y, z, col="lightblue")

enter image description here

相关问题