How to fit multiple distributions of the same type to the sample data based on grouping variables? Brief the steps

9 次查看(过去 30 天)
How to fit multiple distributions of the same type to the sample data based on grouping variables? Brief the steps
  1 个评论
dpb
dpb 2023-8-16
  1. <Start here...>
  2. Figure out how to do what you want with all the data without grouping
  3. Build either an m-file or if simple enough an anonymous function to do (2)
  4. Use varfun or findgroups and splitapply with (3)
  5. Admire handiwork...

请先登录,再进行评论。

回答(1 个)

Shivansh
Shivansh 2024-1-5
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:
  1. Use the “findgroups” function to create a vector of group indices from the grouping variable.
  2. 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.
  3. Apply the function to each subset of the data corresponding to each group using “splitapply”.
  4. 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. Thesplitapply” 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:
  1. findgroups: https://in.mathworks.com/help/matlab/ref/findgroups.html.
  2. splitapply: https://in.mathworks.com/help/matlab/ref/splitapply.html.
Hope it helps!

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by