楼主: crazygod
3926 10

[问答] colMeans函数求助 [推广有奖]

11
不知道用啥用户名了... 学生认证  发表于 2024-2-19 17:16:28
你好,我最近也在replicate Jurado et al (2014)的代码,换成下面的代码可以解决:
  1. # -------------------------------------------------------------------------
  2. # Estimate a first-order autoregressive stochastic volatility model on the
  3. # forecast errors from an AR(4) model on each predictor (series by series)
  4. # -------------------------------------------------------------------------

  5. # Initialization
  6. rm(list=ls())
  7. library("coda")
  8. library("stochvol")
  9. set.seed(1000) # for replication
  10. options(digits=17)
  11. vt   = read.table('vft.txt',sep = '\t')
  12. T    = dim(vt)[1]
  13. N    = dim(vt)[2]

  14. for (i in 1:N){
  15.         if(min(log(vt[,i]^2))== -Inf){
  16.                 vt[,i] = vt[,i] + 0.00001 #offset to avoid taking log of zero
  17.         }
  18. }

  19. # Run MCMC algorithm and store draws
  20. S    = 50000
  21. burn = 50000
  22. m    = matrix(0,T+3,N)
  23. g    = matrix(0,3,N)
  24. for (i in 1:N){
  25.         draws  = svsample(vt[,i],draws=S,burnin=burn,quiet=TRUE,thinpara=10,thinlatent=10)
  26.         all    = cbind(do.call(rbind,draws$para[,1:3]),do.call(rbind,draws$latent))
  27.     # all    = cbind(rbind(draws$para[,1:3]),do.call(rbind,draws$latent))
  28.     m[,i]  = colMeans(all)
  29.         g[,i]  = geweke.diag(draws$para[[1]][,1:3])$z
  30.         name   = sprintf('svfdraws%d.txt',i)
  31. #        write(t(all),file=name,ncolumn=dim(all)[2])
  32. }
  33. out = rbind(m,g) #include Geweke statistics
  34. write(t(out),file='svfmeans.txt',ncolumn=dim(out)[2])
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-12 03:41