[R] Simple question on replace a matrix row

anna lippelanna24 at hotmail.com
Fri Jan 29 22:54:54 CET 2010


I was trying to avoid the code because I wanted to simplify it but here we
go:

mat2<- matrix(nrow = 30, ncol = 7)               
       colnames(mat2) <-c( "A", "B", "C", "D", "E", "F", "G") 

mat1<-mainMat[1,]

I get mainMat[1,] from the following function:

ComputeSignalReturns <- function(vec1, prices){     
       removingNA <-  as.matrix(removeNA(cbind(prices,vec1)))
       prices<-as.matrix(removingNA[,1])
       vec1<- as.matrix(removingNA[,2])
       nbDays <- length(vec1)  
       returnsOneDay <- abs(vec1[1:(nbDays - 1)]) *
((as.ts(lag(prices,1))/as.ts(prices)) ^vec1[1:nbDays -       1)] - 1)
       returnsOneDay<-as.matrix(cbind(vec1[1:(nbDays -
1)],returnsOneDay)[which(vec1[1:(nbDays -          1)]! =0),2])
       returnsOneDayAnnualized <- as.matrix(apply(returnsOneDay,1,             
Return.annualized,scale=252,geometric=FALSE))
       returnsTwoDays <- abs(vec1[1:(nbDays - 2)]) *
((as.ts(lag(prices,2))/as.ts(prices))^vec1[1:(nbDays - 2)] - 1)
       returnsTwoDays<-as.matrix(cbind(vec1[1:(nbDays -
2)],returnsTwoDays)[which(vec1[1:(nbDays - 2)]!=0),2])
       returnsTwoDaysAnnualized <- as.matrix(apply(returnsTwoDays,1,
Return.annualized,scale=252/2,geometric=FALSE))
       returnsThreeDays <- abs(vec1[1:(nbDays - 3)]) *
((as.ts(lag(prices,3))/as.ts(prices))^vec1[1:(nbDays - 3)] - 1)
       returnsThreeDays<-as.matrix(cbind(vec1[1:(nbDays -
3)],returnsThreeDays)[which(vec1[1:(nbDays - 3)]!=0),2])
       returnsThreeDaysAnnualized <- as.matrix(apply(returnsThreeDays,1,
Return.annualized,scale=252/3,geometric=FALSE))
       returnsFiveDays <- abs(vec1[1:(nbDays - 5)]) *
((as.ts(lag(prices,5))/as.ts(prices))^vec1[1:(nbDays - 5)] - 1)
       returnsFiveDays<-as.matrix(cbind(vec1[1:(nbDays -
5)],returnsFiveDays)[which(vec1[1:(nbDays - 5)]!=0),2])
       returnsFiveDaysAnnualized <- as.matrix(apply(returnsFiveDays,1,
Return.annualized,scale=252/5,geometric=FALSE))
       returns <- list(returnsOneDay, returnsTwoDays, returnsThreeDays,
returnsFiveDays)   
       returnsAnnualized <- list( returnsOneDayAnnualized, 
returnsTwoDaysAnnualized, returnsThreeDaysAnnualized,
returnsFiveDaysAnnualized) 
       
       
       avgReturn <- data.matrix(data.frame(lapply(returns,  mean))) 
       cumReturn <- data.matrix(data.frame(lapply(returns, sum)))  
       volReturn<- data.matrix(data.frame(lapply(returns, sd))) 
       sharpeRatio <-
as.matrix((as.numeric(data.matrix(data.frame(lapply(returnsAnnualized, 
mean))) - rep(0.0025,4)) / as.numeric( matrix(lapply(returnsAnnualized, 
sd))))) 
       nbSignals <- data.matrix(data.frame(lapply(returns,  length))) 
      
nbPositives<-list(returnsOneDay[which(returnsOneDay>0)],returnsTwoDays[which(returnsTwoDays>0)],returnsThreeDays[which(returnsThreeDays>0)],returnsFiveDays[which(returnsFiveDays>0)])
       nbPositives<- t(data.matrix(data.frame(lapply(nbPositives,length)))) 
       posRatio<- matrix( as.numeric(nbPositives)  / as.numeric(nbSignals))        
       summary<- matrix(cbind(avgReturn, cumReturn, volReturn,
sharpeRatio,nbSignals,nbPositives,posRatio),nrow = 4, ncol = 7,
dimnames=list(c("one day", "two days", "three days", "five days"),c("A",
"B", "C", "D", "E", "F", "G")))
       return( summary)
  }

I hope it won't be confusing...


-----
Anna Lippel
-- 
View this message in context: http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1432589.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list