replaceZinitNonFiniteLogDens: replaceZinitNonFiniteLogDens

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/initZ.R

Description

Replaces states of Zinit that yield non-finite rLogDen by sampling other states.

Usage

1

Arguments

Zinit

initial states see InitDEMCzsp

logDen

numeric matrix (nCases x nChains): calculated logDensitys for all the states in Zinit. If it is a vector then it is reshaped.

Details

In order for twDEMC to start up effectively, it is important that chains start from values, where the logDensity is finite

Value

Zinit, with several cols (parameter vectors) replaced by other cols

Author(s)

Thomas Wutzler

See Also

initZtwDEMCNormal replaceZinitCases

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
data(twdemcEx1)
data(twLinreg1)
attach( twLinreg1 )
#mtrace(initZtwDEMCNormal)
Zinit <- initZtwDEMCNormal( theta0, diag(4*sdTheta^2), nChainPop=8, nPop=2)
dim(Zinit)
res <- res0 <- twCalcLogDenPar(logDenGaussian, stackChains(Zinit) # chains stack to calculate in parallel
	,fModel=dummyTwDEMCModel		### the model function, which predicts the output based on theta 
	,obs=obs				### vector of data to compare with
	,invCovar=invCovar,		### the inverse of the Covariance of obs (its uncertainty)
	thetaPrior = thetaTrue,	### the prior estimate of the parameters
	invCovarTheta = invCovarTheta,	### the inverse of the Covariance of the prior parameter estimates
	xval=xval
)$logDen
plot(density(res))
res[res < -30] <- NA
resM <- matrix(res, ncol=dim(Zinit)[3] ) # restacking to chains work because it is the last dimension
set.seed(0815)
Zinit2 <- replaceZinitNonFiniteLogDens(Zinit, resM)
set.seed(0815)
Zinit3 <- replaceZinitNonFiniteLogDens(Zinit, res)
identical(Zinit2,Zinit3)

res2 <- twCalcLogDenPar(logDenGaussian, stackChains(Zinit2) # chains stack to calculate in parallel
	,fModel=dummyTwDEMCModel		### the model function, which predicts the output based on theta 
	,obs=obs			### vector of data to compare with
	,invCovar=invCovar,		### the inverse of the Covariance of obs (its uncertainty)
	thetaPrior = thetaTrue,	### the prior estimate of the parameters
	invCovarTheta = invCovarTheta,	### the inverse of the Covariance of the prior parameter estimates
	xval=xval
)$logDen
d2 <- density(res2)
lines( d2$x, d2$y * max(density(res0)$y)/max(d2$y), col="blue")

twDEMC documentation built on May 31, 2017, 3:44 a.m.