I cannot calculate the autocorrelation coefficient
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello everyone,
I am trying to calculate the first order autocorrelation coefficient of a variable as follows:
function f = foauto(x)
x = x(1:end-1);
y = x(2:end);
f = corrcoef(y,x);
end
Work = readtable('ARcal.xlsx');
Work.Groups = findgroups(Work.Numero);
ACOR = splitapply(@foauto,Work.MarketReturn,Work.Groups);
However, I get the following error message when doing so:
Error using splitapply (line 132)
Applying the function 'foauto' to the 1st group of data generated the following error:
X and Y must have the same number of elements.
Error in ARcal (line 64)
ACOR = splitapply(@foauto,Work.MarketReturn,Work.Groups);
I would like to know if anyone could help me with that as normally x and y should have the same number of elements as they treat the same variable Work.MarketReturn for each group.
댓글 수: 0
답변 (1개)
Omega
2024년 7월 12일
Hi Nabil,
I understand that you are facing issues while calculating the autocorrelation cofficient.
The issue arises because your function 'foauto' modifies the length of 'x' and 'y' by removing the first and last elements, respectively. This results in 'x' and 'y' having different lengths, which causes the 'corrcoef' function to throw an error.
To fix this, you need to adjust your function to ensure 'x' and 'y' have the same length. Here is a revised version of your function:
function f = foauto(x)
y = x(2:end);
x = x(1:end-1);
f = corrcoef(x, y);
end
In this version, 'x' and 'y' are guaranteed to have the same length because they are both derived from the same input vector x but shifted by one element and 'y' is updated first then 'x'.
This should resolve the error and correctly calculate the first-order autocorrelation coefficient for each group.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!