---
title: "Effect size computation — pinillossimpsonndexperimentalevidencesupporting"
format:
  html:
    toc: true
execute:
  echo: true
  warning: true
  message: false
---

```{r}
paper_key <- "pinillossimpsonndexperimentalevidencesupporting"
options(digits = 15)

# Helpers (adapted from docs/quarto_effect_template.qmd)
stop_if_missing <- function(x, name) {
  if (is.na(x)) stop(sprintf("Missing required input: %s", name), call. = FALSE)
}

hedges_correction <- function(df) {
  ifelse(df <= 1, NA_real_, exp(lgamma(df/2) - log(sqrt(df/2)) - lgamma((df - 1)/2)))
}

pooled_sd <- function(n_high, n_low, sd_high, sd_low) {
  sqrt(((n_high - 1) * sd_high^2 + (n_low - 1) * sd_low^2) / (n_high + n_low - 2))
}

d_from_groups_independent <- function(n_high, n_low, mean_high, mean_low, sd_high, sd_low) {
  s <- pooled_sd(n_high, n_low, sd_high, sd_low)
  (mean_low - mean_high) / s
}

var_d_independent <- function(d, n_high, n_low) {
  n <- n_high + n_low
  (n / (n_high * n_low)) + (d^2 / (2 * (n - 2)))
}

compute_between_groups <- function(
    study_id,
    effect_id,
    n_high,
    n_low,
    mean_high,
    mean_low,
    sd_high,
    sd_low,
    sign_convention = "d = mean(low) - mean(high)",
    notes_on_assumptions = "",
    imputed_flag = FALSE,
    needs_sensitivity = FALSE
) {
  stop_if_missing(n_high, "n_high")
  stop_if_missing(n_low, "n_low")
  stop_if_missing(mean_high, "mean_high")
  stop_if_missing(mean_low, "mean_low")
  stop_if_missing(sd_high, "sd_high")
  stop_if_missing(sd_low, "sd_low")

  d <- d_from_groups_independent(n_high, n_low, mean_high, mean_low, sd_high, sd_low)
  v <- var_d_independent(d, n_high, n_low)

  df_used <- n_high + n_low - 2
  J <- hedges_correction(df_used)
  g <- J * d
  v_g <- (J^2) * v

  data.frame(
    paper_key = paper_key,
    study_id = study_id,
    effect_id = effect_id,
    method_used = "between_groups",
    sign_convention = sign_convention,
    n_high = n_high,
    n_low = n_low,
    mean_high = mean_high,
    mean_low = mean_low,
    sd_high = sd_high,
    sd_low = sd_low,
    d = d,
    v = v,
    hedges_J = J,
    g = g,
    v_g = v_g,
    notes_on_assumptions = notes_on_assumptions,
    imputed_flag = imputed_flag,
    needs_sensitivity = needs_sensitivity,
    stringsAsFactors = FALSE
  )
}
```

## Study 1 (Experiment 1): Water purifier — evidence-seeking (s1_e1)

```{r}
res_s1_e1 <- compute_between_groups(
  study_id = 1,
  effect_id = "s1_e1",
  n_high = 48,
  n_low = 46,
  mean_high = 1.29,
  mean_low = 0.72,
  sd_high = 1.254,
  sd_low = 0.72,
  notes_on_assumptions = "Independent groups; outcome is number of comparisons/checks. Sign convention: d = mean(low) - mean(high).",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s1_e1
```

## Study 1 (Experiment 2): Airplane — stakes effect within Low Probability (LSLP vs HSLP) (s2_e1)

```{r}
res_s2_e1 <- compute_between_groups(
  study_id = 2,
  effect_id = "s2_e1",
  n_high = 61,
  n_low = 50,
  mean_high = 1.93,
  mean_low = 1.6,
  sd_high = 0.944,
  sd_low = 0.969,
  notes_on_assumptions = "2×2 between-subjects design (stakes × probability). This effect compares High vs Low stakes at Low probability.",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s2_e1
```

## Study 1 (Experiment 2): Airplane — stakes effect within High Probability (LSHP vs HSHP) (s2_e2)

```{r}
res_s2_e2 <- compute_between_groups(
  study_id = 2,
  effect_id = "s2_e2",
  n_high = 61,
  n_low = 58,
  mean_high = 2.15,
  mean_low = 1.76,
  sd_high = 1.152,
  sd_low = 0.823,
  notes_on_assumptions = "2×2 between-subjects design (stakes × probability). This effect compares High vs Low stakes at High probability.",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s2_e2
```

## Study 2 (Coin): Agreement with knowledge ascription (s3_e1)

```{r}
res_s3_e1 <- compute_between_groups(
  study_id = 3,
  effect_id = "s3_e1",
  n_high = 78,
  n_low = 87,
  mean_high = 3.06,
  mean_low = 3.68,
  sd_high = 1.76,
  sd_low = 1.80,
  notes_on_assumptions = "Independent groups; Likert 0–6 agreement scale.",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s3_e1
```

## Study 2 (Air): Agreement with knowledge ascription (s4_e1)

```{r}
res_s4_e1 <- compute_between_groups(
  study_id = 4,
  effect_id = "s4_e1",
  n_high = 30,
  n_low = 25,
  mean_high = 2.03,
  mean_low = 2.16,
  sd_high = 1.0,
  sd_low = 1.03,
  notes_on_assumptions = "Independent groups; Likert 0–4 agreement scale.",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s4_e1
```

## Study 2 (Bridge): Agreement with knowledge ascription (s5_e1)

```{r}
res_s5_e1 <- compute_between_groups(
  study_id = 5,
  effect_id = "s5_e1",
  n_high = 31,
  n_low = 28,
  mean_high = 1.71,
  mean_low = 2.32,
  sd_high = 1.13,
  sd_low = 1.16,
  notes_on_assumptions = "Independent groups; Likert 0–4 agreement scale.",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s5_e1
```

## Modal-reading test (Typo): Evidence-seeking knowledge question (s6_e1)

```{r}
res_s6_e1 <- compute_between_groups(
  study_id = 6,
  effect_id = "s6_e1",
  n_high = 31,
  n_low = 39,
  mean_high = 4.42,
  mean_low = 3.31,
  sd_high = 1.36,
  sd_low = 0.86,
  notes_on_assumptions = "Independent groups; evidence-seeking (numeric free response).",
  imputed_flag = FALSE,
  needs_sensitivity = FALSE
)
res_s6_e1
```

