# Category Archives: Statistics

## Correlation and permutation tests

Permutation tests can be used with many different statistics, including correlation.

For this example, you will need the incanter.core, incanter.stats, incanter.charts, and incanter.datasets libraries. The incanter.datasets library contains sample data sets.

``(use '(incanter core stats charts datasets))``

For more information on using these packages, see the matrices, datasets, and sample plots pages on the Incanter wiki.

``(def data (to-matrix (get-dataset :us-arrests)))``

Now extract the assault and urban population columns:

``````(def assault (sel data :cols 2))
(def urban-pop (sel data :cols 3))``````

Calculate the correlation between assaults and urban-pop:

``(correlation assault urban-pop)``

The sample correlation is 0.259, but is this value statistically significantly different from zero? To answer that, we will perform a permutation test by creating 5000 permuted samples of the two variables and then calculate the correlation between them for each sample. These 5000 values represent the distribution of correlations when the null hypothesis is true (i.e. the two variables are not correlated). We can then compare the original sample correlation with this distribution to determine if the value is too extreme to be explained by null hypothesis.

Start by generating 5000 samples of permuted values for each variable:

``````(def permuted-assault (sample-permutations 5000 assault))
(def permuted-urban-pop (sample-permutations 5000 urban-pop))``````

Now calculate the correlation between the two variables in each sample:

``````(def permuted-corrs (map correlation
permuted-assault
permuted-urban-pop))``````

View a histogram of the correlations

``(view (histogram permuted-corrs))`` And check out the mean, standard deviation, and a 95% interval for the null distribution:

``(mean permuted-corrs)``

The mean is near zero, -0.001,

``(sd permuted-corrs)``

the standard deviation is 0.14,

``(quantile permuted-corrs :probs [0.025 0.975])``

and the values returned by the quantile function are (-0.278 0.289), which means the original sample correlation of 0.259 is within the 95% interval of the null distribution, so the correlation is not statistically significant at an alpha level of 0.05.

The complete code for this example is found here.

## Principal components analysis

Principal components analysis (PCA) is often used to reduce the number of variables, or dimensions, in a data set in order to simplify analysis or aid in visualization. The following is an example of using it to visualize Fisher’s five-dimensional iris data on a two-dimensional scatter plot, revealing patterns that would be difficult to detect otherwise.

First, principal components will be extracted from the four continuous variables (sepal-width, sepal-length, petal-width, and petal-length); next, these variables will be projected onto the subspace formed by the first two components extracted; and then this two-dimensional data will be shown on a scatter-plot. The fifth dimension (species) will be represented by the color of the points on the scatter-plot.

For this example, you will need the incanter.core, incanter.stats, incanter.charts, and incanter.datasets libraries. The incanter.datasets library contains sample data sets.

``(use '(incanter core stats charts datasets))``

For more information on using these packages see the matrices, datasets, and sample plots pages on the Incanter wiki.

Next, load the iris dataset and view it.

``````(def iris (to-matrix (get-dataset :iris)))
(view iris)`````` Then, extract the columns to use in the PCA,

``(def X (sel iris :cols (range 4)))``

and extract the “species” column for identifying the group.

``(def species (sel iris :cols 4))``

Run the PCA on the first four columns only

``(def pca (principal-components X))``

Extract the first two principal components

``````(def components (:rotation pca))
(def pc1 (sel components :cols 0))
(def pc2 (sel components :cols 1))``````

Project the four dimension of the iris data onto the first two principal components

``````(def x1 (mmult X pc1))
(def x2 (mmult X pc2))``````

Now plot the transformed data, coloring each species a different color

``````(view (scatter-plot x1 x2
:group-by species
:x-label "PC1"
:y-label "PC2"
:title "Iris PCA"))`````` The complete code for this example can be found here.