0
votes

I have simulated slope and intercept values for 100 linear regression lines using r. Now I want to plot those regression lines in the same plot.

Basically, the output should be like this,

enter image description here

I have simulated the data as follows :

N=100
intercept=rnorm(N,178,20)
slope=rnorm(N,0,10)

weight=seq(30,60,100)
height=seq(-100,400,100)

Now I am having a difficulty of plotting those lines in the same plot. Can anyone help me to figure that out ?

Thank you.

1

1 Answers

3
votes

This will plot the multiple lines. I'm basically using sapply() as a for loop. You first have to initialize the plot. You can do that with the weight and height variables, but you'd have to specify them differently than above. In fact, all you really need are the minima and maxima.

weight=c(30,60)
height=c(-100,400)
plot(weight, height, type="n")

Then, you can simulate the intercepts and and slopes and put the lines in.

N=100
intercept=rnorm(N,178,20)
slope=rnorm(N,0,10)

sapply(1:N, function(i)abline(a=intercept[i], b=slope[i]))

That gives me something like the following:

enter image description here

It doesn't look exactly like the one you in your question, but my guess is that this independent random drawing of intercepts and slopes wasn't used to produce the one in your figure. The mechanics should work, though.