Learn R Programming

hbmem (version 0.2)

sampleSig2: 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

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.

See Also

hbmem

Examples

Run this code
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=1000
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

Run the code above in your browser using DataLab