Your problem is here ninetyoneclaims$total.chg>q
That produces a logical vector of TRUE/FALSE values, but the length of the vector stays the same even though the number of TRUEs is changing. Try sum(ninetyoneclaims$total.chg>q) ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of smugg55 Sent: Wednesday, October 23, 2013 9:25 AM To: r-help@r-project.org Subject: [R] Function not working as I'd like Hey all, So I wrote/borrowed some code (no, I'm not trying to claim all of the work I've done below as my own). Ideally what I'd like the code to do is calculate ep for each iteration of q. Here's the code: > gpdriskmeasures = function(x , prob = > c(0,.1,.2,.25,.3,.4,.5,.6,.7,.75,.8,.9,.95,.99)) + { + xi = gpd.model$par.ests["xi"] + beta = gpd.model$par.ests["beta"] + u = gpd.model$threshold + q = u + (beta*((1-prob)^(-xi)-1))/xi + es = (q + (beta - xi * u))/(1 - xi) + ep = 1-length(ninetyoneclaims$total.chg[ninetyoneclaims$total.chg>q]) /length(ninetyoneclaims$total.chg) + ans = data.frame(p = prob, quantile = q, tail.expectation = es, empirical.prob = ep) + ans + } Note that there are no problems pulling xi, beta, and u, and es calculates correctly. I'll also show what the results look like: > gpdriskmeasures(ninetyoneclaims$total.chg) p quantile tail.expectation empirical.prob 1 0.00 200000.0 336856.8 0.9878478 2 0.10 210058.6 351517.2 0.9878478 3 0.20 221704.1 368490.5 0.9878478 4 0.25 228270.1 378060.5 0.9878478 As you can see the empirical.prob (which is ep) is not calculating correctly, as that number should be decreasing as the quantile (q) increases. Any help would be most appreciated! Thanks in advance for anyone that can help. -- View this message in context: http://r.789695.n4.nabble.com/Function-not-working-as-I-d-like-t p4678878.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.