UK-based online statistics and data analysis support for USA, UK, and international clients. No exams, no impersonation, no fabricated data.
Chi-Square and Categorical Data Tests

G Test: Formula, Interpretation, Goodness-of-Fit Example, Python, R, SPSS and Excel Guide

Likelihood-ratio goodness-of-fit analysis for one categorical variable G Test: Formula, Interpretation, Goodness-of-Fit Example, Python, R, SPSS and Excel Guide G Test is the likelihood-ratio counterpart to...

Statistics guide Ethical learning support SPSS/R/Python/Excel friendly
G Test: Formula, Interpretation, Goodness-of-Fit Example, Python, R, SPSS and Excel Guide

Likelihood-ratio goodness-of-fit analysis for one categorical variable

G Test: Formula, Interpretation, Goodness-of-Fit Example, Python, R, SPSS and Excel Guide

G Test is the likelihood-ratio counterpart to the Pearson chi-square goodness-of-fit test. This complete guide explains the hypotheses, formula, interpretation, worked manual calculation, Python and R charts, software workflows, APA reporting, common mistakes and downloads through a one-variable four-category example based on students’ school-choice reason.

One-way goodness-of-fit design
649 observations
G² = 142.6067
df = 3, p = 1.04 × 10−30

G Test Model Overview

G Test, often called the likelihood-ratio goodness-of-fit test or the log-likelihood ratio test for one-way categorical data, compares observed category frequencies with expected frequencies under a null model. In the current example, the variable reason has four categories—course, home, other and reputation—and the null hypothesis states that all four categories are equally likely.

What question does the G Test answer?

The question is whether the observed distribution of school-choice reason is consistent with the theoretical distribution stated in the null hypothesis. Here, the null probabilities are all 0.25, so each category is expected to appear equally often. If you need background on hypothesis logic, see null and alternative hypothesis, hypothesis testing and p-value.

H0: pcourse = phome = pother = preputation = 0.25
H1: At least one category probability differs from 0.25

How it differs from Pearson’s chi-square goodness-of-fit test

Both procedures compare observed and expected frequencies, and both usually give very similar conclusions for moderate and large samples. The difference is in the test statistic. Pearson’s test uses squared standardized residuals, whereas the G Test uses the likelihood-ratio expression 2 Σ O ln(O/E). In this example, both point to the same conclusion: the reason distribution is far from equal.

Worked example at a glance

The observed counts are 285 for course, 149 for home, 72 for other and 143 for reputation. Under equal probabilities, each category would be expected to occur 162.25 times. The likelihood-ratio statistic is G² = 142.6067 with df = 3 and p = 1.0360 × 10−30, so the equal-probability null is decisively rejected.

Overview conclusion: the category distribution is not close to uniform. The category course is much more common than expected, while other is much less common than expected. The detailed formulas, step-by-step calculations, chart interpretations and software workflows are given below.
AdvertisementGoogle AdSense top placement reserved here

Quick Answer: G Test Result

The G Test compares the observed distribution of reason with an equal-probability null model. Because the sample size is 649 and the departures from equality are large, the result is overwhelmingly significant.

Sample size649
Expected count per category162.25
G-squared statistic142.6067
P-value1.04 × 10−30

Test summary

  • Procedure: one-way G Test for goodness of fit
  • Null model: four equal category probabilities
  • Statistic: G² = 142.6067
  • Degrees of freedom: 3
  • P-value: 1.0360 × 10−30
  • Decision at α = .05: reject H0

Substantive meaning

  • Course is strongly overrepresented.
  • Other is strongly underrepresented.
  • Home and reputation are slightly below expectation.
  • The pattern is not a small random fluctuation around equality.
  • The equal-probability model is not a plausible description of the data.
Best interpretation: the observed school-choice reasons are not evenly distributed across the four categories. The largest departure comes from the very high count for course and the very low count for other.
Reporting tip: do not report only “significant.” State the observed counts, expected counts, G², degrees of freedom, p-value and which categories drove the departure.

Table of Contents

  1. Research question, hypotheses and design
  2. When to use the G Test
  3. G Test formula and manual calculation
  4. Variables and data dictionary
  5. Observed counts and complete results
  6. G Test versus chi-square goodness-of-fit
  7. G Test calculator workflow
  8. Python chart interpretations
  9. R chart pairs and interpretation
  10. Python, R, SPSS and Excel workflows
  11. Expandable software code
  12. Assumptions, limitations and advanced interpretation
  13. APA reporting for the G Test
  14. Common mistakes and how to correct them
  15. Reports and worked Excel download
  16. Related Salar Cafe guides
  17. Frequently asked questions
  18. Conclusion

Research Question, Hypotheses and Data Design

Variablereason, a one-way categorical variable with four levels.
Null modelAll four category probabilities are equal.
Inferential targetWhether the observed distribution matches the theoretical equal distribution.

Research question

Does the observed distribution of school-choice reason match an equal-probability model across the categories course, home, other and reputation?

Null and alternative hypotheses

HypothesisStatistical statementMeaning
Null hypothesisH0: p1 = p2 = p3 = p4 = 0.25All four reasons are equally likely.
Alternative hypothesisH1: not all pj equal 0.25At least one category probability differs.

Design behind the test

This is a one-variable goodness-of-fit design. There is no second grouping variable, so this is not a chi-square test of independence and not a chi-square test of homogeneity. Instead, one observed categorical distribution is compared with a theoretical distribution.

Why the expected model must be stated first

The goodness-of-fit logic starts with a declared model. If the expected proportions are chosen after inspecting the observed counts, the inferential meaning breaks down. In this workbook, the expected probabilities are equal by design, so the expected count in each cell is 649 × 0.25 = 162.25.

Methods statement: “A one-way G Test of goodness of fit was used to test whether the four-category school-choice reason variable followed an equal-probability distribution.”

When to Use the G Test

Use the G Test when

  • You have one categorical variable and want to compare it with a theoretical distribution.
  • The expected frequencies come from theory, policy, genetics, fairness or a declared benchmark.
  • You want a likelihood-ratio alternative to Pearson’s chi-square goodness-of-fit test.
  • All observed counts are positive, so the log term is well-defined.
  • Sample size is large enough for the chi-square approximation to be reliable.

Choose another method when

  • You have two categorical variables and need association, independence or homogeneity testing.
  • You have binary paired data; then matched methods are needed.
  • You need an exact small-sample one-way test; a binomial or multinomial exact approach may be more appropriate.
  • Your outcome is quantitative rather than categorical.
  • Expected probabilities are unknown or data-derived in a way that changes the null model.

Simple test-selection logic

Question 1One variable or two?

If one variable, you may need a goodness-of-fit test.

Question 2Do you have expected probabilities?

If yes, compare observed counts with that model.

Question 3Likelihood-ratio or Pearson?

Use the G Test for the likelihood-ratio statistic, or Pearson chi-square for the squared-residual version.

For broader context, compare this method with chi-square goodness-of-fit test, binomial test and exact test for proportions. They answer related but not identical questions.

G Test Formula and Manual Calculation

The G Test statistic for a one-way goodness-of-fit analysis is:

G² = 2 Σ Oj ln(Oj / Ej)

Here, Oj is the observed count in category j and Ej is the expected count under the null hypothesis.

Step 1: Compute expected counts

Ej = 649 × 0.25 = 162.25 for each of the four categories

Step 2: Calculate the component for each category

CategoryObserved OExpected E2 O ln(O/E)
course285162.25321.10997
home149162.25−25.38723
other72162.25−116.99600
reputation143162.25−36.12001

Step 3: Add the components

G² = 321.10997 − 25.38723 − 116.99600 − 36.12001 = 142.60673

Step 4: Determine the degrees of freedom

df = k − 1 = 4 − 1 = 3

Step 5: Obtain the p-value

Under the null hypothesis, the statistic is approximately chi-square distributed with 3 degrees of freedom. The verified p-value is 1.0360142426607893 × 10−30.

Manual calculation conclusion: the G Test statistic is extremely large relative to a chi-square distribution with 3 degrees of freedom, so the equal-probability model is rejected decisively.

Variables and Data Dictionary

VariableRoleCategoriesTotal NMeaning in analysis
reasonObserved categorical variablecourse, home, other, reputation649School-choice reason being tested against a theoretical distribution.
Expected modelNull distribution0.25, 0.25, 0.25, 0.25649Equal category probabilities under H0.

Observed and expected table

ReasonObserved countObserved proportionExpected countDifference O − E
course2850.439137162.25+122.75
home1490.229584162.25−13.25
other720.110940162.25−90.25
reputation1430.220339162.25−19.25
Total6491.0000006490

The observed proportions show that course accounts for 43.91% of responses, while other accounts for only 11.09%. Under the null model, each category would account for exactly 25%. See also frequency and relative frequency tables and descriptive statistics.

Observed Counts and Complete Results

Course285

43.91%, well above expectation

Other72

11.09%, well below expectation

G-squared142.6067

Likelihood-ratio statistic

Degrees of freedom3

k − 1 categories

P-value1.04 × 10−30

Essentially zero at standard precision

Cohen’s w0.4750

Large overall departure from the null model

MetricValueInterpretation
G Test statistic142.6067318Large likelihood-ratio departure from the null distribution.
Degrees of freedom3One less than the number of categories.
P-value1.0360142426607893e-30Reject the equal-probability null decisively.
Pearson chi-square146.4329738Very similar conclusion from the Pearson statistic.
Cohen’s w0.4750038Large standardized effect for a goodness-of-fit design.

Which categories drive the result?

The biggest positive departure is course, which exceeds expectation by 122.75 responses. The strongest negative departure is other, which falls short of expectation by 90.25 responses. The categories home and reputation are also below expectation, but their departures are much smaller. This means the rejection is not spread evenly across all categories; it is driven mainly by an excess of course responses and a deficit of other responses.

Result summary: the observed reason distribution is highly uneven relative to the equal null model, and the mismatch is both statistically overwhelming and substantively meaningful.
AdvertisementGoogle AdSense placement reserved after the Results section

G Test Versus Chi-Square Goodness-of-Fit

G Test

Uses the likelihood-ratio statistic 2 Σ O ln(O/E).

Pearson chi-square

Uses the squared-residual statistic Σ (O−E)²/E.

Shared reference

Both are usually compared with a chi-square distribution using the same degrees of freedom.

Practical outcome

In many applications, both give nearly identical decisions.

MethodStatisticdfP-valueDecision
G Test142.606731.0360 × 10−30Reject H0
Pearson chi-square146.43303Extremely smallReject H0

The two statistics are close because the sample is large and the departures are clear. In teaching contexts, it is helpful to emphasize that the G Test has a likelihood-ratio interpretation, which often connects naturally to generalized linear models, deviance and log-likelihood reasoning. If you want to explore the Pearson counterpart in full detail, see chi-square goodness-of-fit test.

Important distinction: this one-way G Test is different from a two-way likelihood-ratio chi-square test for independence. The current design has only one variable and one theoretical distribution.

G Test Calculator Workflow

A reliable G Test calculator should allow the user to enter observed counts and expected probabilities or expected counts, then return the statistic, degrees of freedom, p-value and the contribution of each category.

Calculator inputs for the worked example

InputValueMeaning
Observed counts285, 149, 72, 143Observed frequency for each reason category.
Expected probabilities0.25, 0.25, 0.25, 0.25Equal null model.
Total sample size649Total number of responses.
Alternative modelUnrestricted multinomialObserved proportions are free to differ.

Calculator steps

  • Enter the observed counts in the correct order.
  • Specify the expected probabilities or expected counts.
  • Check that expected counts sum to the same total as observed counts.
  • Review each category contribution, not just the final p-value.
  • Report both the G Test result and the observed-versus-expected pattern.
Expected calculator target: G² = 142.6067, df = 3 and p ≈ 1.04 × 10−30.

Python Chart Interpretations

Chart 1: Observed and expected countsPython

G Test Python chart of observed and expected counts
Observed counts are compared with the equal expected count of 162.25 in each category.
Pattern

Course stands far above the expected bar, while other falls far below it.

Key values

Observed counts are 285, 149, 72 and 143 versus 162.25 expected in every category.

Interpretation

The equal-probability model does not match the observed count structure.

Why it matters

The observed-versus-expected difference is the foundation of the G Test statistic.

Next step: translate the count differences into proportions to compare categories on the same 0–1 scale.

Chart 2: Observed proportionsPython

G Test Python chart of observed proportions
Observed category proportions compared with the 0.25 null probability.
Pattern

Course is close to 44%, whereas other is close to 11%, far from the 25% benchmark.

Key values

Course 0.4391, home 0.2296, other 0.1109 and reputation 0.2203.

Interpretation

The departure from equality is not subtle; it is visually substantial.

Why it matters

Proportions often communicate the substantive difference more clearly than counts alone.

Next step: inspect how much each category contributes to the total likelihood-ratio statistic.

Chart 3: G Test contributionsPython

G Test Python chart of category contributions
Category-wise contributions to the G Test statistic.
Pattern

Course contributes the largest positive amount to G², while other makes the largest negative signed departure in the decomposition view.

Key values

The category components are 321.1100, −25.3872, −116.9960 and −36.1200 before summation.

Interpretation

The total statistic is driven mostly by two categories: course and other.

Why it matters

Knowing which categories drive the mismatch improves interpretation beyond a single p-value.

Next step: examine signed contributions to see the direction of departure as well as its magnitude.

Chart 4: Signed contributionsPython

G Test Python chart of signed contributions
Signed contribution pattern showing which categories lie above or below expectation.
Pattern

Course is the only strongly positive departure, while home, other and reputation are negative relative to expectation.

Key values

Observed minus expected values are +122.75, −13.25, −90.25 and −19.25.

Interpretation

Positive values indicate overrepresentation and negative values indicate underrepresentation.

Why it matters

Direction helps explain the substantive story behind the test statistic.

Next step: connect the observed statistic to its chi-square reference distribution.

Chart 5: Null distributionPython

G Test Python chart of null distribution
The chi-square reference distribution with 3 degrees of freedom and the observed G² marked in the far right tail.
Pattern

The observed statistic lies far into the right tail of the reference distribution.

Key values

G² = 142.6067 with df = 3 produces a p-value of about 1.04 × 10−30.

Interpretation

This statistic is far too large to attribute to ordinary sampling variation under the null.

Why it matters

The null-distribution view turns the observed mismatch into a formal inference.

Next step: summarize the final decision and the main category pattern in one clear result statement.

Chart 6: Result summaryPython

G Test Python result summary chart
Compact summary of the G Test result and decision.
Pattern

The summary panel condenses the statistic, degrees of freedom, p-value and conclusion.

Key values

G² = 142.6067, df = 3, p ≈ 1.04 × 10−30, reject H0.

Interpretation

The equal-probability model is decisively unsupported by the data.

Why it matters

This is the most concise chart for slides, reports and quick interpretation.

Next step: compare the Python visuals with the paired R chart presentation.
AdvertisementGoogle AdSense placement reserved after the Python charts

R Chart Pairs and Interpretation

The R workflow presents the same inferential story in paired charts. This makes it easier to compare counts with proportions, contribution magnitude with direction, and cumulative departure with the final decision.

R pair 1: Observed versus expected counts and contribution by category

R chart of observed and expected counts
Observed and expected frequencies for the four reason categories.
R chart of contribution by category
Magnitude of each category’s contribution to the overall departure.
Pattern

Count imbalance is easy to see

The left panel shows the excess of course responses and the deficit of other responses. The right panel quantifies that imbalance by contribution size.

Key check: course exceeds expectation by 122.75, while other falls short by 90.25.
Meaning

Not all categories matter equally

The result is not driven by every category to the same degree. The primary departures come from course and other, while home and reputation contribute much less.

Interpretation tip: identify the main driving categories in the written result.

R pair 2: Signed root residuals and observed proportions

R chart of signed root residuals
Signed residual-style view of over- and underrepresentation.
R chart of observed proportions
Observed category probabilities compared with the 0.25 null probability.
Pattern

Direction and size are both visible

The residual-style panel shows which categories are above expectation and which are below it. The proportional panel shows how far each category lies from the 25% target.

Course is positive; home, other and reputation are negative.
Meaning

Proportions clarify the substantive story

Course accounts for almost 44% of the sample, while other accounts for only about 11%. That large separation from the null model explains the strong test result.

Observed proportions: 0.4391, 0.2296, 0.1109 and 0.2203.

R pair 3: Cumulative departure and result summary

R chart of cumulative departure
Cumulative pattern of departure from the null model across categories.
R result summary chart
R summary panel reporting G², df, p-value and decision.
Pattern

The departure builds quickly

The cumulative panel shows that the mismatch between observed and expected frequencies is not a tiny fluctuation. The overall departure accumulates rapidly because the main categories deviate strongly from the null model.

The two largest sources of cumulative departure are course and other.
Meaning

The final inference is unambiguous

The summary chart brings the result together: G² = 142.6067, df = 3 and p less than .001 by a very large margin, so the null of equal category probabilities is rejected.

APA-style core: G²(3, N = 649) = 142.61, p < .001.
AdvertisementGoogle AdSense placement reserved after the R charts

G Test in Python, R, SPSS and Excel

Python workflow

Python can compute the one-way G Test directly from the observed counts and expected probabilities. It is also convenient for building observed-versus-expected charts and contribution plots. Related material is also covered in categorical data analysis in Python.

  • Enter the observed counts.
  • Define the null probabilities.
  • Calculate expected counts and G².
  • Obtain the p-value from a chi-square reference distribution.
  • Create diagnostic charts and a result summary.

R workflow

R is well suited to likelihood-ratio and Pearson goodness-of-fit testing, along with publication-ready residual and proportion plots. For broader background, see categorical data analysis in R.

  • Create a count vector.
  • Specify the expected probabilities.
  • Compute the G statistic manually or through likelihood-ratio tools.
  • Compare with Pearson’s chi-square if desired.
  • Visualize contributions and the final decision.

SPSS workflow

SPSS provides goodness-of-fit procedures and descriptive outputs for one-way categorical data. The SPSS report here confirms the same inferential conclusion as the Python and R reports. See categorical data analysis in SPSS.

  • Define the categorical variable.
  • Request one-way frequencies and goodness-of-fit output.
  • Confirm the null proportions.
  • Export the output PDF.
  • Match the SPSS result against the verified workbook.

Excel workflow

The Excel workbook shows every calculation explicitly: the observed table, expected counts, category components, diagnostics and reporting summary. This is especially useful when teaching the likelihood-ratio formula step by step.

  • Enter the counts in a table.
  • Calculate expected counts from null probabilities.
  • Use the natural-log formula for each component.
  • Sum the components to obtain G².
  • Use the chi-square right-tail function for the p-value.

Expandable Software Code

Python pseudo-code for the G Test
observed = [285, 149, 72, 143]
p0 = [0.25, 0.25, 0.25, 0.25]
n = sum(observed)
expected = [n * p for p in p0]
G2 = 2 * sum(o * math.log(o / e) for o, e in zip(observed, expected))
df = len(observed) - 1
p_value = chi2_sf(G2, df)
R pseudo-code for the G Test
observed <- c(285, 149, 72, 143)
p0 <- c(0.25, 0.25, 0.25, 0.25)
expected <- sum(observed) * p0
G2 <- 2 * sum(observed * log(observed / expected))
df <- length(observed) - 1
p_value <- pchisq(G2, df = df, lower.tail = FALSE)
Excel formula idea
=2*Observed*LN(Observed/Expected)
=SUM(component_range)
=CHISQ.DIST.RT(G_statistic, degrees_of_freedom)

Assumptions, Limitations and Advanced Interpretation

Assumptions

  • Observations are independent.
  • The categories are mutually exclusive and collectively exhaustive.
  • The expected probabilities are declared before testing.
  • Observed counts are positive for the log expression.
  • The chi-square approximation is adequate for the sample size.

Limitations

  • The G Test does not tell you why the distribution differs.
  • It does not estimate causal effects.
  • It can be sensitive to very large sample sizes.
  • It summarizes the global mismatch but not all practical implications.
  • It is not a substitute for a two-way association test.

Because the sample size is 649 and the expected counts are all 162.25, the chi-square approximation is very safe here. The result is therefore easy to trust from an asymptotic perspective. For related background on design and assumptions, see chi-square assumptions and effect size.

Common conceptual mistake: do not confuse a one-way G Test with a two-way likelihood-ratio test of independence. Both may be called likelihood-ratio chi-square tests, but they answer different questions and require different data structures.

APA Reporting for the G Test

Short APA statement

A one-way G Test showed that the observed distribution of school-choice reason differed significantly from an equal-probability model, G²(3, N = 649) = 142.61, p < .001.

Longer APA statement

A likelihood-ratio goodness-of-fit analysis was conducted to test whether the four-category school-choice reason variable followed an equal distribution. The result was statistically significant, G²(3, N = 649) = 142.61, p < .001, indicating that the observed category frequencies did not match the theoretical equal-probability model. Course was substantially overrepresented, whereas other was substantially underrepresented.

When teaching or presenting this result, it is helpful to report the observed counts and expected counts alongside the inferential statement. Students preparing for exams can compare this style with the resources in AP Statistics Formula Sheet and What Is the AP Statistics Exam?.

Common Mistakes and How to Correct Them

Common mistakes

  • Using the G Test when there are actually two variables and the design is an independence table.
  • Choosing expected probabilities after looking at the data.
  • Reporting only “significant” without the category pattern.
  • Ignoring the observed-versus-expected direction of departure.
  • Forgetting that goodness-of-fit tests a model, not a practical consequence.

How to fix them

  • Confirm whether the design is one-way or two-way before testing.
  • State the theoretical distribution in advance.
  • Include counts, statistic, degrees of freedom and p-value.
  • Identify which categories are over- and underrepresented.
  • Add an effect size such as Cohen’s w when appropriate.

Reports and Worked Excel Download

Frequently Asked Questions

What is the G Test used for?

The G Test is used to compare observed categorical frequencies with expected frequencies under a null model. In this article, it is used as a one-way goodness-of-fit test.

What is the formula for the G Test?

The one-way G Test formula is G² = 2 Σ O ln(O/E), where O is the observed count and E is the expected count.

What is the difference between the G Test and chi-square?

The G Test uses a likelihood-ratio statistic, while Pearson’s chi-square uses a squared-residual statistic. In many practical settings, both give very similar conclusions.

Can the G Test be used for independence tables?

Yes, there is a two-way likelihood-ratio chi-square test for contingency tables, but that is a different design from the one-way goodness-of-fit example shown here.

How do I report a G Test result?

Report the statistic, degrees of freedom, p-value, sample size, null model and the category pattern. A concise example is G²(3, N = 649) = 142.61, p < .001.

What does a significant G Test mean?

It means the observed category distribution differs from the theoretical distribution stated in the null hypothesis.

AdvertisementGoogle AdSense bottom placement reserved here

Conclusion

G Test is a powerful and interpretable way to assess whether an observed categorical distribution matches a theoretical model. In this worked example, the school-choice reason variable did not come close to the equal-probability null. The likelihood-ratio statistic was extremely large, G²(3, N = 649) = 142.61, p < .001, with the main departure coming from an excess of course responses and a deficit of other responses.

The practical lesson is just as important as the inferential one: after a significant G Test, the analyst should explain which categories are above expectation and which are below it. If you want to continue building your categorical-data toolkit, the next useful pages are p-value, significance level and test statistic, chi-square calculator, frequency and relative frequency tables and AP Statistics Formula Sheet.

Back to top

Need help applying this to your own data?

Salar Cafe can help interpret output, clean datasets, review assumptions, build dashboards and explain statistical results ethically.

Need help interpreting your data analysis results?

Contact Salar Cafe
Engr. Muhammad Yar Saqib author profile photo

Engr. Muhammad Yar Saqib

WhatsApp Get Data Analysis Help