Announcement

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

  • Nested Loops and statistics

    I am trying to build a nested loop setup, which can be used to generate mode of responses. The loops are on treatments and individual IDs. Furthermore, I have to put in some conditions to filter out certain set of data. The conditions are on gender, individual ID and treatments. Within each treatment, I have to exclude the individual for whom I am generating the mode, then I have to filter for gender, either male or female. Then I have to randomly select 5 person from those who remains. Then calculate the modal response of them for the question. I am currently using the following code but it is generating missing values for multiple individuals across multiple treatments. The number is increasing as I am moving towards later iterations.

    The code I am using is as follows -

    set seed 12345
    gen random = runiform()
    forvalues t = 1(1)5 {
    forvalues i = 0(1)4 {
    foreach j in u m c d{
    * Reload the original dataset for each combination
    use "datafile1.dta", clear

    * Filter for current treatment
    keep if treatment == `t'

    * Generate individual ID
    gen t`i'`j'indv_id = _n

    * Get list of IDs
    levelsof t`i'`j'indv_id, local(idp)

    foreach k of local idp {
    * Reload data for each ID to avoid cumulative filtering
    use "${path}/Temp/datafile1.dta", clear
    keep if treatment == `t'
    gen t`i'`j'indv_id = _n

    * Apply filters
    keep if t`i'`j'indv_id != `k'
    keep if p3q2_gender == 0
    sort random
    keep in 1/5

    * Calculate frequency and mode (corrected syntax)
    bysort p1q3_t`i'`j'_s : gen freq_p1q3_t`i'`j'`k' = _N if !missing(p1q3_t`i'`j'_s)
    egen mode_p1q3_t`i'`j'`k' = mode(p1q3_t`i'`j'_s), maxmode
    summarize mode_p1q3_t`i'`j'`k'
    scalar p1q3_t`i'`j'`k'_mode = r(mean)
    * Save and append
    drop freq_p1q3_t`i'`j'`k'
    tempfile temp_t`i'`j'`k'
    save `temp_t`i'`j'`k''

    use "${path}/Temp/clean1.dta", clear
    append using `temp_t`i'`j'`k''
    save "${path}/Temp/clean1.dta", replace
    }
    }
    }
    }

    The used datasets / dta files are already created beforehand. Also after I get the mode, I want to save it as a scalar and associate it with the individual for whom it was generated.
    Every treatment has more than enough observations (around 160 and almost equal number of male and female). I am not getting why is it generating missing values and then no modal value.
    If anyone can help me or find the error in the logic I am using, it would be a great help.
    Thank You.
Working...
X