---
title: "Multivariate chart"
author: ""
date: ""
output: html_document
---

```{r, setup, include=FALSE}
require(mosaic)   # Load additional packages here 

# Some customization.  You can alter or delete as desired (if you know what you are doing).
trellis.par.set(theme=theme.mosaic()) # change default color scheme for lattice
knitr::opts_chunk$set(
  tidy=FALSE,     # display code as typed
  size="small")   # slightly smaller font for code
```

```{r message=FALSE}
library(qcc)
```

## Multivariate chart
The built-in dataset "boiler" is a dataframe. Each row has
temperature readings from eight configured burners on a boiler.

* We have 25 rows of temperature readings.

```{r}
data(boiler)
head(boiler,5)
dim(boiler)
```

* Use the function `mqcc`  to make a multivariate $T^2$-chart.
      Include the argument `type="T2.single"` to tell that
      sample size is equal to one.
      
```{r eval=FALSE}
q <- mqcc(boiler,type="T2.single")
```

* Remark that obsNumber=9 is outlying.

* Standardized values for the variables are obtained by the the function scale.

```{r eval=FALSE}
Zboil <- scale(boiler)
```

* The Z-value(-2.277) for obsNumber=9 and temperature=t3 is quite large

```{r eval=FALSE}
Zboil[9,]
```

* Correlations are obtained by the function cor.

```{r eval=FALSE}
Cboil <- cor(boiler)
Cboil[3,]
```

* The correlation between t3 and t1 is quite high(0.5841), so let us
limit the analysis to these two temperatures, i.e. we restrict to the
dataframe boil.

```{r eval=FALSE}
boil <- boiler[,c(1,3)]
q0   <- mqcc(boil,type="T2.single")
```

* We  plot the observations `t3` versus `t1` together with the least squares line.

```{r eval=FALSE}
xyplot(t3 ~ t1, data = boil, type = c("p", "r"), auto.key = TRUE)
```

* We calculate Zyx - the standardized vertical distances to the line.

```{r eval=FALSE}
model     <- lm(t3~t1,data=boil)   #fit the linear model
res       <- model$residuals       #is an array containing the residuals
stdev     <- summary(model)$sigma  #the standard deviation of the residuals 
Zyx       <- res/stdev             #the standardized vertical distances to the line.
```

* We calculate Zx - the standardized values of t1.

```{r eval=FALSE}
Zx <- scale(boil$t1)
```

* We calculate T2 - the T^2 statistic - and compare to q0.

```{r eval=FALSE}
m      <- dim(boil)[1]           # number of samples
T2     <- (m-1)*Zyx^2/(m-2)+Zx^2 # The T-square statistic
T2     <- t(T2)                  # change T2 from column to row vector
rbind(T2,q0$stat)[,1:9]          # compare ...
```
