Good morning,
I have a dataset containing indicators for each country for each election year. The indicator I am interested in is EC_EU_positive, which assigns a value between 1 and 10 to every party that ran for election in a country in an election year.
I am interested in calculating the weighted mean and median, for each country and for each election year, of the variable EC_EU_positive. The weights are given by the vote shares of the parties that ran for that election.
For the weighted mean I used the following code:
ssc install _gwtmean
bysort countryname year: egen EC_EU_positive_weighted_mean=wtmean(EC_EU_positive ), weight(vote_share)
And it seems to work.
However, I have trouble calculating the weighted median. As far as I am aware, there is no specific command, so I tried with the following loop:
gen wtmedian = .
levelsof year, local(el_year)
foreach i in `el_year' {
summarize EC_EU_positive [w=vote_share] if countryname == "Belgium" & if year == `i', detail
replace wtmedian = r(p50) if countryname == "Belgium" & if year == `i'
}
But, when I run it, I get the error message “if not found” r(111). I suspect that this is due to the fact that we don’t have elections every year, so the loop finds some missing values.
Does anybody know a way to solve this issue?
Thanks in advance
Related Posts with Loop for weighted median
XTIVREG, FE: needs to be FE in stage 1 and 2?Hi all, I'm running a fixed effects regression using an IV variable, but I'm wondering, do I need t…
Can xtologit work with three-level model?I am currently working on a panel data with xtologit command. My data has a three-level structure wh…
Test how rating would have changed given new parameters but old characteristics?Dear colleagues, For firms i=1,...,50 and years t=1,...,10, I have ratings which can be expressed a…
Mean size of householdi am just a beginner in stata , i want to know a simple command for calculating the mean size of hou…
7-point ordinal dependent variable: Is it okay to transform and model with fracreg logit?Greetings, I'm running Stata 15.1 on a Mac OS and working with cross-sectional survey data. My depe…
Subscribe to:
Post Comments (Atom)
0 Response to Loop for weighted median
Post a Comment