## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## ----eval=FALSE---------------------------------------------------------------
# # Install from CRAN (once available)
# install.packages("ISMtools")
# 
# # Or install development version from GitHub
# # devtools::install_github("tangyi/ISMtools")

## -----------------------------------------------------------------------------
library(ISMtools)

## -----------------------------------------------------------------------------
# Create a 5x5 adjacency matrix directly
# 1 indicates element i influences element j
adj_matrix <- matrix(c(
  0, 1, 0, 0, 0,
  0, 0, 1, 0, 0,
  0, 0, 0, 1, 1,
  0, 0, 0, 0, 0,
  0, 0, 0, 0, 0
), nrow = 5, byrow = TRUE)

# Add row and column names
rownames(adj_matrix) <- colnames(adj_matrix) <- paste0("F", 1:5)
print(adj_matrix)

## -----------------------------------------------------------------------------
# Create an empty matrix of specified size
empty_matrix <- create_relation_matrix(4)
print(empty_matrix)

## -----------------------------------------------------------------------------
# From a data frame
edge_df <- data.frame(
  source = c("A", "A", "B", "C"),
  target = c("B", "C", "D", "D")
)

adj_from_df <- convert_to_matrix(edge_df, from = "source", to = "target")
print(adj_from_df)

## -----------------------------------------------------------------------------
# From a matrix edge list
edge_mat <- matrix(c(
  "X", "Y",
  "Y", "Z",
  "X", "Z"
), ncol = 2, byrow = TRUE)

adj_from_mat <- convert_to_matrix(edge_mat)
print(adj_from_mat)

## -----------------------------------------------------------------------------
# Using our 5x5 adjacency matrix
reach_matrix <- compute_reachability(adj_matrix)
print(reach_matrix)

## -----------------------------------------------------------------------------
levels <- level_partitioning(reach_matrix)
print(levels)

## ----fig.width=6, fig.height=6------------------------------------------------
plot_ism(reach_matrix)

## ----eval=FALSE---------------------------------------------------------------
# # Interactive visualization (requires browser)
# plot_interactive_ism(reach_matrix,
#                      node_labels = paste0("Factor ", 1:5),
#                      level_result = levels)

## -----------------------------------------------------------------------------
# Define risk factors
factors <- c(
  "Budget Constraints",      # F1

  "Resource Shortage",       # F2  
  "Technical Complexity",    # F3
  "Poor Communication",      # F4
  "Schedule Pressure",       # F5
  "Quality Issues",          # F6
  "Project Failure"          # F7
)

# Create adjacency matrix based on expert judgment
# F1 -> F2, F5
# F2 -> F3, F6
# F3 -> F6
# F4 -> F2, F3, F6
# F5 -> F4, F6
# F6 -> F7

risk_adj <- matrix(0, nrow = 7, ncol = 7)
rownames(risk_adj) <- colnames(risk_adj) <- paste0("F", 1:7)

# Define relationships
risk_adj["F1", c("F2", "F5")] <- 1
risk_adj["F2", c("F3", "F6")] <- 1
risk_adj["F3", "F6"] <- 1
risk_adj["F4", c("F2", "F3", "F6")] <- 1
risk_adj["F5", c("F4", "F6")] <- 1
risk_adj["F6", "F7"] <- 1

print("Adjacency Matrix:")
print(risk_adj)

## -----------------------------------------------------------------------------
# Compute reachability
risk_reach <- compute_reachability(risk_adj)
print("Reachability Matrix:")
print(risk_reach)

## -----------------------------------------------------------------------------
# Level partitioning
risk_levels <- level_partitioning(risk_reach)
print("Hierarchical Levels:")
print(risk_levels)

## ----fig.width=7, fig.height=7------------------------------------------------
# Visualize
plot_ism(risk_reach)

## -----------------------------------------------------------------------------
sessionInfo()

