Announcement

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

  • collapse command -- how to store standard error of the mean

    Hi Statalisters,

    I'd like to make line graphs of the means with error bars. I've collapsed my data to get the means by treatment and control group, but I want to retain the standard errors of the means so that I can graph error bars. My code is below -- any help is greatly appreciated. Thanks!

    Code:
     collapse id read1 read2 read3 [pw=_weight], by(_treated)

  • #2
    The -collapse- command enables you to do this. You don't specify what kind of variables read1-read3 are. But you can do something like this:

    Code:
    collapse id read1 read2 read3 (sem) se_read1 = read1 se_read2 = read2 se_read3 = read3 [pw = _weight], by(_treated)
    If read* are dichotomous or count variables, then (seb) or (sep) might be more appropriate than (sem) in the above.

    See the online help and manual sections for -collapse- for more information about these options, and the many other statistics you can collect with -collapse-.

    Comment


    • #3
      Thanks, Clyde! The code worked, but do you have any idea how to store the standard error of the means as scalars? Ultimately the code I want in order to graph the lines is this:
      Code:
       lgraph math time [pw=_weight], by(_treated) statistic(mean) errortype(semean) nomarker
      .

      Comment


      • #4
        Some very considerable confusion here.

        1. lgraph (please give the source of user-written programs, as requested) as I understand it calculates standard errors for you; it does not expect to be fed SEs as variables, scalars or anything else.

        2. If using lgraph was what you wanted to do at the outset then resort to collapse is going in the precisely opposite direction.

        3. If you have means of response variables by groups, and want to see graphs of confidence intervals, then that is nothing to do with line graphs as would be drawn by Stata for time series etc.: that's lgraph's principal territory.

        I can imagine a route for you, based on combining mean with standard graph commands to get you want.

        This example shows some technique, but you may need something more complicated.

        Code:
        webuse highschool, clear
        svy: mean weight, over(sex)
        local ytitle `: var label weight'
        mat x = r(table)
        mat li x
        clear
        set obs 2
        gen female = _n - 1
        label def female 0 male 1 female
        label val female female
        gen mean = x[1, _n]
        gen ll = x[5, _n]
        gen ul = x[6, _n]
        twoway rcap ll ul female || scatter mean female, ///
        ytitle("`ytitle'") xla(0 1, valuelabel) xsc(r(-0.5 1.5)) legend(off) ///
        note(95% confidence intervals)


        Last edited by Nick Cox; 02 Mar 2015, 06:51.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          The -collapse- command enables you to do this. You don't specify what kind of variables read1-read3 are. But you can do something like this:

          Code:
          collapse id read1 read2 read3 (sem) se_read1 = read1 se_read2 = read2 se_read3 = read3 [pw = _weight], by(_treated)
          If read* are dichotomous or count variables, then (seb) or (sep) might be more appropriate than (sem) in the above.

          See the online help and manual sections for -collapse- for more information about these options, and the many other statistics you can collect with -collapse-.
          Dear Statalisters,
          I am confused by the code posted above by Clyde Schechter (pls see quote).
          I am using survey data with pweights, and I read in the manual for -collapse- that pweights are not allowed for semean. However, Tracy Lam confirms that the code works. I am using Stata Version 13.1 on Windows 7, update level 19 Dec 2014, and I cannot get standard errors of the mean with -collapse- when using pweights. Did I miss an update?
          Cheers,
          Anna

          Comment


          • #6
            Hmm. You are correct, Anna. -collapse- does not accept pweights with (sem). My code was incorrect. I had not tested it prior to posting it and was unaware of the limitation. I'm not sure what Tracy Lam did that "worked." Perhaps she disregarded the weights or pretended they were aweights. Sorry for causing confusion. Egg on my face.

            Comment


            • #7
              Thanks a lot, Clyde, for the prompt response.
              In order to get standard errors of the mean, there is no other way than using
              Code:
              svy: mean variable, over(id)
              and store the contents of e(b) and e(V) in a matrix and convert them to variables. Is that correct?
              Thank you for you help.
              Last edited by Anna Raggl; 10 Mar 2015, 12:45.

              Comment

              Working...
              X