### Instantiate AEWMA ControlChart Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Example instantiation of an AEWMA control chart. It uses an AEWMA statistic with specific parameters, identical control limits, nominal properties, and Phase II distribution as the EWMA example, highlighting the flexibility in statistic choice. ```julia CH_AE = ControlChart(AEWMA(λ = 0.2, k=3.0), TwoSidedFixedLimit(0.25), ARL(200), Phase2Distribution(Normal(0,1)) ) ``` -------------------------------- ### Instantiate EWMA ControlChart Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Example instantiation of an EWMA control chart. It combines an EWMA statistic, fixed two-sided control limits, ARL nominal properties, and a Phase II distribution for simulating observations. ```julia CH_E = ControlChart(EWMA(λ = 0.2), TwoSidedFixedLimit(0.25), ARL(200), Phase2Distribution(Normal(0,1)) ) ``` -------------------------------- ### Instantiate Multi-Chart Monitoring Scheme Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Example of creating a multi-chart monitoring scheme using two EWMA statistics with different lambda values and corresponding control limits. This demonstrates how to monitor data using parallel control charts with varying parameters. ```julia ControlChart( (EWMA(λ = 0.05), EWMA(λ=0.2)), (TwoSidedFixedLimit(0.25), TwoSidedFixedLimit(0.5)), ARL(200), Phase2Distribution(Normal(0,1)) ) ``` -------------------------------- ### Define LocationScaleStatistic for CUSUM Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Demonstrates how to define a monitoring statistic using the `LocationScaleStatistic` subclass of `ResidualStatistic`. This example sets up a CUSUM statistic with specific parameters for location and scale. ```julia STAT = LocationScaleStatistic(CUSUM(k=1.0), 0.5, 2.0) ``` -------------------------------- ### Create ControlChart Object in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Constructs the main `ControlChart` object by combining the specified statistical method (NEWMA), limit calculation (BootstrapLimit), nominal ARL, and Phase II monitoring setup. This object encapsulates all necessary components to perform process monitoring. ```julia julia> CH = ControlChart(STAT, LIM, NM, PH2) ``` -------------------------------- ### Monitoring Statistics Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Overview of the different monitoring statistics available in the package, including their parameters and references. ```APIDOC ## Monitoring Statistics This table lists the available monitoring statistics in the StatisticalProcessMonitoring.jl package. | Statistic Name | Symbol | Parameters | Reference | |---------------------|---------------------|-------------------------------------------------|-----------------------------------| | R-SADA | `RSADA` | $ k, \mu_{\text{min}}\in \mathbb{R}^+ $ | [Xian et Al. (2019)](https://doi.org/10.1080/00224065.2019.1681924) | | TRAS | `TRAS` | $ k, \mu_{\text{min}}, \Delta \in \mathbb{R}^+ $, $ r\in \{1, \ldots,q} $ | [Liu et Al. (2015)](https://doi.org/10.1080/00401706.2014.947005) | | **Profile monitoring**| | | | | NEWMA | `NEWMA` | $ \lambda\in (0,1)$ | [Zou et Al. (2008)](https://doi.org/10.1198/004017008000000433) | | Risk-adjusted CUSUM | `RiskAdjustedCUSUM` | $ \Delta\in \mathbb{R} $ | [Steiner et Al. (2000)](https://academic.oup.com/biostatistics/article/1/4/441/238348) | *Note: $p$ is the number of quality variables under monitoring. For partially-observed data, $q$ is the number of observed variables.* ### Statistics with Estimated Parameters Separation of monitoring statistic and parameter estimation promotes code compartmentalization, facilitated by subtypes like `ResidualStatistic`. For example, the definition of a monitoring statistic that behaves for $k = 1.0$ as $C_{t} = \max\left{ 0, \left( \frac{X_{t} - 0.5}{2} \right) - k \right},$ can be defined using the `LocationScaleStatistic` subclass of `ResidualStatistic`. #### Example ```julia STAT = LocationScaleStatistic(CUSUM(k=1.0), 0.5, 2.0) ``` ``` -------------------------------- ### Control Limit Design Algorithms Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Details on the algorithms available for designing control limits, including their descriptions, usage, and associated functions. ```APIDOC ## Control Limit Design This table lists the available algorithms for designing fixed and deterministic time-varying control limits in StatisticalProcessMonitoring.jl. | Functions | Symbol | Description | Single charts | Multi-charts | |----------------------------------------|----------------|---------------------------------------------------------------------------------------------------------------------------------|---------------|--------------| | `bisectionCL`, `bisectionCL!` | `:Bisection` | The standard bisection algorithm [Qiu (2013)](https://www.taylorfrancis.com/books/mono/10.1201/b15016/introduction-statistical-process-control-peihua-qiu). Requires an initial interval to search for the control limit. | ✔ | | | `saCL`, `saCL!` | `:SA` | Algorithm based on stochastic approximations. See [Capizzi & Masarotto (2016)](https://www.tandfonline.com/doi/full/10.1080/0740817X.2015.1055392) for a complete description of the tuning parameters. | ✔ | ✔ | | `combinedCL`, `combinedCL!` | `:Combined` | The standard bisection algorithm combined with a preliminary low-accuracy application of the SA algorithm to automatically select the initial interval. | ✔ | | | `bootstrapCL`, `bootstrapCL!` | `:Bootstrap` | Approximates the distribution of the monitoring statistic at each time $t = 1, 2, ..., T$ for $T > 0$, and then applies bisection search. Does not require an initial interval to begin the search. | ✔ | ✔ | ### Bisection Search A common method for determining control limits such that the nominal run length characteristic (e.g. `ARL` or `QRL`) equals the nominal value. Starting from an interval $[0, h_\text{max}]$, the method finds the appropriate control limit value via bisection, by approximating the run length characteristic at each iteration with a large number of simulated run lengths from the Phase 2 chart attribute. This algorithm is implemented in the `bisectionCL!` and `bisectionCL` functions. ### Stochastic Approximations For single- and multi-chart schemes, the method simulates one run length at a time and applies a stochastic gradient descent algorithm, $\bm{h}_{k+1} = \Psi\left( \bm{h}_{k} - \frac{1}{(k+1)^{q}} D \bm{s}(\bm{h}_k) \right),$ which converges to the required control limit value. This algorithm is implemented in the `saCL!` and `saCL` functions. #### Request Example ```julia using Distributions, Random Random.seed!(12345) chart = ControlChart( (CUSUM(k = 0.25), CUSUM(k=0.5)), (OneSidedFixedLimit(5.0, true), OneSidedFixedLimit(5.0, true)), ARL(370), Phase2Distribution(Normal(0,1)) ) saCL(chart) ``` #### Response Example ```json { "h": [7.290634106470891, 4.403721760449869], "iter": 32847, "status": "Convergence" } ``` ``` -------------------------------- ### Simulate Observations using Phase2 (Resampling) Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Implements the Phase2 type for simulating new observations by resampling from an existing in-control reference dataset. This supports techniques like bootstrap and allows for more realistic simulation scenarios. ```julia # Example definition (actual implementation not shown in text) # Phase2(reference_data::Union{Vector, Matrix, DataFrame}) ``` -------------------------------- ### Add StatisticalProcessMonitoring.jl Package (Standard REPL) Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/index.md Instructions to add the StatisticalProcessMonitoring.jl package to a Julia environment using the standard REPL interface, involving the Pkg module. ```julia julia> using Pkg julia> Pkg.add("StatisticalProcessMonitoring") ``` -------------------------------- ### Simulate Observations using Phase2Distribution Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Implements Phase2Distribution for simulating new observations from specified probability distributions using the Distributions.jl package. This is used for Phase II monitoring and simulation studies. ```julia # Example definition (actual implementation not shown in text) # Phase2Distribution(dist::Distribution) # sample(dist) ``` -------------------------------- ### Implement OneSidedBootstrapLimit Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Details OneSidedBootstrapLimit for one-sided time-varying control limits derived from bootstrap resampling. It also targets an approximately constant false alarm rate (alpha). ```julia # Example definition (actual implementation not shown in text) # OneSidedBootstrapLimit(alpha::Real, upw::Bool) ``` -------------------------------- ### Load Julia Packages for Statistical Process Monitoring Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Loads necessary Julia packages for statistical process monitoring, including distributions, random number generation, LOESS smoothing, plotting, and LaTeX string support. These packages provide the foundation for the subsequent data generation and analysis. ```julia using StatisticalProcessMonitoring, Distributions, Random, Loess, Plots, LaTeXStrings ``` -------------------------------- ### Add StatisticalProcessMonitoring.jl Package (Julia REPL) Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/index.md Instructions to add the StatisticalProcessMonitoring.jl package to a Julia environment using the interactive package manager. ```julia pkg> add StatisticalProcessMonitoring ``` -------------------------------- ### Import Required Packages Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Imports necessary libraries for statistical process monitoring, distributions, random number generation, optimization, plotting, parameter handling, and LaTeX string formatting. ```julia using StatisticalProcessMonitoring, Distributions, Random, NLopt, Plots, Parameters, LaTeXStrings ``` -------------------------------- ### Load StatisticalProcessMonitoring.jl Package (Julia) Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/index.md Command to load the StatisticalProcessMonitoring.jl package into the current Julia session, making its functions available for use. ```julia julia> using StatisticalProcessMonitoring ``` -------------------------------- ### Implement Stochastic Approximations for Control Limits Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Shows the implementation of the stochastic approximations algorithm for designing control limits in a multi-chart scheme. It includes setting up a `ControlChart` object and calling the `saCL` function to find the control limits. ```julia using Distributions, Random Random.seed!(12345) chart = ControlChart( (CUSUM(k = 0.25), CUSUM(k=0.5)), (OneSidedFixedLimit(5.0, true), OneSidedFixedLimit(5.0, true)), ARL(370), Phase2Distribution(Normal(0,1)) ) saCL(chart) (h = [7.290634106470891, 4.403721760449869], iter = 32847, status = "Convergence") ``` -------------------------------- ### Implement OneSidedFixedLimit Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Describes the OneSidedFixedLimit, which allows for control limits of the form (0, h) when 'upw' is true, or (-h, 0) when 'upw' is false. This provides flexibility for one-sided monitoring requirements. ```julia # Example definition (actual implementation not shown in text) # OneSidedFixedLimit(h, upw::Bool) # if upw # LCL_t = 0 # UCL_t = h # else # LCL_t = -h # UCL_t = 0 # end ``` -------------------------------- ### Optimize Design for Statistical Process Monitoring Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md The `optimize_design!` function provides a high-level interface for hyperparameter optimization in statistical process monitoring. It requires the ability to simulate from the specific out-of-control scenario of interest to determine optimal parameters. Dependencies include the NLopt.jl package for nonlinear numerical solvers. ```julia using NLopt # Assuming a function `optimize_design!` exists and is defined elsewhere # Example usage: # optimize_design!(data, out_of_control_scenario, optimizer=NLopt.Optimizer(:LN_BOBYQA), lower_bounds, upper_bounds) ``` -------------------------------- ### Implement TwoSidedBootstrapLimit Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Introduces TwoSidedBootstrapLimit for time-varying control limits determined through bootstrap resampling. This method aims to maintain an approximately constant false alarm rate (alpha) over time. ```julia # Example definition (actual implementation not shown in text) # TwoSidedBootstrapLimit(alpha::Real) ``` -------------------------------- ### Load Required Julia Packages Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Loads necessary Julia packages for statistical process monitoring, linear algebra, random number generation, distributions, CSV handling, data frames, and plotting. These packages are essential for executing the subsequent analysis. ```julia using StatisticalProcessMonitoring, LinearAlgebra, Random, Distributions, CSV, DataFrames, Plots ``` -------------------------------- ### Load Statistical Process Monitoring Packages in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Loads essential Julia packages for statistical process monitoring, data manipulation, distributions, random number generation, parameter handling, CSV reading, data frames, categorical arrays, mixed-effects models, and plotting. These are foundational for the subsequent analysis. ```julia using StatisticalProcessMonitoring, Distributions, Random, Parameters, CSV, DataFrames, CategoricalArrays, MixedModels, Plots ``` -------------------------------- ### Nominal Property: Quantile Run Length (QRL) Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Defines the QRL (Quantile Run Length) nominal property. It specifies the nominal p-level quantile of the in-control run length, providing an alternative measure for setting run length constraints. ```julia # Example definition (actual implementation not shown in text) # QRL(quantile::Real, p::Real) ``` -------------------------------- ### Implement TwoSidedCurvedLimit Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Introduces TwoSidedCurvedLimit for deterministic time-varying control limits. These limits are defined by a function g(t) multiplied by a base value 'h', allowing for dynamic adjustment of monitoring boundaries over time. ```julia # Example definition (actual implementation not shown in text) # TwoSidedCurvedLimit(h, g::Function) # LCL_t = h * g(t) # UCL_t = h * g(t) ``` -------------------------------- ### Prepare Phase II Data for Bootstrap Simulation in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Prepares the Phase I data for use in bootstrap resampling to simulate in-control run lengths. This is a necessary step for estimating the control limit accurately. ```julia PH2 = Phase2(Bootstrap(), dat_ic) ``` -------------------------------- ### Generate Initial In-Control Functional Data in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Generates a matrix of functional data points for an in-control process. It initializes 500 profiles, each with 20 observations distributed across a grid of x-values. Random noise from a standard normal distribution is added to a sine wave function to simulate the functional observations. ```julia julia> n = 500 julia> nj = 20 julia> x_grid = collect(0.5:0.5:10) julia> xs = Matrix{Float64}(undef, n, nj) julia> for i in 1:n; xs[i, :] = x_grid; end julia> Random.seed!(41289355) julia> ys = sin.(xs) .+ randn(n, nj) ``` -------------------------------- ### Initialize AR1 Process and Control Chart Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Initializes an AR(1) process with phi=0.5 and sets up the `AR1Statistic` and `EWMA` control chart. It also defines the control limits (`TwoSidedFixedLimit`), the nominal ARL (`ARL`), and creates the `ControlChart` object. ```julia seed = 4398354798 Random.seed!(seed) phi = 0.5 PH2 = Phase2AR1(phi = phi) STAT = AR1Statistic(EWMA(λ = 0.1), phi, 0.0) LIM = TwoSidedFixedLimit(1.0) NOM = ARL(500) CH = ControlChart(STAT, LIM, NOM, PH2) ``` -------------------------------- ### Initialize Phase II Monitoring Object in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Initializes a `Phase2` object for simulating run lengths in a functional control chart. This object is configured with bootstrap resampling and the generated functional data, preparing it for the creation of a control chart. ```julia julia> dat = FunctionalData(xs, ys) julia> PH2 = Phase2(Bootstrap(), dat) ``` -------------------------------- ### Implement OneSidedCurvedLimit Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Details the OneSidedCurvedLimit for one-sided time-varying control limits. Similar to its two-sided counterpart, it uses a base value 'h' and a deterministic function g(t) to define dynamic one-sided boundaries. ```julia # Example definition (actual implementation not shown in text) # OneSidedCurvedLimit(h, g::Function, upw::Bool) ``` -------------------------------- ### Generate Phase II Data Distribution in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Creates a `Phase2Distribution` object using the previously defined in-control distribution (`DIST`). This object represents the distribution from which Phase II observations (monitoring data) will be generated. ```julia PH2 = Phase2Distribution(DIST) ``` -------------------------------- ### Set Control Chart Limits using Bootstrap in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Configures the control limits for the NEWMA chart using one-sided bootstrap simulations. It ensures a constant false-alarm rate and utilizes sequential estimation with 1000 bootstrap samples to establish reliable decision intervals. ```julia julia> LIM = OneSidedBootstrapLimit(STAT, true, 1000) ``` -------------------------------- ### Bootstrap Control Limits in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Constructs a `ControlChart` object using the standardized statistics, limits, desired run length, and Phase II distribution. It then uses the `bootstrapCL!` function to calculate and set the actual control limits based on the specified parameters and data. ```julia CH = ControlChart(EST_STATS, LIMS, NM, PH2) h = bootstrapCL!(CH) print(h) ``` -------------------------------- ### Implement TwoSidedFixedLimit Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Illustrates the concept of fixed control limits, where LCL and UCL are constant values (-h, h) for all time points. This is a fundamental type of control limit used in many basic control charts. ```julia # Example definition (actual implementation not shown in text) # TwoSidedFixedLimit(h) # LCL_t = -h # UCL_t = h ``` -------------------------------- ### Initialize Random Seed in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Sets a specific seed for the random number generator to ensure reproducibility of the results. This is crucial for experiments where random processes are involved. ```julia seed = 54397858713 Random.seed!(seed) ``` -------------------------------- ### Load and Process Phase II Data in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Loads Phase II data from a CSV file ('example77.csv') into a DataFrame and extracts the first three columns as a matrix (`xnew`). This data is then processed using the `apply_chart` function with the configured `ControlChart` object (`CH`). ```julia data = CSV.read("example77.csv", DataFrame) xnew = Matrix(data)[:, 1:3] proc = apply_chart(CH, xnew) ``` -------------------------------- ### Define Multivariate Normal Distribution Parameters in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Initializes the mean vector and covariance matrix for a 3-dimensional multivariate normal distribution under in-control (IC) conditions. This distribution models the behavior of the quality variables before any process shifts occur. ```julia μ = [0, 0, 0] Σ = [1.0 0.8 0.5; 0.8 1.0 0.8; 0.5 0.8 1.0] DIST = MultivariateNormal(μ, Σ) ``` -------------------------------- ### Generate and Monitor Phase II Data Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Generates 100 Phase II observations from the AR(1) process with phi=0.5. A mean shift of delta=2.0 is introduced after the 50th observation. The `apply_chart` function then processes this data using the configured control chart. ```julia n = 100 tau = 50 DGP = Phase2AR1(phi = phi) y = [new_data!(DGP) for _ in 1:n] y[(tau+1):n] .+= delta proc = apply_chart(CH, y) ``` -------------------------------- ### Nominal Property: Average Run Length (ARL) Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Defines the ARL (Average Run Length) nominal property. It specifies the target average number of observations before a false alarm occurs under in-control conditions (E0[RL]). ```julia # Example definition (actual implementation not shown in text) # ARL(expected_rl::Real) ``` -------------------------------- ### Generate Phase II Data with Process Shifts in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Generates three sequences of Phase II data for testing the control chart's performance under different scenarios: in-control, a profile shift, and a variance shift. This involves creating new `xs_oc` and `ys_` matrices with modified functional forms or added noise. ```julia julia> n2 = 150 julia> xs_oc = xs[1:n2, :] julia> ys_ic = sin.(xs_oc) .+ randn(n2, nj) # In-control julia> ys_oc = sin.(xs_oc) + 2*cos.(xs_oc) .+ randn(n2, nj) # Profile shift julia> ys_oc2 = sin.(xs_oc) .+ 2*randn(n2, nj) # Variance shift ``` -------------------------------- ### Define Phase2AR1 for Data Generation Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Defines the `Phase2AR1` struct, a specialization of `AbstractPhase2`, for generating observations from an AR(1) process. It includes the AR(1) coefficient (`phi`), the current observation (`y`), and an initialization flag (`init`). The `new_data!` function generates the next observation in the AR(1) sequence, initializing `y` from the stationary distribution if necessary. ```julia @with_kw mutable struct Phase2AR1 <: StatisticalProcessMonitoring.AbstractPhase2 phi::Float64 y::Float64 = 0.0 init::Bool = false end function new_data!(PH2::Phase2AR1) if !PH2.init PH2.y = randn() / sqrt(1 - PH2.phi^2) PH2.init = true end yhat = PH2.phi * PH2.y + randn() PH2.y = yhat return yhat end ``` -------------------------------- ### Define Control Chart Statistics in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Sets up four different control chart statistics: three MCUSUM (Multivariate CUSUM) charts with varying allowance constants (k) and one MEWMC (Minimum Exponentially Weighted Moving Average Control) chart with a smoothing constant (λ). The number of quality variables (p) is determined from the mean vector. ```julia p = length(μ) STATS = (MCUSUM(k = 0.25, p = p), MCUSUM(k = 0.5, p = p), MCUSUM(k = 1.0, p = p), MEWMC(λ = 0.2, p = p)) ``` -------------------------------- ### Apply Control Chart to Phase II Data Sequences in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Applies the configured `ControlChart` to the three prepared Phase II `FunctionalData` objects: in-control, profile shift, and variance shift. The `apply_chart` function processes each sequence and returns the monitoring results. ```julia julia> proc_ic = apply_chart(CH, dat_ic) julia> proc_oc = apply_chart(CH, dat_oc) julia> proc_oc2 = apply_chart(CH, dat_oc2) ``` -------------------------------- ### Plot Control Chart Results for Phase II Sequences in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Generates plots visualizing the results of applying the control chart to the three Phase II data sequences. It displays the control chart statistics over time for in-control, profile shift, and variance shift scenarios, highlighting the control limits and detected signals. ```julia markersize = 1.5 plt1 = plot_series(proc_ic, marker = :d, title = "In-control", l_linestyle = :dot, markersize = markersize, dpi = 300, label = "", xlab = L"t", ylab = L"C_t") plt2 = plot_series(proc_oc, marker = :o, title = "Profile shift",l_linestyle = :dot, markersize = markersize, dpi = 300, colour = palette(:tab10)[2], label = "", xlab = L"t", ylab = L"C_t") plt3 = plot_series(proc_oc2, marker = :square, title = "Variance shift", l_linestyle = :dot, markersize = markersize, dpi = 300, colour = palette(:tab10)[3], label = "", xlab = L"t", ylab = L"C_t") l = @layout [grid(1,2); _{0.2w} c{0.55w} _{0.25w}] plt = plot(plt1,plt2,plot(frame = :none), plt3; layout = l) ``` -------------------------------- ### Calculate Control Limit using Stochastic Approximation in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Creates a ControlChart object and then uses the `saCL!` function to estimate the control limit (`h`) through stochastic approximation. This process iteratively refines the limit until convergence, with optional verbose output to track progress. ```julia CH = ControlChart(STAT, LIM, NOM, PH2) h = saCL!(CH, verbose = true, gamma = 0.05) ``` -------------------------------- ### Load and Prepare Cardiac Surgery Data in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Reads the `cardiacsurgery.csv` dataset into a Julia DataFrame and converts the 'surgeon' column to a categorical type. This step is crucial for preparing the data for statistical modeling and subsequent process monitoring. ```julia dat = CSV.read("cardiacsurgery.csv", DataFrame) dat.surgeon = categorical(dat.surgeon) ``` -------------------------------- ### Define MultipleControlChart Type Alias Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Defines a type alias 'MultipleControlChart' for control charts where the statistic and limit attributes are tuples. This facilitates the implementation of multi-chart monitoring schemes where multiple statistics and limits are managed simultaneously. ```julia const MultipleControlChart{S,L,N,P} = ControlChart{S,L,N,P} where {S<:Tuple,L<:Tuple,N,P} ``` -------------------------------- ### Update ControlChart Statistic Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Implements the update_chart! function to increment the time point and update the monitoring statistic of a control chart. This function is essential for sequential monitoring of data. ```julia function update_chart!(CH::AbstractChart, x) CH.t += 1 update_statistic!(get_statistic(CH), x) end ``` -------------------------------- ### Convert Phase II Data to FunctionalData Objects in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Converts the generated Phase II observation matrices into `FunctionalData` objects. This transformation is necessary to ensure compatibility with the `apply_chart` function, enabling the control chart to process the new data sequences. ```julia julia> dat_ic = FunctionalData(xs_oc, ys_ic) julia> dat_oc = FunctionalData(xs_oc, ys_oc) julia> dat_oc2 = FunctionalData(xs_oc, ys_oc2) ``` -------------------------------- ### Define AEWMA Statistic Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Defines the AEWMA (Asymptotically Exponentially Weighted Moving Average) statistic, an extension of EWMA. It includes lambda (λ), a parameter k, and the current value. This statistic is updated using Huber's score function. ```julia mutable struct AEWMA <: AbstractStatistic λ::Float64 k::Float64 value::Float64 end ``` -------------------------------- ### Initialize Risk-Adjusted CUSUM Chart in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Initializes a RiskAdjustedCUSUM control chart object using the estimated logistic regression model and specified parameters (Δ = 0.75). This object is configured for monitoring potential increases in post-operative mortality. ```julia Random.seed!(239184367) STAT = RiskAdjustedCUSUM(Δ = 0.75, model = mod, response = :status) ``` -------------------------------- ### Optimize EWMA Smoothing Constant Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Optimizes the smoothing constant (lambda) for the EWMA control chart against a persistent mean shift of delta=2.0. It uses a simulation-based approach (`run_sim_oc`) and the BOBYQA optimizer with specified bounds. ```julia delta = 2.0 rlsim_oc = x -> run_sim_oc(x, shift = delta) settings = OptSettings(verbose = false, minpar = [0.001], maxpar = [0.99]) opt = optimize_design!(CH, rlsim_oc, settings, optimizer = :LN_BOBYQA) print(opt) ``` -------------------------------- ### Define NEWMA Control Chart Parameters in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Defines the parameters for a NEWMA (Nonparametric Exponentially Weighted Moving Average) control chart. It specifies the smoothing parameter (lambda), the estimated in-control profile (g), and the in-control data (dat). This sets up the statistical monitoring procedure. ```julia julia> STAT = NEWMA(0.2, g, dat) ``` -------------------------------- ### Standardize Observations for Control Charts in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Applies standardization to the observations using the `LocationScaleStatistic` type. This involves transforming the raw data $\bm{X}_t$ to $\bm{Z}_t = Sigma_0^{-1 / 2}m{X}_t$ to account for both the mean and covariance of the distribution, making them suitable for the defined control charts. ```julia Σ_sqm1 = inv(sqrt(Σ)) EST_STATS = Tuple(LocationScaleStatistic(s, μ, Σ_sqm1) for s in STATS) ``` -------------------------------- ### Split Data for Phase I and Phase II Monitoring in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Divides the loaded cardiac surgery data into two sets: Phase I (first two years) for model estimation and Phase II (following year) for prospective monitoring. This separation is standard practice in statistical process control. ```julia dat_ic = dat[dat.date .<= 730, :] dat_oc = dat[730 .< dat.date .<= 1095, :] ``` -------------------------------- ### Define ControlChart Type Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Defines the mutable ControlChart struct which serves as the fundamental type for control charts in the package. It holds the monitoring statistic, control limits, nominal properties, and a time point indicator. This structure allows for dynamic updates via the update_chart! function. ```julia mutable struct ControlChart{STAT, LIM, NOM, PH2} <: AbstractChart{STAT, LIM, NOM, PH2} stat::STAT limit::LIM nominal::NOM phase2::PH2 t::Int end ``` -------------------------------- ### Set Control Limit Parameters in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Defines the parameters for the control chart's control limit calculation. ARL (Average Run Length) is set to 1000, and a one-sided fixed limit is established with a value of 1.0. ```julia NOM = ARL(1000) LIM = OneSidedFixedLimit(1.0, true) ``` -------------------------------- ### Apply Control Chart and Plot Results in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Applies the configured control chart to the Phase II data to generate monitoring results (`proc`). Subsequently, it plots the monitoring series using the `plot_series` function, labeling the axes appropriately for time (`t`) and the control statistic (`C_t`). ```julia proc = apply_chart(CH, dat_oc) plt = plot_series(proc, dpi = 300, label = "", xlab = L"t", ylab = L"C_t") ``` -------------------------------- ### Define AR1Statistic for Residuals Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Defines a specialized `AR1Statistic` struct inheriting from `ResidualStatistic` to handle AR(1) residuals. It stores the EWMA statistic, the AR(1) coefficient (phi), and the previous observation (ym1). The `residual!` function calculates the one-step-ahead prediction and updates the previous observation. ```julia import StatisticalProcessMonitoring.residual!, StatisticalProcessMonitoring.new_data! mutable struct AR1Statistic{S} <: ResidualStatistic stat::S phi::Float64 ym1::Float64 end function residual!(x, S::AR1Statistic) yhat = x - S.phi * S.ym1 S.ym1 = x return yhat end ``` -------------------------------- ### Set Nominal In-Control Average Run Length in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Sets the target in-control Average Run Length (ARL) for the control chart to 500. This parameter defines the expected number of samples before a false alarm is triggered when the process is operating normally. ```julia julia> NM = ARL(500) ``` -------------------------------- ### Define Fixed Upper Control Limits in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Initializes a tuple of `OneSidedFixedLimit` objects, each with an upper control limit of 1.0. This defines the threshold that the control chart statistics must not cross to signal an alarm. ```julia LIMS = Tuple(OneSidedFixedLimit(1.0, true) for _ in 1:4) ``` -------------------------------- ### Define EWMA Statistic Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Defines the EWMA (Exponentially Weighted Moving Average) statistic, a common type of monitoring statistic. It includes a weighting factor lambda (λ) and the current value of the statistic. This struct is used within the ControlChart. ```julia mutable struct EWMA <: AbstractStatistic λ::Float64 value::Float64 end ``` -------------------------------- ### Plot Control Chart Series in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Generates a plot of the processed control chart series. The plot displays the behavior of the charts over time, with distinct subtitles for each MCUSUM and MEWMC chart. The axes are labeled for clarity. ```julia plt = plot_series(proc, dpi = 300, label = "", xlab = L"t", ylab = L"C_t", subtitles = ["MCUSUM k = 0.25", "MCUSUM k = 0.5", "MCUSUM k = 1", "MEWMC λ = 0.2"]) ``` -------------------------------- ### Estimate In-Control Profile using LOESS in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Estimates the in-control functional profile ($\hat{f}$) using the LOESS (Locally Estimated Scatterplot Smoothing) estimator. This smoothed profile is crucial for defining the control chart's center line and limits. The plot visualizes the estimated profile against the raw data. ```julia julia> using Loess julia> g = loess(vec(xs), vec(ys), span = 0.3) julia> plt = plot(minimum(xs):0.01:maximum(xs), (x) -> predict(g, x), linewidth = 1.75, label = L"\hat{f}(x)", xlab = L"x", ylab = L"y", dpi = 300) julia> scatter!(plt, vec(xs), vec(ys), markersize = 1, label = "", colour = "black") ``` -------------------------------- ### Set Nominal Median Run Length in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_mean_covariance.md Defines the desired median run length (MRL) for the control charts, setting it to 200 with a probability of 0.5. This parameter is used in determining appropriate control limits. ```julia NM = QRL(200, 0.5) ``` -------------------------------- ### Define FunctionalData Type Alias in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_nonparametric_profiles.md Defines a type alias `FunctionalData` for a `Vector` of `FunctionalObservation` objects. This alias simplifies the representation of functional data within the StatisticalProcessMonitoring.jl package, making the code more readable and concise. ```julia const FunctionalData{A,B} = Vector{FunctionalObservation{A,B}} where {A,B} ``` -------------------------------- ### Update EWMA Statistic Value Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Specialized function to update the value of an EWMA statistic based on a new observation 'x' and the statistic's lambda (λ). It applies the EWMA formula: value = (1-λ)*value + λ*x. ```julia function update_statistic!(stat::EWMA, x::Real) stat.value = (1.0 - stat.λ) * stat.value + stat.λ * x end ``` -------------------------------- ### Plot Control Chart Series Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_autoregressive.md Plots the processed control chart data, labeling the x-axis as 't' and the y-axis as 'C_t' using LaTeX strings. A vertical line is added at tau=50 to indicate the time of the mean shift. ```julia plt = plot_series(proc, dpi = 300, label = "", xlab = L"t", ylab = L"C_t") vline!(plt, [tau], label = "tau", linestyle = :dot, colour = "black") ``` -------------------------------- ### Estimate Logistic Regression Model in Julia Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/monitoring_risk_adjusted.md Estimates a logistic regression model using the Phase I data to predict post-operative mortality. The model includes 'Parsonnet' score as a covariate and 'surgeon' as a random intercept, accounting for variations between surgeons. MixedModels.jl is used for this purpose. ```julia using MixedModels mod = fit(MixedModel, @formula(status ~ Parsonnet + (1|surgeon)), dat_ic, Bernoulli()) print(mod) ``` -------------------------------- ### Update AEWMA Statistic Value Source: https://github.com/dedzago/statisticalprocessmonitoring.jl/blob/main/docs/src/getting-started.md Specialized function to update the value of an AEWMA statistic. It updates the statistic's value by adding the result of the Huber score function applied to the difference between the new observation 'x' and the current statistic value, using parameters λ and k. ```julia function update_statistic!(stat::AEWMA, x::Real) stat.value = stat.value + huber(x - stat.value, stat.λ, stat.k) end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.