\[ %% % Add your macros here; they'll be included in pdf and html output. %% \newcommand{\R}{\mathbb{R}} % reals \newcommand{\E}{\mathbb{E}} % expectation \renewcommand{\P}{\mathbb{P}} % probability \DeclareMathOperator{\logit}{logit} \DeclareMathOperator{\logistic}{logistic} \DeclareMathOperator{\SE}{SE} \DeclareMathOperator{\sd}{sd} \DeclareMathOperator{\var}{var} \DeclareMathOperator{\cov}{cov} \DeclareMathOperator{\cor}{cor} \DeclareMathOperator{\Normal}{Normal} \DeclareMathOperator{\MVN}{MVN} \DeclareMathOperator{\LogNormal}{logNormal} \DeclareMathOperator{\Poisson}{Poisson} \DeclareMathOperator{\Beta}{Beta} \DeclareMathOperator{\Binom}{Binomial} \DeclareMathOperator{\Gam}{Gamma} \DeclareMathOperator{\Exp}{Exponential} \DeclareMathOperator{\Cauchy}{Cauchy} \DeclareMathOperator{\Unif}{Unif} \DeclareMathOperator{\Dirichlet}{Dirichlet} \DeclareMathOperator{\Wishart}{Wishart} \DeclareMathOperator{\StudentsT}{StudentsT} \DeclareMathOperator{\Weibull}{Weibull} \newcommand{\given}{\;\vert\;} \]

Categorical data and permutation tests

Peter Ralph

21 January 2020 – Advanced Biological Statistics

Hair and eye color

Hair and Eye color

data(HairEyeColor)
HairEyeColor             package:datasets              R Documentation

Hair and Eye Color of Statistics Students

Description:

     Distribution of hair and eye color and sex in 592 statistics
     students.

Usage:

     HairEyeColor
     
Format:

     A 3-dimensional array resulting from cross-tabulating 592
     observations on 3 variables.  The variables and their levels are
     as follows:

       No  Name  Levels                    
        1  Hair  Black, Brown, Red, Blond  
        2  Eye   Brown, Blue, Hazel, Green 
        3  Sex   Male, Female              
      
Details:

     The Hair x Eye table comes from a survey of students at the
     University of Delaware reported by Snee (1974).  The split by
     ‘Sex’ was added by Friendly (1992a) for didactic purposes.

     This data set is useful for illustrating various techniques for
     the analysis of contingency tables, such as the standard
     chi-squared test or, more generally, log-linear modelling, and
     graphical methods such as mosaic plots, sieve diagrams or
     association plots.

Source:

     <URL:
     http://euclid.psych.yorku.ca/ftp/sas/vcd/catdata/haireye.sas>

     Snee (1974) gives the two-way table aggregated over ‘Sex’.  The
     ‘Sex’ split of the ‘Brown hair, Brown eye’ cell was changed to
     agree with that used by Friendly (2000).

References:

     Snee, R. D. (1974).  Graphical display of two-way contingency
     tables.  _The American Statistician_, *28*, 9-12.  doi:
     10.2307/2683520 (URL: http://doi.org/10.2307/2683520).
## , , Sex = Male
## 
##        Eye
## Hair    Brown Blue Hazel Green
##   Black    32   11    10     3
##   Brown    53   50    25    15
##   Red      10   10     7     7
##   Blond     3   30     5     8
## 
## , , Sex = Female
## 
##        Eye
## Hair    Brown Blue Hazel Green
##   Black    36    9     5     2
##   Brown    66   34    29    14
##   Red      16    7     7     7
##   Blond     4   64     5     8
haireye <- as.data.frame(HairEyeColor)
names(haireye) <- tolower(names(haireye))
names(haireye)[names(haireye) == "freq"] <- "number"
haireye
##     hair   eye    sex number
## 1  Black Brown   Male     32
## 2  Brown Brown   Male     53
## 3    Red Brown   Male     10
## 4  Blond Brown   Male      3
## 5  Black  Blue   Male     11
## 6  Brown  Blue   Male     50
## 7    Red  Blue   Male     10
## 8  Blond  Blue   Male     30
## 9  Black Hazel   Male     10
## 10 Brown Hazel   Male     25
## 11   Red Hazel   Male      7
## 12 Blond Hazel   Male      5
## 13 Black Green   Male      3
## 14 Brown Green   Male     15
## 15   Red Green   Male      7
## 16 Blond Green   Male      8
## 17 Black Brown Female     36
## 18 Brown Brown Female     66
## 19   Red Brown Female     16
## 20 Blond Brown Female      4
## 21 Black  Blue Female      9
## 22 Brown  Blue Female     34
## 23   Red  Blue Female      7
## 24 Blond  Blue Female     64
## 25 Black Hazel Female      5
## 26 Brown Hazel Female     29
## 27   Red Hazel Female      7
## 28 Blond Hazel Female      5
## 29 Black Green Female      2
## 30 Brown Green Female     14
## 31   Red Green Female      7
## 32 Blond Green Female      8

Questions:

  1. Are hair and eye color independent in this sample?
  2. Do hair and eye color proportions differ by sex?

Method 2: Permutation

recall the \(p\)-value

… is the probability of seeing something at least as extreme as what we saw in the data, if the null hypothesis (model) is true.

A permutation test estimates the “probability … under the null hypothesis” part.

  • “the null hypothesis” is that hair and eye color are independent.
  • we can use… the chi-squared statistic for “at least as extreme”?

First, “individualize” the data:

long_haireye <- haireye[rep(1:nrow(haireye), haireye$number), 
                        c("hair", "eye", "sex")]
stopifnot(nrow(long_haireye) == sum(haireye$number))
long_haireye
##        hair   eye    sex
## 1     Black Brown   Male
## 1.1   Black Brown   Male
## 1.2   Black Brown   Male
## 1.3   Black Brown   Male
## 1.4   Black Brown   Male
## 1.5   Black Brown   Male
## 1.6   Black Brown   Male
## 1.7   Black Brown   Male
## 1.8   Black Brown   Male
## 1.9   Black Brown   Male
## 1.10  Black Brown   Male
## 1.11  Black Brown   Male
## 1.12  Black Brown   Male
## 1.13  Black Brown   Male
## 1.14  Black Brown   Male
## 1.15  Black Brown   Male
## 1.16  Black Brown   Male
## 1.17  Black Brown   Male
## 1.18  Black Brown   Male
## 1.19  Black Brown   Male
## 1.20  Black Brown   Male
## 1.21  Black Brown   Male
## 1.22  Black Brown   Male
## 1.23  Black Brown   Male
## 1.24  Black Brown   Male
## 1.25  Black Brown   Male
## 1.26  Black Brown   Male
## 1.27  Black Brown   Male
## 1.28  Black Brown   Male
## 1.29  Black Brown   Male
## 1.30  Black Brown   Male
## 1.31  Black Brown   Male
## 2     Brown Brown   Male
## 2.1   Brown Brown   Male
## 2.2   Brown Brown   Male
## 2.3   Brown Brown   Male
## 2.4   Brown Brown   Male
## 2.5   Brown Brown   Male
## 2.6   Brown Brown   Male
## 2.7   Brown Brown   Male
## 2.8   Brown Brown   Male
## 2.9   Brown Brown   Male
## 2.10  Brown Brown   Male
## 2.11  Brown Brown   Male
## 2.12  Brown Brown   Male
## 2.13  Brown Brown   Male
## 2.14  Brown Brown   Male
## 2.15  Brown Brown   Male
## 2.16  Brown Brown   Male
## 2.17  Brown Brown   Male
## 2.18  Brown Brown   Male
## 2.19  Brown Brown   Male
## 2.20  Brown Brown   Male
## 2.21  Brown Brown   Male
## 2.22  Brown Brown   Male
## 2.23  Brown Brown   Male
## 2.24  Brown Brown   Male
## 2.25  Brown Brown   Male
## 2.26  Brown Brown   Male
## 2.27  Brown Brown   Male
## 2.28  Brown Brown   Male
## 2.29  Brown Brown   Male
## 2.30  Brown Brown   Male
## 2.31  Brown Brown   Male
## 2.32  Brown Brown   Male
## 2.33  Brown Brown   Male
## 2.34  Brown Brown   Male
## 2.35  Brown Brown   Male
## 2.36  Brown Brown   Male
## 2.37  Brown Brown   Male
## 2.38  Brown Brown   Male
## 2.39  Brown Brown   Male
## 2.40  Brown Brown   Male
## 2.41  Brown Brown   Male
## 2.42  Brown Brown   Male
## 2.43  Brown Brown   Male
## 2.44  Brown Brown   Male
## 2.45  Brown Brown   Male
## 2.46  Brown Brown   Male
## 2.47  Brown Brown   Male
## 2.48  Brown Brown   Male
## 2.49  Brown Brown   Male
## 2.50  Brown Brown   Male
## 2.51  Brown Brown   Male
## 2.52  Brown Brown   Male
## 3       Red Brown   Male
## 3.1     Red Brown   Male
## 3.2     Red Brown   Male
## 3.3     Red Brown   Male
## 3.4     Red Brown   Male
## 3.5     Red Brown   Male
## 3.6     Red Brown   Male
## 3.7     Red Brown   Male
## 3.8     Red Brown   Male
## 3.9     Red Brown   Male
## 4     Blond Brown   Male
## 4.1   Blond Brown   Male
## 4.2   Blond Brown   Male
## 5     Black  Blue   Male
## 5.1   Black  Blue   Male
## 5.2   Black  Blue   Male
## 5.3   Black  Blue   Male
## 5.4   Black  Blue   Male
## 5.5   Black  Blue   Male
## 5.6   Black  Blue   Male
## 5.7   Black  Blue   Male
## 5.8   Black  Blue   Male
## 5.9   Black  Blue   Male
## 5.10  Black  Blue   Male
## 6     Brown  Blue   Male
## 6.1   Brown  Blue   Male
## 6.2   Brown  Blue   Male
## 6.3   Brown  Blue   Male
## 6.4   Brown  Blue   Male
## 6.5   Brown  Blue   Male
## 6.6   Brown  Blue   Male
## 6.7   Brown  Blue   Male
## 6.8   Brown  Blue   Male
## 6.9   Brown  Blue   Male
## 6.10  Brown  Blue   Male
## 6.11  Brown  Blue   Male
## 6.12  Brown  Blue   Male
## 6.13  Brown  Blue   Male
## 6.14  Brown  Blue   Male
## 6.15  Brown  Blue   Male
## 6.16  Brown  Blue   Male
## 6.17  Brown  Blue   Male
## 6.18  Brown  Blue   Male
## 6.19  Brown  Blue   Male
## 6.20  Brown  Blue   Male
## 6.21  Brown  Blue   Male
## 6.22  Brown  Blue   Male
## 6.23  Brown  Blue   Male
## 6.24  Brown  Blue   Male
## 6.25  Brown  Blue   Male
## 6.26  Brown  Blue   Male
## 6.27  Brown  Blue   Male
## 6.28  Brown  Blue   Male
## 6.29  Brown  Blue   Male
## 6.30  Brown  Blue   Male
## 6.31  Brown  Blue   Male
## 6.32  Brown  Blue   Male
## 6.33  Brown  Blue   Male
## 6.34  Brown  Blue   Male
## 6.35  Brown  Blue   Male
## 6.36  Brown  Blue   Male
## 6.37  Brown  Blue   Male
## 6.38  Brown  Blue   Male
## 6.39  Brown  Blue   Male
## 6.40  Brown  Blue   Male
## 6.41  Brown  Blue   Male
## 6.42  Brown  Blue   Male
## 6.43  Brown  Blue   Male
## 6.44  Brown  Blue   Male
## 6.45  Brown  Blue   Male
## 6.46  Brown  Blue   Male
## 6.47  Brown  Blue   Male
## 6.48  Brown  Blue   Male
## 6.49  Brown  Blue   Male
## 7       Red  Blue   Male
## 7.1     Red  Blue   Male
## 7.2     Red  Blue   Male
## 7.3     Red  Blue   Male
## 7.4     Red  Blue   Male
## 7.5     Red  Blue   Male
## 7.6     Red  Blue   Male
## 7.7     Red  Blue   Male
## 7.8     Red  Blue   Male
## 7.9     Red  Blue   Male
## 8     Blond  Blue   Male
## 8.1   Blond  Blue   Male
## 8.2   Blond  Blue   Male
## 8.3   Blond  Blue   Male
## 8.4   Blond  Blue   Male
## 8.5   Blond  Blue   Male
## 8.6   Blond  Blue   Male
## 8.7   Blond  Blue   Male
## 8.8   Blond  Blue   Male
## 8.9   Blond  Blue   Male
## 8.10  Blond  Blue   Male
## 8.11  Blond  Blue   Male
## 8.12  Blond  Blue   Male
## 8.13  Blond  Blue   Male
## 8.14  Blond  Blue   Male
## 8.15  Blond  Blue   Male
## 8.16  Blond  Blue   Male
## 8.17  Blond  Blue   Male
## 8.18  Blond  Blue   Male
## 8.19  Blond  Blue   Male
## 8.20  Blond  Blue   Male
## 8.21  Blond  Blue   Male
## 8.22  Blond  Blue   Male
## 8.23  Blond  Blue   Male
## 8.24  Blond  Blue   Male
## 8.25  Blond  Blue   Male
## 8.26  Blond  Blue   Male
## 8.27  Blond  Blue   Male
## 8.28  Blond  Blue   Male
## 8.29  Blond  Blue   Male
## 9     Black Hazel   Male
## 9.1   Black Hazel   Male
## 9.2   Black Hazel   Male
## 9.3   Black Hazel   Male
## 9.4   Black Hazel   Male
## 9.5   Black Hazel   Male
## 9.6   Black Hazel   Male
## 9.7   Black Hazel   Male
## 9.8   Black Hazel   Male
## 9.9   Black Hazel   Male
## 10    Brown Hazel   Male
## 10.1  Brown Hazel   Male
## 10.2  Brown Hazel   Male
## 10.3  Brown Hazel   Male
## 10.4  Brown Hazel   Male
## 10.5  Brown Hazel   Male
## 10.6  Brown Hazel   Male
## 10.7  Brown Hazel   Male
## 10.8  Brown Hazel   Male
## 10.9  Brown Hazel   Male
## 10.10 Brown Hazel   Male
## 10.11 Brown Hazel   Male
## 10.12 Brown Hazel   Male
## 10.13 Brown Hazel   Male
## 10.14 Brown Hazel   Male
## 10.15 Brown Hazel   Male
## 10.16 Brown Hazel   Male
## 10.17 Brown Hazel   Male
## 10.18 Brown Hazel   Male
## 10.19 Brown Hazel   Male
## 10.20 Brown Hazel   Male
## 10.21 Brown Hazel   Male
## 10.22 Brown Hazel   Male
## 10.23 Brown Hazel   Male
## 10.24 Brown Hazel   Male
## 11      Red Hazel   Male
## 11.1    Red Hazel   Male
## 11.2    Red Hazel   Male
## 11.3    Red Hazel   Male
## 11.4    Red Hazel   Male
## 11.5    Red Hazel   Male
## 11.6    Red Hazel   Male
## 12    Blond Hazel   Male
## 12.1  Blond Hazel   Male
## 12.2  Blond Hazel   Male
## 12.3  Blond Hazel   Male
## 12.4  Blond Hazel   Male
## 13    Black Green   Male
## 13.1  Black Green   Male
## 13.2  Black Green   Male
## 14    Brown Green   Male
## 14.1  Brown Green   Male
## 14.2  Brown Green   Male
## 14.3  Brown Green   Male
## 14.4  Brown Green   Male
## 14.5  Brown Green   Male
## 14.6  Brown Green   Male
## 14.7  Brown Green   Male
## 14.8  Brown Green   Male
## 14.9  Brown Green   Male
## 14.10 Brown Green   Male
## 14.11 Brown Green   Male
## 14.12 Brown Green   Male
## 14.13 Brown Green   Male
## 14.14 Brown Green   Male
## 15      Red Green   Male
## 15.1    Red Green   Male
## 15.2    Red Green   Male
## 15.3    Red Green   Male
## 15.4    Red Green   Male
## 15.5    Red Green   Male
## 15.6    Red Green   Male
## 16    Blond Green   Male
## 16.1  Blond Green   Male
## 16.2  Blond Green   Male
## 16.3  Blond Green   Male
## 16.4  Blond Green   Male
## 16.5  Blond Green   Male
## 16.6  Blond Green   Male
## 16.7  Blond Green   Male
## 17    Black Brown Female
## 17.1  Black Brown Female
## 17.2  Black Brown Female
## 17.3  Black Brown Female
## 17.4  Black Brown Female
## 17.5  Black Brown Female
## 17.6  Black Brown Female
## 17.7  Black Brown Female
## 17.8  Black Brown Female
## 17.9  Black Brown Female
## 17.10 Black Brown Female
## 17.11 Black Brown Female
## 17.12 Black Brown Female
## 17.13 Black Brown Female
## 17.14 Black Brown Female
## 17.15 Black Brown Female
## 17.16 Black Brown Female
## 17.17 Black Brown Female
## 17.18 Black Brown Female
## 17.19 Black Brown Female
## 17.20 Black Brown Female
## 17.21 Black Brown Female
## 17.22 Black Brown Female
## 17.23 Black Brown Female
## 17.24 Black Brown Female
## 17.25 Black Brown Female
## 17.26 Black Brown Female
## 17.27 Black Brown Female
## 17.28 Black Brown Female
## 17.29 Black Brown Female
## 17.30 Black Brown Female
## 17.31 Black Brown Female
## 17.32 Black Brown Female
## 17.33 Black Brown Female
## 17.34 Black Brown Female
## 17.35 Black Brown Female
## 18    Brown Brown Female
## 18.1  Brown Brown Female
## 18.2  Brown Brown Female
## 18.3  Brown Brown Female
## 18.4  Brown Brown Female
## 18.5  Brown Brown Female
## 18.6  Brown Brown Female
## 18.7  Brown Brown Female
## 18.8  Brown Brown Female
## 18.9  Brown Brown Female
## 18.10 Brown Brown Female
## 18.11 Brown Brown Female
## 18.12 Brown Brown Female
## 18.13 Brown Brown Female
## 18.14 Brown Brown Female
## 18.15 Brown Brown Female
## 18.16 Brown Brown Female
## 18.17 Brown Brown Female
## 18.18 Brown Brown Female
## 18.19 Brown Brown Female
## 18.20 Brown Brown Female
## 18.21 Brown Brown Female
## 18.22 Brown Brown Female
## 18.23 Brown Brown Female
## 18.24 Brown Brown Female
## 18.25 Brown Brown Female
## 18.26 Brown Brown Female
## 18.27 Brown Brown Female
## 18.28 Brown Brown Female
## 18.29 Brown Brown Female
## 18.30 Brown Brown Female
## 18.31 Brown Brown Female
## 18.32 Brown Brown Female
## 18.33 Brown Brown Female
## 18.34 Brown Brown Female
## 18.35 Brown Brown Female
## 18.36 Brown Brown Female
## 18.37 Brown Brown Female
## 18.38 Brown Brown Female
## 18.39 Brown Brown Female
## 18.40 Brown Brown Female
## 18.41 Brown Brown Female
## 18.42 Brown Brown Female
## 18.43 Brown Brown Female
## 18.44 Brown Brown Female
## 18.45 Brown Brown Female
## 18.46 Brown Brown Female
## 18.47 Brown Brown Female
## 18.48 Brown Brown Female
## 18.49 Brown Brown Female
## 18.50 Brown Brown Female
## 18.51 Brown Brown Female
## 18.52 Brown Brown Female
## 18.53 Brown Brown Female
## 18.54 Brown Brown Female
## 18.55 Brown Brown Female
## 18.56 Brown Brown Female
## 18.57 Brown Brown Female
## 18.58 Brown Brown Female
## 18.59 Brown Brown Female
## 18.60 Brown Brown Female
## 18.61 Brown Brown Female
## 18.62 Brown Brown Female
## 18.63 Brown Brown Female
## 18.64 Brown Brown Female
## 18.65 Brown Brown Female
## 19      Red Brown Female
## 19.1    Red Brown Female
## 19.2    Red Brown Female
## 19.3    Red Brown Female
## 19.4    Red Brown Female
## 19.5    Red Brown Female
## 19.6    Red Brown Female
## 19.7    Red Brown Female
## 19.8    Red Brown Female
## 19.9    Red Brown Female
## 19.10   Red Brown Female
## 19.11   Red Brown Female
## 19.12   Red Brown Female
## 19.13   Red Brown Female
## 19.14   Red Brown Female
## 19.15   Red Brown Female
## 20    Blond Brown Female
## 20.1  Blond Brown Female
## 20.2  Blond Brown Female
## 20.3  Blond Brown Female
## 21    Black  Blue Female
## 21.1  Black  Blue Female
## 21.2  Black  Blue Female
## 21.3  Black  Blue Female
## 21.4  Black  Blue Female
## 21.5  Black  Blue Female
## 21.6  Black  Blue Female
## 21.7  Black  Blue Female
## 21.8  Black  Blue Female
## 22    Brown  Blue Female
## 22.1  Brown  Blue Female
## 22.2  Brown  Blue Female
## 22.3  Brown  Blue Female
## 22.4  Brown  Blue Female
## 22.5  Brown  Blue Female
## 22.6  Brown  Blue Female
## 22.7  Brown  Blue Female
## 22.8  Brown  Blue Female
## 22.9  Brown  Blue Female
## 22.10 Brown  Blue Female
## 22.11 Brown  Blue Female
## 22.12 Brown  Blue Female
## 22.13 Brown  Blue Female
## 22.14 Brown  Blue Female
## 22.15 Brown  Blue Female
## 22.16 Brown  Blue Female
## 22.17 Brown  Blue Female
## 22.18 Brown  Blue Female
## 22.19 Brown  Blue Female
## 22.20 Brown  Blue Female
## 22.21 Brown  Blue Female
## 22.22 Brown  Blue Female
## 22.23 Brown  Blue Female
## 22.24 Brown  Blue Female
## 22.25 Brown  Blue Female
## 22.26 Brown  Blue Female
## 22.27 Brown  Blue Female
## 22.28 Brown  Blue Female
## 22.29 Brown  Blue Female
## 22.30 Brown  Blue Female
## 22.31 Brown  Blue Female
## 22.32 Brown  Blue Female
## 22.33 Brown  Blue Female
## 23      Red  Blue Female
## 23.1    Red  Blue Female
## 23.2    Red  Blue Female
## 23.3    Red  Blue Female
## 23.4    Red  Blue Female
## 23.5    Red  Blue Female
## 23.6    Red  Blue Female
## 24    Blond  Blue Female
## 24.1  Blond  Blue Female
## 24.2  Blond  Blue Female
## 24.3  Blond  Blue Female
## 24.4  Blond  Blue Female
## 24.5  Blond  Blue Female
## 24.6  Blond  Blue Female
## 24.7  Blond  Blue Female
## 24.8  Blond  Blue Female
## 24.9  Blond  Blue Female
## 24.10 Blond  Blue Female
## 24.11 Blond  Blue Female
## 24.12 Blond  Blue Female
## 24.13 Blond  Blue Female
## 24.14 Blond  Blue Female
## 24.15 Blond  Blue Female
## 24.16 Blond  Blue Female
## 24.17 Blond  Blue Female
## 24.18 Blond  Blue Female
## 24.19 Blond  Blue Female
## 24.20 Blond  Blue Female
## 24.21 Blond  Blue Female
## 24.22 Blond  Blue Female
## 24.23 Blond  Blue Female
## 24.24 Blond  Blue Female
## 24.25 Blond  Blue Female
## 24.26 Blond  Blue Female
## 24.27 Blond  Blue Female
## 24.28 Blond  Blue Female
## 24.29 Blond  Blue Female
## 24.30 Blond  Blue Female
## 24.31 Blond  Blue Female
## 24.32 Blond  Blue Female
## 24.33 Blond  Blue Female
## 24.34 Blond  Blue Female
## 24.35 Blond  Blue Female
## 24.36 Blond  Blue Female
## 24.37 Blond  Blue Female
## 24.38 Blond  Blue Female
## 24.39 Blond  Blue Female
## 24.40 Blond  Blue Female
## 24.41 Blond  Blue Female
## 24.42 Blond  Blue Female
## 24.43 Blond  Blue Female
## 24.44 Blond  Blue Female
## 24.45 Blond  Blue Female
## 24.46 Blond  Blue Female
## 24.47 Blond  Blue Female
## 24.48 Blond  Blue Female
## 24.49 Blond  Blue Female
## 24.50 Blond  Blue Female
## 24.51 Blond  Blue Female
## 24.52 Blond  Blue Female
## 24.53 Blond  Blue Female
## 24.54 Blond  Blue Female
## 24.55 Blond  Blue Female
## 24.56 Blond  Blue Female
## 24.57 Blond  Blue Female
## 24.58 Blond  Blue Female
## 24.59 Blond  Blue Female
## 24.60 Blond  Blue Female
## 24.61 Blond  Blue Female
## 24.62 Blond  Blue Female
## 24.63 Blond  Blue Female
## 25    Black Hazel Female
## 25.1  Black Hazel Female
## 25.2  Black Hazel Female
## 25.3  Black Hazel Female
## 25.4  Black Hazel Female
## 26    Brown Hazel Female
## 26.1  Brown Hazel Female
## 26.2  Brown Hazel Female
## 26.3  Brown Hazel Female
## 26.4  Brown Hazel Female
## 26.5  Brown Hazel Female
## 26.6  Brown Hazel Female
## 26.7  Brown Hazel Female
## 26.8  Brown Hazel Female
## 26.9  Brown Hazel Female
## 26.10 Brown Hazel Female
## 26.11 Brown Hazel Female
## 26.12 Brown Hazel Female
## 26.13 Brown Hazel Female
## 26.14 Brown Hazel Female
## 26.15 Brown Hazel Female
## 26.16 Brown Hazel Female
## 26.17 Brown Hazel Female
## 26.18 Brown Hazel Female
## 26.19 Brown Hazel Female
## 26.20 Brown Hazel Female
## 26.21 Brown Hazel Female
## 26.22 Brown Hazel Female
## 26.23 Brown Hazel Female
## 26.24 Brown Hazel Female
## 26.25 Brown Hazel Female
## 26.26 Brown Hazel Female
## 26.27 Brown Hazel Female
## 26.28 Brown Hazel Female
## 27      Red Hazel Female
## 27.1    Red Hazel Female
## 27.2    Red Hazel Female
## 27.3    Red Hazel Female
## 27.4    Red Hazel Female
## 27.5    Red Hazel Female
## 27.6    Red Hazel Female
## 28    Blond Hazel Female
## 28.1  Blond Hazel Female
## 28.2  Blond Hazel Female
## 28.3  Blond Hazel Female
## 28.4  Blond Hazel Female
## 29    Black Green Female
## 29.1  Black Green Female
## 30    Brown Green Female
## 30.1  Brown Green Female
## 30.2  Brown Green Female
## 30.3  Brown Green Female
## 30.4  Brown Green Female
## 30.5  Brown Green Female
## 30.6  Brown Green Female
## 30.7  Brown Green Female
## 30.8  Brown Green Female
## 30.9  Brown Green Female
## 30.10 Brown Green Female
## 30.11 Brown Green Female
## 30.12 Brown Green Female
## 30.13 Brown Green Female
## 31      Red Green Female
## 31.1    Red Green Female
## 31.2    Red Green Female
## 31.3    Red Green Female
## 31.4    Red Green Female
## 31.5    Red Green Female
## 31.6    Red Green Female
## 32    Blond Green Female
## 32.1  Blond Green Female
## 32.2  Blond Green Female
## 32.3  Blond Green Female
## 32.4  Blond Green Female
## 32.5  Blond Green Female
## 32.6  Blond Green Female
## 32.7  Blond Green Female

Compute the chi-squared statistic with chisq.test( ):

he_tab <- table(long_haireye[long_haireye$sex == 'Female', 1:2])
csq <- chisq.test(he_tab)
str(csq)
## List of 9
##  $ statistic: Named num 107
##   ..- attr(*, "names")= chr "X-squared"
##  $ parameter: Named int 9
##   ..- attr(*, "names")= chr "df"
##  $ p.value  : num 7.01e-19
##  $ method   : chr "Pearson's Chi-squared test"
##  $ data.name: chr "he_tab"
##  $ observed : 'table' int [1:4, 1:4] 36 66 16 4 9 34 7 64 5 29 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ hair: chr [1:4] "Black" "Brown" "Red" "Blond"
##   .. ..$ eye : chr [1:4] "Brown" "Blue" "Hazel" "Green"
##  $ expected : num [1:4, 1:4] 20.3 55.7 14.4 31.6 18.9 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ hair: chr [1:4] "Black" "Brown" "Red" "Blond"
##   .. ..$ eye : chr [1:4] "Brown" "Blue" "Hazel" "Green"
##  $ residuals: 'table' num [1:4, 1:4] 3.494 1.375 0.416 -4.907 -2.284 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ hair: chr [1:4] "Black" "Brown" "Red" "Blond"
##   .. ..$ eye : chr [1:4] "Brown" "Blue" "Hazel" "Green"
##  $ stdres   : 'table' num [1:4, 1:4] 4.899 2.388 0.567 -7.296 -3.137 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ hair: chr [1:4] "Black" "Brown" "Red" "Blond"
##   .. ..$ eye : chr [1:4] "Brown" "Blue" "Hazel" "Green"
##  - attr(*, "class")= chr "htest"

Are hair and eye independent, given sex?

true_val <- 0
for (s in levels(long_haireye$sex)) {
    true_val <- (true_val + 
        with(subset(long_haireye, sex==s), 
                chisq.test(table(hair, eye))$statistic))
}

Permutations:

nperm <- 1000
chisq_perm <- rep(0, nperm)
for (k in 1:nperm) {
    for (s in levels(long_haireye$sex)) {
        fake <- subset(long_haireye, sex == s)
        fake$eye <- sample(fake$eye)
        chisq_perm[k] <- chisq_perm[k] + chisq.test(table(fake$eye, fake$hair))$statistic
    }
}

Result:

plot of chunk r plot_perms

Conclusion?

(What did we actually test?)

Your turn:

Use a permutation test to assess whether the relation between hair and eye color differs by sex.

To do this, we’ll:

  1. Compute the sum of squared differences between each of the 16 proportions (4 eye x 4 hair colors). (test statistic)

  2. Compute the same thing, after shuffling up the sex column, a bunch of times.

  3. Compare the observed value to the distribution under permutations from (2).

// reveal.js plugins