Tech Tutorial: Apply

Tech Tutorial — Apply. This unit extends inference to more than two groups and to relationships between variables: chi-square, ANOVA, and regression. Pick one tool and follow its tab.

We reuse class_survey.csv.

What you’ll do

  1. A chi-square test of independence — are year and housing associated?
  2. A one-way ANOVA — does mean sleep_hours differ across class year?
  3. A simple linear regression — does study_hours predict sleep_hours?

1. Chi-square test of independence

Both variables are categorical, so we cross-tabulate year × housing and test whether the two are independent.

  1. Open the Chi-Square Independence tool.
  2. Confirm the two variables are year and housing.
  3. Read the contingency table, expected counts, χ² statistic, df, and p-value.
  1. Open class_survey.csv.
  2. Frequencies → Independent Samples (χ² test of association). Rows: year; Columns: housing.
  3. Under Statistics, read χ² and p; under Cells, request Expected counts.

(Jamovi screenshots to be added.)

library(readr)
library(dplyr)
library(infer)

survey <- read_csv("../datasets/class_survey.csv")

survey |> chisq_test(housing ~ year)
# A tibble: 1 × 3
  statistic chisq_df p_value
      <dbl>    <int>   <dbl>
1      4.62        3   0.202
Check the conditions

With only 50 students split across a 4×2 table, some expected counts fall below 5 — the chi-square approximation is shaky here. In practice you’d report that caveat (and could fall back to the randomization chi-square from the Simulate unit). For this tutorial the point is the mechanics.


2. One-way ANOVA

ANOVA compares the mean of a numerical variable (sleep_hours) across 3+ groups (the four class years) with a single F-test.

  1. Open the One-Way ANOVA tool.
  2. Confirm response sleep_hours, grouping year.
  3. Read the group means, the F statistic, df, and p-value.
  1. ANOVA → One-Way ANOVA. Dependent: sleep_hours; Grouping: year.
  2. Read the F, df, and p. (Use Descriptives plots to see the group means.)

(Jamovi screenshots to be added.)

aov(sleep_hours ~ year, data = survey) |> summary()
            Df Sum Sq Mean Sq F value Pr(>F)
year         3   2.78  0.9266   1.119  0.351
Residuals   46  38.08  0.8278               

3. Simple linear regression

Both variables are numerical. We fit a line predicting sleep_hours from study_hours and test whether the slope differs from zero. (In the Explore unit the scatter of these two looked flat — regression now tests that formally.)

  1. Open the Regression Slope tool.
  2. Set x to study_hours and y to sleep_hours.
  3. Read the fitted slope, its p-value, and .
  1. Regression → Linear Regression. Dependent: sleep_hours; Covariate: study_hours.
  2. Read the slope (the study_hours coefficient), its p, and the (Model Fit).

(Jamovi screenshots to be added.)

lm(sleep_hours ~ study_hours, data = survey) |> summary()

Call:
lm(formula = sleep_hours ~ study_hours, data = survey)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.1533 -0.5092 -0.1111  0.5444  1.9448 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  7.14892    0.30485  23.450   <2e-16 ***
study_hours -0.00625    0.01982  -0.315    0.754    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.9217 on 48 degrees of freedom
Multiple R-squared:  0.002068,  Adjusted R-squared:  -0.01872 
F-statistic: 0.09947 on 1 and 48 DF,  p-value: 0.7538

Check yourself