% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plotting_helpers.R
\name{plot_BRRAT_by_id}
\alias{plot_BRRAT_by_id}
\title{Per-site partial-pooling lines and credible bands}
\usage{
plot_BRRAT_by_id(fit_obj, n_grid = 150, level = 0.8, facet = TRUE)
}
\arguments{
\item{fit_obj}{A fitted object returned by \code{\link[=BRRAT]{BRRAT()}}, of class \code{BRRAT}.}

\item{n_grid}{Integer; number of x points per site for the prediction grids
(default \code{150}).}

\item{level}{Credible interval level (default \code{0.80}) used for site-specific bands.}

\item{facet}{Logical; if \code{TRUE}, facet the plot by \code{ID}; otherwise overlay all sites
with colour and fill aesthetics (default \code{TRUE}).}
}
\value{
A \code{ggplot2} object showing per-site lines and credible intervals.
}
\description{
Plots \strong{site-specific} regression lines using draws of \code{intercept_id[j]} and
\code{slope_id[j]} for each site \code{ID}. Requires more than 1 ID in the initial
regression by \code{\link[=BRRAT]{BRRAT()}}. The output overlays lines and credible bands
across sites; optionally facetted into separate panels for readability.
}
\details{
Plot per-site lines (random intercepts & slopes) with credible intervals

For site \eqn{j}, the line is computed as:
\deqn{\mu_j(x) = \mathrm{intercept\_id}_j + \mathrm{slope\_id}_j \, x_{\mathrm{std}}}{
mu_j(x) = intercept_id[j] + slope_id[j] * x_std}
evaluated on a grid of x values transformed to the centered/scaled space used
during fitting. The function reconstructs equal-tail credible bands by propagating
posterior draws of \code{intercept_id[j]} and \code{slope_id[j]}.
}
\examples{
\donttest{
set.seed(1)
samples <- 30
make_site <- function(id) {
  Clim <- sort(pmax(0, rnorm(samples, 600, 100)))
  observed <- pmax(0, (Clim - 150) - 450 * tanh((Clim - 150) / 450))
  list(
    Obs = data.frame(P = Clim, Q = observed, year = 1:samples, ID = id),
    Sim = data.frame(Q = observed * rnorm(samples, 1, 0.05), year = 1:samples, ID = id)
  )
}
s1 <- make_site("A")
s2 <- make_site("B")
Obs <- rbind(s1$Obs, s2$Obs)
Sim <- rbind(s1$Sim, s2$Sim)
res <- BRRAT(Sim, Obs, chains = 1, iter = 500)
p_sites <- plot_BRRAT_by_id(res, n_grid = 150, level = 0.80, facet = TRUE)
print(p_sites)
}

}
