如何在 R 中的两个连续变量之间找到有意义的边界

How to find meaningful boundaries between two continuous variables in R

为了找到 iris 数据集两列之间的关系,我正在执行 kruskal.test 和 p.value 显示这两列之间有意义的关系。

data(iris)
kruskal.test(iris$Petal.Length, iris$Sepal.Width)

结果如下:

    Kruskal-Wallis rank sum test

data:  iris$Petal.Length and iris$Sepal.Width
Kruskal-Wallis chi-squared = 41.827, df = 22, p-value = 0.00656

散点图也显示了某种关系。 plot(iris$Petal.Length, iris$Petal.Width)

为了找到这两个变量的有意义的边界,我进行了 运行 pairwise.wilcox.test 测试,但要使该测试起作用,其中一个变量需要是分类变量。如果我将两个连续变量都传递给它,那么结果并不像预期的那样。

pairwise.wilcox.test(x = iris$Petal.Length, g = iris$Petal.Width, p.adjust.method = "BH")

作为输出,我需要一个明确的切点,这两个变量之间存在某种关系,这种关系在此处结束(如上图中的红线所示)

我不确定是否有任何统计测试或其他编程技术可以找到这些边界。

例如手动我可以做这样的事情来标记边界 -

setDT(iris)[, relationship := ifelse(Petal.Length > 3 & Sepal.Width < 3.5, 1, 0)]

但是,R 中是否有编程技术或库来找到这样的边界?

重要的是要注意我的实际数据是有偏差的。

谢谢, 索拉布

没有最好的分裂。在您指定的某些 conditions/criteria 下它可能是最好的。

我想你期待第二个情节,虽然我也添加了第一个情节,你有一条线。使用了线性判别分析。 然而,这是有监督的学习,因为我们有 Species 列。因此,您可能对 K-Nearest 邻域和边界等无监督方法感兴趣 - 然后检查这个 https://stats.stackexchange.com/questions/21572/how-to-plot-decision-boundary-of-a-k-nearest-neighbor-classifier-from-elements-o.

data(iris)
library(MASS)

plot(iris$Petal.Length, iris$Petal.Width, col = iris$Species)

# construct the model
mdl <- lda(Species ~ Petal.Length + Petal.Width, data = iris)

# draw discrimination line
np <- 300
nd.x <- seq(from = min(iris$Petal.Length), to = max( iris$Petal.Length), length.out = np)
nd.y <- seq(from = min(iris$Petal.Width), to = max( iris$Petal.Width), length.out = np)
nd <- expand.grid(Petal.Length = nd.x, Petal.Width = nd.y)

prd <- as.numeric(predict(mdl, newdata = nd)$class)

plot(iris[, c("Petal.Length", "Petal.Width")], col = iris$Species)
points(mdl$means, pch = "+", cex = 3, col = c("black", "red"))
contour(x = nd.x, y = nd.y, z = matrix(prd, nrow = np, ncol = np), 
        levels = c(1, 2), add = TRUE, drawlabels = FALSE)

#create LD sequences from min - max values 
p = predict(mdl, newdata= nd)
p.x = seq(from = min(p$x[,1]), to = max(p$x[,1]), length.out = np) #LD1 scores
p.y = seq(from = min(p$x[,2]), to = max(p$x[,2]), length.out = np) #LD2 scores


contour(x = p.x, y = p.y, z = matrix(prd, nrow = np, ncol = np), 
        levels = c(1, 2, 3), add = TRUE, drawlabels = FALSE)

链接到: