Announcement

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

  • How to work on multiple subsets of a dataset?

    Hi Statalist,

    I am working on a bid dataset (more than 10000 observations). The variable wc varies from 0.1 to 1, which ends up 10 subsets. For each subset, I would like to apply
    Code:
    kdensity wb,  gen(wpdf) at (wb)
    ,then combine the 10 corresponding outputs together to one dataset. Doing each subset one by one works, but is time consuming. Any help to work it out easier? THANKS!
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int nbidder float(wb wc)
    2  .0482752 .1
    2 .04862725 .1
    2 .05051757 .1
    2  .0572238 .1
    2 .06130194 .1
    2  .0696369 .1
    2 .06972899 .1
    2 .07122661 .1
    2 .08323907 .2
    2 .08330324 .1
    2  .0835347 .1
    2 .08376935 .2
    2 .08387282 .2
    2  .0839537 .2
    2 .08401852 .2
    2 .08647278 .1
    2  .0874489 .2
    2 .08905575 .2
    2 .08974329 .1
    2 .09038565 .2
    end

  • #2
    Something like the following untested code might work; at least it should get you started. The technique is common: start with a result variable with all values missing, calculate your result on a subset of the data and store it in a temporary variable, and then only copy the values for that subset to the actual result variable.
    Code:
    generate group = round(10*wc)
    generate wpdf = .
    forvalues g=1/10 {
        kdensity wb if group==`g', generate(temp) at (wb)
        replace wpdf = temp if group==`g'
        drop temp
    }
    The reason for creating the group variable is that testing equality of numbers intended to be multiples of 1/10 is likely to have problems because of precision issues. That's like asking if 1/3 == .3333333 - you can't represent the fraction exactly in binary any more than you can represent 1/3 precisely in decimal.

    Comment


    • #3
      Hi William,

      Thank you for your help! I appreciate it!
      Have a nice day!

      Best,
      Li

      Comment

      Working...
      X