Dear S-PLUS users:
I want to make a script that calculates
returns among variables in a column vector. Specifically, I need a script that
calculates, ln(x[,i+1]/x[,i]) for all elements in a column vector x.
Here, i denotes ith row.
[WNV] In S the first index
denotes the row, the second denotes the column.
For this purpose, I wrote the following
"script" but it does not work. It will be of great help if you could give me
some advice, suggestion, correction.
calcLN <-
function(x) {
x1<-c(matrix(unlist(x),nrow=nrow(x)))
x1<-log(numeric(x1))
for(i
in
1:nrow(x1)-1) {
x1rtn[i]<-x1[i+1]-x1[i]
i<-i+1
}
x1rtn
}
[WNV] the problem is with the line x1 <-
log(numeric(x1)). You probably meant
x1 <-
log(as.numeric(x1))
but your function is way too complicated. The usual way to
do it would be
calcLOG <- function(x)
diff(log(x))
finish. This makes you wonder if you need a special
function to do the job at all...
Bill Venables.
Regards,
Moto