Can I call R function in Matlab?
7 views (last 30 days)
Show older comments
I am trying to minimize a function written in matlab using R function known by optim
The matlab function is as follow
%% Like_R: function description
function [outputs] = Like_R(x_lf,S_inter,r,mu,sigma_epsilon, x_init,varphi,C)
% x_lf S the low frequency benchmark (i.e. annual stocks)
% S_inter X the matrix of indicators
% r the value for rho
% mu the autocorraltion of the shock
% sigma_epsilon sigma the standard error of the innovation
% varphi gamma the vector of coefficients associated with the indicators in X
c=size(S_inter,1);
M_1 =r*diag(ones(c-1,1),-1);
ey_M_1=inv(eye(c)-M_1);
Omega=(mu.^abs(repmat((1:c),c,1)'-repmat((1:c),c,1)))*(sigma_epsilon^2)/(1-mu^2);
Omega_x=ey_M_1*Omega*ey_M_1';
M_2=zeros(c,1);
M_2(1,1)=r;
Theta=x_lf-C*ey_M_1*(M_2*x_init+S_inter*varphi);
Log_likelihood = (-c/2)*log(2*pi) - c*log(sigma_epsilon^2)+ log(abs(1-mu^2)) - Theta'*inv(C*Omega_x*C')*Theta;
outputs=-Log_likelihood;
end
I have entries for each of x_lf, S_inter, and C. However, I want to find the input elements that minimizes the above function. In R, if this function has been writen there one can simply define another function in R known by lev.uncons this:
lev.uncons<-function(y){
Like_R(x_lf, S_inter,y[1],y[2],y[3], y[4],as.matrix(c(y[5],y[6])),C)
}
Then one can call the following function in R as
#you can use any optimisation algorithm
#with or without bondaries, priors,
solDnc<-optim(c(0.9,0.9, 0.5,1000,0.3), lev.uncons, method = "L-BFGS-B",
lower = c(0.1,0, -0.5,0.1,0.01), upper = c(1,0.999,Inf,1), hessian = TRUE)
What I want to do is to use matlab to call the minimization function optim in R. I know there is alternative such as min search or minimume constraint. But is there a way to do so from matlab?
0 Comments
Answers (1)
Anmol Dhiman
on 9 Apr 2020
Hi,
You can refer to the below link.
Thanks,
Anmol Dhiman
1 Comment
Hung Dao
on 20 Jan 2021
Hi,
I got the following error:
"Error using openR (line 68)
Cannot connect to R.
Error using COM.StatConnectorSrv_StatConnector/Init
Error: Object returned error code: 0x80040013"
What should I do?
Thanks,
Hung
See Also
Categories
Find more on Econometrics Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!