Skip to content

Commit

Permalink
further tweaks to vignettes, run-extended
Browse files Browse the repository at this point in the history
  • Loading branch information
santikka committed Nov 15, 2024
1 parent 3a40410 commit 9127506
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
^_pkgdown.yml$
^doc$
^Meta$
^R/rebuild_vignettes\.R$
^vignettes/.*\.qmd\.orig$
^vignettes/rebuild_vignettes\.R$
^vignettes/*_files$
^vignettes/*\.R$
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Description: Easy-to-use and efficient interface for
multiple measurements per individual, time-varying and time-invariant
effects, and a wide range of discrete and continuous distributions.
Estimation of these dynamic multivariate panel models is carried out via
'Stan'. For an in-depth tutorial of the package, see
'Stan'. For an in-depth tutorial of the package, see
(Tikka and Helske, 2024) <doi:10.48550/arXiv.2302.01607>.
License: GPL (>= 3)
URL: https://docs.ropensci.org/dynamite/, https://github.com/ropensci/dynamite/
Expand Down Expand Up @@ -49,8 +49,7 @@ Suggests:
quarto,
testthat (>= 3.0.0),
tidyr
VignetteBuilder:
quarto
VignetteBuilder: quarto
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE, roclets = c ("namespace", "rd",
Expand Down
16 changes: 16 additions & 0 deletions R/rebuild_vignettes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Working directory needs to be /vignettes

rebuild_vignettes <- function() {
files <- c(
"dynamite_custom",
"dynamite_simulation"
)
for (i in seq_along(files)) {
e <- new.env()
knitr::knit(
input = paste0(files[i], ".qmd.orig"),
output = paste0(files[i], ".qmd"),
envir = e
)
}
}
Binary file modified vignettes/custommodelplot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 3 additions & 13 deletions vignettes/dynamite.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ format:
toc: true
number-sections: true
html-math-method: mathjax
pkgdown:
as_is: true
bibliography: dynamite.bib
vignette: >
%\VignetteIndexEntry{dynamite: An R Package for Dynamic Multivariate Panel Models}
Expand Down Expand Up @@ -325,7 +323,6 @@ We can obtain posterior samples or summary statistics of the model using the `as
#| out.width: "100%"
#| fig.width: 8
#| fig.height: 4
#| fig.dpi: 300
#| fig.align: "center"
#| fig.cap: "Posterior means and 90\\% posterior intervals of the time-invariant coefficients for the multivariate model."
library("ggplot2")
Expand Down Expand Up @@ -409,7 +406,6 @@ The resulting data frame `sumrb` is equal to the previous `sumr` (apart from sto
#| out.width: "100%"
#| fig.width: 8
#| fig.height: 3.5
#| fig.dpi: 300
#| fig.align: "center"
#| fig.cap: "Expected causal effects of interventions $do(b_5 = 0)$ and $do(b_5 = 1)$ on $g_t$. The black lines show the posterior means and the gray areas show 90\\% posterior intervals."
ggplot(sumr, aes(time, mean)) +
Expand Down Expand Up @@ -452,7 +448,6 @@ We can also plot the difference between the expected causal effects as shown in
#| out.width: "100%"
#| fig.width: 8
#| fig.height: 3.5
#| fig.dpi: 300
#| fig.align: "center"
#| fig.cap: "Difference between the expected causal effects $E(g_t | do(b_5 = 1)) - E(g_t | do(b_5 = 0))$. The black line shows the posterior mean and the gray area shows a 90\\% posterior interval."
ggplot(sumr_diff, aes(time, mean)) +
Expand Down Expand Up @@ -694,13 +689,11 @@ A `plot()` method is available for `"dynamiteformula"` objects that can be used
```{r}
#| label: fig-multichanneldagplot
#| fig.show: "hold"
#| out.width: "50%"
#| fig.cap: "DAGs for the multivariate model created using the `plot()` method for `\"dynamitefit\"` objects. The left panel shows the model structure including the auxiliary response variable `logp` while the right panel shows the model structure where the auxiliary variable is not included. The latter DAG is obtained via a functional projection where the parents of `logp` become the parents of the children of `logp` and `logp` is removed from the graph at each timepoint."
#| out.width: "100%"
#| fig.cap: "DAGs for the multivariate model created using the `plot()` method for `\"dynamitefit\"` objects. Panel (a) shows the model structure including the auxiliary response variable `logp` while panel (b) shows the model structure where the auxiliary variable is not included. The latter DAG is obtained via a functional projection where the parents of `logp` become the parents of the children of `logp` and `logp` is removed from the graph at each timepoint."
#| fig.subcap:
#| - ""
#| - ""
#| layout-ncol: 2
#| fig.dpi: 300
plot(multi_formula)
plot(multi_formula, show_auxiliary = FALSE)
```
Expand Down Expand Up @@ -877,7 +870,6 @@ For instance, @fig-parameterposteriorplot shows the posterior means and posterio
#| out.width: "100%"
#| fig.width: 10
#| fig.height: 4
#| fig.dpi: 300
#| fig.align: "center"
#| fig.cap: "Posterior means (black lines) and 90\\% posterior intervals (gray areas) for the time-varying coefficients for the response variable `y` in the `gaussian_example_fit` model. The panels from left to right show the time-varying intercept for `y`, the time-varying effect of `x` on `y`, and the time-varying effect of `lag(y)` (the previous time-point) on `y`."
plot(
Expand All @@ -894,7 +886,6 @@ While `plot_type = "default"` produces plots such as @fig-parameterposteriorplot
#| out.width: "100%"
#| fig.width: 9
#| fig.height: 4
#| fig.dpi: 300
#| fig.align: "center"
#| fig.cap: "Marginal posterior density and traceplot of the MCMC chains of the time-invariant regression coefficient `beta_y_z` of `z` for the response variable `y` in the `gaussian_example_fit` model."
plot(gaussian_example_fit, plot_type = "trace", types = "beta")
Expand Down Expand Up @@ -936,7 +927,6 @@ For example, we can obtain posterior predictive samples for the first 4 groups i
#| out.width: "100%"
#| fig.width: 8
#| fig.height: 5
#| fig.dpi: 300
#| fig.align: "center"
#| fig.cap: "Posterior predictive samples for the first 4 groups of the `gaussian_example` data. Lines in red represent the observed values."
pred <- predict(gaussian_example_fit, n_draws = 50)
Expand Down Expand Up @@ -1001,7 +991,7 @@ In the future, we plan to extend the capabilities of **dynamite** by adding supp
::: {#refs}
:::
# Details on latent factors {#sec-latentfactor .appendix}
# Details on latent factors {#sec-latentfactor .unnumbered}
Latent factor models with product terms $\lambda_i\psi_t$ are known to suffer from identifiability issues. For example, it is possible to multiply each $\lambda_i$ by some constant $c$ while simultaneously multiplying $\psi_i, t=1,\ldots, T$ with the reciprocal of the same constant, leading to the same likelihood value as the original model. In case of multiple latent factors and (vector) autoregressive process on $\psi_t$, @bai2015 discuss two alternative identifiability constraints, which in our single factor model translate to fixing $\lambda_i = 1$ for some $i$, or constraining $\lambda_i > 0$ for some $i$, with an additional constraint that the standard deviation of the noise term of $\psi_t$ is 1. In both cases, we need to decide which individual is used as a reference for the constrained $\lambda_i$. This choice can lead to computational issues if the true value of $\lambda_i$ is not compatible with these constrains (e.g., the true value is zero). Instead, we define the constraints via the mean of $\lambda$.
Expand Down
8 changes: 3 additions & 5 deletions vignettes/dynamite_custom.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ format:
theme: none
minimal: true
embed-resources: true
pkgdown:
as_is: true
vignette: >
%\VignetteIndexEntry{Model customization and non-MCMC estimation with dynamite}
%\VignetteEngine{quarto::html}
Expand Down Expand Up @@ -70,7 +68,7 @@ It is important to note that in order to use the post-processing functions of `d
plot(fit, type = "nu", n_params = 20)
```

![](nuplot-1.png)
<img src="nuplot-1.png" width="100%" />

perform leave-one-out cross-validation:

Expand Down Expand Up @@ -107,7 +105,7 @@ ggplot2::ggplot(pp, ggplot2::aes(time, y_new, group = .draw)) +
ggplot2::theme_bw()
```

![](custommodelplot-1.png)
<img src="custommodelplot-1.png" width="100%" />

# Using non-MCMC algorithms in `dynamite`

Expand Down Expand Up @@ -160,4 +158,4 @@ ggplot2::ggplot(pp2, ggplot2::aes(time, y_new, group = .draw)) +
ggplot2::theme_bw()
```

![](vbplot-1.png)
<img src="vbplot-1.png" width="100%" />
8 changes: 3 additions & 5 deletions vignettes/dynamite_custom.qmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ format:
theme: none
minimal: true
embed-resources: true
pkgdown:
as_is: true
vignette: >
%\VignetteIndexEntry{Model customization and non-MCMC estimation with dynamite}
%\VignetteEngine{quarto::html}
Expand Down Expand Up @@ -98,7 +96,7 @@ It is important to note that in order to use the post-processing functions of `d
```{r}
#| label: nuplot
#| fig.cap: ""
#| fig.dpi: 300
#| out.width: "100%"
plot(fit, type = "nu", n_params = 20)
```

Expand All @@ -111,7 +109,7 @@ and compute predictions:
```{r}
#| label: custommodelplot
#| fig.cap: ""
#| fig.dpi: 300
#| out.width: "100%"
newdata <- data.frame(
time = 1:30,
id = 51,
Expand Down Expand Up @@ -172,7 +170,7 @@ summary(fit_vb_dynamite, types = "beta")
```{r}
#| label: vbplot
#| fig.cap: ""
#| fig.dpi: 300
#| out.width: "100%"
pp2 <- predict(
fit_vb_dynamite, newdata = newdata, new_levels = "original", n_draws = 50
)
Expand Down
2 changes: 0 additions & 2 deletions vignettes/dynamite_priors.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ format:
minimal: true
embed-resources: true
html-math-method: mathjax
pkgdown:
as_is: true
vignette: >
%\VignetteIndexEntry{Priors for dynamic multivariate panel models}
%\VignetteEngine{quarto::html}
Expand Down
3 changes: 1 addition & 2 deletions vignettes/dynamite_simulation.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ format:
theme: none
minimal: true
embed-resources: true
pkgdown:
as_is: true
vignette: >
%\VignetteIndexEntry{Simulating data from a dynamic multivariate panel model}
%\VignetteEngine{quarto::html}
Expand Down Expand Up @@ -181,6 +179,7 @@ Again, we apply the `get_parameter_dims()` function to get the model parameters

``` r
get_parameter_dims(x = f, data = d, time = "time", group = "id")
#> recompiling to avoid crashing R session
#> $omega_y
#> [1] 2 10
#>
Expand Down
3 changes: 0 additions & 3 deletions vignettes/dynamite_simulation.qmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ format:
theme: none
minimal: true
embed-resources: true
pkgdown:
as_is: true
vignette: >
%\VignetteIndexEntry{Simulating data from a dynamic multivariate panel model}
%\VignetteEngine{quarto::html}
Expand Down Expand Up @@ -249,7 +247,6 @@ We can plot individual trajectories of `y` over time to visualize the data.
#| fig.width: 7
#| fig.height: 3.5
#| fig.cap: ""
#| fig.dpi: 300
#| out.width: "100%"
library("ggplot2")
sim |>
Expand Down
Binary file modified vignettes/gaussianplot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/nuplot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/vbplot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9127506

Please sign in to comment.