Skip to content

Simple question on replace a matrix row

16 messages · David Winsemius, Dennis Murphy, William Dunlap +1 more

#
Hello, I have a matrix mat1 of dim [1,8] and mat2 of dim[30,8], I want to
replace the first row of mat2 with mat1, this is what I do:
mat2[1,]<-mat1 but it transforms mat2 in a list I don't understand, I want
it to stay a matrix...

-----
Anna Lippel
#
On Jan 29, 2010, at 3:55 PM, anna wrote:

            
What makes you think mat1 was a matrix to begin with?

 > mat1 <- matrix(8:1, 1,8)
 > mat2 <-matrix(1:(8*8), 8,8)

 > mat2[1,] <- mat1
 > mat2  # an 8 x 8 matrix

We cannot tell what sort of errors you are making (since your provided  
no reproducible example), but you surely are making errors.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
Here is how I get mat1: I have a matrix mainMat of  4 rows and 8 columns, I
take the first row of it to build mat1. The problem might come from how I
build mainMat --> each column comes from an lapply function which returns a
list but I convert it to a matrix [4,1]...

-----
Anna Lippel
#
On Jan 29, 2010, at 4:33 PM, anna wrote:

            
Code... we want code.
#
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
#
What I did now is that I checked the class of both matrix before assigning
the first row of mat2:
class(mainMat) --> "matrix"
class(mainMat[1,]) --> "list"
class(mat2) --> "matrix"
mat2[1,]<-mainMat[1,]
class(mat2) --> "list"
 I think the problem comes from mainMat[1,] that should be of class "matrix"
but is of class "list"...

-----
Anna Lippel
#
On Jan 29, 2010, at 4:54 PM, anna wrote:

            
Wouldn't it have been much more simple to just show us the output of...

dput(ComputeSignalReturns)   # ?   or...
dput(mainMat)

I do not see any one plowing through that obscure code with no data to  
work with.
#
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
You need to look further into the structure of
those objects to see what is going on.  A matrix
can contain data of any simple enough (vector-like)
class, e.g., "numeric", "integer", "list", or "logical".
The class of the matrix is always "matrix", no matter
what is in it.  A "matrix" can be subscripted with 2
subscripts and contains one vector of data, in column
major order, of length nrow(matrix)*ncol(matrix).
(A "data.frame" is another 2 dimensional object with
a quite different structure and significantly different
behavior.)

I'd recommend using str() to get a fuller picture:

  > matList<-matrix(lapply(1:4,seq_len), 2, 2)
  > matNumeric<-matrix(exp(0:3), 2, 2)
  > class(matList)
  [1] "matrix"
  > class(matNumeric)
  [1] "matrix"
  > str(matList)
  List of 4
   $ : int 1
   $ : int [1:2] 1 2
   $ : int [1:3] 1 2 3
   $ : int [1:4] 1 2 3 4
   - attr(*, "dim")= int [1:2] 2 2
  > str(matNumeric)
   num [1:2, 1:2] 1 2.72 7.39 20.09 
 
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
p1427857p1437234.html
#
David, I think I am going close to the problem, at the end of the function
ComputeSignalReturns I build the matrix summary with cbind. So when I make
class(summary) I get "matrix" but when I make class(summary[1,]) I get
"list"
Excuse me if I didn't show the problem the right way, I am still new and I
am trying to do my best
Here is one output of ComputeSignalReturns(summary):
                       A                   B               C                     
D               E       F     G        
one day    0.001754569       0.1754569      0.01713514      0.1018170     
100    46    0.46     
two days   0.003142898      0.3111469      0.02332515      0.1338922      
99     53    0.5353535
three days -0.0008794462   -0.08618573   0.02814039     -0.03230971     98    
45     0.4591837
five days  -0.0008868757    -0.08514007   0.03332889     -0.02809811     96    
3       0.40625  



-----
Anna Lippel
#
Dennis, as soon as I do : > mat3[1, ] <- mat1  and class(mat3) I get a list
and not a matrix anymore...So yes you drove me to the problem, mat1 is not
one-row numeric matrix  but a list.

-----
Anna Lippel
#
Bill this is exactly what is happening, by using lapply I am having a list
and not a numeric vector, I want a numeric vector, is there a way to convert
that list to a numeric vector?

-----
Anna Lippel
#
On Jan 29, 2010, at 6:14 PM, anna wrote:

            
You could see if substituting sapply would yield a matrix. It will if  
the inputs are appropriate.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
Well I just tried to convert it with data.frame and then data.matrix and it
returned me a numeric vector but I am not done yet, let's see if it works,
if it doesn't I will try sapply. I think I didn't use sapply because the
vectors I am using in lapply are of different lengths.

-----
Anna Lippel
#
On Jan 29, 2010, at 6:53 PM, anna wrote:

            
You really ought to make an effort to test you data structures and  
operations with compact, simple examples first.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT