필터 지우기
필터 지우기

apply function to each group

조회 수: 2 (최근 30일)
Qifan
Qifan 2014년 7월 27일
댓글: Qifan 2014년 7월 27일
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
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?
Qifan
Qifan 2014년 7월 27일
My data is in dataset. For example, the total asset is one variable (one column). Suppose it has 100 different firms with 10 year obs for each. So total asset variable, the column, will have 1000 rows. Now I have the rolling standard deviation function already, the problem is I need to apply this function to each firms data (each firm can be viewed as a group since it contains 10 obs). Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 27일
If your data are cell array, you can use cellfun
  댓글 수: 1
Qifan
Qifan 2014년 7월 27일
My data is a dataset, and can be easily tranformed into a matrix. I'll have a look of this function.Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
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?
  댓글 수: 1
Qifan
Qifan 2014년 7월 27일
Thanks, let me try reshape. The window width is 5.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mortgage-Backed Securities에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by