apply function to each group
2 views (last 30 days)
Show older comments
Can anyone tell me how to apply a function to each group data? For example, there are 100 firms, and each firm has 10 years total assets observations. So in total there are 1000 firm-year observations, with 10 observations each group. I need to apply a movingstd function to each group observations, simple grpstats function can not solve, because moving standard deviation need other inputs. Thanks for having a look.
3 Comments
Image Analyst
on 27 Jul 2014
What is the definition of a "group"? Is that 10 observations/measurements of one firm over 10 years? Is it multiple firms? Is your data a double array of 100 rows by 10 columns? If you want "moving" stats, that implies that there is some window that moves along, so you're not just taking the mean of all 10 observations, but of some subset of it in a window as the window moves along. What is the window width. How do you want to handle the edge effects when the center of the window is such that some of the window would be outside your array?
Accepted Answer
More Answers (1)
Image Analyst
on 27 Jul 2014
Extract out one column of 1000, then reshape into 100 rows by 10 columns
newMatrix = reshape(oneColumn, [100, 10]);
Each column will now be just one firm, not multiple firms. Then use a for loop to go down each column.
I still don't know the window width of the function that takes the standard deviation. We can "roll" that down a row at a time to get a moving/rolling average but how do you want to handle the case where the window is outside the column? Like if the window is 5 wide but the center of the window is at row #1. Or do you want to just start and end inside so that the window does not leave your data?
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!