---
title: "Examples of discrete patterns"
author: "Joe Song"
date: "Updated: October 25, 2018; Created October 24, 2018"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Examples of discrete patterns}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

We showcase applications of the functinal chi-square (FunChisq) test on several types of discrete patterns. Here we use the row to represent independent variable $X$ and column for the dependent variable $Y$. The FunChisq test statistically determines whether $Y$ is a function of $X$.

A pattern represents a *perfect* function if and only if function index is 1; otherwise, the pattern represents an *imperfect* function. A function pattern is *statistically significant* if the p-value from the FunChisq test is less than or equal to 0.05.

## Perfect functional patterns

A significant perfect functional pattern:
```{r}
require(FunChisq)
f1 <- matrix(c(5,0,0,0,0,7,0,4,0), nrow=3)
f1
plot_table(f1, ylab="X (row)", xlab="Y (column)", 
           main="f1: significant perfect function")
fun.chisq.test(f1)
```

An significant perfect many-to-one functional pattern:
```{r}
f2 <- matrix(c(7,0,3,0,6,0), nrow=3)
f2
plot_table(f2, col="salmon", ylab="X (row)", xlab="Y (column)",
           main="f2: sigificant perfect\nmany-to-one function")
fun.chisq.test(f2)
```

An insignificant perfect functional pattern:
```{r}
f3 <- matrix(c(5,10,0,0,0,1), nrow=3)
f3
plot_table(f3, col="deepskyblue4", ylab="X (row)", xlab="Y (column)",
           main="f3: insigificant perfect function")
fun.chisq.test(f3)
```


A perfect constant functional pattern:
```{r}
f4 <- matrix(c(5,4,7,0,0,0,0,0,0), nrow=3)
f4
plot_table(f4, col="brown", ylab="X (row)", xlab="Y (column)",
           main="f4: insignificant\nperfect constant function")
fun.chisq.test(f4)
```


## Imperfect patterns

We contrast four imperfect patterns to illustrate the differences in FunChisq test results. `p1` and `p4` represent the same non-monotonic function pattern in different sample sizes; `p2` is the transpose of `p1`, no longer functional; and `p3` is another non-functional pattern. Among the first three examples, `p3` is the most statistically significant, but `p1` has the highest function index $\xi_f$. This can be explained by a larger sample size but a smaller effect in `p3` than `p1`. However, when `p1` is linearly scaled to `p4` to have exactly the same sample size with `p3`, both the $p$-value and the function index $\xi_f$ favor `p4` over `p3` for representing a stronger function.

```{r}
p1 <- matrix(c(5,1,5,1,5,1,1,0,1), nrow=3)
p1
plot_table(p1, ylab="X (row)", xlab="Y (column)", 
           main="p1: significant function pattern")
fun.chisq.test(p1)
```

```{r}
p2=matrix(c(5,1,1,1,5,0,5,1,1), nrow=3)
p2
plot_table(p2, col="red3", ylab="X (row)", xlab="Y (column)",
           main="p2: insignificant\nfunction pattern")
fun.chisq.test(p2)
```

```{r}
p3=matrix(c(5,1,1,1,5,0,9,1,1), nrow=3)
p3
plot_table(p3, col="orange", ylab="X (row)", xlab="Y (column)",
           main="p3: significant function pattern")
fun.chisq.test(p3)
```

```{r}
p4=p1*sum(p3)/sum(p1)
p4
plot_table(p4, col="purple", ylab="X (row)", xlab="Y (column)",
           main="p4: significant function pattern")
fun.chisq.test(p4)
```