---
title: "Generalizability (G-Study) and Decision (D-Study) Planning"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Generalizability (G-Study) and Decision (D-Study) Planning}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6.5, fig.height = 4.2)
```

## 1. Crossed Observed-Score Generalizability and Decision Studies

Suppose an assessment team can afford eight ratings per person. Would two
raters using four criteria or four raters using two criteria give more precise
scores? A G-Study estimates how observed-score variation is distributed across
persons, raters, criteria and their interactions. A D-Study uses those estimated
components to project precision under candidate rating plans; no latent-trait
MFRM fit is required.

First decide what the score is for. Use $G$ for relative decisions, such as
ranking persons, and $\Phi$ for absolute decisions against a fixed standard.
Systematic rater or criterion differences enter absolute error even when they
do not change relative ordering. Neither coefficient is a probability that an
individual pass/fail decision is correct.

The bundled `example_core` contains 1-to-4 ratings. For this illustration we
treat both raters and criteria as random samples from the intended assessment
universe. This assumption matters: deleting fixed, substantively required
rubric criteria changes what is assessed and is not justified by this D-Study.

```{r}
library(mfrmr)

ratings <- load_mfrmr_data("example_core")

gt <- mfrm_g_study(
  ratings,
  scores = "Score",
  person = "Person",
  facets = c("Rater", "Criterion"),
  method = "urgenova",
  d_study_grid = expand.grid(Rater = c(1, 2, 3, 4), Criterion = c(2, 3, 4))
)
print(gt)
confint(gt)
plans <- mfrm_d_study(gt)
plans
equal_budget <- plans[plans$TotalRatingsPerPerson == 8, ]
equal_budget[, c("Rater", "Criterion", "TotalRatingsPerPerson", "G", "Phi", "AbsSEM")]
plot(gt, type = "dstudy", palette = "okabe_ito")
```

Read the equal-budget rows together: the larger relevant coefficient and the
smaller corresponding SEM indicate greater projected precision. `AbsSEM` is
on the original score scale, whereas $G$ and $\Phi$ are variance ratios. These
rows answer the allocation question only under the same universe and scoring
conditions as the G-Study. Costs, content coverage and feasible assignments can
still rule out the statistically more precise plan.

For these data, four raters with two criteria give $\Phi =
`r round(equal_budget$Phi[equal_budget$Rater == 4], 3)`$, compared with
`r round(equal_budget$Phi[equal_budget$Rater == 2], 3)` for two raters with four
criteria. Thus the former has a slightly higher point projection for absolute
decisions. Both are below 0.80, an illustrative planning target rather than a
universal acceptability threshold. The difference is too small to establish a
preferred design from these point estimates alone.

Inspect `gt$variance_components` and `confint(gt)` before relying on small
differences. Negative raw component estimates remain visible in `RawEstimate`
and are truncated to zero in the default projections. The reported intervals
use a Gaussian plug-in covariance and Satterthwaite or delta approximations;
they do not provide exact small-sample coverage. In this example,
`Person:Criterion` has an estimate much smaller than its SE. Its Satterthwaite
degrees of freedom are below 1, so the approximate interval is withheld with
status `unstable_satterthwaite_df`. This reporting guard avoids presenting an
extremely large, unstable upper bound as informative uncertainty. It does not
establish coverage for the remaining components. An unavailable interval is
`NA`, not zero uncertainty. `mfrm_d_study()` reports point projections, so this
table alone does not establish that one plan is reliably superior. See
`?mfrm_g_study` for assumptions and `?mfrm_d_study` for the output definitions.

## 2. Nested Designs and Multivariate Composite Weights

The next example answers a different question: how dependable is a prespecified
60% Content / 40% Organization composite when each task has its own rater
panel? Declaring `Rater` nested within `Task` represents that assignment; treating
the panels as crossed would describe a different design. The component
covariances matter because the two subscores come from the same ratings.
Specify the weights from the assessment purpose before inspecting reliability:

```{r}
nested_ratings <- load_mfrmr_data("example_gtheory_nested")
head(nested_ratings, 4)

gt_nested <- mfrm_g_study(
  nested_ratings,
  scores = c("Content", "Organization"),
  weights = c(0.6, 0.4),
  person = "Person",
  facets = c("Rater", "Task"),
  nesting = c(Rater = "Task"),
  method = "urgenova"
)
print(gt_nested)
gt_nested$univariate
confint(gt_nested, component = "coefficients")
```

Explicit composite weights retain their scale: `c(1, 1)` defines a sum and
`c(.5, .5)` an average. Multiplying all weights by two doubles SEM and
quadruples variance components without changing G or Phi. Omitted weights
define an equal-weight average.


The composite summary answers the question for the stated weights, while
`gt_nested$univariate` describes the separate subscores. A dependable composite
does not establish that either subscore supports individual reporting. When
planning further ratings with `mfrm_d_study(gt_nested, design_grid = ...)`, a
`Rater` count is per task, not the total number across tasks. The nested design,
score definitions and intended populations must remain appropriate for the
proposed assessment.
