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)
> 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
请把一个[可重现的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)来分析你的代码。 –
现在检查出来,并告诉我,如果我仍然做错了,请! –