title: Finding Leverage/Hatvalue of Influential Observations
body: <p>Two datasets: one clearly has an influential observation and the other does not. However, when I find the hatvalues and use rule of thumb tests, the first does not indicate undo leverage and the second does:</p>
  
  <pre><code>df1 &lt;- dplyr::tribble(
    ~input_date,          ~input_reading,
    as.Date('2006-02-01'),         12428,
    as.Date('2006-02-17'),         12543,
    as.Date('2006-02-23'),         12555,
    as.Date('2006-03-14'),         12716,
    as.Date('2006-06-16'),         13275
  )
  fit1 &lt;- lm(input_reading ~ input_date, data=df1)
  hatvalues(fit1)
  
          1         2         3         4         5 
  0.3594735 0.2625274 0.2376641 0.2002821 0.9400529
  </code></pre>
  
  <p>Notice the last observation (5). It has a hatvalue of 0.94 which is more than 2x or 3x the mean of hatvalues so it is flagged as influential.</p>
  
  <pre><code>df2 &lt;- dplyr::tribble(
    ~input_date,          ~input_reading,
    as.Date('2006-02-17'),         12543,
    as.Date('2006-02-23'),         12555,
    as.Date('2006-03-14'),         12716,
    as.Date('2006-06-16'),         13275,
    as.Date('2006-07-23'),           247 # Obseravation is influential
  )
  fit2 &lt;- lm(input_reading ~ input_date, data=df2)
  hatvalues(fit2)
  
          1         2         3         4         5 
  0.3833232 0.3491395 0.2641404 0.3635198 0.6398770
  </code></pre>
  
  <p>Now see the last observation, it is clearly influential but it's hatvalue is not more than 2x the average.</p>
  
  <p><em>Background:</em> The dataset is values over time. Every so often the values go wonky (anomalies, spikes, resets, go to zero). My thought is to use a for loop to calculate the regressions for every 5 data points. When I hit an anomaly, then I can write some logic to fix it.</p>