Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Support for negative time-format (duration)


From   Steinar Fossedal <[email protected]>
To   [email protected]
Subject   Re: st: RE: Support for negative time-format (duration)
Date   Thu, 1 Oct 2009 13:16:33 +0200

Thank you for your answer Nick, I was afraid there was no quick
solution for my problem.

Having slept on it, I came up with a workaround solution which covers
my needs. By binning the variable timediff before creating the
histogram and then using labels to display the time values, I can get
my time values displayed as negative. In case someone else has the
same need, I'll attach my small program below.

It still has one issue: The labels should appear exactly between the
pillars, however they appear offset compared to the pillars. Is there
any way of offsetting the labels or the pillars to make them fit each
other the way they should?

Cheers,
-Steinar


capture program drop durhist
program durhist
	version 10
	syntax varlist(max=1) [if] [in] [fw] [,width(real 0) bin(real 10)
Format(string)] [*]

	marksample touse

	tempvar group

	if "`format'"=="" {
		local format: format `varlist'
	}

	qui sum `varlist' [`weight'`exp'] if `touse'
	local minval `r(min)'
	local maxval `r(max)'
	if `width'==0 local width = (`maxval'-`minval')/`bin'
	else local bin = (`maxval'-`minval')/`width'

	// Create categorical variable containing grouping for the histogram
	local binnr 0
	forvalues cutpoint = `minval'(`width')`maxval' {
		local atdef `atdef' `cutpoint'
		local binlbl = string(abs(`cutpoint'), "`format'")
		if `cutpoint'<0 local binlbl -`binlbl'
		local lbldef `lbldef' `binnr' "`binlbl'"
		local binnr = `binnr' + 1
	}
	egen `group' = cut(`varlist') if `touse', at(`atdef') icodes

	// Display histogram
	hist `group' [`weight'`exp'] if `touse', discrete xlabel(`lbldef')
xtitle("`varlist'") `options'
end

// Create testdata
clear
set obs 100
gen n = _n
gen timediff = int((uniform()-.5)*100000)
format timediff %tcHH:MM:SS

// Run program
durhist timediff, format(%tcMM:SS) xlabel(,angle(45))





2009/9/30 Nick Cox <[email protected]>:
> For listing I think you need StataCorp to come up with a new display format, or to do it yourself by creating a string variable that displays as you wish.
>
> Nick
> [email protected]
>
> Steinar Fossedal
>
> I'm currently working on a dataset where I am interested in examining
> the observations' time difference compared to a base time. Thus I have
> defined the variable
>
> gen double timediff = observedtime - basetime
> format timediff %tcHH:MM:SS
>
> In cases with negative timediff, this format displays the results as
> e.g. 23:50:00 (for 10 minutes negative). I would like the results
> displayed as -00:10:00, so they can be easily related to the positive
> timediffs. Besides improved readability in the dataset, this would
> enable me to create a histogram on timediff, showing the spread in
> timediff on both the positive and negative side.
>
> I need to work on the timediff as the basetime is different for
> different groups in the dataset, so I can't simply make a histogram on
> observedtime for all observations.
>
>
> *
> *   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/



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