Note that there are some explanatory texts on larger screens.

plurals
  1. POMost robust way to measure the alignment between points in R
    text
    copied!<p>Until now I used to use <code>cor</code> to measure the alignment between points. I am quite happy with the results, values between <code>-1</code> and <code>0</code> always spot the lines I want. However, thanks to the answers and comments in <a href="https://stackoverflow.com/questions/17319910/na-from-correlation-function/17320976#17320976">here</a>, I realised it's not the most robust approach because of the standard deviation being zero for flat lines, such as:</p> <pre><code>&gt; cor(1:10, rep(10,10)) [1] NA Warning message: In cor(1:10, rep(10, 10)) : the standard deviation is zero </code></pre> <p>My aim is to define a function which gives <code>1</code> for points perfectly aligned (regardless the slope) and values closer to <code>0</code> for points not in line. Would you suggest a more robust approach than mine?</p> <p>EDIT:</p> <p>following the suggestion of @Hong Ooi I got</p> <pre><code>data1 &lt;- data.frame(date = c(13636, 13636, 14403, 14761, 15201, 15741), value = c(865310, 999989, 999989, 2, 999989, 26)) data2 &lt;- data.frame(date = c(12667, 12745, 13106, 13276, 13461, 13626), value = c(1904, 2055, 2740, 3376, 3567, 4099)) m &lt;- cbind(data1$date, data1$value) sdev &lt;- prcomp(m)$sdev sdev[1]/sum(sdev) # 0.9986399 m &lt;- cbind(data2$date, data2$value) sdev &lt;- prcomp(m)$sdev sdev[1]/sum(sdev) # 0.961 </code></pre> <p>However, I was expecting the a very low value for <code>data1</code> </p> <p><img src="https://i.stack.imgur.com/ErWZC.jpg" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload