Apply formula to a set array of each group

2 ビュー (過去 30 日間)
Nabil Benhadda
Nabil Benhadda 2020 年 9 月 3 日
編集済み: Cris LaPierre 2020 年 9 月 4 日
Hello everyone,
I have a set of 6672 observations that I was able to split into groups. Now I am trying to calculate the auto-correlation of a variable present in each group by using the code as follows:
Work = readtable('ARcal.xlsx');
Work.Groups = findgroups(Work.Numero);
ACor = splitapply(@(a,b) {corrcoef(a,b)},Work.MarketReturn(1:length(Work.MarketReturn)-1),Work.MarketReturn(2:length(Work.MarketReturn)),Work.Groups);
However, I get the following error message:
Error using splitapply (line 99)
The data variables must have the same number of rows as the vector of group numbers. The group number vector has 6672 row(s), and data variable 1 has 6671 row(s).
Error in ARcal (line 56)
ACor = splitapply(@(a,b) {corrcoef(a,b)},Work.MarketReturn(1:length(Work.MarketReturn)-1),Work.MarketReturn(2:length(Work.MarketReturn)),Work.Groups);
Any help would be much appreciated.

回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 9 月 3 日
It looks like you are using a table. If so, every variable in the table has the same number of rows. It looks like your table has 6672 rows. However, you inputs a and b only have 6671. This is because you are indexing Work.MarketReturn to remove 1 row (last for a, first for b), but you pass in the grouping variable, Work.Groups, without indexing.
Try this
ACor = splitapply(@(a,b) {corrcoef(a,b)},Work.MarketReturn(1:length(Work.MarketReturn)-1),Work.MarketReturn(2:length(Work.MarketReturn)),Work.Groups(1:end-1));
  2 件のコメント
Nabil Benhadda
Nabil Benhadda 2020 年 9 月 4 日
I would like to ask you if that would still take into consideration all the data?
What I am trying to do is for each group of data, calculate the first-order autocorrelation coefficient by taking the correlation of the data presented by (1:end(group)-1) and (2:end(group))
Cris LaPierre
Cris LaPierre 2020 年 9 月 4 日
編集済み: Cris LaPierre 2020 年 9 月 4 日
I'm afraid you're in a better position to answer that question. You need to specify 6671 groups. The obvious choices are
  • Work.Groups(1:end-1)
  • Work.Groups(2:end)
Basically, do you want to group your data by the first input to corrcoef or the second input?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by