我正在對我的數據進行QDA,需要繪製R
或MATLAB
中的決策邊界,它看起來像下面的函數。在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,似乎無法找到解決方案。
我正在對我的數據進行QDA,需要繪製R
或MATLAB
中的決策邊界,它看起來像下面的函數。在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,似乎無法找到解決方案。
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)
+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)
它不應該是三維的。它應該看起來像從https://onlinecourses.science.psu.edu/stat557/book/export/html/35第二個從底部的情節 – user35824
也許我會嘗試輪廓 – user35824