2
votes

Due to the nature of my specification, the results of my regression coefficients provide the slope (change in yield) between two points; therefore, I would like to plot these coefficients using the slope of a line between these two points with the first point (0, -0.7620) as the intercept. Please note this is a programming question; not a statistics question.

I'm not entirely sure how to implement this in base graphics or ggplot and would appreciate any help. Here is some sample data.

Sample Data:

df <- data.frame(x = c(0, 5, 8, 10, 12, 15, 20, 25, 29), y = c(-0.762,-0.000434, 0.00158, 0.0000822, -0.00294, 0.00246, -0.000521, -0.00009287, -0.01035) )

Output:

   x          y
1  0 -7.620e-01
2  5 -4.340e-04
3  8  1.580e-03
4 10  8.220e-05
5 12 -2.940e-03
6 15  2.460e-03
7 20 -5.210e-04
8 25 -9.287e-05
9 29 -1.035e-02

Example:

enter image description here

1
does it mean you want nine lines in your graph? - MLavoie
@MLavoie Yes, there should be 9 lines - Vedda
I am not sure if this is right the answer so let try it as a comment first, run ggplot() + geom_abline(data=df, aes(intercept = x, slope = y)) + ylim(0, 100) + xlim(0, 100) - MLavoie
@MLavoie That is not correct. Please see updated question with example picture - Vedda
how do you want the y value calculated. Gor example, using your figure, what is the y-coord for when x=5. Is it -7.620e-01 + 5* 4.340e-04 or ... - user20650

1 Answers

2
votes

You can use cumsum, the cumulative sum, to calculate intermediate values

  df <- data.frame(x=c(0, 5, 8, 10, 12, 15, 20, 25, 29),y=cumsum(c(-0.762,-0.000434, 0.00158, 0.0000822, -0.00294, 0.00246, -0.000521, -0.00009287, -0.0103)))
  plot(df$x,df$y)

enter image description here