当值随时间变化时取幂矩阵

时间:2013-07-31 20:15:03

标签: r matrix

我正在使用带有R编程矩阵的马尔可夫模型来解决健康经济问题。

我有一个矩阵,我想指数200次。矩阵内的值随时间而变化。例如,位置x12上的值在第二个0.2中的第一个矩阵0.1和第三个0.3中。我不想写下200个矩阵,而是写一个并用200取指数。对于x12:我可以使用200个值的向量,矩阵会使用该向量吗?

对于常数值,没问题:

############### Imatinib Base-line Strategy ##########################
trans_matrix_dasa_no2nd <- matrix (,nrow=3,ncol=3)

trans_matrix_dasa_no2nd[1,] <- c(0.9,0.05,0.05)
trans_matrix_dasa_no2nd[2,] <- c(0,0.8,0.2)
trans_matrix_dasa_no2nd[3,] <- c(0,0,1)

cycle_patient_distribution_dasa_no2nd <- matrix (,nrow=2,ncol=3)

for (i in 1:2){cycle_patient_distribution_dasa_no2nd[i,] <- 
           initial_patient_distribtion%*%(trans_matrix_dasa_no2nd%^%i)}

cycle_patient_distribution_dasa_no2nd
     [,1] [,2] [,3]
[1,]  900   50   50
[2,]  810   85  105

但如果我使用矢量,它就不再起作用了:

############### Imatinib Base-line Strategy ##########################
aaa <- c(0.9,0.5)
bbb <- c(0.05, 0.4)
ccc <- c(0.05, 0.1)

trans_matrix_dasa_no2nd <- matrix (,nrow=3,ncol=3)

trans_matrix_dasa_no2nd[1,] <- c(aaa,bbb,ccc)
Error in trans_matrix_dasa_no2nd[1, ] <- c(aaa, bbb, ccc) : 
  number of items to replace is not a multiple of replacement length
trans_matrix_dasa_no2nd[2,] <- c(0,0.8,0.2)
trans_matrix_dasa_no2nd[3,] <- c(0,0,1)

cycle_patient_distribution_dasa_no2nd <- matrix (,nrow=2,ncol=3)

for (i in 1:2){cycle_patient_distribution_dasa_no2nd[i,] <- 
         initial_patient_distribtion%*%(trans_matrix_dasa_no2nd%^%i)}

cycle_patient_distribution_dasa_no2nd
     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA

如果我将i引入矩阵,则结果不正确:

############### Imatinib Base-line Strategy ##########################
aaa <- c(0.9,0.5)
bbb <- c(0.05, 0.4)
ccc <- c(0.05, 0.1)

trans_matrix_dasa_no2nd <- matrix (,nrow=3,ncol=3)

trans_matrix_dasa_no2nd[1,] <- c(aaa[i],bbb[i],ccc[i])
trans_matrix_dasa_no2nd[2,] <- c(0,0.8,0.2)
trans_matrix_dasa_no2nd[3,] <- c(0,0,1)

cycle_patient_distribution_dasa_no2nd <- matrix (,nrow=2,ncol=3)

for (i in 1:2){cycle_patient_distribution_dasa_no2nd[i,] <- 
      initial_patient_distribtion%*%(trans_matrix_dasa_no2nd%^%i)}

cycle_patient_distribution_dasa_no2nd
     [,1] [,2] [,3]
[1,]  500  400  100
[2,]  250  520  230

我怎么能解决这个问题? 非常感谢!

2 个答案:

答案 0 :(得分:0)

首先在循环外注释掉这一行:

# trans_matrix_dasa_no2nd[1,] <- c(aaa[i],bbb[i],ccc[i])

然后将其放入循环中,以便它可以适当地访问值并更新先前的状态值。您将需要以不同方式处理第一种情况,以便i-1上的索引不引用0:

 cycle_patient_distribution_dasa_no2nd[1,] <-initial_patient_distribtion
 for (i in 2:n){ 
        trans_matrix_dasa_no2nd[1,] <- c(aaa[i],bbb[i],ccc[i])
        cycle_patient_distribution_dasa_no2nd[i,] <-
               cycle_patient_distribution_dasa_no2nd[i-1,]%*%    
                                      (trans_matrix_dasa_no2nd)
                 }

答案 1 :(得分:0)

问题解决了:在循环之前插入矩阵

initial_patient_distribution <- c (1000,0,0)
aaa <- c(1,0.7,0.6,0.5,0.4)
bbb <- c(1, 0.2,0.3, 0.4, 0.5)
ccc <- c(1, 0.1,0.1,0.1,0.1)

cycle_patient_distribution_dasa_no2nd <- matrix (,nrow=5,ncol=3)

cycle_patient_distribution_dasa_no2nd[1,] <-initial_patient_distribution
 for (i in 2:5){
 trans_matrix_dasa_no2nd <- matrix (,nrow=3,ncol=3)
 trans_matrix_dasa_no2nd[1,] <- c(aaa[i],bbb[i],ccc[i])
 trans_matrix_dasa_no2nd[2,] <- c(0,0.5,0.5)
 trans_matrix_dasa_no2nd[3,] <- c(0,0,1)
cycle_patient_distribution_dasa_no2nd[i,] <- cycle_patient_distribution_dasa_no2nd[i-    1,]%*%(trans_matrix_dasa_no2nd)
}

cycle_patient_distribution_dasa_no2nd
     [,1]  [,2]  [,3]
[1,] 1000   0.0   0.0
[2,]  700 200.0 100.0
[3,]  420 310.0 270.0
[4,]  210 323.0 467.0
[5,]   84 266.5 649.5