Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trend test in Stata

    Hello,

    I have been using Stata to analyse all of my data, however my supervisor has asked me to perform a trend test similar to SPSS' "linear-by-linear association" in crosstab>chi2.

    I have two different things to test trends for:
    1. test trend of a continuous variable (example age) across an ordered variable (quantiles in my case, score 1 2 3 4).
    2. test trend for categorical variable (example type of diet - not ordered) across an ordered variable (again, same quantiles).

    How do I perform these tests in Stata?
    I have been using nptrend for 1. and thought it was correct but it doesn't give me the same output as in SPSS.
    I have read the Stata article about trends, also tried the vwls command (again similar but not the same output as SPSS).

    Is it okay to use nptrend in case 2. ?

    Thank you for your help and time,
    Céline (a beginner epidemiologist)

  • #2
    Hello Celine. When I started using Stata, I too wondered about how to get the test of "linear-by-linear" association SPSS CROSSTABS gives. Here are some notes by (the late) David Howell on Chi-square With Ordinal Data. As Howell's notes show, the Chi-square value for the test of linear-by-linear association = (N-1)r2, where r = the Pearson correlation between the two ordinal variables.

    Here's some code I wrote last year, but updated today adding another example from this Stata FAQ page. Perhaps you'll find it helpful.

    Code:
    * =================================================
    * File:  Ordinal_Chi-square.do
    * Name:  Bruce Weaver, [email protected]
    * Date:  1-Aug-2019
    * =================================================
    
    * Use David Howell's example from this page:
    * https://www.uvm.edu/~dhowell/methods7/Supplements/OrdinalChiSq.html
    
    * Stuff the calculations in a program called -ordchi2-.
    * This program uses r-class postestimation info from -tabulate-,
    * therefore, it must be called immediately after a -tabulate- commmand.
    
    capture program drop ordchi2
    program ordchi2
    local dfPearson = (r(r)-1)*(r(c)-1)
    local Pearson = r(chi2)
    local p1 = r(p)
    quietly correlate r c [fweight = n]
    local Linear = (r(N)-1)*r(rho)^2
    local Dev = `Pearson'-`Linear'
    local dfDev = `dfPearson'-1
    local p2 = chi2tail(1,`Linear')
    local p3 = chi2tail(`dfPearson'-1,`Pearson'-`Linear')
    
    display _newline ///
    "The Mantel-Haenszel Chi-square reported below is equivalent to the" _newline ///
    "test of linear-by-linear association in the SPSS CROSTABS output." _newline ///
    "It is only appropriate when both variables are ordinal (or dichotomous)." _newline ///
    "For 2x2 tables, the Mantel-Haenszel Chi-square = the N-1 Chi-square." _newline(2) ///
    "         Pearson chi2 = " `Pearson' ", df = " `dfPearson' ", p = " `p1' _newline ///
    " Mantel-Haenszel chi2 = " `Linear' ", df = 1, p = " `p2' _newline ///
    "Deviation from linear = " `Dev' ", df = " `dfDev' ", p = " `p3' _newline(2)
    end
    
    * -------------------------------------------------------------
    
    * Read in the data
    clear
    input r c n
    1 1 25    
    1 2 13    
    1 3  9    
    1 4 10
    1 5  6
    2 1 31
    2 2 21
    2 3  6
    2 4  2
    2 5  3
    end
    
    tabulate r c [fweight = n], chi2 lrchi2
    ordchi2 // Compute the Mantel-Haenszel (ordinal) Chi-square
    
    * -------------------------------------------------------------
    
    * Another example from this Stata FAQ:
    * https://www.stata.com/support/faqs/statistics/test-for-trend/
    
    * Read in the data
    clear
    input r c n
    1 1 19    
    1 2 31    
    1 3 67    
    2 1  1
    2 2  5
    2 3 21
    end
    
    tabulate r c [fweight = n], chi2 lrchi2
    ordchi2 // Compute the Mantel-Haenszel (ordinal) Chi-square
    
    * Notice that my result for the Mantel-Haenszel Chi-Square
    * matches the results from SAS shown on the FAQ page.
    * Notice too that the statistic the FAQ author calls Qs
    * is identical to the Mantel-Haenszel Chi-Square.  
    
    * =================================================
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 18.5 (Windows)

    Comment


    • #3
      Hello,

      First thank you for your help, I don't think it solves my questions but it might be helpful in the future.
      You wrote "It is only appropriate when both variables are ordinal (or dichotomous)." _newline ///"
      But I only have one ordinal variable (the quantiles) and I'm trying to see if there is a trend across the quartiles for a continuous variables (like age or number of dropout in the example you mention) or a categorical one (like diet, which is not ordinal, it's many types).

      Best,
      Céline

      Comment


      • #4
        please see the FAQ that Bruce Weaver cited in #2 which goes through many tests/procedures; my own preference would be to use regression and the FAQ has an example

        Comment


        • #5
          I'm sorry Céline. I got too focused on your supervisor asking for something like the SPSS CROSSTABS linear-by-linear association test, and did not read carefully enough what it is you actually want to do. Here are the two scenarios you described:

          1. test trend of a continuous variable (example age) across an ordered variable (quantiles in my case, score 1 2 3 4).
          2. test trend for categorical variable (example type of diet - not ordered) across an ordered variable (again, same quantiles).
          I agree with Rich (in #4) that these sound like regression problems. Assuming continuous age (in 1) and type of diet (in 2) are explanatory variables, it looks like ordinal logistic regression (-ologit-). You'll be able to test for various kinds of trends (e.g., linear, quadratic, cubic) for age in the first analysis; but with a nominal categorical variable, I don't think there's any way to look for trends. But perhaps I misunderstand what you are trying to do.

          HTH.
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 18.5 (Windows)

          Comment


          • #6
            If I understood right, you have at least one ordered variable, hence you may think about the Jonckheere-Terpstra "trend" test.

            For that, you need to download the user-written jonter program.

            Best regards,

            Marcos

            Comment


            • #7
              Hello,

              Bruce, I think you are right, it is a kind of regression problem.
              I have tried using "vwls" (like in the Stata article) but I don't know if it is correct. My problem is also that although the group variable is ordinal (from no consumption to higher quartiles of consumption, 0 to 4), it is not equally spaced. Meaning that are more non-consumers in group 0 than people in each quartiles of consumers (1-4). It is "unbalanced" you might say.
              https://www.ncbi.nlm.nih.gov/pubmed/21899787 In this paper, they seem to have done what I am trying to achieve using a GLM with "coefficient set of linear combinations"? As I understand it, it is because of this problem of equally within the ordinal variable. But they have used SAS which I am not at all familiar with.

              Best,
              Céline

              Comment

              Working...
              X