---
title: "Real-World Example: Krackhardt Advice Network"
author: "Sima Najafzadehkhoei"
date: today
vignette: >
  %\VignetteIndexEntry{Real-world example of imaginarycss}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
---

# Krackhardt Advice Network

This vignette demonstrates a complete `imaginarycss` workflow using the
classic Krackhardt (1987) dataset of 21 managers in a high-tech company.
We construct a CSS object from the advice network and its individual
perceptions, then compute accuracy rates and the imaginary census.

```{r}
#| label: setup
library(imaginarycss)
```

## Data and Perceptions

The package ships the advice network as a data frame (`krackhardt_advice`) and
a list of 21 perception matrices (`krackhardt_advice_perceptions`).
We convert the edge-list to an adjacency matrix and bundle it with the
perception data into a `barry_graph` object.

```{r}
#| label: data
data(krackhardt_advice)
data(krackhardt_advice_perceptions)

# Convert edge-list data frame to adjacency matrix
n_people <- max(c(krackhardt_advice$from, krackhardt_advice$to))
advice_matrix <- matrix(0L, nrow = n_people, ncol = n_people)
advice_matrix[cbind(krackhardt_advice$from, krackhardt_advice$to)] <- krackhardt_advice$value

krack_graph <- new_barry_graph(c(list(advice_matrix), krackhardt_advice_perceptions))
```

We can see how the network looks (as an adjacency matrix):

```{r}
#| label: plot-network
print(krack_graph)
```

## Individual Accuracy

The function `tie_level_accuracy()` decomposes each perceiver's accuracy
into four probabilities, separating ego-involved from alter-only dyads and
true positives from true negatives.

```{r}
#| label: accuracy
acc <- tie_level_accuracy(krack_graph)
acc
```

We can further view this as a summary:

```{r}
#| label: accuracy-summ
#| fig.height: 5
#| fig.width: 6

# Mean accuracy rates as a data.frame
data.frame(
  Measure = c("TP (Ego)", "TN (Ego)", "TP (Alter)", "TN (Alter)"),
  Mean    = round(c(
    mean(acc$p_1_ego,   na.rm = TRUE),
    mean(acc$p_0_ego,   na.rm = TRUE),
    mean(acc$p_1_alter, na.rm = TRUE),
    mean(acc$p_0_alter, na.rm = TRUE)
  ), 3)
)

acc_mat <- as.matrix(acc[, c("p_0_ego", "p_1_ego", "p_0_alter", "p_1_alter")])
boxplot(
  acc_mat,
  names = c("TN (Ego)", "TP (Ego)", "TN (Alter)", "TP (Alter)"),
  ylab = "Probability",
  main = "Individual Accuracy Rates",
  col = c("#3498db", "#2980b9", "#e74c3c", "#c0392b"),
  border = "gray30"
)
```

## Perceptual Structure

The imaginary census and reciprocity error counts summarize which types
of misperception are most prevalent across all perceivers.

```{r}
#| label: structure
#| collapse: true
krack_census <- count_imaginary_census(krack_graph)
krack_recip  <- count_recip_errors(krack_graph)

# Top imaginary census motifs
head(krack_census, 5)

# Reciprocity errors
head(krack_recip, 5)
```

## Organizational Takeaways

We now consolidate key statistics from the analysis above. The summary table
below reports network-level properties (size and density) alongside
individual-level accuracy measures, identifying which managers perceive the
advice network most and least accurately.

```{r}
#| label: takeaways
#| collapse: true
density <- sum(advice_matrix) / (n_people * (n_people - 1))
avg_tp  <- mean(acc$p_1_ego, na.rm = TRUE)
best    <- which.max(acc$p_1_ego)
worst   <- which.min(acc$p_1_ego)

# Network summary
data.frame(
  Statistic = c("Employees", "Advice network density",
                "Avg TP (Ego)", "Most accurate perceiver",
                "Least accurate perceiver"),
  Value     = c(n_people, round(density, 3), round(avg_tp, 3),
                paste0(best, " (", round(acc$p_1_ego[best], 3), ")"),
                paste0(worst, " (", round(acc$p_1_ego[worst], 3), ")"))
)
```

The advice network is relatively dense (45%), meaning nearly half of all
possible ties exist. On average, managers correctly identify about 70% of
existing ego-involved advice ties. Manager 10 stands out as the most
perceptive, while Manager 13 shows the most room for improvement. These
differences in perceptual accuracy may reflect variation in organizational
position or engagement with the informal advice structure.

