Comprehensive Guide to Univariate and Multivariate Chi-Square Analysis in R

Midterm Exam Guidelines and Course Administration

  • Assignment Grading Updates:
    • Grading coordination with teaching assistants Abraham and Taiwan is scheduled for the upcoming week.
    • Virtual tutorial notes are permitted on assessments; these notes were subject to grading or verification prior to the exam session.
  • Submission and Resource Rules for the Midterm:
    • Submissions must be formal documents; syntax and execution errors will not receive lenient passes.
    • Strict prohibition of digital materials: no digital copies, no internet access, and no online resources of any sort are allowed during the evaluation.
    • Independent completion policy: collaboration is strictly prohibited for the examination. While regular assignments and real-world projects model collaborative problem-solving and open inquiry, the midterm evaluation measures purely individual mastery and baseline competency.
  • Office Hours Adjustment:
    • In-office consultations are relocated because the physical office window cannot be opened/cracked.
    • Office hours will operate as informal "roaming office hours" (walking discussions) immediately following class.

Survey Analysis: Student Well-Being and Emotion Networks

  • Pedagogical Purpose and Preview of Quarter Content:
    • Student check-in survey data serves as an instructional bridge to upcoming course units on text visualization, semantic phrasing, and network analysis.
    • Future analytical tasks include parsing text corpora, separating words based on positive and negative valences, and creating data visualizations.
    • The second real-world data report, due prior to the final exam, utilizes social media text scraped from Twitter (currently branded as X), requiring students to navigate both toxic text content and positive text clusters.
  • Survey Findings on Student Experiences:
    • The dataset pools current class responses with historical cohort records addressing the prompt: "How are you doing this quarter?"
    • Text frequency analysis demonstrates high repetition of academic terms such as "work", "classes", and "assignments".
    • Positive coping indicators include students reporting feeling "good" and attempting to stay "balanced".
  • Emotion Network Analysis:
    • Network modeling visualizes psychological topology by linking co-occurring emotional states:
      • Feelings of guilt demonstrate direct topological ties to feelings of irritation.
      • Restlessness and distraction are mutually interconnected.
      • Tension exhibits a strong linkage to anxiety.
      • Depressive feelings remain relatively disconnected from happiness levels in this dataset.
    • Cohort Structural Differences:
      • Past historical cohorts yielded an undifferentiated, singular network "blob" wherein all negative emotional states directly coupled with and dragged down positive emotional states.
      • The current cohort exhibits modularity: negative emotional disruptions (e.g., distraction) remain insulated from positive emotions, reflecting higher baseline psychological resilience.

Student Feedback and Exam Design Discussion

  • Student Sentiment Regarding Course Components:
    • Positives: High student satisfaction is reported for tutorials and lectures.
    • Difficulties: Students report notable friction with labs, assignment pacing, and overall time management.
  • Dialogue on Midterm Question Architecture:
    • Prompt/Question: Inquiry into what causes students the greatest difficulty—the sheer number of individual questions or the time required per question.
    • Response: Students express that a high question count creates severe time pressure, requiring them to rush to complete all sections before the time expires.
    • Prompt/Question: Consideration of whether the examination should feature one comprehensive, highly complex problem with 1010 subparts, or several isolated, smaller problems.
    • Response: Students strongly prefer smaller, standalone problems. A single multi-component problem (such as executing an entire chi-square workflow from scratch) carries compounding risk, where an early error ruins all downstream steps.
    • Prompt/Question: Inquiry regarding comprehension and clarity of lab instructions.
    • Response: Students report that brief, five-word prompts can be ambiguous, creating confusion about expected procedural steps, whereas explicit instructions reduce uncertainty.
    • Instructor Grading Reassurance: Evaluation rubrics award partial credit based on procedure and effort. Downstream calculations that follow correctly from an early miscalculation are protected and not penalized repeatedly.

Univariate Chi-Square Test for Goodness of Fit

  • Test Purpose and Foundations:
    • The univariate chi-square goodness-of-fit test evaluates whether observed categorical counts diverge significantly from theoretical, expected baseline probabilities.
  • Worked Dating App Scenario:
    • Sample Size (NN): N=213N = 213 students.
    • Categories (k=3k = 3): Selection of preferred opening message on a dating app under an equal likelihood assumption:
      1. "Do we tell people we met here, or do we make up a bookstore?" (O1=90O_1 = 90)
      2. "Would you still love me if I told you I work for tax?" (O2=62O_2 = 62)
      3. "What's your most unreasonable ick?" (O3=61O_3 = 61)
    • Research Question: "For dating app returners, do population proportions differ across message responses?"
  • Hypothesis Formulation:
    • Null Hypothesis (H0H_0): In the population, each listed answer is equally likely (p1=p2=p3=13p_1 = p_2 = p_3 = \frac{1}{3}).
    • Alternative Hypothesis (H1H_1): At least one population proportion differs from the equal share expectation.
    • Statistical Logic of H1H_1: The alternative hypothesis requires only the minimum divergence needed to invalidate equality. If one category deviates, the fixed sum of counts forces compensation elsewhere. It does not require every single category to deviate independently from one another.
  • Expected Counts Calculation:
    • Under the null hypothesis of equal share across k=3k = 3 categories:         E=Nk=2133=71E = \frac{N}{k} = \frac{213}{3} = 71
  • Degrees of Freedom (dfdf):
    • For a univariate goodness-of-fit test with kk categories:         df=k−1df = k - 1
    • Given k=3k = 3:         df=3−1=2df = 3 - 1 = 2
  • Statistical Outputs and Decision Rule:
    • Significance Level (α\alpha): α=0.01\alpha = 0.01
    • Observed Chi-Square Test Statistic: χ2=7.6338\chi^2 = 7.6338
    • Critical Value (χcrit2\chi^2_{\text{crit}}) at α=0.01\alpha = 0.01, df=2df = 2: χcrit2=9.21\chi^2_{\text{crit}} = 9.21
    • Calculated p-valuep\text{-value}: p=0.022p = 0.022
    • Decision: Compare the calculated p-valuep\text{-value} to α\alpha:         p=0.022>0.01p = 0.022 > 0.01         Because the test statistic (7.63387.6338) does not exceed the critical threshold (9.219.21) and p>αp > \alpha, the test fails to reject the null hypothesis (H0H_0). There is insufficient empirical evidence to conclude that preferences across the three messages differ significantly at the 1%1\% level.

R Programming Procedures and Data Hygiene for Categorical Analysis

  • Cleaning Imported Data:
    • Importing .csv data often generates an unwanted index column (labeled X).
    • Strip unwanted columns using indexing syntax (e.g., selecting the second column via data[, 2]).
  • Converting Variables to Factors:
    • The factor() function formats variables for statistical modeling by defining levels and human-readable labels: R factor(x, levels = c(1, 2, 3), labels = c("Label1", "Label2", "Label3"), ordered = FALSE)         
  • Critical Boolean Variable Warning in R:
    • Never use single-letter abbreviations T or F for boolean values.
    • In base R, F and T are global variables that can be overwritten (e.g., executing F <- 10 assigns the integer 1010 to F, breaking subsequent logical evaluations).
    • The full keywords TRUE and FALSE are reserved, protected language primitives that cannot be overwritten.
  • Visualizing Categorical Distributions:
    • Vanilla bar plots should be avoided in favor of more informative visualizations.
    • Mosaic plots display relative categorical proportions and multi-way associations: R mosaicplot(table(data)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  • Proper Execution of chisq.test():
    • Passing raw character vectors or non-numeric factors directly into chisq.test() generates an error: all entries of x must be non-negative and finite.
    • The function requires a numeric vector of aggregated counts or a frequency table object generated by table(): R chisq.test(table(factor_data)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Multivariate Chi-Square Test of Independence

  • Conceptual Foundations:
    • The chi-square test of independence assesses whether two categorical variables share a significant statistical association or are independent.
    • Predictive Intuition: Independence implies that knowing an observation's category in Variable A gives no better-than-chance capability to predict its category in Variable B.
    • Real-World Conceptual Metaphor: Identifying an individual by a specific political party label (e.g., blue vs. red) provides above-chance predictive power regarding their stance on the death penalty. If knowing one attribute predicts the other, the dimensions are dependent.
  • Worked Scenario: Purchases vs. Savings Strategies:
    • Sample Size (NN): N=204N = 204 students.
    • Categorical Dimensions:
      • Variable 1 (Tempting Bad Purchases - 33 levels):
        1. Buying a ticket to get out of buying out later
        2. A 6060 dollar candle called "financial freedom"
        3. Buying an unexpected luxury purchase
      • Variable 2 (Desperate Money-Saving Plans - 22 levels):
        1. Eating free samples until it counts as dinner
        2. Utilizing office hours / cafeteria freebies
  • Hypotheses Formulation:
    • Null Hypothesis (H0H_0): Student choices of bad purchases and student choices of desperate money-saving plans are independent (unrelated).
    • Alternative Hypothesis (H1H_1): Student choices of bad purchases are associated with their choices of desperate money-saving plans.
  • Degrees of Freedom Formula:
    • For a contingency table with rr rows and cc columns:         df=(r−1)(c−1)df = (r - 1)(c - 1)
    • For a 3×23 \times 2 table:         df=(3−1)(2−1)=2×1=2df = (3 - 1)(2 - 1) = 2 \times 1 = 2
  • Expected Cell Count Formula:
    • For any cell located at row ii and column jj:         Eij=Row Totali×Column TotaljGrand TotalE_{ij} = \frac{\text{Row Total}_i \times \text{Column Total}_j}{\text{Grand Total}}
  • Contingency Matrix Observed Counts:
    • Observed cell counts across the 3×23 \times 2 design: 2929, 3131, 3333, 3636, 4040, and 3535 (∑=204\sum = 204).
  • Test Results and Statistical Decision:
    • Calculated Test Statistic: χ2≈0\chi^2 \approx 0 (substantially below 2.02.0).
    • Significance Level (α\alpha): α=0.05\alpha = 0.05
    • Critical Threshold (χcrit2\chi^2_{\text{crit}}) at df=2df = 2: χcrit2=5.99\chi^2_{\text{crit}} = 5.99
    • Calculated p-valuep\text{-value}: p=0.767p = 0.767
    • Decision: Because χ2=0<5.99\chi^2 = 0 < 5.99 and p=0.767>0.05p = 0.767 > 0.05, the test fails to reject the null hypothesis (H0H_0). The variables are statistically independent.

Data Import Functions and Computational Heuristics in R

  • Distinctions Among Base R File Readers:
    • read.csv(): Specifically tuned for comma-delimited files; automatically treats commas as delimiters.
    • read.table(): General tabular data reader; requires an explicit delimiter argument: R read.table("file.csv", sep = ",") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    • readRDS(): Deserializes native, single R binary data objects stored in .rds format.
  • Exam Preparation Heuristics:
    • Exam questions may supply input files in .csv, .table, or .rds formats. Students must identify the file extension and select the matching reader function.
    • The Chi-Square Magnitude Shortcut:
      • In categorical tests, the minimum critical value for significance at α=0.05\alpha = 0.05 is approximately 3.843.84 (for df=1df = 1) and 5.995.99 (for df=2df = 2).
      • If an observed χ2\chi^2 statistic is extremely small (e.g., χ2<2.0\chi^2 < 2.0), it cannot exceed any critical value, meaning the result will invariably fail to reject H0H_0. Under extreme time constraints, a χ2<2.0\chi^2 < 2.0 confirms non-significance immediately without manually computing exact tail probabilities.