How to use prctile within grpstats?

5 次查看(过去 30 天)
NT
NT 2014-9-29
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{:});

回答(1 个)

Nalini Vishnoi
Nalini Vishnoi 2014-10-1
编辑:Nalini Vishnoi 2014-10-1
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.

类别

Help CenterFile Exchange 中查找有关 Repeated Measures and MANOVA 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by