Skip to content

Commit 46f94c0

Browse files
committed
improve handling of defined parameters in blavInspect for #98
1 parent f564412 commit 46f94c0

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: blavaan
22
Title: Bayesian Latent Variable Analysis
3-
Version: 0.5-9.1371
3+
Version: 0.5-9.1372
44
Authors@R: c(person(given = "Edgar", family = "Merkle",
55
role = c("aut", "cre"),
66
email = "merklee@missouri.edu",

R/blav_object_inspect.R

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,44 @@ blavInspect <- function(blavobject, what, ...) {
8484
if(add.labels) names(OUT) <- labs
8585
OUT
8686
} else if(what %in% c("mcmc", "draws", "samples", "hpd")){
87-
## add defined parameters to labels
87+
## add defined parameters to labels or, for stan, compute defined parameters
8888
pt <- blavobject@ParTable
89-
pt$free[pt$op == ":="] <- max(pt$free, na.rm = TRUE) + 1:sum(pt$op == ":=")
90-
labs <- lav_partable_labels(pt, type = "free")
91-
draws <- make_mcmc(blavobject@external$mcmcout)
92-
draws <- lapply(draws, function(x) mcmc(x[, idx, drop = FALSE]))
93-
draws <- mcmc.list(draws)
94-
if(add.labels) {
89+
if (!(blavobject@Options$target %in% c("stan", "cmdstan", "vb"))) {
90+
pt$free[pt$op == ":="] <- max(pt$free, na.rm = TRUE) + 1:sum(pt$op == ":=")
91+
labs <- lav_partable_labels(pt, type = "free")
92+
draws <- make_mcmc(blavobject@external$mcmcout)
93+
draws <- lapply(draws, function(x) mcmc(x[, idx, drop = FALSE]))
94+
draws <- mcmc.list(draws)
95+
if(add.labels) {
96+
for (i in 1:length(draws)) {
97+
colnames(draws[[i]]) <- labs
98+
}
99+
}
100+
} else {
101+
labs <- lav_partable_labels(pt, type = "free")
102+
draws <- make_mcmc(blavobject@external$mcmcout)
103+
draws <- lapply(draws, function(x) mcmc(x[, idx, drop = FALSE]))
104+
draws <- mcmc.list(draws)
105+
labs <- lav_partable_labels(pt, type = "free")
106+
if(add.labels) {
95107
for (i in 1:length(draws)) {
96-
colnames(draws[[i]]) <- labs
108+
colnames(draws[[i]]) <- labs
97109
}
110+
}
111+
112+
ndef <- sum(pt$op == ":=")
113+
if (ndef > 0) {
114+
defpt <- which(pt$op == ":=")
115+
for (j in 1:length(draws)) {
116+
for (i in defpt) {
117+
thisop <- pt$rhs[i]
118+
draws[[j]] <- cbind(draws[[j]], eval(parse(text = thisop), envir = as.data.frame(draws[[j]])))
119+
colnames(draws[[j]])[ncol(draws[[j]])] <- pt$lhs[i]
120+
}
121+
}
122+
}
98123
}
124+
99125
if(what == "hpd"){
100126
pct <- .95
101127
if("level" %in% dotNames) pct <- dotdotdot$level

0 commit comments

Comments
 (0)