[R] Code translation from R to MATLAB
Ana Cristiuc
cristiucana at gmail.com
Wed Oct 14 20:49:30 CEST 2015
Dear Ted,
I have found on Internet your very good comments on R vs MATLAB languages.
I am a beginner in both languages, but unfortunately I have got a task to
translate a huge code from R to MATLAB.
Could you please recommend me some references or advices how to handle the
problems appearing from the differences between languages.
I was trying today to translate the below lines of code from R to MATLAB,
but very unsuccessfully.
Could you please help me by saying, at least, if I have any chances to
translate this R function to a function in MATLAB or it will be simpler
to avoid a function body for the initialization.
This is the first function from the attached script. It is the hardest part
of the code:
init <- function(N=3) {
## initialize model settings and load data
## Globals, side effects:
## sets n.per, N, W, Y, dates, mats
n.per <<- 12 # monthly data
N <<- N # three risk factors
## load yield data
## cat("loading GSW yield data...\n")
## if ('data' %in% dir()) {
## load("data/gsw_data_monthly.RData") # Y, dates, mats
## } else {
## load("../data/gsw_data_monthly.RData") # Y, dates, mats
## }
## ##mat.sel <- c(1:12)
## ##mat.sel <- c(3:12) ## one through ten years
## mat.sel <- 2+c(1,2,3,4,5,7,10)
## Y <- Y[,mat.sel]/100/n.per
## mats <- mats[mat.sel]*n.per
cat("loading Anh Le's yield data...\n")
if ('data' %in% dir()) {
load("data/le_data_monthly.RData") # Y, dates
} else {
load("../data/le_data_monthly.RData") # Y, dates
}
## mats <- c(3,6,12,24,36,48,60,72,84,96,108,120)
##mats <- c(12,24,36,48,60,72,84,96,108,120)
mats <- c(12,24,36,48,60,84,120)
Y <- Y[,mats]/n.per
if (dim(Y)[2]!=length(mats)) stop("wrong number of columns in yield
matrix");
start.sample <- 19900101
start.date <<- c(1990,1)
##start.sample <- 19850101
##start.date <<- c(1985,1)
end.sample <- 20071231
sel.sample <- dates>=start.sample & dates<=end.sample
Y <- Y[sel.sample,] ## change local variable
dates <- dates[sel.sample]
cat("sample from", as.character(min(dates)), "to",
as.character(max(dates)), "\n")
## first N principal components => pricing factors
W <<- getW(Y, N)
## make local variables global -- alt: assign("Y", Y, env=.GlobalEnv)
Y <<- Y; dates <<- dates; mats <<- mats
}
My understanding is that I can specify this initialization outside of a
function, but I am not sure.
I hope you will have some free minutes to help me.
Many thanks in advance for your help!
Best regards,
Ana Cristiuc,
More information about the R-help
mailing list