+ Reply to Thread
Results 1 to 3 of 3

Chi squared statistic function

Hybrid View

  1. #1
    Registered User
    Join Date
    10-06-2008
    Location
    London
    Posts
    2

    Chi squared statistic function

    Hi!

    First post so I hope this is okay.

    I'm trying to write a VBA function which performs the chi squared test (a simple statistical test to compare proportions).

    It works (roughly) by comparing the difference between observed values for cells in (say) a two-by-two grid, and the expected values for those cells (calculated by multiplying the row total by the column total and dividing by the overall total).

    I hope to get it to work without having to calculate the expected values as set out in the CHITEST Excel function.

    I’m trying to convert over code which works in ASP, but I don’t think I’m calling it correctly, as I keep getting #VALUE! or #NUM!

    Here's the code...
    Function NewChi(a As Integer, b As Integer, c As Integer, d As Integer)
    
    Dim N, ea, eb, ec, ed, chival As Integer
        N = a + b + c + d
       ea = ((a + c) * (a + b)) / N
       eb = ((b + d) * (a + b)) / N
       ec = ((a + c) * (c + d)) / N
       ed = ((b + d) * (c + d)) / N
      chival = (((a - ea) ^ 2) / ea) + (((b - eb) ^ 2) / eb) + (((c - ec) ^ 2) / ec) + (((d - ed) ^ 2) / ed)
      
    NewChi= chival
    
    End Function
    ...and an example's attached.

    Thanks!

    Iain
    Attached Files Attached Files
    Last edited by igillesp; 10-06-2008 at 03:14 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Using Integer you get overflow. Use Doubles.
    Function NewChi(a As Double, b As Double, c As Double, d As Double) As Double
        Dim N       As Double
        Dim ea      As Double
        Dim eb      As Double
        Dim ec      As Double
        Dim ed      As Double
    
        N = a + b + c + d
        ea = ((a + c) * (a + b)) / N
        eb = ((b + d) * (a + b)) / N
        ec = ((a + c) * (c + d)) / N
        ed = ((b + d) * (c + d)) / N
        NewChi = (((a - ea) ^ 2) / ea) + _
                 (((b - eb) ^ 2) / eb) + _
                 (((c - ec) ^ 2) / ec) + _
                 (((d - ed) ^ 2) / ed)
    End Function
    Also, in this declaration,
    Dim N, ea, eb, ec, ed, chival As Integer
    ... everything but chival is declared as a Variant.

    You can do this,
    Dim N As Variant, s As String, d As Double, ...
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-06-2008
    Location
    London
    Posts
    2
    Brilliant!

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How does this code output the coefficients of a trendline?
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-13-2010, 05:54 PM
  2. Excel 2007 error when adding custom help file to user defined function
    By sabotuer99 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2009, 01:10 AM
  3. Modify a Function to add an extra condition
    By King_Quake in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-03-2008, 04:32 PM
  4. Link together different cells in specific order?
    By Sandman4432 in forum Excel Formulas & Functions
    Replies: 19
    Last Post: 01-03-2008, 05:01 AM
  5. Analysis Toolpak Function XIRR and VBA - XL 2007
    By rvExcelNewTip in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2007, 04:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1