---
title: "Psychological Network Analysis with Nestimate"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Psychological Network Analysis with Nestimate}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

Nestimate supports psychological network analysis (PNA) through partial correlation and graphical lasso estimation. This vignette uses the `srl_strategies` dataset — frequency counts of 9 self-regulated learning strategies for 250 students — to estimate, regularize, and bootstrap a psychological network.

## Data

The 9 strategies fall into three clusters: metacognitive (Planning, Monitoring, Evaluating), cognitive (Elaboration, Organization, Rehearsal), and resource management (Help_Seeking, Time_Mgmt, Effort_Reg).

```{r data}
library(Nestimate)
data(srl_strategies)
head(srl_strategies)
```

## Correlation network

The simplest approach estimates pairwise Pearson correlations.

```{r cor}
net_cor <- build_network(srl_strategies, method = "cor")
net_cor
```

## Partial correlation network

Partial correlations control for all other variables, revealing direct associations.

```{r pcor}
net_pcor <- build_network(srl_strategies, method = "pcor")
net_pcor
```

## Regularized network (EBICglasso)

The graphical lasso applies L1 regularization to the precision matrix, producing a sparse network. The `gamma` parameter controls sparsity via EBIC model selection — higher values yield sparser networks.

```{r glasso}
net_glasso <- build_network(srl_strategies, method = "glasso",
                            params = list(gamma = 0.5))
net_glasso
```

## Predictability

Node predictability measures how well each node is predicted by its neighbors (R-squared from the network structure).

```{r predictability}
pred <- predictability(net_glasso)
round(pred, 3)
```

## Bootstrap inference

Non-parametric bootstrap assesses edge stability and significance.

```{r bootstrap}
boot <- boot_glasso(net_glasso, iter = 100,
                    centrality = c("strength", "expected_influence"),
                    seed = 42)
```

### Edge significance

```{r boot-edges}
summary(boot, type = "edges")
```

### Centrality stability

```{r boot-stability}
summary(boot, type = "centrality")
```
