R语言 用LDA边界制作数据的3D图

erhoui1w  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在玩线性判别分析,并试图在R中绘制数据,然后绘制判别平面。当我尝试这样做时,我可以得到数据和平面,但平面与3D绘制的数据不匹配。我得到的数据“浮动”在平面上方,而不是平面切割数据。使用lda函数给我们的系数,参数SB.lda,我试图在假设方程为SBx + OPSy + tShoz = 0的情况下求解z,因为lda函数没有给予常数。
当我引用这篇文章(https://stats.stackexchange.com/questions/166942/why-are-discriminant-analysis-results-in-r-lda-and-spss-different-constant-t)并使用该方法生成常数时,我得到了一个常数-13.82201,当绘制时与数据不匹配。
我错过了什么?

library(plotly)
library(MASS)
train <- data.frame(read.table("https://raw.githubusercontent.com/gunsnfloyd/algorithmdata/main/train.tsv",header=TRUE))
head(train)
model <- lda(WS~ SB + OPS  + tSho ,data = train)

SB.lda <- 0.008056699
OPS.lda <- 15.174308879
tSho.lda <- 0.205465030

groupmean<-(model$prior%*%model$means)
constant<-(groupmean%*%model$scaling)
-constant

fig <- plot_ly(train, x = ~SB, y = ~OPS, z = ~tSho, color = ~WS, colors = c('#BF382A', '#0C4B8E'))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'SB'),
                                   yaxis = list(title = 'OPS'),
                                   zaxis = list(title = 'tSho')))

x.lda <- seq(0, 400, length.out = 100) #Range based on SB values
y.lda <- seq(0.5, 0.9, length.out = 100) #Range based on OPS values
z.lda <- outer(x.lda, y.lda, function(x, y) ( (-SB.lda * x - OPS.lda * y) / tSho.lda + 13.82201))

fig <- fig %>% add_surface(x = x.lda, y = y.lda, z = z.lda,colors = "#0C4B8E")      
fig

字符串

noj0wjuj

noj0wjuj1#

我解方程得到了一个不同的结果:

# general equation: A * x + B * y + C * z + c = 0

# solve for z:
  SB * x + OPS * y + tSho * z - 13.82201 = 0
  SB * x + OPS * y - 13.82201            = - tSho * z
 -SB * x - OPS * y + 13.82201            = tSho * z
(-SB * x - OPS * y + 13.82201) / tSho    = z

# calculate values of plane:
x.lda <- seq(0, 400, length.out = 100) #Range based on SB values
y.lda <- seq(0.5, 0.9, length.out = 100) #Range based on OPS values
z.lda <- outer(x.lda, y.lda, function(x, y) (-SB.lda * x - OPS.lda * y + 13.82201) / tSho.lda )

字符串
结果图:

相关问题