rotate a time series with constraints for start and end

577 Views Asked by At

I have a time ordered series of data that I can represent in R like:

library(ggplot)

x<-c(0,1,2,3,4,5,6,7)
y<-c(0,4,5,5,3,4,5,8)
df <- data.frame(x,y)

> df
  x y
1 0 0
2 1 4
3 2 5
4 3 5
5 4 3
6 5 4
7 6 5
8 7 8

enter image description here

I want to rotate the series (preserving the original shape), and obtain a new series (new_x, new_y) in such a way that the starting point is preserved (0,0), but the end point is now placed at (new_x, 0). Think of it as if you had the original series cutout from a piece of cardboard, placed one end on (0,0), put one finger on (0,0) and rotated the cutout until the end of the series lies on y=0. How do I go about finding that transformation?

1

There are 1 best solutions below

0
On BEST ANSWER

In matrix notation, the transformation you want is $U$, so that each $(x,y)$ transforms to $(x_{new}y_{new})$ according to $$ \left( \begin{array}{} x_{new} \\ y_{new} \end{array} \right)= U \left( \begin{array}{} x \\ y \end{array} \right)= \left[ \begin{array}{} cos\theta & sin\theta \\ -sin\theta & cos\theta \end{array} \right] \left( \begin{array}{} x \\ y \end{array} \right) = \left( \begin{array}{} x cos\theta+y sin\theta \\ -x sin\theta+y cos\theta \end{array} \right) $$ The value of $\theta$ to use is $tan^{-1}(y/x)$, using the $(x,y)$ coordinates of the point you want to land on the $x$-axis.

For example, your last data point is (7,8), so $\theta=tan^{-1}(8/7)=0.852$, $cos\theta = 0.659$ and $sin\theta=0.753$. For this one point,$$ x_{new}=7(.659)+8(.753)=10.6$$ and$$ y_{new}=-7(.753)+8(.659)=0.0$$

In R, calculate $\theta$ as $atan2(y,x)$. Use the same $\theta$ for all the points. You will not get any distortion.