简体   繁体   English

R 中矩阵的逆和数值积分

[英]Inverse of matrix and numerical integration in R

in RI try to 1) get a general form of an inverse of a matrix (I mean a matrix with parameters instead of specific numbers), 2) then use this to compute an integral.在 RI 中尝试 1) 获得矩阵逆矩阵的一般形式(我的意思是带有参数而不是特定数字的矩阵),2)然后使用它来计算积分。

I mean, I've got a P matrix with a parameter theta, I need to add and subtract something, then take an inverse of this and multiply it by a vector so that I am given a vector pil.我的意思是,我有一个带有参数 theta 的 P 矩阵,我需要加减一些东西,然后取倒数并将其乘以一个向量,这样我就得到了一个向量 pil。 From the vector pil I take term by term and multiply it by a function with again the parameter theta and the result must be integrated from 0 to infinity.从向量 pil 中,我逐项将其乘以具有参数 theta 的函数,结果必须从 0 积分到无穷大。

I tried this, but it didn't work because I know the result should be pst= (0.3021034 0.0645126 0.6333840)我试过了,但没有用,因为我知道结果应该是 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

This was just for the first term of the vector pst, because when I didn't know how to a for cycle for this.这只是向量 pst 的第一项,因为当我不知道如何为此循环时。

Please, do you have any idea why it won't work and how to make it work?请问,您知道为什么它不起作用以及如何使其起作用吗?

Functions used in integrate should be vectorized as stated in the help. integrate使用的函数应该按照帮助中的说明进行矢量化。 At the end of your code add this在你的代码末尾添加这个

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

The result is the first element of your expected result.结果是您预期结果的第一个元素。

You will have to present more information about the input to get your expected vector.您必须提供有关输入的更多信息才能获得预期的向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM