| Title: | Conjugate Power Priors for Bayesian Analysis of Normal Data |
|---|---|
| Description: | Implements conjugate power priors for efficient Bayesian analysis of normal data. Power priors allow principled incorporation of historical information while controlling the degree of borrowing through a discounting parameter (Ibrahim and Chen (2000) <doi:10.1214/ss/1009212519>). This package provides closed-form conjugate representations for both univariate and multivariate normal data using Normal-Inverse-Chi-squared and Normal-Inverse-Wishart distributions, eliminating the need for MCMC sampling. The conjugate framework builds upon standard Bayesian methods described in Gelman et al. (2013, ISBN:978-1439840955). |
| Authors: | Yusuke Yamaguchi [aut, cre], Yifei Huang [aut] |
| Maintainer: | Yusuke Yamaguchi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-05-15 10:16:08 UTC |
| Source: | https://github.com/cran/powerprior |
Computes the Bayes factor comparing two models with different discounting parameters.
bayes_factor( historical_data, current_data, a0_1, a0_2 = 0, multivariate = FALSE, ... )bayes_factor( historical_data, current_data, a0_1, a0_2 = 0, multivariate = FALSE, ... )
historical_data |
Historical data |
current_data |
Current data |
a0_1 |
First discounting parameter |
a0_2 |
Second discounting parameter (default: 0) |
multivariate |
Logical indicating multivariate data (default: FALSE) |
... |
Additional arguments passed to powerprior functions |
The Bayes factor compares the marginal likelihoods under two different discounting parameters. BF > 1 favors a0_1, BF < 1 favors a0_2.
Note: This is a simple approximation using the observed data likelihood evaluated at posterior means.
Bayes factor (BF_12 = P(data|a0_1) / P(data|a0_2))
set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) # Compare moderate borrowing (0.5) vs no borrowing (0) bf <- bayes_factor(historical, current, a0_1 = 0.5, a0_2 = 0) cat("Bayes Factor:", bf, "\n")set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) # Compare moderate borrowing (0.5) vs no borrowing (0) bf <- bayes_factor(historical, current, a0_1 = 0.5, a0_2 = 0) cat("Bayes Factor:", bf, "\n")
Performs a simple compatibility check between historical and current data to help guide the choice of discounting parameter.
check_compatibility(historical_data, current_data, alpha = 0.05)check_compatibility(historical_data, current_data, alpha = 0.05)
historical_data |
Historical data |
current_data |
Current data |
alpha |
Significance level for compatibility test (default: 0.05) |
A list with compatibility test results
set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) compatibility <- check_compatibility(historical, current) print(compatibility)set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) compatibility <- check_compatibility(historical, current) print(compatibility)
Computes posteriors for multiple values of a0 to facilitate sensitivity analysis.
compare_discounting( historical_data, current_data, a0_values = seq(0, 1, by = 0.1), multivariate = FALSE, n_samples = 1000, ... )compare_discounting( historical_data, current_data, a0_values = seq(0, 1, by = 0.1), multivariate = FALSE, n_samples = 1000, ... )
historical_data |
Historical data (vector for univariate, matrix for multivariate) |
current_data |
Current data (vector for univariate, matrix for multivariate) |
a0_values |
Vector of discounting parameters to compare |
multivariate |
Logical indicating if data is multivariate (default: FALSE) |
n_samples |
Number of posterior samples per a0 value (default: 1000) |
... |
Additional arguments passed to powerprior functions |
A data frame with summary statistics for each a0 value
set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) comparison <- compare_discounting( historical, current, a0_values = c(0, 0.25, 0.5, 0.75, 1.0) ) print(comparison)set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) comparison <- compare_discounting( historical, current, a0_values = c(0, 0.25, 0.5, 0.75, 1.0) ) print(comparison)
Computes the effective sample size from historical data given a discounting parameter.
effective_sample_size(m, a0)effective_sample_size(m, a0)
m |
Sample size of historical data |
a0 |
Discounting parameter |
Effective sample size (numeric)
# With 100 historical observations and 50% discounting effective_sample_size(100, a0 = 0.5) # Returns 50# With 100 historical observations and 50% discounting effective_sample_size(100, a0 = 0.5) # Returns 50
Creates a plot showing how posterior estimates vary with the discounting parameter.
plot_sensitivity(comparison_results, parameter = "mu", dimension = 1)plot_sensitivity(comparison_results, parameter = "mu", dimension = 1)
comparison_results |
Output from compare_discounting() |
parameter |
Name of parameter to plot (default: "mu") |
dimension |
For multivariate case, which dimension to plot (default: 1) |
A ggplot2 object (if ggplot2 is available) or base R plot
set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) comparison <- compare_discounting(historical, current) plot_sensitivity(comparison)set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) comparison <- compare_discounting(historical, current) plot_sensitivity(comparison)
Updates a multivariate power prior with current trial data to obtain the posterior distribution.
posterior_multivariate(powerprior, current_data)posterior_multivariate(powerprior, current_data)
powerprior |
Object of class "powerprior_multivariate" from powerprior_multivariate() |
current_data |
Matrix or data frame of current observations. Must have the same number of columns as the historical data used to create the power prior. Rows represent observations, columns represent variables. |
Given a power prior P(, | X, ) and new current data Y, the posterior
distribution combines both through Bayes' theorem. The conjugate NIW structure
ensures the posterior remains a NIW distribution with closed-form parameters.
The updating mechanism mirrors the univariate case but extends to handle the
covariance matrix structure. The scale matrix incorporates:
Discounted historical sum of squares and crossproducts ( * S_x)
Prior scale information (, if using informative prior)
Current data sum of squares and crossproducts (S_y)
Disagreement terms that increase uncertainty when historical and current means differ
The posterior mean vector is a precision-weighted average of the historical mean, prior mean (if provided), and current mean, allowing for flexible incorporation of multiple information sources with different precisions.
A list of class "posterior_multivariate" containing:
mu_star |
Posterior mean vector |
kappa_star |
Posterior precision parameter |
nu_star |
Posterior degrees of freedom |
Lambda_star |
Posterior scale matrix |
n |
Sample size of current data |
p |
Dimension of data |
ybar |
Sample mean vector of current data |
Sy |
Sum of squares and crossproducts matrix for current data |
powerprior |
Original power prior object |
library(MASS) Sigma_true <- matrix(c(4, 1, 1, 2), 2, 2) historical <- mvrnorm(50, mu = c(10, 5), Sigma = Sigma_true) current <- mvrnorm(30, mu = c(10.5, 5.2), Sigma = Sigma_true) # With vague prior pp <- powerprior_multivariate(historical, a0 = 0.5) posterior <- posterior_multivariate(pp, current) print(posterior) # With informative prior pp_inform <- powerprior_multivariate( historical, a0 = 0.5, mu0 = c(10, 5), kappa0 = 1, nu0 = 5, Lambda0 = diag(2) ) posterior_inform <- posterior_multivariate(pp_inform, current) print(posterior_inform)library(MASS) Sigma_true <- matrix(c(4, 1, 1, 2), 2, 2) historical <- mvrnorm(50, mu = c(10, 5), Sigma = Sigma_true) current <- mvrnorm(30, mu = c(10.5, 5.2), Sigma = Sigma_true) # With vague prior pp <- powerprior_multivariate(historical, a0 = 0.5) posterior <- posterior_multivariate(pp, current) print(posterior) # With informative prior pp_inform <- powerprior_multivariate( historical, a0 = 0.5, mu0 = c(10, 5), kappa0 = 1, nu0 = 5, Lambda0 = diag(2) ) posterior_inform <- posterior_multivariate(pp_inform, current) print(posterior_inform)
Provides comprehensive posterior summary statistics.
posterior_summary(posterior, n_samples = 10000, prob = 0.95)posterior_summary(posterior, n_samples = 10000, prob = 0.95)
posterior |
Posterior object from posterior_univariate() or posterior_multivariate() |
n_samples |
Number of samples to draw (default: 10000) |
prob |
Probability for credible intervals (default: 0.95) |
A list with posterior summaries
set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) pp <- powerprior_univariate(historical, a0 = 0.5) posterior <- posterior_univariate(pp, current) summary <- posterior_summary(posterior) print(summary)set.seed(123) historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) pp <- powerprior_univariate(historical, a0 = 0.5) posterior <- posterior_univariate(pp, current) summary <- posterior_summary(posterior) print(summary)
Updates a power prior with current trial data to obtain the posterior distribution.
posterior_univariate(powerprior, current_data)posterior_univariate(powerprior, current_data)
powerprior |
Object of class "powerprior_univariate" from powerprior_univariate() |
current_data |
Numeric vector of current trial observations. Must contain at least 2 observations. Missing values (NAs) are automatically removed. |
Given a power prior distribution P(, | x, ) and new current data y,
the posterior distribution is computed by combining the power prior with
the likelihood of the current data using Bayes' theorem.
The conjugate structure ensures the posterior remains a NIX distribution. For both informative and vague initial priors, the updating follows standard conjugate rules, leveraging the fact that both the power prior and likelihood are in the NIX family.
This eliminates the computational burden of MCMC and allows direct posterior
inference and sampling (see sample_posterior_univariate).
A list of class "posterior_univariate" containing:
mu_star |
Posterior mean parameter |
kappa_star |
Posterior precision parameter |
nu_star |
Posterior degrees of freedom |
sigma2_star |
Posterior scale parameter |
n |
Sample size of current data |
ybar |
Sample mean of current data |
Sy |
Sum of squared deviations of current data |
powerprior |
Original power prior object |
# Generate data historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) # Compute power prior and posterior pp <- powerprior_univariate(historical, a0 = 0.5) posterior <- posterior_univariate(pp, current) print(posterior) # With informative prior pp_inform <- powerprior_univariate( historical, a0 = 0.5, mu0 = 10, kappa0 = 1, nu0 = 3, sigma2_0 = 4 ) posterior_inform <- posterior_univariate(pp_inform, current) print(posterior_inform)# Generate data historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) # Compute power prior and posterior pp <- powerprior_univariate(historical, a0 = 0.5) posterior <- posterior_univariate(pp, current) print(posterior) # With informative prior pp_inform <- powerprior_univariate( historical, a0 = 0.5, mu0 = 10, kappa0 = 1, nu0 = 3, sigma2_0 = 4 ) posterior_inform <- posterior_univariate(pp_inform, current) print(posterior_inform)
This function launches the Shiny application.
powerprior_launch_shinyapp()powerprior_launch_shinyapp()
No return value, called for side effects (launches Shiny app)
Computes the power prior for multivariate normal data using a conjugate Normal-Inverse-Wishart (NIW) representation.
powerprior_multivariate( historical_data, a0, mu0 = NULL, kappa0 = NULL, nu0 = NULL, Lambda0 = NULL )powerprior_multivariate( historical_data, a0, mu0 = NULL, kappa0 = NULL, nu0 = NULL, Lambda0 = NULL )
historical_data |
Matrix or data frame where rows are observations and columns are variables. Must have at least 2 rows and 2 columns. Missing values are not automatically handled; rows with any NA values should be removed before calling this function. |
a0 |
Discounting parameter in
|
mu0 |
Prior mean vector of length p (number of variables). Only used when specifying an informative initial prior. If NULL, a vague (non-informative) prior is used. Represents the prior belief about the center of the multivariate distribution across all variables. |
kappa0 |
Prior precision parameter (scalar). Controls the concentration of the prior around mu0. Only used for informative priors. If NULL, vague prior is applied. Interpreted as the "prior effective sample size" for the mean estimate. Higher values indicate greater prior confidence. |
nu0 |
Prior degrees of freedom for the inverse Wishart distribution. Only used for informative priors. If NULL, vague prior is applied. Must satisfy nu0 >= p. Higher values indicate greater prior confidence in the covariance structure. Typical values range from p to 2p for informative priors. |
Lambda0 |
Prior scale matrix (p x p, symmetric positive definite). Only used for informative priors. If NULL, vague prior is applied. Represents the prior belief about the covariance structure. Larger values correspond to greater prior uncertainty about variable spread and relationships. If Lambda0 is provided, must be symmetric and positive definite. |
The power prior framework extends naturally to multivariate normal data when
the mean vector and covariance matrix are jointly unknown. This is essential
for modern applications involving multiple correlated endpoints, such as clinical
trials measuring multiple health outcomes simultaneously.
The power prior for multivariate data is defined analogously to the univariate case:
where:
is the m x p historical data matrix
is the p-dimensional mean vector
is the p x p covariance matrix
is the discounting parameter
The key advantage of this implementation is that power priors applied to multivariate normal data with Normal-Inverse-Wishart (NIW) conjugate initial priors remain in closed form as NIW distributions. This preserves:
Exact posterior computation without approximation
Closed-form parameter updates and marginalization
Efficient sampling from standard distributions
Computational tractability for high-dimensional problems (within reason)
Natural joint modeling of correlations via the covariance structure
For practitioners, this means you can incorporate historical information on multiple correlated endpoints while maintaining full Bayesian rigor and computational efficiency.
Informative Initial Prior (all of mu0, kappa0, nu0, Lambda0 provided):
Uses a Normal-Inverse-Wishart (NIW) conjugate prior with hierarchical structure:
The power prior parameters are updated:
where is sample size, is the sample mean vector,
and is the sum of squares and crossproducts matrix.
Vague (Non-informative) Initial Prior (all of mu0, kappa0, nu0, Lambda0 are NULL):
Uses a non-informative prior that places minimal constraints on parameters.
The power prior parameters simplify to:
The vague prior is recommended when there is minimal prior information, or when you want the analysis driven primarily by the historical data.
Effective Sample Size (): Quantifies how much "effective historical data"
has been incorporated. The formula shows the
discounted historical sample size combined with prior precision. This controls the
concentration of the posterior distribution for the mean vector.
Mean Vector (): The updated mean is a precision-weighted average:
This naturally balances the historical sample mean and prior mean, with weights
proportional to their respective precisions.
Degrees of Freedom (): Controls tail behavior and the concentration of the
Wishart distribution. Higher values indicate greater confidence in the covariance
estimate. The minimum value needed is p (number of variables) for the Inverse-Wishart
to be well-defined; is always maintained.
Scale Matrix (): The p x p scale matrix that captures both the dispersion
of individual variables and their correlations. The term
adds uncertainty when the historical
mean conflicts with the prior mean, naturally reflecting disagreement between data sources.
Dimension: This implementation works efficiently for moderate-dimensional problems
(typically p 10). For higher dimensions, consider data reduction techniques or
structural assumptions on the covariance matrix.
Prior Specification: When specifying Lambda0, ensure it is symmetric positive definite. A simple approach is to use a multiple of the identity matrix (e.g., Lambda0 = diag(p)) for a weakly informative prior.
Discounting: The same a0 parameter is used for all variables and their correlations. If you suspect differential reliability of historical information across variables, consider multiple analyses with different a0 values and sensitivity analyses.
A list of class "powerprior_multivariate" containing:
mu_n |
Updated mean vector (p-dimensional) |
kappa_n |
Updated precision parameter (effective sample size) |
nu_n |
Updated degrees of freedom |
Lambda_n |
Updated scale matrix (p x p) |
a0 |
Discounting parameter used |
m |
Sample size of historical data |
p |
Dimension (number of variables) of the data |
xbar |
Sample mean vector of historical data |
Sx |
Sum of squares and crossproducts matrix |
vague_prior |
Logical indicating if vague prior was used |
mu0 |
Prior mean vector (if informative prior used) |
kappa0 |
Prior precision parameter (if informative prior used) |
nu0 |
Prior degrees of freedom (if informative prior used) |
Lambda0 |
Prior scale matrix (if informative prior used) |
Huang, Y., Yamaguchi, Y., Homma, G., Maruo, K., & Takeda, K. (2024). "Conjugate Representation of Power Priors for Efficient Bayesian Analysis of Normal Data." (unpublished).
Ibrahim, J. G., & Chen, M. H. (2000). "Power prior distributions for regression models." Statistical Science, 15(1), 46-60.
Gelman, A., Carlin, J. B., Stern, H. S., et al. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.
# Generate multivariate historical data with correlation library(MASS) Sigma_true <- matrix(c(4, 1, 1, 2), 2, 2) historical <- mvrnorm(50, mu = c(10, 5), Sigma = Sigma_true) # Compute power prior with vague prior pp <- powerprior_multivariate(historical, a0 = 0.5) print(pp) # Compute power prior with informative prior pp_inform <- powerprior_multivariate( historical, a0 = 0.5, mu0 = c(10, 5), kappa0 = 1, nu0 = 5, Lambda0 = diag(2) ) print(pp_inform)# Generate multivariate historical data with correlation library(MASS) Sigma_true <- matrix(c(4, 1, 1, 2), 2, 2) historical <- mvrnorm(50, mu = c(10, 5), Sigma = Sigma_true) # Compute power prior with vague prior pp <- powerprior_multivariate(historical, a0 = 0.5) print(pp) # Compute power prior with informative prior pp_inform <- powerprior_multivariate( historical, a0 = 0.5, mu0 = c(10, 5), kappa0 = 1, nu0 = 5, Lambda0 = diag(2) ) print(pp_inform)
Computes the power prior for univariate normal data using a conjugate Normal-Inverse-Chi-squared (NIX) representation.
powerprior_univariate( historical_data, a0, mu0 = NULL, kappa0 = NULL, nu0 = NULL, sigma2_0 = NULL )powerprior_univariate( historical_data, a0, mu0 = NULL, kappa0 = NULL, nu0 = NULL, sigma2_0 = NULL )
historical_data |
Numeric vector of historical observations. Must contain at least 2 observations. Missing values (NAs) are automatically removed. The function assumes data are independent and identically distributed from a normal distribution with unknown mean and variance. |
a0 |
Discounting parameter in
The parameter acts as a multiplicative discount on the historical likelihood. |
mu0 |
Prior mean for the normal distribution of the mean parameter. Only used when specifying an informative initial prior. If NULL, a vague (non-informative) prior is used instead. Represents the prior belief about the center of the data distribution. |
kappa0 |
Prior precision parameter (inverse of scaled variance) for the mean. Only used for informative priors. If NULL, vague prior is applied. Higher values indicate greater prior confidence in mu0. Interpreted as the "prior sample size" contributing to the mean estimate. For example, kappa0 = 1 is roughly equivalent to one prior observation. |
nu0 |
Prior degrees of freedom for the inverse chi-squared distribution of variance. Only used for informative priors. If NULL, vague prior is applied. Higher values indicate greater prior confidence in sigma2_0. Typically set to small positive values (e.g., 1-5) for weakly informative priors. |
sigma2_0 |
Prior scale parameter for the inverse chi-squared distribution. Only used for informative priors. If NULL, vague prior is applied. Represents the prior belief about the scale (spread) of the data. Should be on the variance scale (not standard deviation). |
Power priors provide a principled framework for incorporating historical information in Bayesian analysis while controlling the degree of borrowing through a discounting parameter. The power prior is defined as:
where
is the likelihood function evaluated on historical data
is the discounting parameter
is the initial prior distribution
This approach is especially valuable in clinical trial design, where historical trial data can improve statistical efficiency while maintaining appropriate skepticism about whether historical and current populations are similar.
Typically, power priors result in non-closed-form posterior distributions requiring computationally expensive Markov Chain Monte Carlo (MCMC) sampling, especially problematic for simulation studies requiring thousands of posterior samples.
This implementation exploits a key mathematical result: when applying power priors to normal data with conjugate initial priors (Normal-Inverse-Chi-squared), the resulting power prior and posterior distributions remain in closed form as NIX distributions. This allows:
Direct computation without MCMC approximation
Closed-form parameter updates
Exact posterior sampling from standard distributions
Efficient sensitivity analyses across different a0 values
Informative Initial Prior (all of mu0, kappa0, nu0, sigma2_0 provided):
Uses a Normal-Inverse-Chi-squared (NIX) conjugate prior with structure:
The power prior parameters are updated:
where is the sample size, is the sample mean, and
is the sum of squared deviations.
Vague (Non-informative) Initial Prior (all of mu0, kappa0, nu0, sigma2_0 are NULL):
Uses a non-informative prior that is
locally uniform in log-space. This places minimal prior constraints on the parameters.
The power prior parameters simplify to:
The vague prior approach is recommended when there is no strong prior information, or when you want the analysis to be primarily driven by the (discounted) historical data.
Effective Sample Size (kappa_n): The updated precision parameter can be interpreted
as an effective sample size. Higher values indicate more concentrated posterior distributions
for the mean. The formula shows that the historical
sample size is discounted by before combining with the prior's
contribution .
Posterior Mean (mu_n): A weighted average of the historical sample mean ,
prior mean , and their relative precision:
This naturally interpolates between the data and prior, with weights determined by
their precision.
Degrees of Freedom (nu_n): Controls the tail behavior of posterior distributions derived from the NIX. Higher values produce lighter tails, indicating greater confidence.
Scale Parameter (sigma2_n): Estimates the variability in the data. The term involving
captures disagreement between the historical mean and prior mean,
which increases uncertainty in variance estimation when they conflict.
A list of class "powerprior_univariate" containing:
mu_n |
Updated posterior mean parameter from power prior |
kappa_n |
Updated posterior precision parameter (effective sample size) |
nu_n |
Updated posterior degrees of freedom |
sigma2_n |
Updated posterior scale parameter (variance scale) |
a0 |
Discounting parameter used |
m |
Sample size of historical data |
xbar |
Sample mean of historical data |
Sx |
Sum of squared deviations of historical data |
vague_prior |
Logical indicating if vague prior was used |
mu0 |
Prior mean (if informative prior used) |
kappa0 |
Prior precision (if informative prior used) |
nu0 |
Prior degrees of freedom (if informative prior used) |
sigma2_0 |
Prior scale parameter (if informative prior used) |
Huang, Y., Yamaguchi, Y., Homma, G., Maruo, K., & Takeda, K. (2024). "Conjugate Representation of Power Priors for Efficient Bayesian Analysis of Normal Data." Statistical Science (unpublished).
Ibrahim, J. G., & Chen, M. H. (2000). "Power prior distributions for regression models." Statistical Science, 15(1), 46-60.
Gelman, A., Carlin, J. B., Stern, H. S., et al. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.
# Generate historical data historical <- rnorm(50, mean = 10, sd = 2) # Compute power prior with informative initial prior pp_inform <- powerprior_univariate( historical_data = historical, a0 = 0.5, mu0 = 10, kappa0 = 1, nu0 = 3, sigma2_0 = 4 ) print(pp_inform) # Compute power prior with vague prior (no prior specification) pp_vague <- powerprior_univariate( historical_data = historical, a0 = 0.5 ) print(pp_vague) # Compare different discounting levels pp_weak <- powerprior_univariate(historical_data = historical, a0 = 0.1) pp_strong <- powerprior_univariate(historical_data = historical, a0 = 0.9) cat("Strong discounting (a0=0.1) - kappa_n:", pp_weak$kappa_n, "\n") cat("Weak discounting (a0=0.9) - kappa_n:", pp_strong$kappa_n, "\n")# Generate historical data historical <- rnorm(50, mean = 10, sd = 2) # Compute power prior with informative initial prior pp_inform <- powerprior_univariate( historical_data = historical, a0 = 0.5, mu0 = 10, kappa0 = 1, nu0 = 3, sigma2_0 = 4 ) print(pp_inform) # Compute power prior with vague prior (no prior specification) pp_vague <- powerprior_univariate( historical_data = historical, a0 = 0.5 ) print(pp_vague) # Compare different discounting levels pp_weak <- powerprior_univariate(historical_data = historical, a0 = 0.1) pp_strong <- powerprior_univariate(historical_data = historical, a0 = 0.9) cat("Strong discounting (a0=0.1) - kappa_n:", pp_weak$kappa_n, "\n") cat("Weak discounting (a0=0.9) - kappa_n:", pp_strong$kappa_n, "\n")
Print method for compatibility_check
## S3 method for class 'compatibility_check' print(x, digits = 4, ...)## S3 method for class 'compatibility_check' print(x, digits = 4, ...)
x |
Object of class "compatibility_check" |
digits |
Number of digits to round to |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Print method for posterior_multivariate
## S3 method for class 'posterior_multivariate' print(x, ...)## S3 method for class 'posterior_multivariate' print(x, ...)
x |
Object of class "posterior_multivariate" |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Print method for posterior_univariate
## S3 method for class 'posterior_univariate' print(x, ...)## S3 method for class 'posterior_univariate' print(x, ...)
x |
Object of class "posterior_univariate" |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Print method for powerprior_comparison
## S3 method for class 'powerprior_comparison' print(x, digits = 4, ...)## S3 method for class 'powerprior_comparison' print(x, digits = 4, ...)
x |
Object of class "powerprior_comparison" |
digits |
Number of digits to round to |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Print method for powerprior_multivariate
## S3 method for class 'powerprior_multivariate' print(x, ...)## S3 method for class 'powerprior_multivariate' print(x, ...)
x |
Object of class "powerprior_multivariate" |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Print method for powerprior_summary
## S3 method for class 'powerprior_summary' print(x, digits = 4, ...)## S3 method for class 'powerprior_summary' print(x, digits = 4, ...)
x |
Object of class "powerprior_summary" |
digits |
Number of digits to round to |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Print method for powerprior_univariate
## S3 method for class 'powerprior_univariate' print(x, ...)## S3 method for class 'powerprior_univariate' print(x, ...)
x |
Object of class "powerprior_univariate" |
... |
Additional arguments |
Invisibly returns the input object (for method chaining)
Generates samples from the multivariate posterior distribution using exact closed-form expressions from the Normal-Inverse-Wishart conjugate family.
sample_posterior_multivariate(posterior, n_samples = 1000, marginal = FALSE)sample_posterior_multivariate(posterior, n_samples = 1000, marginal = FALSE)
posterior |
Object of class "posterior_multivariate" from posterior_multivariate() |
n_samples |
Number of samples to generate (default: 1000). For large n_samples or high dimensions, computation time increases. |
marginal |
Logical. If TRUE, samples only |
Joint Sampling (marginal=FALSE):
Implements the standard hierarchical sampling algorithm for the NIW distribution:
Draw ~ Inverse-Wishart(, )
Draw | ~ N_p(, / )
This produces samples from the joint distribution P(, | Y, X, ) and captures
both uncertainty in the mean and uncertainty in the covariance structure, including
their dependence.
Marginal Sampling (marginal=TRUE):
Uses the marginal t-distribution of the mean:
This is computationally faster and useful when you primarily care about inference on the mean vector, marginalizing over uncertainty in the covariance.
If marginal=FALSE, a list with components:
mu |
n_samples x p matrix of mean samples |
Sigma |
p x p x n_samples array of covariance samples |
If marginal=TRUE, an n_samples x p matrix of mean samples.
library(MASS) Sigma_true <- matrix(c(4, 1, 1, 2), 2, 2) historical <- mvrnorm(50, mu = c(10, 5), Sigma = Sigma_true) current <- mvrnorm(30, mu = c(10.5, 5.2), Sigma = Sigma_true) pp <- powerprior_multivariate(historical, a0 = 0.5) posterior <- posterior_multivariate(pp, current) # Sample from joint distribution (covariance included) samples_joint <- sample_posterior_multivariate(posterior, n_samples = 100) cat("Mean samples shape:", dim(samples_joint$mu), "\n") cat("Covariance samples shape:", dim(samples_joint$Sigma), "\n") # Sample only means (faster) samples_marginal <- sample_posterior_multivariate(posterior, n_samples = 100, marginal = TRUE)library(MASS) Sigma_true <- matrix(c(4, 1, 1, 2), 2, 2) historical <- mvrnorm(50, mu = c(10, 5), Sigma = Sigma_true) current <- mvrnorm(30, mu = c(10.5, 5.2), Sigma = Sigma_true) pp <- powerprior_multivariate(historical, a0 = 0.5) posterior <- posterior_multivariate(pp, current) # Sample from joint distribution (covariance included) samples_joint <- sample_posterior_multivariate(posterior, n_samples = 100) cat("Mean samples shape:", dim(samples_joint$mu), "\n") cat("Covariance samples shape:", dim(samples_joint$Sigma), "\n") # Sample only means (faster) samples_marginal <- sample_posterior_multivariate(posterior, n_samples = 100, marginal = TRUE)
Generates samples from the posterior distribution using the conjugate representation. Can sample the joint distribution (mu, sigma2) or just the marginal distribution of mu.
sample_posterior_univariate(posterior, n_samples = 1000, marginal = FALSE)sample_posterior_univariate(posterior, n_samples = 1000, marginal = FALSE)
posterior |
Object of class "posterior_univariate" from posterior_univariate() |
n_samples |
Number of samples to generate (default: 1000) |
marginal |
Logical. If TRUE, samples only mu from t-distribution. If FALSE, samples joint (mu, sigma2) from NIX distribution (default: FALSE) |
If marginal=FALSE, a matrix with columns "mu" and "sigma2". If marginal=TRUE, a vector of mu samples.
# Generate data and compute posterior historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) pp <- powerprior_univariate(historical, a0 = 0.5) posterior <- posterior_univariate(pp, current) # Sample from joint distribution samples_joint <- sample_posterior_univariate(posterior, n_samples = 1000) # Sample from marginal distribution of mu samples_marginal <- sample_posterior_univariate(posterior, n_samples = 1000, marginal = TRUE)# Generate data and compute posterior historical <- rnorm(50, mean = 10, sd = 2) current <- rnorm(30, mean = 10.5, sd = 2) pp <- powerprior_univariate(historical, a0 = 0.5) posterior <- posterior_univariate(pp, current) # Sample from joint distribution samples_joint <- sample_posterior_univariate(posterior, n_samples = 1000) # Sample from marginal distribution of mu samples_marginal <- sample_posterior_univariate(posterior, n_samples = 1000, marginal = TRUE)