MATLAB Answers

Translation R code to Matlab

21 views (last 30 days)
Annalisa Schiavon
Annalisa Schiavon on 30 Jan 2020
Answered: Hans Scharler on 30 Jan 2020
Hi, I am trying to translate an R code into Matlab, and with this piece of code i get different result and I don't understand the problem! This is the Matlab code
for i=1:size(CV,3)
if var1.Description~="AR-Stationary 4-Dimensional VAR(4) Model" & i>1
D = normalize(armafevd(var1.AR,[],"Method","generalized","NumObs",10,'InnovCov',EstMdl.Covariance),2, 'norm', 1);
CV(:,:,i)= D(10,:,:);
and this is the R code
t = nrow(Y)
space = 200 # 200 days rolling window estimation
CV = array(NA, c(k, k, (t-space)))
colnames(CV) = rownames(CV) = colnames(Y)
for (i in 1:dim(CV)[3]) {
var1 = VAR(Y[i:(space+i-1),], p=nlag, type="const")
if(any(roots(var1)>1) & i>1){ # controls if the VAR process is stationary
CV[,,i] = CV[,,(i-1)]
} else {
CV[,,i] = gfevd(var1, n.ahead=nfore)$fevd
The VAR estimated coefficients are equal but the when it does the variance decomposition the results are different. So I guess it is a problem of how I do the loop. Can anyone spot the mistake? thanks!!

Answers (1)

Hans Scharler
Hans Scharler on 30 Jan 2020
I don't have a direct answer, but you could use a COM-based interface to call your R function from within MATLAB. There is a project on File Exchange that might help if you are usign Windows.


Community Treasure Hunt

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

Start Hunting!