Zswitch_univ: Label switch fix for Univariate Normals (MAIN ONE)

Description Usage Arguments Examples

Description

This function fixes the label switching in the output of a univariate Gibbs sampler. Requires a constant number of non-empty groups in samples, but can deal with empty groups.

Usage

1
Zswitch_univ(out_trim, LineUpBy = 1, PropMin = 0.1)

Arguments

LineUpBy

: currently can be set to 1 or 2. It is only used to rename the FINAL output 1: final output will order Components by posterior weight (Z=1 for max(weight),etc) 2: final output ordered by variance of components Again, note this doesnt affect the actualy unswitching, which uses no ordering. it simply relabels the reference groups according to chosen parameter to nice output.

PropMin

= Minimum allowable proportion of a group required to activate second level check., Usually 0.1 sufficient. Smaller values increase time function takes, but are needed for Smaller samples or very close components (try 0.05) If function gives error, this is first thing to try (make it smaller)

out_trim:

LIST of output of Gibbs sampler with the following components:

[[Zs]] estimated component (allocation) for each Y at each iteration (rows=n by columns=Iterations)

[[Mu]] matrix of mu samples (rows=iterations, columns = components),

[[Sig]] matrix variance (really Sig^2, but doesnt matter here anyway)

[Ps]] matrix weights

[[Loglike]] Loglikelihood (vector the length of iterations). Change if needed, currently used to find reference iteration (max(loglikelihood)).

[[SteadyScore]] vector of number of non-empty groups at each iteration (Not important, ok if not there)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
# if you have an unsteady number of posterior  non-empty groups:
# subset posterior samples first by this then unswitch.

# Simple example:


set.seed(88); library(ggplot2)
# Make simple mixture dataset,
	dat1<-c(rnorm(50, mean=1), rnorm(100, mean=5))
# Run gibbs sampler of choice
 run1<-Zmix_univ_tempered(dat1,isSim=FALSE,  iter=1000, k=10, alphas= c(30, 20, 10, 5, 3, 1, 0.5, 1/2^(c(2,3,4,5,6, 8, 10, 15, 20, 30))))
	run1<- trimit(run1, nEnd=500) # trim
	K0<-as.numeric(names(table(run1$SteadyScore))) # check out number of non-empty groups in iterations, if available

 # Undo the LABEL SWITCHING
	runUS<-QuickSwitch_allPars(run1, LineUpBy=1,PropMin=0.1 )

 # Check out results
	p1<-ggplot(data=runUS$Pars, aes(x=Iteration, y=P, group=factor(k), colour=factor(k))) + geom_line() + geom_point()+ggtitle("Unswitched Weights")
	p2<-ggplot(data=runUS$Pars, aes(x=Iteration, y=Mu, group=factor(k), colour=factor(k))) + geom_line() + geom_point()+ggtitle("Means")
	p3<-ggplot(data=runUS$Pars, aes(x=Iteration, y=Sig, group=factor(k), colour=factor(k))) + geom_line() + geom_point()+ggtitle("Variance")
	multiplot(p1,p2,p3)

zoevanhavre/Zmix_devVersion2 documentation built on May 4, 2019, 11:25 p.m.