On Oct 23, 2013, at 21:06 , David Carlson wrote: > Actually I wrote too quickly. Since the logical vector is used > to subset a vector, the value of > length(ninetyoneclaims$total.chg[ninetyoneclaims$total.chg>q]) > should be changing. >
It would if q did, but q is a single vector which gets recycled to the length of total.chg. R's vectorization capabilities do not extend into "do what I mean".... Presumably the intention was something like sapply(q, function(qi) mean(ninetyoneclaims$total.chg <= qi) ) (untested) -pd > David > > -----Original Message----- > From: David Carlson [mailto:dcarl...@tamu.edu] > Sent: Wednesday, October 23, 2013 1:58 PM > To: 'smugg55'; 'r-help@r-project.org' > Subject: RE: [R] Function not working as I'd like > > 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd....@cbs.dk Priv: pda...@gmail.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.