Skip to content

Commit

Permalink
build the pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
eeholmes committed Jul 29, 2021
1 parent 9b4d807 commit 5e8213d
Show file tree
Hide file tree
Showing 64 changed files with 844 additions and 12,002 deletions.
10,935 changes: 0 additions & 10,935 deletions Applied_Time_Series_Analysis.Rmd

This file was deleted.

2 changes: 1 addition & 1 deletion Lab-fitting-multi-ss-models/multivariate-ss-ECN-NO2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ mod.list.corr = list(B=B, Q=Q, R=R, U=U, x0=x0, A=A, tinitx=0)

Now we can fit a MARSS model and get estimates of the missing SWEs. Convergence is slow. We set $\mathbf{a}$ equal to the mean of the time series to speed convergence.

```{r mssmiss-snotelfit-corr, results="hide"}
```{r mssmiss-snotelfit-corr, results="hide", cache = TRUE}
m <- apply(dat.feb, 1, mean, na.rm=TRUE)
fit.corr <- MARSS(dat.feb, model=mod.list.corr, control=list(maxit=5000),
inits=list(A=matrix(m,ns,1)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ mod.list.corr = list(B=B, Q=Q, R=R, U=U, x0=x0, A=A, tinitx=0)

Now we can fit a MARSS model and get estimates of the missing SWEs. Convergence is slow. We set $\mathbf{a}$ equal to the mean of the time series to speed convergence.

```{r mssmiss-snotelfit-corr, results="hide"}
```{r mssmiss-snotelfit-corr-missing, results="hide", cache = TRUE}
m <- apply(dat.feb, 1, mean, na.rm=TRUE)
fit.corr <- MARSS(dat.feb, model=mod.list.corr, control=list(maxit=5000),
inits=list(A=matrix(m,ns,1)))
Expand Down
4 changes: 1 addition & 3 deletions Lab-intro-to-jags/intro-to-jags.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@ y_t = x_t + v_t, \, v_t \sim \N(0, r)
Because $x$ is a random walk model not a stationary AR(1), we will place a vague weakly informative prior on $x_0$: $x_0 \sim \N(y_1, 1000)$. We had to pass in `Y1` as data because JAGS would complain if we used `Y[1]` in our prior (because have `X0` in our model for $Y[1]$). `EY` is added so that we can track the model fits for $y$. In this case it is just `X` but in more complex models it will involve more parameters.

```{r jags-ss1, echo=TRUE, results='hide', cache=TRUE}
# 5. MAKE THE SS MODEL for a stochastic level model
model.loc <- ("ss_model.txt")
jagsscript <- cat("
model {
Expand Down Expand Up @@ -836,7 +834,7 @@ removefiles <- c(removefiles, model.loc)
There are a number of different approaches to using Bayesian time series models to perform forecasting. One approach might be to fit a model, and use those posterior distributions to forecast as a secondary step (say within R). A more streamlined approach is to do this within the JAGS code itself. We can take advantage of the fact that JAGS allows you to include NAs in the response variable (but never in the predictors). Let's use the same Wind dataset, and the univariate state-space model described above to forecast three time steps into the future. We can do this by including 3 more NAs in the dataset, and incrementing the variable ```N``` by 3.

```{r jags-cov-forecast, results='hide', cache=TRUE}
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3))
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3), Y1 = Wind[1])
jags.params <- c("q", "r", "EY", "u")
model.loc <- ("ss_model.txt")
mod_ss_forecast <- jags(jags.data,
Expand Down
15 changes: 0 additions & 15 deletions ar1_intercept.txt

This file was deleted.

4 changes: 1 addition & 3 deletions cleanedRmd/intro-to-jags.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@ y_t = x_t + v_t, \, v_t \sim \,\text{N}(0, r)
Because $x$ is a random walk model not a stationary AR(1), we will place a vague weakly informative prior on $x_0$: $x_0 \sim \,\text{N}(y_1, 1000)$. We had to pass in `Y1` as data because JAGS would complain if we used `Y[1]` in our prior (because have `X0` in our model for $Y[1]$). `EY` is added so that we can track the model fits for $y$. In this case it is just `X` but in more complex models it will involve more parameters.

```{r jags-ss1, echo=TRUE, results='hide', cache=TRUE}
# 5. MAKE THE SS MODEL for a stochastic level model
model.loc <- ("ss_model.txt")
jagsscript <- cat("
model {
Expand Down Expand Up @@ -836,7 +834,7 @@ removefiles <- c(removefiles, model.loc)
There are a number of different approaches to using Bayesian time series models to perform forecasting. One approach might be to fit a model, and use those posterior distributions to forecast as a secondary step (say within R). A more streamlined approach is to do this within the JAGS code itself. We can take advantage of the fact that JAGS allows you to include NAs in the response variable (but never in the predictors). Let's use the same Wind dataset, and the univariate state-space model described above to forecast three time steps into the future. We can do this by including 3 more NAs in the dataset, and incrementing the variable ```N``` by 3.

```{r jags-cov-forecast, results='hide', cache=TRUE}
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3))
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3), Y1 = Wind[1])
jags.params <- c("q", "r", "EY", "u")
model.loc <- ("ss_model.txt")
mod_ss_forecast <- jags(jags.data,
Expand Down
Binary file modified docs/Applied_Time_Series_Analysis.pdf
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions docs/Rcode/intro-to-jags.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ mod_ar1_intercept <- R2jags::jags(jags.data,


## ----jags-ss1, echo=TRUE, results='hide', cache=TRUE-------------------------------------------
# 5. MAKE THE SS MODEL for a stochastic level model

model.loc <- ("ss_model.txt")
jagsscript <- cat("
model {
Expand Down Expand Up @@ -517,7 +515,7 @@ mod_ss <- jags(jags.data,


## ----jags-cov-forecast, results='hide', cache=TRUE---------------------------------------------
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3))
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3), Y1 = Wind[1])
jags.params <- c("q", "r", "EY", "u")
model.loc <- ("ss_model.txt")
mod_ss_forecast <- jags(jags.data,
Expand Down
4 changes: 1 addition & 3 deletions docs/Rmds/intro-to-jags.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@ y_t = x_t + v_t, \, v_t \sim \,\text{N}(0, r)
Because $x$ is a random walk model not a stationary AR(1), we will place a vague weakly informative prior on $x_0$: $x_0 \sim \,\text{N}(y_1, 1000)$. We had to pass in `Y1` as data because JAGS would complain if we used `Y[1]` in our prior (because have `X0` in our model for $Y[1]$). `EY` is added so that we can track the model fits for $y$. In this case it is just `X` but in more complex models it will involve more parameters.

```{r jags-ss1, echo=TRUE, results='hide', cache=TRUE}
# 5. MAKE THE SS MODEL for a stochastic level model
model.loc <- ("ss_model.txt")
jagsscript <- cat("
model {
Expand Down Expand Up @@ -836,7 +834,7 @@ removefiles <- c(removefiles, model.loc)
There are a number of different approaches to using Bayesian time series models to perform forecasting. One approach might be to fit a model, and use those posterior distributions to forecast as a secondary step (say within R). A more streamlined approach is to do this within the JAGS code itself. We can take advantage of the fact that JAGS allows you to include NAs in the response variable (but never in the predictors). Let's use the same Wind dataset, and the univariate state-space model described above to forecast three time steps into the future. We can do this by including 3 more NAs in the dataset, and incrementing the variable ```N``` by 3.

```{r jags-cov-forecast, results='hide', cache=TRUE}
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3))
jags.data <- list("Y" = c(Wind, NA, NA, NA), "N" = (N + 3), Y1 = Wind[1])
jags.params <- c("q", "r", "EY", "u")
model.loc <- ("ss_model.txt")
mod_ss_forecast <- jags(jags.data,
Expand Down
1 change: 0 additions & 1 deletion docs/analysis-goal.html
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ <h2><span class="header-section-number">16.2</span> Analysis goal</h2>
<li>The amplitude of the cycle changes over time.</li>
</ol>
<p>Both of these will cause us problems when we try to estimate a stochastic level.</p>
<pre><code>Warning: Removed 3 row(s) containing missing values (geom_path).</code></pre>
<p><img src="Applied_Time_Series_Analysis_files/figure-html/unnamed-chunk-59-1.png" width="80%" style="display: block; margin: auto;" /></p>
<p>ACF of the spawners showing the 5-year cycle in most of the rivers.
<img src="Applied_Time_Series_Analysis_files/figure-html/unnamed-chunk-60-1.png" width="80%" style="display: block; margin: auto;" /></p>
Expand Down
6 changes: 3 additions & 3 deletions docs/chap-cyclic-sockeye.html
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ <h1><span class="header-section-number">Chapter 16</span> Modeling cyclic sockey
<p>A script with all the R code in the chapter can be downloaded <a href="./Rcode/cyclic-sockeye.R">here</a>. The Rmd for this chapter can be downloaded <a href="./Rmds/cyclic-sockeye.Rmd">here</a></p>
<div id="data-and-packages-10" class="section level3 unnumbered">
<h3>Data and packages</h3>
<div class="sourceCode" id="cb732"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb732-1"><a href="chap-cyclic-sockeye.html#cb732-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(atsalibrary)</span>
<span id="cb732-2"><a href="chap-cyclic-sockeye.html#cb732-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb732-3"><a href="chap-cyclic-sockeye.html#cb732-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(MARSS)</span></code></pre></div>
<div class="sourceCode" id="cb720"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb720-1"><a href="chap-cyclic-sockeye.html#cb720-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(atsalibrary)</span>
<span id="cb720-2"><a href="chap-cyclic-sockeye.html#cb720-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb720-3"><a href="chap-cyclic-sockeye.html#cb720-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(MARSS)</span></code></pre></div>
</div>
</div>
</section>
Expand Down
28 changes: 14 additions & 14 deletions docs/chap-stan.html
Original file line number Diff line number Diff line change
Expand Up @@ -625,23 +625,23 @@ <h1><span class="header-section-number">Chapter 13</span> Stan for Bayesian time
<div id="data-and-packages-9" class="section level3 unnumbered">
<h3>Data and packages</h3>
<p>You will need the <strong>atsar</strong> and <strong>bayesdfa</strong> packages we have written for fitting state-space time series models with Stan. Install using the <strong>devtools</strong> package.</p>
<div class="sourceCode" id="cb644"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb644-1"><a href="chap-stan.html#cb644-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(devtools)</span>
<span id="cb644-2"><a href="chap-stan.html#cb644-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Windows users will likely need to set this</span></span>
<span id="cb644-3"><a href="chap-stan.html#cb644-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Sys.setenv(&#39;R_REMOTES_NO_ERRORS_FROM_WARNINGS&#39; = &#39;true&#39;)</span></span>
<span id="cb644-4"><a href="chap-stan.html#cb644-4" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;nwfsc-timeseries/atsar&quot;</span>)</span>
<span id="cb644-5"><a href="chap-stan.html#cb644-5" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;nwfsc-timeseries/tvvarss&quot;</span>)</span>
<span id="cb644-6"><a href="chap-stan.html#cb644-6" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;fate-ewi/bayesdfa&quot;</span>)</span></code></pre></div>
<div class="sourceCode" id="cb640"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb640-1"><a href="chap-stan.html#cb640-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(devtools)</span>
<span id="cb640-2"><a href="chap-stan.html#cb640-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Windows users will likely need to set this</span></span>
<span id="cb640-3"><a href="chap-stan.html#cb640-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Sys.setenv(&#39;R_REMOTES_NO_ERRORS_FROM_WARNINGS&#39; = &#39;true&#39;)</span></span>
<span id="cb640-4"><a href="chap-stan.html#cb640-4" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;nwfsc-timeseries/atsar&quot;</span>)</span>
<span id="cb640-5"><a href="chap-stan.html#cb640-5" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;nwfsc-timeseries/tvvarss&quot;</span>)</span>
<span id="cb640-6"><a href="chap-stan.html#cb640-6" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;fate-ewi/bayesdfa&quot;</span>)</span></code></pre></div>
<p>In addition, you will need the <strong>rstan</strong>, <strong>datasets</strong>, <strong>parallel</strong> and <strong>loo</strong> packages. After installing, if needed, load the packages:</p>
<div class="sourceCode" id="cb645"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb645-1"><a href="chap-stan.html#cb645-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(atsar)</span>
<span id="cb645-2"><a href="chap-stan.html#cb645-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rstan)</span>
<span id="cb645-3"><a href="chap-stan.html#cb645-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(loo)</span></code></pre></div>
<div class="sourceCode" id="cb641"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb641-1"><a href="chap-stan.html#cb641-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(atsar)</span>
<span id="cb641-2"><a href="chap-stan.html#cb641-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rstan)</span>
<span id="cb641-3"><a href="chap-stan.html#cb641-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(loo)</span></code></pre></div>
<p>Once you have Stan and <strong>rstan</strong> installed, optimize Stan on your machine:</p>
<div class="sourceCode" id="cb646"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb646-1"><a href="chap-stan.html#cb646-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rstan_options</span>(<span class="at">auto_write =</span> <span class="cn">TRUE</span>)</span>
<span id="cb646-2"><a href="chap-stan.html#cb646-2" aria-hidden="true" tabindex="-1"></a><span class="fu">options</span>(<span class="at">mc.cores =</span> parallel<span class="sc">::</span><span class="fu">detectCores</span>())</span></code></pre></div>
<div class="sourceCode" id="cb642"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb642-1"><a href="chap-stan.html#cb642-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rstan_options</span>(<span class="at">auto_write =</span> <span class="cn">TRUE</span>)</span>
<span id="cb642-2"><a href="chap-stan.html#cb642-2" aria-hidden="true" tabindex="-1"></a><span class="fu">options</span>(<span class="at">mc.cores =</span> parallel<span class="sc">::</span><span class="fu">detectCores</span>())</span></code></pre></div>
<p>For this lab, we will use a data set on air quality in New York from the <strong>datasets</strong> package. Load the data and create a couple new variables for future use.</p>
<div class="sourceCode" id="cb647"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb647-1"><a href="chap-stan.html#cb647-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(airquality, <span class="at">package =</span> <span class="st">&quot;datasets&quot;</span>)</span>
<span id="cb647-2"><a href="chap-stan.html#cb647-2" aria-hidden="true" tabindex="-1"></a>Wind <span class="ot">&lt;-</span> airquality<span class="sc">$</span>Wind <span class="co"># wind speed</span></span>
<span id="cb647-3"><a href="chap-stan.html#cb647-3" aria-hidden="true" tabindex="-1"></a>Temp <span class="ot">&lt;-</span> airquality<span class="sc">$</span>Temp <span class="co"># air temperature</span></span></code></pre></div>
<div class="sourceCode" id="cb643"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb643-1"><a href="chap-stan.html#cb643-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(airquality, <span class="at">package =</span> <span class="st">&quot;datasets&quot;</span>)</span>
<span id="cb643-2"><a href="chap-stan.html#cb643-2" aria-hidden="true" tabindex="-1"></a>Wind <span class="ot">&lt;-</span> airquality<span class="sc">$</span>Wind <span class="co"># wind speed</span></span>
<span id="cb643-3"><a href="chap-stan.html#cb643-3" aria-hidden="true" tabindex="-1"></a>Temp <span class="ot">&lt;-</span> airquality<span class="sc">$</span>Temp <span class="co"># air temperature</span></span></code></pre></div>
</div>
</div>
</section>
Expand Down
Loading

0 comments on commit 5e8213d

Please sign in to comment.