Problem with accessing data after splitapply and regress

1 visualización (últimos 30 días)
Luca
Luca el 15 de Abr. de 2021
Editada: Vatsal el 15 de Mayo de 2024
Hi,
I have two stocks with daily stock returns and the marketreturns also daily. I ran a regression seperatly for each year and each stock with the splitapply() function. My goal is to get the residuals for each day of observation and stock. I uploaded the set I'm using.
Data description:
ID = Stock id
dates= the day of observation
years = the years (for group)
RET = stock returns
MRET = market returns
The model looks like this :
RET = Beta0 + Beta1 * MRET
I tried it with fitlm.
[group6, years, ID]= findgroups(subset1.years, subset1.ID);
[group7, dates]= findgroups(subset1.years);
[lm2] = splitapply(@(x,y){fitlm(x,y)}, subset1.MRET, subset1.RET,group6)
Which gives me 59*1 cell array where all the information are stored in. Unfortunatly this was a little bit a dead end for me. I don't know how to access the residuals all at ones and assgin them to the right date and ID.
I Also tried.
A = [ones(size(subset1.MRET)), subset1.MRET];
b = subset1.RET;
[m5] = splitapply(@(x,y) {regress(y,x)},A,b,group6);
But here i'm also not able to get the residuals. The output is again cell array where each cell contains the beta0 and beta1.
My new idea is to get a table which looks like this.
ID years Beta0 Beta1
10001 1986 b0 b1
10001 1987 b0 b1
.
.
.
10002 1986 b0 b1
.
.
.
Then I would be able to calculate the residuals my self.
Does anyone know how to get the residuals directly and create a table with ID, dates and residuals.
Or how I can create the table with the betas
Thank you in advance
Luca

Respuestas (1)

Vatsal
Vatsal el 15 de Mayo de 2024
Editada: Vatsal el 15 de Mayo de 2024
Hi,
To directly obtain and correctly assign residuals by date and ID, the provided initial method can be enahnced by incorporating residuals extraction with "fitlm" as part of the "splitapply" process.
Here is how to do it:
% Assuming subset1 is your table with columns: ID, dates, years, RET, MRET
% Find unique groups
[group, ~] = findgroups(subset1.years, subset1.ID);
% Preallocate a residuals array
residuals = NaN(size(subset1.RET)); % Same size as your RET column
for i = 1:max(group)
idx = group == i; % Logical index for the current group
lm = fitlm(subset1.MRET(idx), subset1.RET(idx));
residuals(idx) = lm.Residuals.Raw; % Assign residuals
end
% Add residuals to your table
subset1.residuals = residuals;
I hope this helps!

Categorías

Más información sobre Analysis of Variance and Covariance en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by