Splitapply array to fit distributions
1 次查看(过去 30 天)
显示 更早的评论
I have an Mx1 vector and an Mx1 column of categorical variables. Is there a way to splitapply so that I can get a distribution for each data in a column according to their group? I want to do this as compactly as possible because in reality I have an MxN array and I will iterate column-wise such that I get a probability distribution for each cateogries values at each column. Like the code below:
%%Generate some data
X1 = 10 + 5 * randn(200, 1);
X2 = 20 + 8 * randn(250 ,1);
cat1=repmat("a",200,1);
cat2=repmat("b",250,1);
X = [X1; X2];
cats=[cat1:cat2]
%%Fit a distribution using a kernel smoother
myFit1 = fitdist(X1, 'kernel')
myFit2 = fitdist(X2, 'kernel')
I would like to make sure each of the fits are the same as when I do something like:
newfit=splitapply(@fitdist,X,G)
but I get the error that fitdist doesn't have enough input arguments. I'm new to anonymous functions, but I suspect I need to somehow pass 'kernel' to fitdist in splitapply. Can anyone help?
0 个评论
采纳的回答
Cris LaPierre
2020-12-9
编辑:Cris LaPierre
2020-12-9
You are close. You need to use findgroups to create your grouping variable G. Then it's just a matter of setting up your function handle correctly. Here's your code with a slight modification from me. If you compare the results, you'll see they are the same.
%%Generate some data
X1 = 10 + 5 * randn(200, 1);
X2 = 20 + 8 * randn(250 ,1);
cat1=repmat("a",200,1);
cat2=repmat("b",250,1);
X = [X1; X2];
cats=[cat1;cat2];
%%Fit a distribution using a kernel smoother
myFit1 = fitdist(X1, 'kernel')
myFit2 = fitdist(X2, 'kernel')
% Now use findgroups/splitapply
G=findgroups(cats);
newfit=splitapply(@(X)fitdist(X,'kernel'),X,G);
newfit(1)
newfit(2)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Probability Distributions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!