\def\author{Joel B. Greenhouse}
\def\title{I x J Contingency Tables: Test for Independence}
\def\titleColor{grey}
%\def\theme{cosmo}
\def\backgroundColor{Khaki}
\def\verbatimColor{LightYellow}
\def\titleBackground{#C0B870}
\def\headerBackground{#C0B870}
\def\navbarBackground{lightBlue}

\begin{document}


%%%%%%%%%%%%%%%%%% Ready Tab %%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Am I ready?}

\shinyVideo{filename=chiSquareCrop.mp4; autoplay=TRUE}

You should first take the quiz below.
If you don't know the answers to these questions, you are not prepared to understand this app.

\shinyQuiz{bank=chisquareReady}

\shinyGotoTab{name=Learning about the Chi-square Test; text=Jump to Learning tab; textColor=White; backgroundColor=DodgerBlue}

\end{shinyTab}


%%%%%%%%%%%%%%%%%% Learning About Tab %%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Learning about the Chi-square Test}

{\bf Background.}  A study from the University of Texas Southwestern
Medical Center examined whether the risk of {\em hepatitis C} was
related to whether people had tattoos and to where they got their
tattoos.  Subjects in the study were patients being treated for
non-blood-related disorders in Texas.  Hepatitis C causes about 10,000
deaths each year in the United States, but often lies undetected for
years after infection.  The data from this study can be summarized in 
a two-way table:

\hr


{\bf Data:} 3 x 2 Contingency Table \\
\begin{tabular}{l|cc|c} 
 & Hepatitis C &  No Hepatitis C & Total \\ \hline \hline
Tattoo - parlor & 17 & 35 & 52 \\
Tattoo - elsewhere & 8 & 53 & 61  \\
None  &  22 & 491 & 513 \\ \hline
Total & 47 & 579 & 626 \\ \hline
\end{tabular}

\bigskip


{\sc Statistical Analysis:}

$H_0:$ Tattoo status and hepatitis status are independent

$H_a:$ Tattoo status and hepatitis status are {\bf not} independent \\

\medskip

{\sc Methods:} Chi-square test for Independence, Residual Analysis\\

\medskip
{\sc Computer Analysis: Using R}\\

\begin{verbatim}
#Read and format the data into a 3x2 table
> hepC<-matrix(c(17,35,8,53,22,491), nrow=3, byrow=T)
# Name the columns and rows
> colnames(hepC)<-c("Hepatitis C", "No Hepatitis C")
> rownames(hepC)<- c("Tatoo-parlor", "Tatoo-elsewhere", "None")

> hepC  #Display table to be sure it is read correctly
                   Hepatitis C   No Hepatitis C
Tatoo-parlor             17             35
Tatoo-elsewhere           8             53
None                     22            491

> chisq.test(hepC)  #Pearson Chi-Square test for independence

        Pearson's Chi-squared test

data:  hepC 
X-squared = 57.9122, df = 2, p-value = 2.658e-13
\end{verbatim}

\medskip

{\sc Table of Expected Values}

\begin{verbatim}
> chisq.test(hepC)$expected

                Hepatitis C No Hepatitis C
Tatoo-parlor       3.904153       48.09585
Tatoo-elsewhere    4.579872       56.42013
None              38.515974      474.48403
\end{verbatim}

\medskip

{\sc Residual Analysis:}  

Standardized residuals: $r_{ij} = \frac{(O_{ij} - E_{ij})}{\sqrt{E_{ij}}}$

\begin{verbatim}
> E<-chisq.test(hepC)$expected
> O<-chisq.test(hepC)$observed

> residual<- (O-E)/sqrt(E)

> residual
                 Hepatitis C   No Hepatitis C
Tatoo-parlor       6.627811     -1.8883383
Tatoo-elsewhere    1.598143     -0.4553290
None              -2.661238      0.7582168
\end{verbatim}

\medskip
{\bf Residuals} \\
\begin{tabular}  {l|cc|} \\
 & {\em Hepatitis C} &  No Hepatitis C \\ \hline \hline
Tattoo - parlor & 6.63 & -1.89 \\
Tattoo - elsewhere & 1.60 & -0.46  \\
None  &  -2.66 & 0.76 \\ \hline
\end{tabular}


\medskip
{\bf Interpretation:}

The the chi-square value is very large, or equivalently, the p-value
is very small which indicates evidence against the null hypothesis.
We reject the null hypothesis and conclude that hepatitis status is
{\em not} independent of tattoo status.  From the residual analysis,
we see that the cell for people with tattoos obtained in a tattoo
parlor who have hepatitis C is large and positive, indicating there
are more people in that cell than the null hypothesis of independence
would predict.  This result suggests that a principle source of
infection may be tattoo parlors.

The second largest residual in absolute value is a negative value for
those with no tattoos who test positive for hepatitis C.  A negative
value says that there are fewer people in this cell than independence
would expect.  That is, those who have no tattoos are less likely to
be infected with hepatitis C than we might expect if the two variables
are independent.

-- Joel Greenhouse, Fall, 2013

\shinyGotoTab{name=Learning with Simulations; text=Jump to Simulation Tab; textColor=White; backgroundColor=DodgerBlue}

\end{shinyTab}



%%%%%%%%%%%%%%%%%% Simulations Tab %%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Learning with Simulations}


Here are results from simulated data for 
two treatment groups.  Initial values are n=20 subjects per group and true
success probabilities of 50\% and 70\% respectively, but you can and
should try other combinations.  You can improve your intuition by examining 
these simulated data sets and their corresponding
Chi-square analyses.


\shinyActionButton{inputId=action; label=re-simulate; textColor=White; backgroundColor=DodgerBlue}
\shinySlider{inputId=n; label=n; min=10; max=200; value=20; step=5}
\shinySlider{inputId=A; label=A success rate; min=0.05; max=0.95; value=0.5; step=0.05}
\shinySlider{inputId=B; label=B success rate; min=0.05; max=0.95; value=0.7; step=0.05}
\medskip

\begin{shinyReactive}{dataInput}
  input$action
  n = input$n
  p1 = input$A
  p2 = input$B
  success = rbinom(2, n, c(p1,p2))
  mat = t(rbind(n-success, success))
  dimnames(mat) = list(LETTERS[1:2], c("Failure", "Success"))
  return(mat)
\end{shinyReactive}

\begin{shinyCodeOutput}
  simDat = dataInput()
  print(simDat)
  suppressWarnings(chisq.test(simDat))
\end{shinyCodeOutput}


\begin{shinyCodeOutput}
  obj = suppressWarnings(chisq.test(dataInput()))
  O = obj$observed
  E = obj$expected
  residuals = (O-E)/sqrt(E)
  cat("Residuals\n")
  print(round(residuals,3))
\end{shinyCodeOutput}


\hr

\textbf{Guiding questions:}

\begin{enumerate} 
\item Effects of different sample sizes 
\begin{enumerate}
\item Set n=40,  What would you expect to happen to the p-value?  What would you expect to happen to the residuals?

\item Set n=80.  What would you expect to happen to the p-value?  What would you expect to happen to the residuals?
\end{enumerate}

\item  Leave p(A)=0.5, but set p(B)=0.6.  What would you expect to happen to the p-value?  What would you expect to happen to the residuals?
\end{enumerate}

\shinyGotoTab{name=Other Datasets; text=Jump to Other Datasets Tab; textColor=White; backgroundColor=DodgerBlue}

\end{shinyTab}




%%%%%%%%%%%%%%%%%% Datasets Tab %%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Other Datasets}

\begin{shinyCode}
  source("http://www.stat.cmu.edu/~hseltman/files/report.R")
\end{shinyCode}

\begin{shinyReactive}{otherDatasets}
  choices = c("Aspirin and MI", "Education and Fundamentalism")
  data = list(matrix(c(28, 18,  656, 658), nrow=2,
                     dimnames=list(treatment=c("Placebo","Aspirin"), 
                                   outcome=c("MI", "no MI"))),
              matrix(c(178, 570, 138,  138, 648, 252,  108, 442, 252), nrow=3,
                     dimnames=list(education=c("< HS", "HS or jun. college", "Bach. or Grad."),
                                   belief=c("Fundamentalist","Moderate","Liberal"))))
  sources=c("Categorical Data Analysis\nAlan Agresti\nWiley (2002)\npage 72",
            "Categorical Data Analysis\nAlan Agresti\nWiley (2002)\npage 80")
  return(list(choices=choices, data=data, sources=sources))
\end{shinyReactive}

\begin{shinyReactive}{story}
  otherData = otherDatasets()
  index = match(input$otherChoice, otherData$choices)
  return(otherData$stories[index])
\end{shinyReactive}

\shinyRadioButtons{inputId=otherChoice; label=Choose a dataset; 
                   choices=Aspirin and MI, Education and Fundamentalism}
\smallskip

\textbf{Story}

\begin{shinyConditionalPanel}{inputId=otherChoice; value=Aspirin and MI}
In a Swedish study of the association between aspirin and heart attack
(myocardial infarction) 1360 patients were randomly assigned to aspirin
or placebo.  
Subjects were then classified as having death due to MI in
the next three years or not.
\end{shinyConditionalPanel}

\begin{shinyConditionalPanel}{inputId=otherChoice;value=Education and Fundamentalism}
These data come from the United States 1996 General Social Survey conducted
by the National Opinion Research Center.
\end{shinyConditionalPanel}

\smallskip

\textbf{Data}
\begin{shinyCodeOutput}
  otherData = otherDatasets()
  index = match(input$otherChoice, otherData$choices)
  print(pct(total(otherData$data[[index]])))
\end{shinyCodeOutput}


\smallskip

\textbf{Chi-square results}
\begin{shinyCodeOutput}
  otherData = otherDatasets()
  index = match(input$otherChoice, otherData$choices)
  print(suppressWarnings(chisq.test(otherData$data[[index]])))
\end{shinyCodeOutput}


\smallskip

\textbf{Source}

\textem{Categorical Data Analysis}

by Alan Agresti



\begin{shinyConditionalPanel}{inputId=otherChoice; value=Aspirin and MI}
Wiley (2002), Page 72
\end{shinyConditionalPanel}

\begin{shinyConditionalPanel}{inputId=otherChoice; value=Education and Fundamentalism}
Wiley (2002), Page 80
\end{shinyConditionalPanel}

\shinyGotoTab{name=Try it myself; text=Jump to Try It Myself Tab; textColor=White; backgroundColor=DodgerBlue}

\end{shinyTab}


%%%%%%%%%%%%%%%%%% Try it yourself %%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Try it myself}

Consider a dataset collected on a political opinion where we record
the subject's gender and whether they favor or oppose a particular bill.
The data are in an R object called "opinion".  

\begin{shinyCode}
  opinion = matrix(c(10,20, 15, 15), nrow=2, dimnames=list(gender=c("male","female"), opinion=c("favor","oppose"))) 
  opProp2 = round(prop.table(opinion,1),2)
\end{shinyCode}

\smallskip

The R formula to convert a table of counts called, say, "myCounts", to a table of proportions
is \textsc{prop.table(myCounts, MARGIN=NULL)}.  Setting the MARGIN to 1 computes
row proportions, while setting the MARGIN to 2 computes column proportions.  The default
value of MARGIN=NULL computes proportions that add to 1.0 for all cells in the table.

\shinyTextInput{inputId=tabCode; label=Enter the R code needed to show row proportions, then click the Evaluate button}
\shinyActionButton{inputId=tabEval; label=Evaluate}
\smallskip
\begin{shinyCodeOutput}
input$tabEval
if (str_trim(isolate(input$tabCode))=="") {
  cat("(No input)\n")
} else {
  output = try(eval(parse(text=isolate(input$tabCode))), silent=TRUE)
  if (is(output, "try-error")) {
    cat("No, that is not valid R code.  Review the previous paragraph and try again\n.")
    cat("Error message: ", attr(output, "condition")[[1]])
  } else if (!is.matrix(output)) {
    cat("Your code ran, but you didn't make a table!  Here is your ouput:\n")
  } else if (!is.numeric(output) || !all(dim(output)==c(2,2)) || !all(round(output,2)==opProp2)) {
    cat("Your code ran, but you did not create row proportions!  Here is your ouput:\n")
  } else {
    cat("Good job!  Here is your ouput:\n")
  }
  print(output)
}
\end{shinyCodeOutput}


\smallskip

\shinyTextInput{inputId=csCode; label=Enter your R code needed to perform a chi-square test, then click the Evaluate button}
\shinyActionButton{inputId=csEval; label=Evaluate}
\smallskip
\begin{shinyCodeOutput}
input$csEval
if (str_trim(isolate(input$tabCode))=="") {
  cat("(No input)\n")
} else {
  output = try(eval(parse(text=isolate(input$csCode))), silent=TRUE)
  if (is(output, "try-error")) {
    cat("No, that is not valid R code.  Check your spelling and punctuation, review the 'Learning about chi square' tab, and try again. \n")
    cat("Error message: ", attr(output, "condition")[[1]])
  } else {
    if (regexpr("chisq.test\\(", isolate(input$csCode)) == -1) {
      cat("Your code ran, but it looks like you didn't do a chi-square test!  Here is your ouput:\n")
    } else {
      cat("Good job!  Here is your ouput:\n")
    }
    print(output)
  }
}
\end{shinyCodeOutput}

\smallskip
\shinyCheckbox{inputId=revealCode; label=Reveal solutions; value=FALSE}
\begin{shinyConditionalPanel}{inputId=revealCode; value=TRUE}
\begin{verbatim}
# Show row proportions
prop.table(opinion, margin=1)
# Show row proportions as percentages
round(100 * prop.table(opinion, margin=1), digits=1)
# Do chi-square test
chisq.test(opinion)
\end{verbatim}
\end{shinyConditionalPanel}

\shinyGotoTab{name=Did I learn well?; text=Jump to Did I learn Tab; textColor=White; backgroundColor=DodgerBlue}

\end{shinyTab}





%%%%%%%%%%%%%%%%%% Testing Tab %%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Did I learn well?}
The quiz below will help you self-evaluate what you learned.
\shinyQuiz{bank=chisquareReview; randomizeAnswers=FALSE}
\end{shinyTab}


\end{document}
