library(dagitty)

# ---- Step 1: Define your causal system ----
# Example: Does NDVI (vegetation) reduce LST (urban heat)?
# Confounders: Land Use, Population Density
# Mediator: Tree Canopy
# Collider: Air Pollution (affected by both LST and Population Density)

g_myowl <- dagitty("
dag {
  NDVI -> TreeCanopy -> LST
  NDVI -> LST
  LandUse -> NDVI
  LandUse -> LST
  PopDensity -> NDVI
  PopDensity -> LST
  LST -> AirPollution
  PopDensity -> AirPollution
}
")

# ---- Step 2: Plot it ----
plot(g_myowl)
## Plot coordinates for graph not supplied! Generating coordinates, see ?coordinates for how to set your own.

# ---- Step 3: Analyze paths ----
impliedConditionalIndependencies(g_myowl)
## ArPl _||_ LndU | LST, PpDn
## ArPl _||_ NDVI | LST, PpDn
## ArPl _||_ TrCn | LST, PpDn
## LndU _||_ PpDn
## LndU _||_ TrCn | NDVI
## PpDn _||_ TrCn | NDVI
# Check adjustment set: what do we condition on to estimate NDVI → LST?
adjustmentSets(g_myowl, exposure = "NDVI", outcome = "LST")
## { LandUse, PopDensity }
# Check paths from NDVI to LST
paths(g_myowl, from = "NDVI", to = "LST")
## $paths
## [1] "NDVI -> LST"                              
## [2] "NDVI -> TreeCanopy -> LST"                
## [3] "NDVI <- LandUse -> LST"                   
## [4] "NDVI <- PopDensity -> AirPollution <- LST"
## [5] "NDVI <- PopDensity -> LST"                
## 
## $open
## [1]  TRUE  TRUE  TRUE FALSE  TRUE
action_box <- function(title, items, border = "#e91e63", bg = "#fff5f7") {
  html <- paste0(
    '<div style="border-left:6px solid ', border,
    ';padding:12px 14px;background:', bg,
    ';border-radius:10px;margin:14px 0 6px 0">',
    '<h4 style="margin:0 0 8px 0;color:', border, ';font-weight:bold;">',
    title, '</h4>',
    '<ul style="margin:0;list-style-type:disc;color:black;">',
    paste0("<li>", items, "</li>", collapse = ""),
    "</ul></div>"
  )
  cat(html)
}
action_box(
  title  = "Action Items — Your Research, Drawing your Owl
 ",
  items  = c(
    "Sketch your causal question. 
    - What is the main predicrot (X)? NDVI
    - What is the outcome (Y)? LST", 
    "Identify key variables
    - List plausible confounders (U), mediators (M), and colliders       (C). Confounders (U): Land Use, Population Density, Mediator     (M): Tree Canopy, Collider (C): Air Pollution"
    
  ),
  border = "#e91e63", bg = "#fff5f7"
)

Action Items — Your Research, Drawing your Owl