title: Function for estimating Generalized Pareto Distribution parameters in R not working
body: <p>In this paper <a href="https://www.jstor.org/stable/27867255?seq=1#metadata_info_tab_contents" rel="nofollow noreferrer">https://www.jstor.org/stable/27867255?seq=1#metadata_info_tab_contents</a> they provide a method of estimation for the GPD, based on the profile likelihood function and the empirical Bayesian method. The code they provide is the following:</p>
  <pre><code># x is the sample data from the GPD
  f &lt;- function(x) {
    n &lt;- length(x)
    x &lt;- sort(x)
    lx &lt;- function(b,x) {
      k &lt;- -mean(log(1-b*x))
      if (b==0) {
        k-1-log(mean(x))
      } else {
        k-1+log(b/k)
      }
      }
    p &lt;- (3:9)/10
    xp &lt;- x[round(n*(1-p)+.5)]
    m &lt;- 20+round(n^.5)
    xq &lt;- x[round(n*(1-p*p)+.5)]
    k &lt;-  log(xq/xp-1,p)
    a &lt;- k*xp/(1-p^k)
    a[k==0] &lt;- (-xp/log(p))[k==0]
    k &lt;- -1
    b &lt;- w &lt;- L &lt;- (n-1)/(n+1)/n[n]-(1-((1:m-.5)/m)^k)/k/median(a)/2
    for (i in 1:m) L[i] &lt;- n*lx(b[i],x)
    for (i in 1:m) w[i] &lt;- 1/sum(exp(L-L[i]))
    b &lt;- sum(b*w)
    k &lt;- -mean(log(1-b*x))
    list(sigma=k/b,k=k)
  }
  </code></pre>
  <p>I simulated a vector of x values with size n = 100 from a GPD with shape parameter equals to 1 and scale parameter equals to -1. Then I get the error:</p>
  <pre><code>Error in if (condition) { : missing value where TRUE/FALSE needed
  </code></pre>
  <p>According to this question <a href="https://stackoverflow.com/questions/7355187/error-in-if-while-condition-missing-value-where-true-false-needed">Error in if/while (condition) {: missing Value where TRUE/FALSE needed</a> the error can be attributed to the NA values in the b vector, but I don't understand why this happens.</p>
  <p>My x vector:</p>
  <pre><code>x
    [1]   0.24264350   0.71418670   0.90929131   1.31011008   1.26467953   0.66491141  40.16132902
    [8]   0.24815930   0.83783287   1.18879459   1.87167421   0.00396890   1.58125086   0.32042532
   [15]   0.01330135   0.80800732   0.67832942   0.32623323  10.87447448   0.64779973   0.16550198
   [22]   1.03595024   0.08463867   6.17820208  15.07862358   0.19188437   0.14925543   2.20031552
   [29]   1.14093185   2.92184174   0.79279984   0.14736789   0.32954571  24.41728358   5.64925376
   [36]   1.31141081   1.62897827   0.01491289   0.86641233   0.82289179   0.12105522   1.66701079
   [43]   4.65817711  53.08764134   8.87696704   0.61560327   0.77817268   0.65148331   2.33976096
   [50]   2.96629395   0.43783850   3.11005777   0.44923740   0.27073261   2.98854135   1.85962571
   [57]   0.67065561   1.09661475   1.55934896   1.65683579   0.62373160  27.04125805   0.16137916
   [64]   0.32925148   0.40487288   1.41714764   0.48798225   0.07624247   2.32993211   2.13228723
   [71]   6.74938943   2.10121108   0.56772145   2.84504482   1.67119601   1.68640938  23.65633183
   [78]   7.78195561   0.21317910   0.33639542   1.14508402   6.61466064   2.03818446   2.28166528
   [85]   0.28095778  30.93008603   0.16512528   1.21974281   0.32121843 103.90277963   0.09672460
   [92]   1.04882224   1.68444513  29.93956683   0.43961534   2.60623811   0.25039076   0.76208631
   [99] 568.46662349   1.66133511
  </code></pre>