Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Looping over datetimes for simultaneous variable generation |
Date | Thu, 3 Oct 2013 16:58:55 +0100 |
Sorry; I am quantising my attention given what else I am working on. qui forval j = 2/`=_N' { replace y = y[_n-1] if mi(x) & z[_n-1] < x[_n-1] in `j' replace z = z[_n-1] + y if mi(x) & z[_n-1] < x[_n-1] in `j' replace x = x[_n-1] if mi(x) & z[_n-1] < x[_n-1] Nick njcoxstata@gmail.com On 3 October 2013 16:47, Paul Balcombe <paul.balcombe@postgrad.manchester.ac.uk> wrote: > This doesn't work either because the command must estimate x, y and z at time point n before moving on to point n+1. > The code you suggest estimates y at all time points first, before estimating x and z. This doesn't work because y depends upon z(n-1) and x(n-1), as shown in the code. > So I need the computation to work out multiple variables (x,y,z) at each time point before moving on to the next time point, which is why I thought I needed a looping command such as forvalues. Does that make sense? > Thanks, > Paul > > _______________________________________ > From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Nick Cox [njcoxstata@gmail.com] > Sent: 03 October 2013 16:34 > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: Looping over datetimes for simultaneous variable generation > > Fair enough. So -replace- y first. > > replace y = y[_n-1] if mi(x) & z[_n-1] < x[_n-1] > replace z = z[_n-1] + y if mi(x) & z[_n-1] < x[_n-1] > replace x = x[_n-1] if mi(x) & z[_n-1] < x[_n-1] > > Nick > njcoxstata@gmail.com > > > On 3 October 2013 14:57, Paul Balcombe > <paul.balcombe@postgrad.manchester.ac.uk> wrote: >> But this won't work because when we replace z(n) we need y(n), which is dependent on knowing z(n-1). So, I must use a command that calculates y(n), x(n), z(n) and then y(n+1), x(n+1), z(n+1), rather than calculating all the values for x then y then z. >> >> Thanks, >> Paul >> >> ________________________________________ >> From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Nick Cox [njcoxstata@gmail.com] >> Sent: 03 October 2013 12:56 >> To: statalist@hsphsun2.harvard.edu >> Subject: Re: st: Looping over datetimes for simultaneous variable generation >> >> Might be as simple as >> >> replace z = z[_n-1] + y if mi(x) & z[_n-1] < x[_n-1] >> replace y = y[_n-1] if mi(x) & z[_n-1] < x[_n-1] >> replace x = x[_n-1] if mi(x) & z[_n-1] < x[_n-1] >> >> Nick >> njcoxstata@gmail.com >> >> >> On 3 October 2013 11:53, Paul Balcombe >> <paul.balcombe@postgrad.manchester.ac.uk> wrote: >>> Thankyou for the response. >>> >>> I'll try to explain what I want to calculate more clearly: >>> I have a dataset with observations of 4 variables every hour for one year. An example of the data is below. I now want to calculate 5 minutely values in between this hourly data. At each 5 minute interval I want to estimate w, x, y and z with the following rules (not in stata language): >>> >>> At time t, replace the following variables if mi(x) and if z(t-1) < x(t-1): >>> z(t) = z(t-1) + y(t) >>> y(t) = y(t-1) >>> x(t) = x(t-1) >>> >>> Where the if statements are false, do not generate new values for the variables. >>> >>> I need to calculate all variables at each time period, rather than estimate one variable at a time because they are dependent on each other at each time point. >>> >>> I hope this is slightly clearer. >>> Thanks, >>> Paul >>> >>> >>> datetime w x y z >>> 19/08/2012 00:00 0 0 0 0 >>> 19/08/2012 00:05 . . . . >>> 19/08/2012 00:10 . . . . >>> 19/08/2012 00:15 . . . . >>> 19/08/2012 00:20 . . . . >>> 19/08/2012 00:25 . . . . >>> 19/08/2012 00:30 . . . . >>> 19/08/2012 00:35 . . . . >>> 19/08/2012 00:40 . . . . >>> 19/08/2012 00:45 . . . . >>> 19/08/2012 00:50 . . . . >>> 19/08/2012 00:55 . . . . >>> 19/08/2012 01:00 3.4 0.2448 0.2833333 0.2833333 >>> 19/08/2012 01:05 . . . . >>> 19/08/2012 01:10 . . . . >>> 19/08/2012 01:15 . . . . >>> 19/08/2012 01:20 . . . . >>> 19/08/2012 01:25 . . . . >>> 19/08/2012 01:30 . . . . >>> 19/08/2012 01:35 . . . . >>> 19/08/2012 01:40 . . . . >>> 19/08/2012 01:45 . . . . >>> 19/08/2012 01:50 . . . . >>> 19/08/2012 01:55 . . . . >>> 19/08/2012 02:00 0 0 0 0 >>> >>> >>> >>> ________________________________________ >>> From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Nick Cox [njcoxstata@gmail.com] >>> Sent: 03 October 2013 10:48 >>> To: statalist@hsphsun2.harvard.edu >>> Subject: Re: st: Looping over datetimes for simultaneous variable generation >>> >>> There are several problems evident here, and I guess some latent too. >>> >>> You don't refer to any `datetime' macro within the loop, so at best it >>> would do the same things, regardless of the loop. >>> >>> The syntax ' ' means nothing to Stata. You may wish to evaluate >>> numeric constants on the fly, for which the syntax would be something >>> like `= tc(19aug2012 0:00:00)’ >>> >>> The constant tc(00:05:00) seems unlikely to be what you intend. >>> >>> For an arbitrary numlist you need -foreach- not -forvalues-. >>> >>> Those points aside, a better strategy might to explain what you want >>> to calculate, with enough about your data and some example data for >>> people to follow. >>> Nick >>> njcoxstata@gmail.com >>> >>> >>> On 3 October 2013 10:18, Paul Balcombe >>> <paul.balcombe@postgrad.manchester.ac.uk> wrote: >>>> I have a timeseries data set and I want to generate a number of variables that are conditional on each other and their previous values, so I want to create a value for the set of variables at each time period in my data. I thought I should use a forvalues command, but this is new to me and I have so far been unsuccessful: invalid syntax. below is the command I wish to make: >>>> >>>> forvalues datetime= ‘tc(19aug2012 00:00:00)’ (‘tc(00:05:00)’) ‘tc(18aug2013 23:00:00)’ { >>>> replace seen=seen[_n-1] if seen==. & seen3[_n-1]<seen[_n-1] >>>> replace segaspow=segaspow[_n-1] if seen==. & seen3[_n-1]<seen[_n-1] >>>> replace seen2=seen2[_n-1] if seen==. & seen3[_n-1]<seen[_n-1] >>>> replace seen3=seen2+seen3[_n-1] if seen==. & seen3[_n-1]<seen[_n-1] >>>> } >>>> >>>> I may be describing the datetime functions incorrectly. >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/