Announcement

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

  • psmatch using foreach loop

    Dear All,

    My study aims to assess the policy effect of treatment_d. I conducted logit with the condition 'before_policy == 1'. I intend to perform one-to-one psmatch based on pscore; however, there are some challenges to address.
    I attempted to perform one-to-one psmatch for each 'group_number'. While trying to utilize 'gen' and 'egen' functions within a foreach loop, I encountered an error stating 'variable already defined'. Consequently, I created a macro outside the foreach loop. Despite this adjustment, the foreach loop failed to generate the required matches and halted after a single iteration. Working on this code for over two months has been quite frustrating.



    Code:
    logit treatment_d ncategory npublisher rank ncontinent price_range c.rating_score c.install_base i.group_number if before_policy == 1
    predict pscore,pr
    sum pscore,detail
    gen pscore2 = group_number * 1000000 + pscore
    scalar cal=r(sd)*0.00000000000000000000000000000000000000000000001
    
    
    gen treated = !mi(treatment_date)
    levelsof group_number, local(groups)
    local next_pair = 1
    
    gen pair = .
    local pairGen "pair = _id if before_treatmentd == 0. & before_policy == 1"
    local genPairCount "gen paircount = ."
    local replacePairCount "replace paircount = _N if before_treatmentd ~= . & before_policy == 1"
    local replacePairCount2 "replace paircount=. if paircount<2 & before_treatmentd~=. & before_policy == 1"
    
    foreach g of local groups {
        if `g' > 8 {
            psmatch2 treated if group_number == `g', pscore(pscore) outcome(downloads) neighbor(1) noreplace
            `pairGen'
            replace pair = _n1 if before_treatmentd == 1. & before_policy == 1
           
            local this_group_pairs = max(pair)
            local next_pair = `this_group_pairs' + 1
        
            bysort pair (ncategory npublisher rank ncontinent price_range c.rating_score c.install_base): keep if _n <= 2
            `genPairCount'
            by pair: `replacePairCount'
            `replacePairCount2'
            bysort pair (ncategory npublisher rank ncontinent price_range c.rating_score c.install_base): pstest
            *pstest ncategory npublisher rank ncontinent price_range c.rating_score c.install_base if _treated != . & group_number == `g', sum after labels
        }
            
    }

    Following the matching process, my goal is to utilize xfill to create matches for the remaining data where "before_policy==0.", which I haven't tried it yet.

    I would greatly appreciate your advice on this matter.
    Last edited by Eunah Cho; 16 Oct 2023, 21:06.
Working...
X