2013-07-31 64 views
0

我正在使用帶R中編程矩陣的馬爾可夫模型來解決健康經濟問題。當值隨時間變化時求冪矩陣

我有一個矩陣,我想指數200次。矩陣內的值隨時間而變化。例如,位置x12上的值在第一個矩陣0.1中在第二個0.2和第三個0.3中。我不想寫下200個矩陣,而是寫一個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 

如果我介紹我到矩陣中,結果不正確:

############### 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 

我該如何解決這個問題? 非常感謝!圈外超出該線

+1

之前插入基質有沒有辦法,'MTX ^我'會與不同的mtx論證一起工作。 –

+0

仍然沒有'initial_patient_distribtion'並不能明白爲什麼你認爲目前的輸出是錯誤的。 –

回答

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) 
       } 
+0

謝謝!幾乎解決我的問題,只是現在我是出界: – user2639563

+0

也許用'的(我在2:長度(AAA))' –

+0

> initial_patient_distribtion < - 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) > > trans_matrix_dasa_no2nd < - 基質( (0,0.5,nrow = 3,ncol = 3) > trans_matrix_dasa_no2nd [1,] <-c(aaa [i],bbb [i],ccc [i]) > trans_matrix_dasa_no2nd [ 0。5) > trans_matrix_dasa_no2nd [3,] < - C(0,0,1) > > > cycle_patient_distribution_dasa_no2nd [1,] <-initial_patient_distribtion – user2639563

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