# Examples of discrete patterns

#### Updated: October 25, 2018; Created October 24, 2018

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:

require(FunChisq)
f1 <- matrix(c(5,0,0,0,0,7,0,4,0), nrow=3)
f1
#>      [,1] [,2] [,3]
#> [1,]    5    0    0
#> [2,]    0    0    4
#> [3,]    0    7    0
plot_table(f1, ylab="X (row)", xlab="Y (column)",
main="f1: significant perfect function")

fun.chisq.test(f1)
#>
#>  Functional chi-squared test
#>
#> data:  f1
#> statistic = 31.125, parameter = 4, p-value = 2.887e-06
#> sample estimates:
#> non-constant function index xi.f
#>                                1

An significant perfect many-to-one functional pattern:

f2 <- matrix(c(7,0,3,0,6,0), nrow=3)
f2
#>      [,1] [,2]
#> [1,]    7    0
#> [2,]    0    6
#> [3,]    3    0
plot_table(f2, col="salmon", ylab="X (row)", xlab="Y (column)",
main="f2: sigificant perfect\nmany-to-one function")

fun.chisq.test(f2)
#>
#>  Functional chi-squared test
#>
#> data:  f2
#> statistic = 15, parameter = 2, p-value = 0.0005531
#> sample estimates:
#> non-constant function index xi.f
#>                                1

An insignificant perfect functional pattern:

f3 <- matrix(c(5,10,0,0,0,1), nrow=3)
f3
#>      [,1] [,2]
#> [1,]    5    0
#> [2,]   10    0
#> [3,]    0    1
plot_table(f3, col="deepskyblue4", ylab="X (row)", xlab="Y (column)",
main="f3: insigificant perfect function")

fun.chisq.test(f3)
#>
#>  Functional chi-squared test
#>
#> data:  f3
#> statistic = 3.75, parameter = 2, p-value = 0.1534
#> sample estimates:
#> non-constant function index xi.f
#>                                1

A perfect constant functional pattern:

f4 <- matrix(c(5,4,7,0,0,0,0,0,0), nrow=3)
f4
#>      [,1] [,2] [,3]
#> [1,]    5    0    0
#> [2,]    4    0    0
#> [3,]    7    0    0
plot_table(f4, col="brown", ylab="X (row)", xlab="Y (column)",
main="f4: insignificant\nperfect constant function")

fun.chisq.test(f4)
#>
#>  Functional chi-squared test
#>
#> data:  f4
#> statistic = 0, parameter = 4, p-value = 1
#> sample estimates:
#> non-constant function index xi.f
#>                                0

## 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.

p1 <- matrix(c(5,1,5,1,5,1,1,0,1), nrow=3)
p1
#>      [,1] [,2] [,3]
#> [1,]    5    1    1
#> [2,]    1    5    0
#> [3,]    5    1    1
plot_table(p1, ylab="X (row)", xlab="Y (column)",
main="p1: significant function pattern")

fun.chisq.test(p1)
#>
#>  Functional chi-squared test
#>
#> data:  p1
#> statistic = 10.043, parameter = 4, p-value = 0.03971
#> sample estimates:
#> non-constant function index xi.f
#>                         0.544288
p2=matrix(c(5,1,1,1,5,0,5,1,1), nrow=3)
p2
#>      [,1] [,2] [,3]
#> [1,]    5    1    5
#> [2,]    1    5    1
#> [3,]    1    0    1
plot_table(p2, col="red3", ylab="X (row)", xlab="Y (column)",
main="p2: insignificant\nfunction pattern")

fun.chisq.test(p2)
#>
#>  Functional chi-squared test
#>
#> data:  p2
#> statistic = 8.3805, parameter = 4, p-value = 0.07859
#> sample estimates:
#> non-constant function index xi.f
#>                        0.4582991
p3=matrix(c(5,1,1,1,5,0,9,1,1), nrow=3)
p3
#>      [,1] [,2] [,3]
#> [1,]    5    1    9
#> [2,]    1    5    1
#> [3,]    1    0    1
plot_table(p3, col="orange", ylab="X (row)", xlab="Y (column)",
main="p3: significant function pattern")

fun.chisq.test(p3)
#>
#>  Functional chi-squared test
#>
#> data:  p3
#> statistic = 10.221, parameter = 4, p-value = 0.03686
#> sample estimates:
#> non-constant function index xi.f
#>                        0.4701105
p4=p1*sum(p3)/sum(p1)
p4
#>      [,1] [,2] [,3]
#> [1,]  6.0  1.2  1.2
#> [2,]  1.2  6.0  0.0
#> [3,]  6.0  1.2  1.2
plot_table(p4, col="purple", ylab="X (row)", xlab="Y (column)",
main="p4: significant function pattern")

fun.chisq.test(p4)
#>
#>  Functional chi-squared test
#>
#> data:  p4
#> statistic = 12.051, parameter = 4, p-value = 0.01697
#> sample estimates:
#> non-constant function index xi.f
#>                         0.544288