Announcement

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

  • t-test with multiple conditions

    Dear all,

    I have a panel data set of individuals across multiple year. I want run a simple t-test and then plot the means of the two groups on a graph.
    In particular, I wan to compare hourly wages of women comparing two groups (moms and women without kids), but across age quantiles. If I repeat t-tests for each quantile, the code would look like:

    Code:
    ttest hpay if _sex == 2 & agequant == 1, by(parent)
    ttest hpay if _sex == 2 & agequant == 2, by(parent)
    ttest hpay if _sex == 2 & agequant == 3, by(parent)
    ttest hpay if _sex == 2 & agequant == 4, by(parent)
    Where _sex indicate females, parent is a dichotomous variable equal to 1 if a female in the sample has kids and agequant is a variable that divides ages in 10 quantiles.

    1 Is there a practical way to do this bunch of codes in one line?
    2 How can I plot the means of each group to be tested in a two-way line?

    Thank You in advance

    Best

    Stefano


  • #2
    For the series of t-tests, if 1, 2, 3, and 4 constitute all of the value of agequant, the simplest code would be:
    Code:
    by agequant, sort: ttest hpay if _sex == 2, by(parent)
    If agequant takes on other values and you don't want to be bothered with output for those other values, then you could do it as:
    [code]
    forvalues a = 1/4 {
    ttest hapy if _sex == 2 & agequant == `a', by(parent)
    }

    As for graphing the means:
    Code:
    keep if _sex == 2
    collapse (mean) hpay, by(agequant parent)
    will get you to a data set with just the relevant means and indicators of age and father/mother. But it is not clear what kind of line graph you want to make out of these. Do you want agequant on the horizontal axis, with two separate panels, one for fathers and one for mothers? If so:

    Code:
    graph twoway line hpay agequant, sort by(parent)
    Or if you want both the father and mother curves in a single panel, it takes a little more work:

    Code:
    reshape wide hpay,i(agequant) j(parent)
    graph twoway line hpay* agequant, sort
    Note: If parent is a string variable, you have to add -string- to the end of the -reshape- command.

    Comment


    • #3
      Thank you very much Clyde, it works fine.

      S

      Comment


      • #4
        Clyde, I am having trouble with the sort function. I used an adaptation of
        by agequant, sort: ttest hpay if _sex == 2, by(parent) but I am getting an error code "variable sort not found" do you have any thoughts?

        Comment


        • #5
          You don't show any example data, but I have run similar commands using the auto.dta and they run without difficulty. I can't replicate your problem.

          Are you sure you didn't mistakenly omit the comma after agequant? That would produce precisely the error message you got.

          If that's not the solution to your problem and you can't otherwise figure it out, please post back with example data that reproduces the problem. Use the -dataex- command to do that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

          Comment

          Working...
          X