2016-11-19 64 views
0

我是R的新手,我一直在试图创建Fisher LDA,但是我在R中使用向量和度量方法的时间很困难,如果有人能够告诉我我做得对,因为我得到这个错误当我尝试绘制决策边界如何绘制R中Fisher LDA的决策边界?

Error in xy.coords(x, y, xlabel, ylabel, log) : 
'x' and 'y' lengths differ 

,当我删除XLIM和ylim我得到这个

Error in plot.window(...) : need finite 'xlim' values 
In addition: Warning messages: 
1: In min(x) : no non-missing arguments to min; returning Inf 
2: In max(x) : no non-missing arguments to max; returning -Inf 
3: In min(x) : no non-missing arguments to min; returning Inf 
4: In max(x) : no non-missing arguments to max; returning -Inf 

,如果我设置XLIM和ylim成数字我得到一个空的阴谋。

这是我的代码

> mydata = read.table("Data1.txt") 
> head(mydata,5) 
     V1  V2 V3 
1 -4.7675 -1.8947 1 
2 1.2126 -3.9255 1 
3 -1.2398 -2.9562 1 
4 -3.9951 -2.2204 1 
5 -1.1304 -3.8818 1 
> target <- mydata[,3] 
> f <- as.factor(target) 
> x = mydata[,1] 
> y = mydata[,2] 
> xtmp <- mydata[,1:2] 
> plot(xtmp, col = f) 

enter image description here

> m1 = c(mean(x)) 
> m2 = c(mean(y)) 
> m = as.matrix(m2-m1) 
> for (k in x){ 
+ sw1 = as.matrix(sum(k-m1)) 
+ t(sw1) 
+ sw1 = sum(sw1 %*% t(sw1)) 
+ sw1 
+ } 
> for (l in y){ 
+ sw2 = as.matrix(sum(l-m2)) 
+ sw2 = sum(sw2 %*% t(sw2)) 
+ } 
> sw = as.matrix(sw1) + as.matrix(sw2) 
> require(MASS) 
> A = ginv(sw) 
> A 
      [,1] 
[1,] 0.05621734 
> W = A %*% m 
> W    #where W is supposed to equal sw(inverse) * (m2-m1) 
      [,1] 
[1,] 0.006281023 
> x1 = seq(min(x), max(x), 0.5) 
> plot(x1) 
> j = length(x1) 
> x2 = seq(1,j,1) 
> for (i in 1:j) { 
+ x2[i] = (((-W[1])*x1[i])/W[2]) 
+ } 
> z = lines(x1,x2) 
> plot(z, (xlim = c(min(mydata),max(mydata)))) 
Error in xy.coords(x, y, xlabel, ylabel, log) : 
    'x' and 'y' lengths differ 
+0

请把一个[可重现的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)来分析你的代码。 –

+0

现在检查出来,并告诉我,如果我仍然做错了,请! –

回答

1

试试这个绘制decision boundaries(您的实现为Fisher's LDA有几个问题,纠正他们)

# generate some random data with class labels 1 & 2 
mydata = rbind(data.frame(V1=rnorm(100,-3,3), V2=rnorm(100,-3,1), V3=2), 
      data.frame(V1=rnorm(100,3,3), V2=rnorm(100,3,1), V3=1)) 

mydata1 = as.matrix(mydata[mydata$V3==1,1:2]) # class-specific data 
mydata2 = as.matrix(mydata[mydata$V3==2,1:2]) 

m1 = colMeans(mydata1) 
m2 = colMeans(mydata2) 

m = m2-m1 

sw1 = 0 
for (i in 1:nrow(mydata1)) { 
    sw1 = sw1 + (mydata1[i,]-m1) %*% t(mydata1[i,]-m1) 
} 

sw2 = 0 
for (i in 1:nrow(mydata2)) { 
    sw2 = sw2 + (mydata1[i,]-m2) %*% t(mydata1[i,]-m2) 
} 

sw = as.matrix(sw1) + as.matrix(sw2) # sum class-specific scatters 

require(MASS) 
A = ginv(sw) 
A 
W = A %*% m 
W #where W is supposed to equal inverse(sw) * (m2-m1), generalized eigenvalue solution 
x12 = as.matrix(expand.grid(x1=seq(-10,12,0.3),x2=seq(-7,6,0.3))) 
c = as.numeric(matrix((m1 + m2)/2, nrow=1) %*% W) 

# plot the decision boundaries 
plot(xtmp, col = f, pch=19, cex=1.2, xlim=c(-10,12), ylim=c(-7,6)) 
points(x12[,1], x12[,2], col=ifelse(x12 %*% W > c, 2, 1)) 

enter image description here