Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: RE: unexplained discrepancy between

 From Steve Nakoneshny To "statalist@hsphsun2.harvard.edu" Subject Re: st: RE: RE: unexplained discrepancy between Date Wed, 21 Dec 2011 14:29:35 -0700

```Nick,

Thank you very much for the quick response. Like you, I had initially suspected that the issue related to precision, but I wanted the opinion of the list to validate my assumption. Just as interesting: when I substituted in the condensed loop that you provided for the longer one we had written, the discrepancies disappeared. Rather than chasing that down any further, we inserted your loop into our do file and will chalk that one up to experience. I hadn't been aware of the -cond- function previously.

Thanks,
Steve

On 2011-12-21, at 11:46 AM, Nick Cox wrote:

> No; belay that. You are using the same median variables in both comparisons according to this code.
>
> So, what I said looks wrong. Sorry,
>
> Nick
> n.j.cox@durham.ac.uk
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
> Sent: 21 December 2011 18:22
> To: 'statalist@hsphsun2.harvard.edu'
> Subject: st: RE: unexplained discrepancy between
>
> At the heart of this are comparisons that include a test for equality with the median. In principle the median will always be one of the observed values if the sample size is odd but it may not be if the sample size is even. So in principle looking for equality with the median clearly does make sense.
>
> However, looking at the code for -egen, median()- makes it evident that that function is doing its calculations with a -double- version of the variable supplied; you make clear that you are working with -float-s so I suspect therefore that there is a minor problem of precision here as your other operations are not guaranteed to produce identical results.
>
> In general it is optimistic to do any testing for equality with a non-integer value unless you maintain absolute consistency of variable types throughout.
>
> A secondary issue is that your code can be cut right down. On the evidence here you don't need a separate variable holding the median for each variable.
>
> foreach x in min max mean {
> 		local var cnratio_`x'_p
> 		qui su `var', detail
> 		gen cn`x'hilo = cond(missing(`var'), . , `var' > r(p50))
> }
>
> That aside, if you -list- the values for which the indicator is 1 one way and 0 the other way I suspect that you will find that they are very close indeed and that all that has happened is that a knife-edge decision went different ways depending on a few bits, perhaps even one.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Steve Nakoneshny
>
> We are working with a dataset of biomarker expression data. A colleague created some dummy variables using the median value as a dichotomous cut point for high / low expression. We also felt that this process would lend itself extremely well to using a loop. Here is the code we wrote / executed:
>
> --- begin code ---
>
> local y cnratio_min_p cnratio_max_p cnratio_mean_p		//create the median cut points for each min, max and mean ratio
>
> foreach x of local y	{
> 		egen `x'_median = median(`x')
> 		label variable `x'_median "Median Cut Point `x'"
> }
>
> *** create hilo vars with loop ***
> local x cnminhilo cnmaxhilo cnmeanhilo
>
>
> foreach var of local x	{
> 	gen `var' = .
> 		foreach val in `y' {
> 	replace `var' = 1 if `val' > `val'_median & `val' < .
> 	replace `var' = 0 if `val' <= `val'_median
> 	}
> }
>
> *** Here's the old school way to create hilo variables for each of cn min max and mean ***
> gen cnminhilo_jcd = .
> replace cnminhilo_jcd=1 if cnratio_min_p > cnratio_min_p_median & cnratio_min_p < .
> replace cnminhilo_jcd=0 if cnratio_min_p <= cnratio_min_p_median
> label variable cnminhilo_jcd "CN Ratio > Median Cutpoint of Min"
> tab cnminhilo_jcd,m
>
> gen cnmaxhilo_jcd = .
> replace cnmaxhilo_jcd =1 if cnratio_max_p > cnratio_max_p_median & cnratio_max_p < .
> replace cnmaxhilo_jcd =0 if cnratio_max_p <= cnratio_max_p_median
> label variable cnmaxhilo_jcd "CN Ratio > Median Cutpoint of Max"
> tab cnmaxhilo_jcd,m
>
> gen cnmeanhilo_jcd = .
> replace cnmeanhilo_jcd =1 if cnratio_mean_p > cnratio_mean_p_median & cnratio_mean_p < .
> replace cnmeanhilo_jcd =0 if cnratio_mean_p <= cnratio_mean_p_median
> label variable cnmeanhilo_jcd "CN Ratio > Median Cutpoint of Mean"
> tab cnmeanhilo_jcd,m
>
> --- end code ---
>
>
>
> We then crosstabbed the results from each method to validate the results and found some discrepancies. Here is the output:
>
> --- begin code ---
>
>
> . tab cnminhilo cnminhilo_jcd,m
>
>           |  CN Ratio > Median Cutpoint of
>           |               Min
> cnminhilo |         0          1          . |     Total
> -----------+---------------------------------+----------
>         0 |        51          6          0 |        57
>         1 |         6         50          0 |        56
>         . |         0          0         13 |        13
> -----------+---------------------------------+----------
>     Total |        57         56         13 |       126
>
>
> . tab cnmaxhilo cnmaxhilo_jcd,m
>
>           |  CN Ratio > Median Cutpoint of
>           |               Max
> cnmaxhilo |         0          1          . |     Total
> -----------+---------------------------------+----------
>         0 |        50          7          0 |        57
>         1 |         7         49          0 |        56
>         . |         0          0         13 |        13
> -----------+---------------------------------+----------
>     Total |        57         56         13 |       126
>
>
> . tab cnmeanhilo cnmeanhilo_jcd,m
>
>           |  CN Ratio > Median Cutpoint of
>           |               Mean
> cnmeanhilo |         0          1          . |     Total
> -----------+---------------------------------+----------
>         0 |        57          0          0 |        57
>         1 |         0         56          0 |        56
>         . |         0          0         13 |        13
> -----------+---------------------------------+----------
>     Total |        57         56         13 |       126
>
> --- end code ---
>
> We then explored the data and found that the 6 obs where cnminhilo==1 & cnminhilo_jcd==0 were incorrectly coded in cnminhilo. The same held true for the other discrepancies in cnminhilo and cnmaxhilo.
>
> We've looked at the syntax of the loop and cannot see any differences between it and the longer hand-coding method used. We're at a loss to explain why and how these discrepancies arose. If it helps at all, all variables used here are stored as floats and we're using Stata/IC 11.2 for Mac. Hopefully someone can help enlighten us.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```