4

I have timeserie:

x

4557  9940  9855  9894 10142  9501  9532  9229  9169  9214  9347  9176  8951  9344  9873  9970
9139  9420  9476  9205  9271  8632  8730  9336  9150  9601 10012  9841  9951  9222  8799  9316
10087  9677  9154  9019 10549  9629  9131  9560 10246 10982 11748  9054  8690  9923

tt<-1:length(x)

plot(x, xaxt = "n", type = "l", xlab = NULL, ylab = NULL, col = "royalblue2", lwd = 2.3)

I want to add linear trend:

fit <- lm(x ~ tt)
co <- coef(fit)
co
    (Intercept)             tt 

    8940.23478           21.27031 

And then I need to find angle between two red lines:

abline(8940.23478, 21.27031, col = "red", lwd = 2)
abline(8940.23478, 0, col = "red", lwd = 2)

How can I manage that?

Here is my graph: Plot with two redlines

llrs
  • 3,308
  • 35
  • 68
Marta
  • 3,493
  • 6
  • 28
  • 43

1 Answers1

3

Since your second slope is 0, the calculation of the angle between this line and the one with a slope of 21.27031 is very easy:

atan(21.27031) * 180 / pi

# [1] 87.30828

The angle between the line and the x axis is about 87°.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thank you for the answer. I have already compute that. But look at the image: http://gyazo.com/85aa9963769fb9f60a4545a328162ed2 There cant' be angle 87 degrees – Marta Feb 18 '14 at 07:52
  • 3
    @user3227114 Without x-axis labels you can't see the angle from the graphic. Note that you can change height and width of the plot, and the angle might appear different though it's always the same. – Sven Hohenstein Feb 18 '14 at 08:08
  • Thanks a lot! But I still have a problem. Maybe you can help me to solve it? I have another time serie x 1.133421 1.309256 1.322476 1.351425 1.319168 1.334070 1.323227 1.328096 1.321736 1.340243 1.348989 1.325523 1.321640 1.360659 1.366150 1.344534 1.327169 1.326433 1.372520 1.395546 1.365872 1.335496 1.380183 1.425236 1.330601 1.334444 1.314822 1.395691 1.393629 1.356322 1.285601 1.333727 1.358382 1.356309 1.339961 1.307129 1.331690 1.301070 1.290220 1.302197 1.497267 1.531415 1.549200 1.393307 1.324051 1.323692 – Marta Feb 18 '14 at 08:20
  • @SvenHohenstein , Graph looks same, but slope is 0.001841031. http://gyazo.com/ae2de73dcec3d95941b7c63746b2c68d Why is that? – Marta Feb 18 '14 at 08:29
  • 1
    @user3227114 Use these commands to draw a plot with identical x axis and y axis limits: `fit <- lm(x ~ seq_along(x)); co <- coef(fit); plot(x, type = "l", xlim = c(0, length(x)), ylim = c(0, length(x))); abline(co); abline(c(co[1], 0))`. Now, you can see that the slope is extremely small. – Sven Hohenstein Feb 18 '14 at 08:40