-2
fact.weight <- matrix(NA,nrow(dax.p),2)

fact.ret <- matrix(NA,nrow(dax.p),2)  
fact.sd <- matrix(NA,nrow(dax.p),2)  
fact.cov <- matrix(NA,ncol(fact.weight),ncol(fact.weight))

for (row in 15:nrow(fact.weight)) {
   for (column in 1:ncol(fact.weight)) {
     fact.ret[row,column] <- mean(fact.wealth.return[row-1:row,column])  
     fact.sd[row,column] <- sd(fact.wealth.return[row-1:row,column])
  }
}

There is something wrong with my loop. fact.ret and fact.sd only returns NA values. The problem may be here somewhere

mean(fact.wealth.return[row-1:row,column])
sd(fact.wealth.return[row-1:row,column])

But I can't see the problem myself. Do anyone see the problem?

Thank you in advance!

digEmAll
  • 53,114
  • 9
  • 109
  • 131
R_learner
  • 39
  • 4
  • 2
    Can you please provide a sample of your data, what is dax.p? – Antonis Feb 18 '18 at 15:21
  • Make sure that your data, i.e. `fact.wealth.return` does not contain any `NA`. For more info: https://stackoverflow.com/questions/15924398/subsetting-r-data-frame-with-nas-in-index-variable/15924914#15924914 – nadizan Feb 18 '18 at 15:27

1 Answers1

0

The problem seems to be in your subsetting of the vector, I think you want:

mean(fact.wealth.return[(row-1):row,column])
sd(fact.wealth.return[(row-1):row,column])

To see why, try:

row = 10    
row-1:row

[1] 9 8 7 6 5 4 3 2 1 0

and compare that with

row = 10
(row-1):row

[1]  9 10
Florian
  • 21,690
  • 4
  • 34
  • 66