## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6.5,
  fig.height = 4.5
)

## ----load---------------------------------------------------------------------
library(Stem)

## ----data---------------------------------------------------------------------
data(povalley)

Tn <- 150L                       # a five-month slice, to keep the vignette quick
Tfull <- nrow(povalley$z)
d <- ncol(povalley$z)
keep <- as.vector(outer(seq_len(Tn), (seq_len(d) - 1L) * Tfull, "+"))

z <- povalley$z[seq_len(Tn), ]
X <- povalley$covariates[keep, ]

dim(z)
head(round(povalley$coords, 3), 3)

## ----network, fig.alt = "Location of the 36 monitoring stations"--------------
plot(povalley$coords, pch = 19, cex = 0.9,
     xlab = "Longitude", ylab = "Latitude",
     main = "Po Valley background stations")

## ----model--------------------------------------------------------------------
phi <- list(
  beta        = matrix(c(1.25, -0.00003, 0.64), 3, 1),
  sigma2eps   = 18.66,
  sigma2omega = 1e-06,
  theta       = 2e-06,
  G           = matrix(0.59, 1, 1),
  Sigmaeta    = matrix(4.25, 1, 1),
  m0          = as.matrix(0),
  C0          = as.matrix(1)
)

mod <- STEM_Model(z = z, covariates = X, coordinates = povalley$coords,
                  phi = phi, A = matrix(1, d, 1))

## ----fit----------------------------------------------------------------------
fit <- SCSTEM_Estimation(mod, K = 3, phi_penalty = 0.05, distance = "geo",
                    precision = 0.05)
fit

## ----trace--------------------------------------------------------------------
fit$obj_trace

## ----map, fig.alt = "Estimated spatial regimes"-------------------------------
plot(povalley$coords, col = fit$group, pch = 19, cex = 1.2,
     xlab = "Longitude", ylab = "Latitude", main = "Estimated regimes")
legend("topright", legend = paste("regime", seq_len(3)),
       col = seq_len(3), pch = 19, bty = "n")

## ----scale--------------------------------------------------------------------
c(multiplier = fit$phi_multiplier, effective_phi = fit$phi_effective)

## ----grid---------------------------------------------------------------------
ic <- SCSTEM_Infocrit(mod, K_grid = 1:3, phi_grid = c(0, 0.025, 0.05, 0.1, 0.2),
                      distance = "geo", precision = 0.05)
ic$table[, c("K", "phi", "K_eff", "admissible", "loglik", "BIC", "min_size")]

## ----select-------------------------------------------------------------------
sel <- SCSTEM_Select(ic, band = c(0.025, 0.2))
sel

## ----boot---------------------------------------------------------------------
best <- sel$fit
boot <- SCSTEM_Bootstrap(best, B = 12, seed = 1)
boot

## ----inference----------------------------------------------------------------
inf <- SCSTEM_BootInference(boot)
inf

## ----ci-----------------------------------------------------------------------
s <- inf$summary
s[grepl("beta", s$parameter),
  c("cluster", "parameter", "estimate", "se",
    grep("^perc_", names(s), value = TRUE))]

## ----tests--------------------------------------------------------------------
inf$tests[grepl("beta", inf$tests$parameter), ]

## ----coclust------------------------------------------------------------------
round(inf$coclustering[1:6, 1:6], 2)

