gibbs 采样法 R语言 遇到问题求助各位高人

a532737525 |浏览879次
2018/12/14 20:47

代码如下,初始 m=0,(tau=1/var(y)=1)


y<-rnorm(100)

y<-matrix(y,ncol=1)

n=100


Gibbs<-function(y,m0,tau0,a,b,M)

 

  tau0=1

  n<-length(y)

  k<-matrix(numeric(2),nrow=1)

  for(r in 1:M){

   mu<-(n*tau*mean(y)+tau0*m0)/(n*tau+tau0)

   eta<-n*tau+tau0

   

   m<-sqrt(eta^(-1))*norm(1)+mu

   tau<-rgamma(1,a+n/2,b+sum(y-m)^2/2)

   

   k<-rbind(k,cbind(m,tau^(-1)))

 }

  k<-k(2:(M+1),)

 

  return(k)

 

 


  • 问题补充 : 2018/12/14 21:05

    Error in norm(1) : 'A' must be a numeric matrix

    In addition: Warning message:

    In 1:M : numerical expression has 16 elements: only the first used

    >   k<-k(2:(M+1),)

    Error in k(2:(M + 1), ) : could not find function "k"

    这样该怎么解决的

收藏关注
全部回答(0)
0人关注该问题
 加载中...