Converting matlab script into r script

Hi!
I have an assignment about converting matlab codes into R ones. It's just 62 rows, but i don't know how matlab works, so i tried to use a package called mat2r to convert it, but after i convert my codes I don't see big changes (therefore its full off errors). I'm a beginner and It's an important assignment for me, I would be so grateful if someone could help me.
The matlab codes:
Date = readmatrix('BankSystemicRiskAnalysis.xlsx',...
'Sheet','close',...
'Range','A:A'); %read the dates
Date = x2mdate(Date); %convert date format

Prices = readmatrix('BankSystemicRiskAnalysis.xlsx',...
'Sheet','close',...
'Range','B:Q'); % read the asset prices
AssetListShort = {'JPM','BAC','C','WFC','GS','MS','BK','USB','PNC','COF',...
'STT','BBT','STI','AXP','FITB','RF'}; %short asset names
AssetListLong = {'JPMorgan','BankofAmerica','Citigroup','WellsFargo','GoldmanSachs','MorganStanley',...
'BankofNewYorkMellon','USBancorp','PNCFinancialSG','CapitalOneFinancial','StateStreetCorp',...
'BB&TCorp','SunTrustBanks','AmericanExpress','FifthThirdBancorp','RegionsFinancialCorp'}; %long asset names

Returns = diff(log(Prices)); % calculate the log returns
Date = Date(2:end);
MarketReturn = sum(Returns,2); % market return

%find 2008-09-15
Indice = datefind(datetime(2008,09,15),Date);
RegressionWindowSize = 120; % initial window size lenght
EventWindow = 20; % event window 2*parameter+1 - symmetric

% filter the original data set
RegressionData = Returns(Indice-EventWindow-RegressionWindowSize+1:Indice-EventWindow,:);
Market1 = MarketReturn(Indice-EventWindow-RegressionWindowSize+1:Indice-EventWindow,1);
EventData = Returns(Indice-EventWindow:Indice+EventWindow,:);
Market2 = MarketReturn(Indice-EventWindow:Indice+EventWindow,1);

% estimate the market model for every asset - save the alpha and beta values
for i = 1:numel(AssetListShort)
mdl = fitlm(Market1,RegressionData(:,i));
alpha(1,i)= mdl.Coefficients.Estimate(1);
beta(1,i)= mdl.Coefficients.Estimate(2);
var(1,i) = sum(mdl.Residuals.Raw.^2)/(RegressionWindowSize-2);
end

% fit the market model on the event window and calculate the abnormal returns
for i = 1:numel(AssetListShort)
AbnormalReturn(:,i) = EventData(:,i) - alpha(1,i)+beta(1,i)*Market2;
end
% cummulative abnomral return
CAR = cumsum(AbnormalReturn);
%average of abnormal returns and CAR-s
avAbnormalReturn = sum(AbnormalReturn,2);
avCAR = sum(CAR,2);

% variance of the abnormal return and CAR
varAbnormalReturn = sum(var,2)/(numel(AssetListShort))^2;
varCAR = varAbnormalReturn*(EventWindow*2+1);

upperconf = avCAR + 1.96* varCAR;
lowerconf = avCAR - 1.96* varCAR;

normalizedValue = avCAR/sqrt(varCAR);

figure
plot(avCAR,'b');
hold on
plot(upperconf,'g');
hold on
plot(lowerconf,'r');

And the converted (to r) codes:
Date <- readmatrix('BankSystemicRiskAnalysis.xlsx',...
'Sheet','close',...
'Range','A:A')#read the dates
Date <- x2mdate(Date)#convert date format

Prices <- readmatrix('BankSystemicRiskAnalysis.xlsx',...
'Sheet','close',...
'Range','B:Q')# read the asset prices
AssetListShort <- {'JPM','BAC','C','WFC','GS','MS','BK','USB','PNC','COF',...
'STT','BBT','STI','AXP','FITB','RF'}#short asset names
AssetListLong <- {'JPMorgan','BankofAmerica','Citigroup','WellsFargo','GoldmanSachs','MorganStanley',...
'BankofNewYorkMellon','USBancorp','PNCFinancialSG','CapitalOneFinancial','StateStreetCorp',...
'BB&TCorp','SunTrustBanks','AmericanExpress','FifthThirdBancorp','RegionsFinancialCorp'}){#long asset names

Returns <- diff(log(Prices))){# calculate the log returns
Date <- Date(2:length(Date))
MarketReturn <- sum(Returns,2)# market return

#find 2008-09-15
Indice <- datefind(datetime(2008,09,15),Date)
RegressionWindowSize <- 120# initial window size lenght
EventWindow <- 20# event window 2*parameter+1 - symmetric

filter the original data set

RegressionData <- Returns(Indice-EventWindow-RegressionWindowSize+1:Indice-EventWindow,:slight_smile:
Market1 <- MarketReturn(Indice-EventWindow-RegressionWindowSize+1:Indice-EventWindow,1)
EventData <- Returns(Indice-EventWindow:Indice+EventWindow,:slight_smile:
Market2 <- MarketReturn(Indice-EventWindow:Indice+EventWindow,1)

estimate the market model for every asset - save the alpha and beta values

for (i in 1:numel(AssetListShort)){
mdl <- fitlm(Market1,RegressionData(:,i))
alpha(1,i) <- mdl.Coefficients.Estimate(1)
beta(1,i) <- mdl.Coefficients.Estimate(2)
var(1,i) <- sum(mdl.Residuals.Raw.^2)/(RegressionWindowSize-2)
}

fit the market model on the event window and calculate the abnormal returns

for (i in 1:numel(AssetListShort)){
AbnormalReturn(:,i) <- EventData(:,i) - alpha(1,i)+beta(1,i)*Market2
}

cummulative abnomral return

CAR <- cumsum(AbnormalReturn)
#average of abnormal returns and CAR-s
avAbnormalReturn <- sum(AbnormalReturn,2)
avCAR <- sum(CAR,2)

variance of the abnormal return and CAR

varAbnormalReturn <- sum(var,2)/(numel(AssetListShort))^2
varCAR <- varAbnormalReturn*(EventWindow*2+1)

upperconf <- avCAR + 1.96* varCAR
lowerconf <- avCAR - 1.96* varCAR

normalizedValue <- avCAR/sqrt(varCAR)

figure
plot(avCAR,'b')
hold on
plot(upperconf,'g')
hold on
plot(lowerconf,'r')

Thank you for your time!

This might help.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.