Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 968335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:27:44+00:00 2026-05-16T02:27:44+00:00

For 1,000,000 observations, I observed a discrete event, X, 3 times for the control

  • 0

For 1,000,000 observations, I observed a discrete event, X, 3 times for the control group and 10 times for the test group.

I need to preform a Chi square test of independence in Matlab. This is how you would do it in r:

m <- rbind(c(3, 1000000-3), c(10, 1000000-10))
#      [,1]   [,2] 
# [1,]    3 999997
# [2,]   10 999990
chisq.test(m)

The r function returns chi-squared = 2.7692, df = 1, p-value = 0.0961.

What Matlab function should I use or create to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:27:44+00:00Added an answer on May 16, 2026 at 2:27 am

    Here is my own implementation that I use:

    function [hNull pValue X2] = ChiSquareTest(o, alpha)
        %#  CHISQUARETEST  Pearson's Chi-Square test of independence
        %#
        %#    @param o          The Contignecy Table of the joint frequencies
        %#                      of the two events (attributes)
        %#    @param alpha      Significance level for the test
        %#    @return hNull     hNull = 1: null hypothesis accepted (independent)
        %#                      hNull = 0: null hypothesis rejected (dependent)
        %#    @return pValue    The p-value of the test (the prob of obtaining
        %#                      the observed frequencies under hNull)
        %#    @return X2        The value for the chi square statistic
        %#
    
        %# o:     observed frequency
        %# e:     expected frequency
        %# dof:   degree of freedom
    
        [r c] = size(o);
        dof = (r-1)*(c-1);
    
        %# e = (count(A=ai)*count(B=bi)) / N
        e = sum(o,2)*sum(o,1) / sum(o(:));
    
        %# [ sum_r [ sum_c ((o_ij-e_ij)^2/e_ij) ] ]
        X2 = sum(sum( (o-e).^2 ./ e ));
    
        %# p-value needed to reject hNull at the significance level with dof
        pValue = 1 - chi2cdf(X2, dof);
        hNull = (pValue > alpha);
    
        %# X2 value needed to reject hNull at the significance level with dof
        %#X2table = chi2inv(1-alpha, dof);
        %#hNull = (X2table > X2);
    
    end
    

    And an example to illustrate:

    t = [3 999997 ; 10 999990]
    [hNull pVal X2] = ChiSquareTest(t, 0.05)
    
    hNull =
         1
    pVal =
         0.052203
    X2 =
           3.7693
    

    Note that the results are different from yours because chisq.test performs a correction by default, according to ?chisq.test

    correct: a logical indicating whether
    to apply continuity correction
    when computing the test statistic for 2×2 tables: one half is
    subtracted from all |O – E| differences.


    Alternatively if you have the actual observations of the two events in question, you can use the CROSSTAB function which computes the contingency table and return the Chi2 and p-value measures:

    X = randi([1 2],[1000 1]);
    Y = randi([1 2],[1000 1]);
    [t X2 pVal] = crosstab(X,Y)
    
    t =
       229   247
       257   267
    X2 =
         0.087581
    pVal =
          0.76728
    

    the equivalent in R would be:

    chisq.test(X, Y, correct = FALSE)
    

    Note: Both (MATLAB) approaches above require the Statistics Toolbox

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need about 1,000 words to be able to be accessed constantly by my
I have a several data sets with 75,000 observations and a type variable that
I have over 11,000 pages in a site and need to change the tag
I have 30,000 rows in a database that need to be similarity checked (using
I have 28,000 images I need to convert into a movie. I tried mencoder
I have about 20,000 records (coming from an SQLite db) that I need to
I have 50,000,000 (integer, string) pairs in a text file. The integers are times
I have 100,000 images which are not under my control. Some of these images
So I have 4,000 large gzipped text files. Because of their size, I need
For instance: 0:000> ?? testFile //check this variable char * 0x009c6758 e:\TEST\example.FOO Question: How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.