setwd("~/new/Bayes/Beyes/rjags/rjags1")

library('rjags')
## Loading required package: coda
## Linked to JAGS 3.4.0
## Loaded modules: basemod,bugs
df <- read.csv("gamma.csv")
class(df)
## [1] "data.frame"
plot(1:nrow(df),df$X,type="l")

gamma_code <- '
  model
{
  for (i in 1:N)
  {
  x[i] ~ dgamma(shape, rate)
  }
  
  shape ~ dgamma(0.0001, 0.0001)
  rate ~ dgamma(0.0001, 0.0001)
  }
  '

writeLines(gamma_code,con="gamma.txt")


jags <- jags.model("gamma.txt",
                   data = list('x' = with(df, X),
                               'N' = nrow(df)),
                   n.chains = 4,
                   n.adapt = 1000)
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
##    Graph Size: 504
## 
## Initializing model
mcmc.samples <- coda.samples(jags,
                             c('shape', 'rate'),
                             5000,
                             thin = 5)

plot(mcmc.samples)

summary(mcmc.samples)
## 
## Iterations = 1005:6000
## Thinning interval = 5 
## Number of chains = 4 
## Sample size per chain = 1000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##          Mean        SD  Naive SE Time-series SE
## rate  0.00995 0.0006655 1.052e-05      1.824e-05
## shape 2.94497 0.1795265 2.839e-03      5.025e-03
## 
## 2. Quantiles for each variable:
## 
##           2.5%      25%      50%    75%  97.5%
## rate  0.008681 0.009489 0.009935 0.0104 0.0113
## shape 2.602190 2.824068 2.937613 3.0644 3.3007
# GLM values
# Conjugate values