2014-03-12 56 views
1

我試圖繪製一個非線性的決策邊界,這應該是這個樣子:出圖非線性決策邊界

enter image description here

我已經裝形式的正則非線性迴歸:

enter image description here

這是我的數據的摘錄:

ones  test1 test2 use 
1 1 0.051267 0.69956 1 
2 1 -0.092742 0.68494 1 
3 1 -0.213710 0.69225 1 
4 1 -0.375000 0.50219 1 
5 1 -0.513250 0.46564 1 
6 1 -0.524770 0.20980 1 

這是我所使用的Optim()函數計算的參數:

[1] 0.377980476 -0.085951551 0.445140731 
[4] -1.953080687 -0.506554404 -0.330330236 
[7] 0.414649938 0.270281786 0.183804530 
[10] -0.155359467 -0.753665545 0.351880543 
[13] 0.238052214 0.619714119 -0.582420943 
[16] 0.150625144 0.266319363 -0.331130949 
[19] 0.177759335 -0.005402135 -0.124253913 
[22] 0.085607070 0.580258782 0.973785263 
[25] 0.387313615 0.237754576 -0.011198804 
[28] -0.514447404 

我還是新的R,和我真的沒有對如何解決這個問題的任何想法,任何人都可以請幫我嗎?

ones  test1  test2 use 
1  1 0.0512670 0.699560 1 
2  1 -0.0927420 0.684940 1 
3  1 -0.2137100 0.692250 1 
4  1 -0.3750000 0.502190 1 
5  1 -0.5132500 0.465640 1 
6  1 -0.5247700 0.209800 1 
7  1 -0.3980400 0.034357 1 
8  1 -0.3058800 -0.192250 1 
9  1 0.0167050 -0.404240 1 
10  1 0.1319100 -0.513890 1 
11  1 0.3853700 -0.565060 1 
12  1 0.5293800 -0.521200 1 
13  1 0.6388200 -0.243420 1 
14  1 0.7367500 -0.184940 1 
15  1 0.5466600 0.487570 1 
16  1 0.3220000 0.582600 1 
17  1 0.1664700 0.538740 1 
18  1 -0.0466590 0.816520 1 
19  1 -0.1733900 0.699560 1 
20  1 -0.4786900 0.633770 1 
21  1 -0.6054100 0.597220 1 
22  1 -0.6284600 0.334060 1 
23  1 -0.5938900 0.005117 1 
24  1 -0.4210800 -0.272660 1 
25  1 -0.1157800 -0.396930 1 
26  1 0.2010400 -0.601610 1 
27  1 0.4660100 -0.535820 1 
28  1 0.6733900 -0.535820 1 
29  1 -0.1388200 0.546050 1 
30  1 -0.2943500 0.779970 1 
31  1 -0.2655500 0.962720 1 
32  1 -0.1618700 0.801900 1 
33  1 -0.1733900 0.648390 1 
34  1 -0.2828300 0.472950 1 
35  1 -0.3634800 0.312130 1 
36  1 -0.3001200 0.027047 1 
37  1 -0.2367500 -0.214180 1 
38  1 -0.0639400 -0.184940 1 
39  1 0.0627880 -0.163010 1 
40  1 0.2298400 -0.411550 1 
41  1 0.2932000 -0.228800 1 
42  1 0.4832900 -0.184940 1 
43  1 0.6445900 -0.141080 1 
44  1 0.4602500 0.012427 1 
45  1 0.6273000 0.158630 1 
46  1 0.5754600 0.268270 1 
47  1 0.7252300 0.443710 1 
48  1 0.2240800 0.524120 1 
49  1 0.4429700 0.670320 1 
50  1 0.3220000 0.692250 1 
51  1 0.1376700 0.575290 1 
52  1 -0.0063364 0.399850 1 
53  1 -0.0927420 0.553360 1 
54  1 -0.2079500 0.355990 1 
55  1 -0.2079500 0.173250 1 
56  1 -0.4383600 0.217110 1 
57  1 -0.2194700 -0.016813 1 
58  1 -0.1388200 -0.272660 1 
59  1 0.1837600 0.933480 0 
60  1 0.2240800 0.779970 0 
61  1 0.2989600 0.619150 0 
62  1 0.5063400 0.758040 0 
63  1 0.6157800 0.728800 0 
64  1 0.6042600 0.597220 0 
65  1 0.7655500 0.502190 0 
66  1 0.9268400 0.363300 0 
67  1 0.8231600 0.275580 0 
68  1 0.9614100 0.085526 0 
69  1 0.9383600 0.012427 0 
70  1 0.8634800 -0.082602 0 
71  1 0.8980400 -0.206870 0 
72  1 0.8519600 -0.367690 0 
73  1 0.8289200 -0.521200 0 
74  1 0.7943500 -0.557750 0 
75  1 0.5927400 -0.740500 0 
76  1 0.5178600 -0.594300 0 
77  1 0.4660100 -0.418860 0 
78  1 0.3508100 -0.579680 0 
79  1 0.2874400 -0.769740 0 
80  1 0.0858290 -0.755120 0 
81  1 0.1491900 -0.579680 0 
82  1 -0.1330600 -0.448100 0 
83  1 -0.4095600 -0.411550 0 
84  1 -0.3922800 -0.258040 0 
85  1 -0.7436600 -0.258040 0 
86  1 -0.6975800 0.041667 0 
87  1 -0.7551800 0.290200 0 
88  1 -0.6975800 0.684940 0 
89  1 -0.4038000 0.706870 0 
90  1 -0.3807600 0.918860 0 
91  1 -0.5074900 0.904240 0 
92  1 -0.5478100 0.706870 0 
93  1 0.1031100 0.779970 0 
94  1 0.0570280 0.918860 0 
95  1 -0.1042600 0.991960 0 
96  1 -0.0812210 1.108900 0 
97  1 0.2874400 1.087000 0 
98  1 0.3968900 0.823830 0 
99  1 0.6388200 0.889620 0 
100 1 0.8231600 0.663010 0 
101 1 0.6733900 0.641080 0 
102 1 1.0709000 0.100150 0 
103 1 -0.0466590 -0.579680 0 
104 1 -0.2367500 -0.638160 0 
105 1 -0.1503500 -0.367690 0 
106 1 -0.4902100 -0.301900 0 
107 1 -0.4671700 -0.133770 0 
108 1 -0.2885900 -0.060673 0 
109 1 -0.6111800 -0.067982 0 
110 1 -0.6630200 -0.214180 0 
111 1 -0.5996500 -0.418860 0 
112 1 -0.7263800 -0.082602 0 
113 1 -0.8300700 0.312130 0 
114 1 -0.7206200 0.538740 0 
115 1 -0.5938900 0.494880 0 
116 1 -0.4844500 0.999270 0 
117 1 -0.0063364 0.999270 0 
118 1 0.6326500 -0.030612 0 
+0

基本上,我想我需要做的是繪製一個隱式方程如下形式:0.377 - 0.86x_1 + 0.45x_2 - 1.95x_1^2 + ..... - 0.51x_2^6 = 0。找到這個話題:https://stackoverflow.com/questions/5285178/r-plot-implicit-function-outer-command在這種情況下使用輪廓函數,我可以寫一個函數,對應於上面的隱式方程式並使用該函數在輪廓函數中繪製決策邊界? – eager2learn

+0

您發佈的等式有很多變量,但它似乎是一個2D問題。如果是這樣,只要'w0 + w1 * x1 + w2 * x2 = 0',然後隔離'x2 = f(x1)'。 – Fernando

+0

我將整個數據集添加到原始帖子中。 – eager2learn

回答

1

雖然不是理想的答案,你可以使用一個SVM模型可視化這一點(它讓〜0.83樣品中錯誤):

require(e1071) 
data = data[, c("use", "test1", "test2")] 

fit = svm(use ~ ., data = data) 
plot(fit, data = data) 

enter image description here

使用簡單的改造,我們可以嘗試獲得可線性分離的數據集:

data2 = data.frame(
    y = factor(data[, "use"]), 
    x1 = data[, "test1"]^2, 
    x2 = data[, "test2"]^2) 

require(MASS) 
fit = glm(y ~ x2 + x1, data = data2, family = binomial(link = "logit")) 
plot(x2 ~ x1, data = data2, bg = as.numeric(y) + 1, pch = 21, main = "Logistic regression on Y ~ X1 + X2") 
abline(-fit$coefficients[1]/fit$coefficients[2], -fit$coefficients[3]/fit$coefficients[2], col = 'blue', lwd = 2) 

W HICH讓你這個(〜0.73樣本誤差):

enter image description here

所以,現在你有

Y = w0 + w1 * test1^2 + w2 * test2^2 

,您可以使用隔離test2 = f(test1)並繪製非線性邊界。

+0

嘿,非常感謝,我感謝你的努力。這絕對有幫助,但我仍然想用我估計的參數繪製一個決策邊界。如果我能在接下來的幾天中弄清楚這一點,我會在這裏發佈。再次感謝你的幫助。 – eager2learn

+0

不客氣,我會嘗試看看非線性物流,我會回到這裏。 – Fernando