Announcement

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

  • Descriptive stats table

    I have been trying to create a descriptive stats table with the following commands

    foreach var of $variablelist {

    di ""
    di "T test"
    di "ttest `var', by(binaryear1)"
    di ""

    ttest $variablelist, by(binaryear1)

    global mean $variablelist Y1 = r(mu_1)
    global mean $variablelist Y2 = r(mu_2)
    global mean $variablelist Y3 = r(mu_3)
    global $variablelist pvalueY1Y2Y3 = r(p)

    su $variablelist if binaryear1 == 0
    su $variablelist if binaryear1 == 1

    di ""
    di "mean`var'Y1 ${mean`var'Y1}"
    di "mean`var'Y2 ${mean`var'Y2}"
    di "mean`var'Y3 ${mean`var'Y3}"
    di "`var'pvalueY1Y2Y3 ${`var'pvalueY1Y2Y3}"
    di ""

    di ""
    di "T test"
    di "ttest `var', by($testvariable)"
    di ""
    ttest $variablelist , by($testvariable)

    global mean $variablelist 0 = r(mu_1)
    global mean $variablelist 1 = r(mu_2)
    global $variablelist pvalue = r(p)

    su $variablelist if $testvariable == 0
    su $variablelist if $testvariable == 1

    di ""
    di "mean`var'0 ${mean`var'0}"
    di "mean`var'1 ${mean`var'1}"
    di "`var'pvalue ${`var'pvalue}"
    di ""




    }


    but i recieve different error messages

  • #2
    Welcome to Statalist.

    There seems to be a confusion in when to use "var" and when to use "$variablelist". The premise, as I guessed, was to apply two t-tests (each with a different binary group indicator) on many continuous variables listed in "$variablelist".

    So, $variablelist will contain likely more than one variable. And that's the first source of mistake. For example, line 8 will already cause an error:

    Code:
    . sysuse auto
    (1978 automobile data)
    
    . ttest price mpg, by(foreign)
    too many variables specified
    r(103);
    because ttest only takes one variable as argument.

    So, how to move on from here, I can give two suggestions:

    If you really wish to use our help:
    1. Use dataex to provide some sample data
    2. Describe what you'd like to achieve
    3. The users here may be able to provide you with usable codes.
    If you wish to rely on your own (at least initially):
    1. Write out 1 copy of the the code without any loop. Make sure that it works.
    2. Make a copy, then wrap the copy with the foreach command.
    3. Assign a couple variable into the global macro like: "global variablelist x1 x2"
    4. Replace the variable inside the bracket with `var' inside the loop, test run them as you go along.
    5. Repeat the above and debug along the way.
    Here is a working chunk if one wants to loop through the variables:

    Code:
    sysuse nlsw88, clear
    
    global variablelist age wage tenure
    
    foreach var in $variablelist{
    
    display _newline(1)
    display "Analysis of `var'"
    display _newline(1)
    
    * Summarize
    bysort married: sum `var'
    
    * T-test
    ttest `var', by(married)
    
    display _newline(1)
    display "Mean `var' 1 = `r(mu_1)'"
    display "Mean `var' 2 = `r(mu_2)'"
    display "p-value = `r(p)'"
    }
    However, if you wish to create an output that is not just for one variable at a time, but a list of all the means and all the p-values from the t-tests, then I'd try other options such as statsby, putexcel, or writing a program to do that.
    Last edited by Ken Chui; 08 Sep 2023, 08:05.

    Comment

    Working...
    X