apply function to each group
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2014-7-27
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?
采纳的回答
更多回答(1 个)
Image Analyst
2014-7-27
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!