---
title: "Rspc user guide"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteEngine{knitr::knitr}
  %\VignetteIndexEntry{Rspc quick guide}
  %\VignetteEncoding{UTF-8}
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## SPC quick guide

Rspc is an implementation of nelson rules for control charts in R. The RSPC package implements some Statistical Process Control methods, namely Levey-Jennings type of I (individuals) chart, Shewhart C (count) chart and Nelson rules. Typical workflow is taking the time series, specify the control limits, and list of Nelson rules you want to evaluate. There are several options how to modify the rules (one sided limits, numerical parameters of rules, etc.). Package is also capable of calculating the control limits from the data (so far only for i-chart and c-chart are implemented).


## Instalation

So far the package is not at CRAN. Only way to install it is from the archive file:

```{r instalation, eval=FALSE}
path = "path_to_the_archive/Rspc_x.x.x.tar.gz" # set the path to the archive
install.packages(path, repos = NULL, type="source") # install package
```

## Functions

* `EvaluateRules()`

* `SetParameters()`

* `CalculateLimits()`

* `CalculateZoneBorders()`

* `NelsonRules()`

* `Rule1()-Rule8()`

There are two main functions: EvaluateRules() and SetParameters(). The first one starts the evaluation, the second one could be used for customizing the rules 


### EvaluateRules()

This function evaluates the Nelson rules (1 through 8) selected by the user. It uses output from the CalculateLimits function. 

```{r}
#load the package
library(Rspc)
# generate some data
data <- rnorm(10) 
# evaluate all the Nelson rules, 
# calculate control limits from data (lcl, cl, ucl are not provided) using formulas for i-chart, 
# don't modify any rule parameters (parRules = NULL)
EvaluateRules(x = data, type = 'i', whichRules = 1:8, lcl = NA, cl = NA, ucl = NA)
```

The vector 'x' is the only compulsory parameter, default values for the rest is as above. The code above is equivalent to data = rnorm(100); EvaluateRules(x = data).

### SetParameters()

Optional parameters are generated using SetParameters() function.
Function provides flexibility to change some default settings in the Rules 1-8 functions, such as the number of data points matching a non-random pattern for identification of run rules violations, or whether to use ‘jmp’ or ‘minitab’ convention for rules 3 and 4. 

```{r}
pars = SetParameters()
print(pars)
```

Detailed information about optional pamateres are written in package documentation. If you want to change the number of consecutive points for this rule3 from 6 to 5 ...


```{r}
pars$Rule3$nPoints = 5
```

Modified **pars** is then plugged to **EvaluateRules(…,parRules = pars)** function call.

### CalculateLimits()

If no predefined limits are entered, this function calculates k sigma limits from the center line, where k can take the values 2 or 3 (3 by default) as defined by user, the sigma is calculated as the standard deviation of the numeric data values, and the center line is calculated as the average of the numeric data values. Providing a center line as input overrides calculation of the mean from the input data. Providing any of the limits as input overrides calculation of the standard deviation from the input data.

```{r}
CalculateLimits(x = rnorm(10), lcl = NA, cl = 100, ucl = NA, type = 'i')
```


### CalculateZoneBorders()

This function determines the values at ±1 sigma, ±2 sigma and ±3 sigma that define the zones required for the evaluation of some Nelson Rules. It requires the input from the CalculateLimits function.

```{r}
limits = CalculateLimits(x = rnorm(10), lcl = NA, cl = NA, ucl = NA, type = 'i')
CalculateZoneBorders(limits, controlLimitDistance = 3)
```
Limits is object created by CalculateLimits() function.

### NelsonRules()

Wrapper function to evaluate the Rule 1-Rule 8 functions

### Rule1()-Rule8()

These functions detect and flag violation of Nelson rules test 1 through 8 on numeric data values, relative to the center line and limits as passed by the CalculateLimits function, and delimited zones as passed by the CalculateZoneBorders function.  For definition of the Nelson Rules, see Appendix 1. These functions are all called within the NelsonRules function.

### For more information about parameters read the package manual. 











