从模拟数据中分离出两类

问题描述

想知道是否有人可以帮助我理解abline()中的r。我的代码如下:

N = 500
fx1 <- runif(N,-2,2)
fx2 <- runif(N,2)
X <- cbind(fx1,fx2)

a <- runif(1,-1,1) 
b <- runif(1,1)
  
plot(X,xlim=c(-2,2),ylim=c(-2,xlab = "X",ylab = "Y")
abline(a,b)

这是生成的数据的图像:

enter image description here

我想做的是在计算abline行之后使用此数据创建两个类,尽管我不确定该如何去做。想知道是否有人可以提供帮助。

解决方法

有这两个说明

a <- runif(1,-1,1) 
b <- runif(1,1)

您正在生成一行的参数。具体来说,拦截和倾斜

abline(a,b)根据截距a和斜率b构造一条线

详细信息here

,

我明白了,我已经阅读了文档并且对我有了更好的理解。

基本上,我需要修改现有代码,以使变量命名更符合以下要求:

N = 1000
x <- runif(N,-2,2)
y <- runif(N,2)
a <- runif(1,1)

使用ggplot时,以下图显示

ggplot(X,aes(x= x,y = y)) +
  geom_point() + 
  geom_abline(slope = b,intercept = a)

enter image description here

然后,我只创建一个ifelse并插入数字以查看其是否大于y

class <- as.matrix(ifelse(
 y > a + x * b,'0','1' 
))

组合成一个数据框并重新绘制:

X <- cbind.data.frame(x,y,class)
ggplot(X,y = y,color = class)) +
  geom_point() +
  geom_abline(slope = b,intercept = a)

enter image description here