How to detect to groups which shows significant slope differences from a single scatter plot

22 Views Asked by At

Is it possible to create a dummy or continuous variable which separates the points into two separate groups which show significant slope differences? In effect, I would like to know whether a "moderator" variable exists or not.

x = rnorm(400)
y = 0.6 * x + rnorm(400, 0, 1)
rn = 1:length(x)
df = data.frame(x, y, rn)

enter image description here

1

There are 1 best solutions below

0
On

You can try something like this

set.seed(123)
n = 400
x = rnorm(n)
rn = 1:length(x)
y[1:(n/2)] = 0.3 * x[1:(n/2)] + rnorm(n/2, 0, 1)
y[(n/2 + 1):n] =1.5 + 1.2 * x[(n/2 + 1):n] + rnorm(n/2, 0, 1)
plot(x, y, 
 bty = "n")
grid()
points(x[1:(n/2)], y[1:(n/2)], col = "blue")
points(x[(n/2+1):n], y[(n/2+1):n], col = "red")
df = data.frame(x, y, rn)

enter image description here