2017-10-12 214 views
1

在RI嘗試到 1)得到矩陣的逆矩陣的一般形式(我的意思是一個帶參數的矩陣,而不是具體的數字),然後用這個來計算一個矩陣積分。我的意思是,我得到一個帶參數theta的P矩陣,我需要加上和減去一些東西,然後把這個反過來並乘以一個向量,這樣我得到一個向量pil。從矢量pil中逐項求出並乘以一個函數再次得到參數theta並且結果必須從0到無窮大積分。矩陣和數值積分的逆矩陣R

我嘗試這樣做,但它沒有工作,因爲我知道結果應PST = (0.3021034 0.0645126 0.6333840)

c<-0.1 
g<-0.15 
    integrand1 <- function(theta) { 
    pil1 <- function(theta) { 
    P<-matrix(c( 
     1-exp(-theta), 1-exp(-theta),1-exp(-theta),exp(-theta),0,0,0,exp(-theta),exp(-theta) 
    ),3,3); 
    pil<-(rep(1,3))%*%solve(diag(1,3)-P+matrix(1,3,3)); 
    return(pil[[1]]) 
    } 
    q<-pil1(theta)*(c^g/gamma(g)*theta^(g-1)*exp(-c*theta)) 
    return(q)} 

(pst1<-integrate(integrand1, lower = 0, upper = Inf)$value) 
#0.4144018 

這只是爲載體PST的第一項,因爲當我不知道如何爲此循環。

請問,你有什麼想法,爲什麼它不會工作,以及如何使它工作?

回答

1

integrate中使用的函數應按照幫助中的說明進行矢量化。 在你的代碼的末尾添加此

integrand2 <- Vectorize(integrand1) 
integrate(integrand2, lower = 0, upper = Inf)$value 
#[1] 0.3021034 

結果是您預期的結果的第一要素。

您將不得不提供有關輸入的更多信息以獲得預期的矢量。

+0

非常感謝你,這大大幫助了我:) – Bee