How to use splitapply with fitlm

Hi,
I want to run a regression for 2 diffrent stocks for each year of observation separately. So i get a new Betas for each stock and each year. The goal is to assign the residuals to ID and dates. I upload the set I'm using. The regression looks like this:
RET = Beta0 + Beta1 * MRET
RET is the returns of the stocks and MRET the return of the Market.
My idea was to you use the splitapply function. But my code isn't working. I guess the problem is in line 3.
[group6, years, ID]= findgroups(subset1.years, subset1.ID);
[group7, dates]= findgroups(subset1.years, subset1.ID);
regression = @(y) fitlm(subset1.MRET,subset1.RET);
lm2 = splitapply(regression, subset1.MRET,group6);
Resitbl = table (ID,dates, lm2.Residuals.Raw);
regress function would also work for me. But it also didn't work for me.
Thanks in advance

 Respuesta aceptada

Matt J
Matt J el 14 de Abr. de 2021

0 votos

Your regression(y) function takes an argument y that is never used.

5 comentarios

Luca
Luca el 14 de Abr. de 2021
Hi Matt,
Thanks for your answer.
regression = @(subset1.RET) fitlm(subset1.MRET,subset1.RET);
Is this the right way to correct it ?
Matt J
Matt J el 14 de Abr. de 2021
This might be what you want,
lm2 = splitapply(@(x,y){fitlm(x,y)}, subset1.MRET, subset1.RET,group6);
Luca
Luca el 14 de Abr. de 2021
Editada: Luca el 14 de Abr. de 2021
Thank you very much the code is working now.
But there is a new problem.
I get a tabe for lm 2 where each regression is stored in one row. I attached a foto.
And I'm not able to access the residuals all at ones. Do you know a way?
Regards
Matt J
Matt J el 14 de Abr. de 2021
Editada: Matt J el 14 de Abr. de 2021
In the photo you've shown, lm2 is a cell array, not a table.
Luca
Luca el 15 de Abr. de 2021
Right, sorry I did not pay attention.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2021a

Preguntada:

el 14 de Abr. de 2021

Comentada:

el 15 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by