<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>time-series-2 &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/time-series-2/</link>
	<description>Feed of posts on WordPress.com tagged "time-series-2"</description>
	<pubDate>Thu, 20 Jun 2013 07:16:40 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Time series]]></title>
<link>http://markrblog.wordpress.com/2013/06/13/time-series/</link>
<pubDate>Thu, 13 Jun 2013 20:31:49 +0000</pubDate>
<dc:creator>firstmark</dc:creator>
<guid>http://markrblog.wordpress.com/2013/06/13/time-series/</guid>
<description><![CDATA[CRAN task view: time series package zoo Display time series, spatial and space-time data with R]]></description>
<content:encoded><![CDATA[<p><a href="http://cran.r-project.org/web/views/TimeSeries.html">CRAN task view: time series</a></p>
<p>package zoo</p>
<p><a href="http://oscarperpinan.github.io/spacetime-vis/">Display time series, spatial and space-time data with R</a></p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Forecasting in STATA: Tools and Tricks]]></title>
<link>http://statablog.wordpress.com/2012/02/28/forecasting-in-stata-tools-and-tricks/</link>
<pubDate>Mon, 27 Feb 2012 23:47:22 +0000</pubDate>
<dc:creator>Gichure</dc:creator>
<guid>http://statablog.wordpress.com/2012/02/28/forecasting-in-stata-tools-and-tricks/</guid>
<description><![CDATA[This is first of a series of tutorials that will show you how to deal with Dates, time, and periodic]]></description>
<content:encoded><![CDATA[<p><font size="4">This is first of a series of tutorials that will show you how to deal with Dates, time, and periodic data. In this series, we shall focus on time series in STATA.</font></p>
<p><font size="4">In previous posts, I have shown you how to rename, generate, and even recode variables.</font></p>
<p><font size="4">For time‐series analysis, dates and times are critical. You need to have one variable which records the time index.</font></p>
<p><font size="5"><u>Annual Data</u></font><br /><font size="4">For annual data it is convenient if the time index is the year number (e.g. 2010). Suppose your first<br />observation is the year 1947. You can generate the time index by the commands:<br /><font face="Courier New">generate t=1947+_n-1<br />tsset t, annual<br /></font>The variable “_n” is the natural index of the observation, starting at 1 and running to the number of<br />observations n. The <font face="Courier New">generate</font> command creates a variable “t” which adds 1947 to “_n”, and then<br />subtracts 1, so it is a series with entries “1947”, “1948”, “1949”, etc. The <font face="Courier New">tsset </font>command declares the variable “t” to be the time index. The option “annual” is not necessary, but tells STATA that the time index is measured at the annual frequency.</font></p>
<p><font size="5"><u>Quarterly Data</u></font><br /><font size="4">STATA stores the time index as an integer series. It uses the convention that the first quarter of 1996 is 0. The second quarter of 1996 is 1, the first quarter of 1999 is 4, etc. Dates before 1996 are negative integers, so that the fourth quarter of 1995 is ‐1, the third is ‐2, etc..</font><font size="4"><br />When formatted as a date, STATA displays quarterly time periods as “1957q2”, meaning the second<br />quarter of 1957. (Even though STATA stores the number “‐11”, the eleventh quarter before 1996q1.)<br />STATA uses the formula “tq(1957q2)” to translate the formatted date “1957q2” to the numerical index “‐11”.<br />Suppose that your first observation is the third quarter of 1947. You can generate a time index for the data set by the commands;</font></p>
<p><font size="4" face="Courier New">generate t=tq(1947q3)+_n-1<br />format t %tq<br />tsset t</font><br /><font size="4">The generate command creates a variable “t” with integer entries, normalized so that 0 occurs in<br />1060q1. The format command formats the variable “t” using the time‐series quarterly format. The “tq” refers to “time‐series quarterly”. The tsset command declares that the variable “t” is the time index.<br />You could have alternatively typed<br /><font face="Courier New">tsset t, quarterly</font><br />to tell STATA that it is a quarterly series, but it is not necessary as “t” has already been formatted as<br />quarterly. Now, when you look at the variable “t” you will see it displayed in year‐quarter format.<br /></font><u><font size="5">Monthly Data</font></u></p>
<p><font size="4">Monthly data is similar, but with “m” replacing “q”. STATA stores the time index with the convention<br />that 1960m1 is 0. To generate a monthly index starting in the second month of 1962, use the commands</font></p>
<p><font size="4" face="Courier New">generate t=tm(1962m2)+_n-1<br /></font><font size="4" face="Courier New">format t %tm<br />tsset t</font></p>
<p><font size="5"><u>Weekly Data</u></font><br /><font size="4">Weekly data is similar, with “w” instead of “q” and “m”, and the base period is 1960w1. For a series<br />starting in the 7th week of 1973, use the commands<br /><font face="Courier New">generate t=tw(1973w7)+_n-1<br />format t %tw<br />tsset t</font></font></p>
<p><font size="5"><u>Daily Data<br /></u></font><font size="4">Daily data is stored by dates. For example, “01jan1960” is Jan 1, 1960, which is the base period. To<br />generate a daily time index staring on April 18, 1962, use the commands<br /><font face="Courier New">generate t=td(18apr1962)+_n-1<br />format t %td</font></font></p>
<p><font size="4"><font face="Courier New">tsset t</font></font></p>
<p><font size="4">Next session, we shall do regression on time series data and I will show you how to reshape data from one form to another. </font></p>
<div class="wlWriterHeaderFooter" style="margin:0;padding:0;"></div>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Generating sets of permutations]]></title>
<link>http://ucfagls.wordpress.com/2011/10/21/228/</link>
<pubDate>Fri, 21 Oct 2011 10:06:50 +0000</pubDate>
<dc:creator>ucfagls</dc:creator>
<guid>http://ucfagls.wordpress.com/2011/10/21/228/</guid>
<description><![CDATA[Image via Wikipedia In previous posts&nbsp;I discussed how to generate a single permutation from a f]]></description>
<content:encoded><![CDATA[<div class="zemanta-img">
<div class="wp-caption alignleft" style="width: 310px"><a href="http://commons.wikipedia.org/wiki/File:Golden_Jackal_sa02.jpg"><img class="zemanta-img-configured" title="Golden Jackal" src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Golden_Jackal_sa02.jpg/300px-Golden_Jackal_sa02.jpg" alt="Golden Jackal" width="300" height="200" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>In <a title="permute: a package for generating restricted&#160;permutations" href="http://ucfagls.wordpress.com/2011/10/04/permute-a-package-for-generating-restricted-permutations/">previous</a> <a title="Generating restricted permutations with&#160;permute" href="http://ucfagls.wordpress.com/2011/10/18/generating-restricted-permutations-with-permute/">posts</a>&#160;I discussed how to generate a single permutation from a fully-randomised or restricted permutation design using <code>shuffle()</code>. Here I want to&#160;briefly&#160;mention the <code>shuffleSet()</code> function and illustrate it&#8217;s usage.<br />
<!--more--><br />
Every time you call <code>shuffle()</code> it has to interpret the <code>control</code> list to identify the type of permutation required. Whilst the overhead of this interpretation is not too high, there is no reason that it need be incurred just to generate a set of permutations. This is where <code>shuffleSet()</code> comes in. It works exactly like <code>shuffle()</code> taking the number of observations and a <code>control</code> object but in addition it takes an extra argument <code>nset</code> which is the number of permutations required for the set.</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; require(permute) ## load the package
&#62; args(shuffleSet)
function (n, nset = 1, control = permControl())
NULL
</pre>
<p>To generate 10 random permutations of ten observations you would use</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; set.seed(2)
&#62; shuffleSet(10, 10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    2    7    5   10    6    8    1    3    4     9
 [2,]    6    3    7    2    9    5    4    1   10     8
 [3,]    7    4   10    2    3    6    1    8    5     9
 [4,]    1    2    7    8    4    6    5   10    9     3
 [5,]   10    3    1    2    6    4    5    7    9     8
 [6,]    1   10    6    7    2    5    4    3    8     9
 [7,]    8   10    6    2    9    3    7    4    1     5
 [8,]    3   10    1    2    7    4    6    9    8     5
 [9,]    4    7    1    3    2    5   10    8    6     9
[10,]   10    4    9    8    3    1    2    5    6     7
</pre>
<p>If those 10 observations were collected as a time series and we wanted 10 restricted permutations you would use</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; set.seed(2)
&#62; shuffleSet(10, 10, control = permControl(within = Within(type = &#34;series&#34;)))
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    3    4    5    6    7    8    9   10    1     2
 [2,]    9   10    1    2    3    4    5    6    7     8
 [3,]    7    8    9   10    1    2    3    4    5     6
 [4,]    3    4    5    6    7    8    9   10    1     2
 [5,]    1    2    3    4    5    6    7    8    9    10
 [6,]    1    2    3    4    5    6    7    8    9    10
 [7,]    3    4    5    6    7    8    9   10    1     2
 [8,]   10    1    2    3    4    5    6    7    8     9
 [9,]    6    7    8    9   10    1    2    3    4     5
[10,]    7    8    9   10    1    2    3    4    5     6
</pre>
<p>From the above set of permutations, the cyclic shifts employed in the <code>"series"</code> permutation type are clear. One problem with the set we just produced is that the same permutation was returned more than once. In fact, there were only six unique permutations in the set requested. This is due to there only being 10 possible permutations of the numbers 1, 2, &#8230;, 10 if we allow cyclic shifts in a single direction</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; numPerms(10, control = permControl(within = Within(type = &#34;series&#34;)))
[1] 10
</pre>
<p><code>shuffle()</code> and <code>shuffleSet()</code> know nothing of these limits, but there are functions in the <strong>permute</strong> package that can tell you the number of possible permutations (<code>numPerms()</code>) and generate the entire set of permutations for a stated design (<code>allPerms()</code>). I&#8217;ll take a look at <code>allPerms()</code> in a future posting.</p>
<p>I return now to the Golden Jackal mandible length example I used in an <a title="permute: a package for generating restricted&#160;permutations" href="http://ucfagls.wordpress.com/2011/10/04/permute-a-package-for-generating-restricted-permutations/">earlier post</a> but update the example to make use of <code>shuffleSet()</code> instead of <code>shuffle()</code>. I will just show the code and output for the permutation test, refer to the <a title="permute: a package for generating restricted&#160;permutations" href="http://ucfagls.wordpress.com/2011/10/04/permute-a-package-for-generating-restricted-permutations/">previous post</a> for details:</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; data(jackal) ## load the data
&#62; ## function to compute the difference of means
&#62; meanDif &#60;- function(x, grp) {
+     mean(x[grp == &#34;Male&#34;]) - mean(x[grp == &#34;Female&#34;])
+ }
&#62; N &#60;- nrow(jackal)
&#62; ## make this reproducible and like previous post
&#62; set.seed(42)
&#62; ## generate the set of 4999 random permutations
&#62; pSet  &#60;- shuffleSet(N, 4999)
&#62; ## iterate over the set
&#62; Djackal &#60;- apply(pSet, 1, function(i, data) with(data, meanDif(Length, Sex[i])),
+                  data = jackal)
&#62; ## add the observed difference of mean mandible length
&#62; Djackal &#60;- c(Djackal, with(jackal, meanDif(Length, Sex)))
## No. obs equal to or greater than observed
&#62; (Dbig &#60;- sum(Djackal &#62;= Djackal[5000]))
[1] 12
&#62; ## permutational p-value
&#62; Dbig/length(Djackal)
[1] 0.0024
</pre>
<p>The last two lines of R code compute the number of observations in the Null distribution with differences in mean mandible length as great or greater than the observed difference, and the resulting permutation <em>p</em>-value. These are the same as those computed in the previous post.</p>
<p>Generating entire sets of permutations is useful for several reasons. One recent example that we came across is with the new parallel processing capabilities in the forthcoming version of R. We are able to generate a set of permutations and then distribute the process of the permutation test over a number of CPUs or worker threads, each dealing with a subset of the permutations we generated. This can greatly reduce the compute time needed for the permutation test, especially where the objective function is computationally complex, but allows us to not worry about controlling the random number generator in each separate process — this is all done within the main function and only the relevant subset of permutations is passed to each worker process. An additional reason for generating a set of permutation to work with rather than individual permutations is that it is easy to switch between using a set of randomly generated permutations or the set of all possible permutations where that set is not overly large. <code>allPerms()</code> returns the set of permutations in the same way that <code>shuffleSet()</code> does, so we can simplify our code if we write the test to iterate over a set of permutations.</p>
<p>The full script for the Golden Jackal permutation test is shown below:</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
data(jackal) ## load the data
## function to compute the difference of means
meanDif &#60;- function(x, grp) {
    mean(x[grp == &#34;Male&#34;]) - mean(x[grp == &#34;Female&#34;])
}
N &#60;- nrow(jackal)
## make this reproducible and like previous post
set.seed(42)
## generate the set of 4999 random permutations
pSet  &#60;- shuffleSet(N, 4999)
## iterate over the set
Djackal &#60;- apply(pSet, 1, function(i, data) with(data, meanDif(Length, Sex[i])),
                 data = jackal)
## add the observed difference of mean mandible length
Djackal &#60;- c(Djackal, with(jackal, meanDif(Length, Sex)))
## No. obs equal to or greater than observed
(Dbig &#60;- sum(Djackal &#62;= Djackal[5000]))
## permutational p-value
Dbig/length(Djackal)
</pre>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Generating restricted permutations with permute]]></title>
<link>http://ucfagls.wordpress.com/2011/10/18/generating-restricted-permutations-with-permute/</link>
<pubDate>Tue, 18 Oct 2011 21:24:43 +0000</pubDate>
<dc:creator>ucfagls</dc:creator>
<guid>http://ucfagls.wordpress.com/2011/10/18/generating-restricted-permutations-with-permute/</guid>
<description><![CDATA[In a previous post I introduced the permute package and the function shuffle(). In that post I got a]]></description>
<content:encoded><![CDATA[<p>In a <a href="http://ucfagls.wordpress.com/2011/10/04/permute-a-package-for-generating-restricted-permutations/" title="permute: a package for generating restricted&#160;permutations">previous post</a> I introduced the <strong>permute</strong> package and the function <code>shuffle()</code>. In that post I got as far as replicating R&#8217;s base function <code>sample()</code>. Here I&#8217;ll briefly outline how <code>shuffle()</code> can be used to generate restricted permutations.</p>
<p><!--more--></p>
<p><code>shuffle()</code> has two arguments: i) <code>n</code>, the number of observations in the data set to be permuted, and ii) <code>control</code>, a list that defines the permutation design describing how the samples are permuted.</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
R&#62; args(shuffle)
function (n, control = permControl())
NULL
</pre>
<p><code>control</code> is a list, and for complex permutation designs. As a result, several convenience functions are provided that make it easier to specify the design you want. The main convenience function is <code>permControl()</code> which if passed no arguments populates an appropriate <code>control</code> object with defaults that result in free permutation of observations.</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; str(permControl())
List of 10
 $ strata     : NULL
 $ nperm      : num 199
 $ complete   : logi FALSE
 $ within     :List of 5
  ..$ type    : chr &#34;free&#34;
  ..$ constant: logi FALSE
  ..$ mirror  : logi FALSE
  ..$ ncol    : NULL
  ..$ nrow    : NULL
 $ blocks     :List of 4
  ..$ type  : chr &#34;none&#34;
  ..$ mirror: logi FALSE
  ..$ ncol  : NULL
  ..$ nrow  : NULL
 $ maxperm    : num 9999
 $ minperm    : num 99
 $ all.perms  : NULL
 $ observed   : logi FALSE
 $ name.strata: chr &#34;NULL&#34;
 - attr(*, &#34;class&#34;)= chr &#34;permControl&#34;
</pre>
<p>Several types of permutation can be produced by functions in <strong>permute</strong>:</p>
<ul>
<li>Free permutation of objects, which we saw in the earlier post</li>
<li>Time series or line transect designs, where the temporal or spatial ordering is preserved</li>
<li>Spatial grid designs, where the spatial ordering is preserved in both coordinate directions</li>
<li>Permutation of blocks or groups of samples</li>
</ul>
<p>The first three of these can be nested within the levels of a factor or to the levels of that factor, or to both. Such flexibility allows the analysis of split-plot designs using permutation tests. <code>permControl()</code> is used to set up the design from which <code>shuffle()</code> will draw a permutation. <code>permControl()</code> has two main arguments that specify how samples are permuted within blocks of samples or at the block level itself. These are within and blocks. Two convenience functions, <code>Within()</code> and <code>Blocks()</code> can be used to set the various options for permutation. For example, to permute the observations 1:10 assuming a time series design for the entire set of observations, the following control object would be used</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; set.seed(4)
&#62; x &#60;- 1:10
&#62; CTRL &#60;- permControl(within = Within(type = &#34;series&#34;))
&#62; perm &#60;- shuffle(10, control = CTRL)
&#62; perm
 [1]  7  8  9 10  1  2  3  4  5  6
&#62; x[perm]
 [1]  7  8  9 10  1  2  3  4  5  6
</pre>
<p>It is assumed that the observations are in temporal or transect order. We only specified the type of permutation within blocks, the remaining options are set to their defaults via <code>Within()</code>.</p>
<p>A more complex design, with three blocks, and a 3 by 3 spatial grid arrangement within each block can be created as follows</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; set.seed(4)
&#62; block &#60;- gl(3, 9)
&#62; CTRL &#60;- permControl(strata = block,
+                     within = Within(type = &#34;grid&#34;, ncol = 3, nrow = 3))
&#62; perm &#60;- shuffle(length(block), control = CTRL)
&#62; perm
 [1]  6  4  5  9  7  8  3  1  2 14 15 13 17 18 16 11 12 10 22 23
[21] 24 25 26 27 19 20 21
</pre>
<p>Visualising the permutation as the 3 matrices may help illustrate how the data have been shuffled</p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; ## Original
&#62; lapply(split(1:27, block), matrix, ncol = 3)
$`1`
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$`2`
     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

$`3`
     [,1] [,2] [,3]
[1,]   19   22   25
[2,]   20   23   26
[3,]   21   24   27

&#62; ## Shuffled
&#62; lapply(split(perm, block), matrix, ncol = 3)
$`1`
     [,1] [,2] [,3]
[1,]    6    9    3
[2,]    4    7    1
[3,]    5    8    2

$`2`
     [,1] [,2] [,3]
[1,]   14   17   11
[2,]   15   18   12
[3,]   13   16   10

$`3`
     [,1] [,2] [,3]
[1,]   22   25   19
[2,]   23   26   20
[3,]   24   27   21
</pre>
<p>In the first grid, the lower-left corner of the grid was set to row 2 and column 2 of the original, to row 1 and column 2 in the second grid, and to row 3 column 2 in the third grid.</p>
<p>To have the same permutation within each level of block, use the constant argument of the <code>Within()</code> function, setting it to <code>TRUE</code></p>
<pre class="brush: r; light: true; title: ; notranslate" title="">
&#62; set.seed(4)
&#62; CTRL &#60;- permControl(strata = block,
+                     within = Within(type = &#34;grid&#34;, ncol = 3, nrow = 3,
+                                     constant = TRUE))
&#62; perm2 &#60;- shuffle(length(block), control = CTRL)
&#62; lapply(split(perm2, block), matrix, ncol = 3)
$`1`
     [,1] [,2] [,3]
[1,]    6    9    3
[2,]    4    7    1
[3,]    5    8    2

$`2`
     [,1] [,2] [,3]
[1,]   15   18   12
[2,]   13   16   10
[3,]   14   17   11

$`3`
     [,1] [,2] [,3]
[1,]   24   27   21
[2,]   22   25   19
[3,]   23   26   20
</pre>
<p>As you can see, at the moment, I make some assumptions about the ordering of samples within each spatial/temporal structure. The samples do not have the be arranged in <code>strata</code> order, but within the levels of the grouping variable the observations must be in the right order. For spatial grids, this means in column-major order&#8212;just as in the way R fills matrices by columns. In a future release, I hope to relax some of these assumptions to make it easier to apply permutations to the data to hand.</p>
<p>In the next post in this series, I&#8217;ll take a look at generating sets of permutations using the <code>shuffleSet()</code> function.</p>
]]></content:encoded>
</item>

</channel>
</rss>
