2013-12-07 53 views
1

我正在对我的数据进行QDA,需要绘制RMATLAB中的决策边界,它看起来像下面的函数。在R中绘制函数

0.651 - 0.728(x_1) - 0.552(x_2) - 0.006(x_1 * x_2) - 0.071(x_1)^2 +.170 (x_2)^2 = 0

谁能帮助我?我一直在搜索interwebs,似乎无法找到解决方案。

+0

它不应该是三维的。它应该看起来像从https://onlinecourses.science.psu.edu/stat557/book/export/html/35第二个从底部的情节 – user35824

+0

也许我会尝试轮廓 – user35824

回答

3

R没有绘制这样一个隐式函数的函数,但是可以用等高线图很好地伪装它。假设你想绘制在区域[0,1]^2(它可以很容易地改变),你需要一些这样的代码:

#f is your function 
f <- function (x_1, x_2) 0.651 - 0.728 * x_1 - 0.552 * x_2 - 0.006 * x_1 * x_2 - 0.071 * x_1^2 +.170 * x_2^2 

#length=1000 to be safe, could be set lower 
x_1 <- seq(0, 1, length=1000) 
x_2 <- seq(0, 1, length=1000) 
z <- outer(x_1, x_2, FUN=f) 
contour(x_1, x_2, z, levels=0) 
+0

+1这是一个很酷的想法。 –

1

同样的事情用ggplot:

library(ggplot2) 
library(reshape2) # for melt(...) 
## same as first response 
x <- seq(0,10,length=1000) 
y <- seq(-10,10, length=1000) 
z <- outer(x,y,FUN=f) 
# need this extra step 
gg <- melt(z, value.name="z",varnames=c("X","Y")) 
# creates the plot 
ggplot(gg) + stat_contour(aes(x=X, y=Y, z=z), breaks=0)