How to use prctile within grpstats?

12 views (last 30 days)
NT on 29 Sep 2014
Edited: Nalini Vishnoi on 1 Oct 2014
Hi, I am trying to find the 5th, 25th, 50th (median), 75th and 95th percentiles for a few different groups in a dataset but unable to do so using grpstats.
fundpercentiles = grpstats(DDD, 'fund', prctile(DDD.pl_vwap_bps, [5 25 50 75 95]), 'DataVars', 'pl_vwap_bps');
Warning: Out of range or non-integer values truncated during conversion to character. > In stats\private\dsgrpstats at 116 In grpstats at 135 Error using dsgrpstats (line 142) WHICHSTATS must be a function handle or name, or a cell array of function handles or names.
Error in grpstats (line 135) [varargout{1:nargout}] = dsgrpstats(x,group,whichstats,varargin{:});

Answers (1)

Nalini Vishnoi
Nalini Vishnoi on 1 Oct 2014
Edited: Nalini Vishnoi on 1 Oct 2014
I understand that you are trying to compute the percentiles on the values grouped using a column. The general syntax of 'grpstats' looks like the following:
statarray = grpstats(tbl,groupvar,whichstats,Name,Value)
where whichstats is the summary statistics to compute, specified as a string or function handle, or a cell array of strings and function handles.
To compute the percentile using grpstats, you need to provide a handle to the 'prctile' function. For example:
x = rand(10,1);
l = logical([zeros(5,1); ones(5,1)]);
stats = grpstats(x, l, @(y) prctile(y, [5 25 50 75 95]'));
This seems to work for me. I hope this solves your issue as well.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!