我是 R 的新手,我一直在尝试创建 Fisher LDA,但我很难在 R 中解决向量和指标。如果有人能告诉我我做得对吗,因为当我遇到这个错误时尝试绘制决策边界
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