MATLAB Answers

# Translation R code to Matlab

13 views (last 30 days)
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
t=size(Y,1);
space=200;
nlag=4
CV=NaN(k,k,(t-space));
for i=1:size(CV,3)
var=varm(4,4);
var1=estimate(var,Y(i:(space+i-1),:));
if var1.Description~="AR-Stationary 4-Dimensional VAR(4) Model" & i>1
CV(:,:,i)=CV(:,:,(i-1));
else
D = normalize(armafevd(var1.AR,[],"Method","generalized","NumObs",10,'InnovCov',EstMdl.Covariance),2, 'norm', 1);
CV(:,:,i)= D(10,:,:);
end
end
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)) {
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!!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

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.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!