- <Start here...>
- Figure out how to do what you want with all the data without grouping
- Build either an m-file or if simple enough an anonymous function to do (2)
- Use varfun or findgroups and splitapply with (3)
- Admire handiwork...
How to fit multiple distributions of the same type to the sample data based on grouping variables? Brief the steps
9 views (last 30 days)
Show older comments
How to fit multiple distributions of the same type to the sample data based on grouping variables? Brief the steps
1 Comment
dpb
on 16 Aug 2023
Answers (1)
Shivansh
on 5 Jan 2024
Hi Payel!
I understand that you are trying to fit multiple distributions of the same type to the sample data based on the grouping variables. I am assuming that the data is in a “table” format that separates the sample data from the grouping variables.
You can follow the below steps:
- Use the “findgroups” function to create a vector of group indices from the grouping variable.
- Create a function that takes a subset of the sample data and fits the desired distribution to it. The function should return the parameters or the distribution object.
- Apply the function to each subset of the data corresponding to each group using “splitapply”.
- Save the fitted parameters for and use functions like “plot” for analysis.
You can refer to the example code below to get a better idea of the above approach:
% Assuming 'dataTable' is a table with 'Samples' and 'Group' columns
groups = findgroups(dataTable.Group);
% Define a function or use an existing function that fits a distribution to the data
fitDistFunc = @(data) fitdist(data, 'Normal');
% Use splitapply to fit the distribution to each subset of data
fittedDistributions = splitapply(fitDistFunc, dataTable.Samples, groups);
In this code, store the data in the “dataTable” variable. The “splitapply” automatically splits the “Samples” column of “dataTable” into subsets based on the unique values in groups created by “findgroups”. The function “fitDistFunc” is then applied to each subset, and the results are aggregated into the “fittedDistributions” cell array.
You can refer to the below links for more information:
- findgroups: https://in.mathworks.com/help/matlab/ref/findgroups.html.
- splitapply: https://in.mathworks.com/help/matlab/ref/splitapply.html.
Hope it helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!