Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: jitter


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: jitter
Date   Fri, 20 Jan 2006 00:48:51 -0000

Here is a rough program that allows
separate jittering. Given #, the sd of the noise
is #/500 of the range on the axis specified, 
or #/500 when that variable is a constant. 
I made up this criterion and anyone is 
at liberty to clone the program and use 
their own. 

*! NJC 1.0.0 20 January 2006
*! separate jittering of x and y coordinates 
*! Example: jscatter mpg rep78, xj(2) 
program jscatter
	version 9 
	syntax varlist(min=2 max=2) [if] [in] ///
	 [, XJitter(numlist max=1 >=0) YJitter(numlist max=1 >=0) *]

	marksample touse
	qui count if `touse'
	if r(N) < 2 error 2001

	tokenize `varlist'
	args y x

	qui if "`xjitter'" != "" {
		tempvar xclone
		clonevar `xclone' = `x'
		if `"`: var label `xclone''"' == "" label var `xclone' "`x'" 
		
		su `x' if `touse', meanonly
		if r(max) == r(min) { 
			local range = 1 
			local xopt "xsc(r(`= r(max) - 0.5' `= r(max) + 0.5'))"
			local xopt "`xopt' xla(`= r(max)')"
		}
		else local range = r(max) - r(min)
		
		replace `xclone' = `xclone' + ///
		invnorm(uniform()) * (`xjitter' / 500) * `range'
		local x "`xclone'"
	}

	qui if "`yjitter'" != "" {
		tempvar yclone
		clonevar `yclone' = `y'
		if `"`: var label `yclone''"' == "" label var `yclone' "`x'" 
		
		su `y' if `touse', meanonly
		if r(max) == r(min) { 
			local range = 1 
			local yopt "ysc(r(`= r(max) - 0.5' `= r(max) + 0.5'))"
			local yopt "`yopt' yla(`= r(max)')"
		}
		else local range = r(max) - r(min)
		 
		replace `yclone' = `yclone' + ///
		invnorm(uniform()) * (`yjitter' / 500) * `range'         
		local y "`yclone'"
	}

	scatter `y' `x' if `touse', `xopt' `yopt' `options'
end

Nick 
n.j.cox@durham.ac.uk 

n j cox
 
> There is no need to write off adding noise yourself
> as a "manual" solution. This should be easily programmable,
> and without action from StataCorp, so why not do that to get
> what you want?
> 
> Alternatively, there are other solutions to get a
> neat display of repeated values, by stacking not
> jittering. I have found that jittering often has two disadvantages:
> 
> 1. It can be tricky to get exactly what you want, because
> of the randomness.
> 
> 2. Lots of people seem not to have heard of jittering, so
> with some audiences you have to go into explanations.
> Stacking needs far less explanation.
> 
> Stacking is precisely what -dotplot- does. If you don't like the way
> that -dotplot- offers you control of the binning, then
> -stripplot- from SSC is an alternative.
> 
> David Airey
> 
> The jitter option in Stata graphs adds user controlled amounts of
> spherical noise to plotted data, useful for categorical data plots to
> prevent overprinting. But when only the X axis of a plot is
> categorical, I don't necessarily want to add scatter in my Y axis. I
> just want to add scatter in the X axis. It would be nice to have the
> jitter option take two arguments, one for X and one for Y, say,
> 
> scatter y x, jitter(0 7)
> 
> to jitter the x axis only.
> 
> I know I can manually add some amount of random noise to a new
> generated variable of X and plot that, but making the jitter option
> do the above would be more desirable for me!

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index