Grpstats function handles with multiple columns

6 views (last 30 days)
I want to use a function handle in grpstats which takes multiple columns of a dataset as inputs. The two columns are the lower and higher limits of a confidence interval. I wrote a function for a rough estimate of accumulated confidence intervals.
function [ci_new] = ci_estimate(ci_low_array, ci_high_array)
ci_int = sqrt(sum((ci_low_array - ci_high_array).^2));
ci_mean = mean(mean([ci_low_array, ci_high_array], 2));
ci_new_low = ci_mean - ci_int;
ci_new_high = ci_mean + ci_int;
ci_new = [ci_new_low, ci_new_high];
end
I'm calling the function within grpstats as follows:
ds_o = grpstats( ds_o, {'observer' }, @ci_estimate, 'DataVar', { 'ci_low', 'ci_high' } );
Have tried multiple variations of the above code, including creating a new variable 'ci' which is essentially a matrix containing both low and high values, none of which are working. In the documentation of grpstats, it says that it's possible to write a function that takes a matrix as input instead of a column of dataset, but there's no example, and I can't seem to figure out the current syntax..

Accepted Answer

Cris LaPierre
Cris LaPierre on 4 Nov 2019
From what I see, the issue is that you are using a table despite wanting to use the matrix syntax. Grpstats will compute the indicated stats for a single variable at a time, even if you specify multiple variables. You don't have access to multiple variables at the same time. To do that, the values must be part of the same table variable.
Since you didn't share your data, the best I can do is show you how you could do this using some made-up data. Basically, you need to combine your low and high CI values into an array and add that as a single variable to your table. This way, the matrix is passed to your input function, and you can extract the low and high values inside the function.
% dummy data
observer = [1 2 3 4 5]';
ci_low = [0 1 2 3 4]';
ci_high = [10 11 12 13 14]';
ds_o = table(observer,ci_low,ci_high);
% Add a new variable to the table that is a matrix of ci values
ds_o.ci = [ci_low ci_high];
% Set DataVars as the array variable. Update the function accordingly.
ds_o = grpstats( ds_o,'observer',@ci_estimate,'DataVars','ci')
function [ci_new] = ci_estimate(ci_array)
ci_int = sqrt(sum(diff(ci_array,[],2)).^2);
ci_mean = mean(mean(ci_array, 2));
ci_new_low = ci_mean - ci_int;
ci_new_high = ci_mean + ci_int;
ci_new = [ci_new_low, ci_new_high];
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!