Cross-correlation and Auto-correlation for panel dataset
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
Hi all, I would like to computer the cross correlation and autocorrelation function in a panel dataset.
Say I have dataset of time=10 in which 150 persons are monitored. For each person I have two variables x and y, id (telling me the person id) and time (telling the the time shot of the observation). I would like to computer the autocorrelation of x and the cross correlation between x and y. Using the commands autocorr(x) and crosscorr(x,y) would clearly produce a wrong result because matlab does not recognize the panel nature of the dataset, and it would consider the whole vectors x and y as a time series ignoring that it is indeed a time series but for different individuals . Anyone can help me? Thank you
Simone
댓글 수: 1
답변 (1개)
  UDAYA PEDDIRAJU
      
 2025년 1월 3일
        To compute the autocorrelation and cross-correlation for a panel dataset in MATLAB, you can loop through each individual and calculate the correlations separately. Here's an example of how you can do this:
% Sample data generation
numPersons = 150;
timePoints = 10;
id = repmat((1:numPersons)', timePoints, 1);
time = repmat((1:timePoints)', numPersons, 1);
x = rand(numPersons * timePoints, 1); % Random variable x
y = rand(numPersons * timePoints, 1); % Random variable y
% Initialize arrays to store results
autoCorrResults = zeros(numPersons, 1);
crossCorrResults = zeros(numPersons, 1);
% Loop through each individual
for personId = 1:numPersons
    % Extract data for the current individual
    x_person = x(id == personId);
    y_person = y(id == personId);
    % Compute autocorrelation for x
    [autoCorr, lags] = autocorr(x_person);
    autoCorrResults(personId) = autoCorr(2); % Store lag 1 autocorrelation
    % Compute cross-correlation between x and y
    [crossCorr, lags] = crosscorr(x_person, y_person);
    crossCorrResults(personId) = crossCorr(2); % Store lag 1 cross-correlation
end
% Display results
disp('Autocorrelation results:');
disp(autoCorrResults);
disp('Cross-correlation results:');
disp(crossCorrResults);
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


