The code in that thread is outdated since this feature is now officially part of unmarked. I wrote up a quick example below using the built-in frogs dataset which should get you started. See also ?predict, specifically the final section in the help file related to unmarkedRanef objects.
library(unmarked)
data(frogs)
umf <- formatMult(masspcru)[1:200,]
# Make fake categorical covariate "x" with two levels a and b
set.seed(123)
siteCovs(umf) <- data.frame(x = factor(sample(letters[1:2],numSites(umf), replace=T)))
head(siteCovs(umf))
# Fit model with effect of x on initial occupancy
(fit <- colext(~x, ~1, ~1, ~1, umf))
# Get estimates of latent occupancy state (z) at each site using empirical bayes
r <- ranef(fit)
# Now we can generate an empirical bayes posterior predictive distribution
# for mean occupancy in each group a and b
# Function that takes argument zmat, which will be a matrix M x T of latent occupancy
# zmat will be 200 x 7 in this case since there are 200 sites and 7 primary periods
split_z_by_x <- function(zmat){
T <- ncol(zmat) # primary periods
x <- siteCovs(umf)$x
z_a <- zmat[x=="a",] # sites where x == "a"
z_b <- zmat[x=="b",] # sites where x == "b"
#output object: 2 rows (for categories a and b) and 7 columns (for 7 primary periods)
out <- matrix(NA, nrow=2, ncol=T)
out[1,] <- colMeans(z_a) # mean of posterior of z for sites with x == "a"
out[2,] <- colMeans(z_b) # same for b
rownames(out) <- c("a","b")
colnames(out) <- 1:T
out
}
# Use predict method for unmarkedRanef, supplying the function, and get 100 samples
pr <- predict(r, func=split_z_by_x, nsim=100)
# Output will be 2 groups x 7 periods x 100 simulations
dim(pr)
#Get mean of posterior for each group and primary period
(post_mean <- apply(pr, c(1,2), mean))
# Get 95% CI for each group and primary period
post_ci <- apply(pr, c(1,2), quantile, c(0.025,0.975))
# Plot means and 95% CIs
plot(x=1:7-0.1, y=post_mean[1,], col='blue', type='b', ylim=c(0.82,1),
ylab="Proportion occupied", xlab="Primary period")
for (i in 1:7){
segments(i-0.1, post_ci[1,1,i], i-0.1, post_ci[2,1,i], col='blue')
}
lines(1:7+0.1, post_mean[2,], type='b', col='red')
for (i in 1:7){
segments(i+0.1, post_ci[1,2,i], i+0.1, post_ci[2,2,i], col='red')
}
legend("bottomright", col=c("blue","red"), legend=c("a","b"),pch=21,
title="Value of x")