Function sampleSig2

Description

Samples posterior of the variance of a normal distibution which has an additive structure on the mean, and a single variance for all values. Usually used within MCMC loop.

Usage

1
sampleSig2(sig2,block,y,cond,sub,item,lag,N,ncond,I,J,a,b)

Arguments

sig2

Sample of sig2 from previous iteration.

block

Vector of parameters for mean of distribution

y

Vector of data

cond

Vector that indexs condition (e.g., deep vs. shallow)

sub

Vector of subject index, starting at zero.

item

Vector of item index, starting at zero.

lag

Vector of lag index, zero-centered.

N

Number of conditions.

ncond

Number of trials per condition.

I

Number of subjects.

J

Number of items.

a

Shape parameter for inverse gamma prior on Sigma2.

b

Rate parameter for inverse gamma prior on Sigma2. Setting 'a' and 'b' to small values (e.g., .01, .01) makes the prior non-informative.

Value

The function returns a new sample of Sigma2.

Author(s)

Michael S. Pratte

See Also

hbmem

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
library(hbmem)
true.mean=c(0,0)
true.sigma2=c(10,20)
N=2
I=1
J=1
R=1000
cond=rep(0:1,R/2)
ncond=table(cond)
sub=rep(0,R)
item=rep(0,R)
lag=rep(0,R)

#make some data
dat=rnorm(R,true.mean[cond+1],sqrt(true.sigma2[cond+1]))
true.block=c(true.mean,rep(0,(I+J+3)))

a=b=.01

M=10
s.sigma2=matrix(1,M,N)

for(m in 2:M)
{
s.sigma2[m,]=sampleSig2(s.sigma2[m-1,],true.block,dat,cond,sub,item,lag,N,ncond,I, J,a,b)
}

par(mfrow=c(1,1),pty='s')

matplot(s.sigma2,t='l')

abline(h=true.sigma2,col="blue")
abline(h=colMeans(s.sigma2),col="green") #post mean

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.