Computer Labs

Computer Labs Software

You'll be using R for the course and we encourage you to use RStudio as your R interface, since that is what your instructors will be using. Download R  Download RStudio You'll be able to download all the packages that you'll need in class.

STAN: We will use STAN in some of our labs to fit time series models in a Bayesian framework (info here:

Install the rstan package from within R using this command: 

> install.packages("rstan")

or use the package installer in RStudio. After installing the rstan package, type

> library(rstan)
> ?rstan

and run the example code to make sure everything works. You can more examples in the Stan vignette:

JAGS: Install JAGS to your laptop from JAGS Sourceforge. Then install the R packages coda and R2jags.

statss: This is an R wrapper for fitting Bayesian models with Stan. Install from the Github site here

MARSS Reference Sheet

Quick reference sheet for the MARSS model specification
Adobe Acrobat file icon MARSS_Reference_Sheet.pdf124K Download View

Prerequisite - Matrices in R & matrix algebra

The material later in this course relies heavily on writing time series models in matrix notation, and working with matrices in R. Please look over this primer and make sure that you are comfortable with all of the material.
Adobe Acrobat file icon basic-matrix-math.pdf176K Download View
Adobe Acrobat file icon basic-matrix-math-key.pdf62K Download View Solutions to the HW problems in the chapter. You won't turn this in. But make sure you can do the problems as you will need to know how to manipulate matrices in R for this course. Give the problems a try before looking at the solutions.

Week 1, Writing linear regression models in matrix form

Your homework this week is on writing regression models in matrix form. We won't spend much time on this during the computer lab. You'll work through this mainly on your own for homework #1 (due Tues Jan 10).

1) Get used to matrices. This will be a little painful if you have not used matrices much, but plug away at the homework and you'll get used to it by the end. You'll be learning how to develop ORIGINAL multivariate models in this class and to do that, you need to change the math (the matrices) instead of using someone else's "black boxes".

2) Work through some examples of writing and estimating linear regression models in matrix form.
R script file icon linear-regression-models-matrix.R11K Download All the R code for the linear regression lab
Adobe Acrobat file icon Lab 1 Linear Regression in Matrix Form.pdf255K Download View The HW questions require you to write out matrix equations. Look at problem 6 in the RMarkdown homework template to see how to write out matrices in LaTeX (which RMarkdown understands). The homework for week 1 is problems 1-6 at the end of the chapter.
Adobe Acrobat file icon Homework-Key-Linear-Regression.pdf206K Download View

Week 1 & 2, Time series functions in R

We will be working on introductory time series functions in R. This will cover material from lectures 1, 2 and 3. Homework #2 (due Jan 17th) is based on materials in this lab. Our goals are to:

1) learn basic R functions for time series and learn how to do some basic diagnostics, filtering, and decomposition; and
Adobe Acrobat file icon Intro to ts functions in R.pdf505K Download View
R script file icon Intro to ts functions in R.R31K Download

Week 3

This week you will practice fitting univariate state-space models and computing some model selection metrics.
Adobe Acrobat file icon fitting-univariate-state-space.pdf304K Download View The homework is based on this file. Do homework #s 1.1, 1.5, 1.6, and 1.8. If you get stuck, ask. The questions walk you through each step and none require more than 1-3 lines of R code. The other homework questions will help you understand the material, so we encourage you to look at them. We'll post a key with answers to all.
R script file icon Fitting univariate state-space models..GS.r4K Download This script shows you how to fit univariate state-space models with JAGS and R. See the STAN write-up for how to do this with STAN.
Adobe Acrobat file icon fitting-univariate-ss-key.pdf260K Download View

Week 4

This week you will practice fitting multivariate state-space models with and without covariates. On Tuesday, Eli will walk through the lab on fitting MARSS models with covariates. You'll need this for next week's homework. On Thursday's lab, we'll work through some more complex examples of fitting multivariate state-space models without covariates.

Starting this week, your homework will be more focused on analyzing datasets and the questions are more open ended. These are some common mistakes that students have made in past years:
* In most cases, you need to de-mean your data, both response variable and the covariates. If you subset your data, you change the mean so you need to de-mean again.
* If you square a covariate c with a mean of 0 (meaning c^2), the mean of c^2 is not zero. So if you use c^2 as your covariate, you need to de-mean that separately.
* You cannot compare AICs across models with different data. So if you subset the data, you cannot compare AICc with a data subset to a model with the full data or different data subset.
* A slowly decaying acf is exactly what you expect for AR-1. A pacf with only a peak at 1 is what you expect for AR-1.
* try method="BFGS" in your MARSS call. That is much faster than the default EM-algorithm used by MARSS
Adobe Acrobat file icon Homework-4.pdf73K Download View For the homework, do problems 1-5. Optionally, try problems 6 & 7.
Adobe Acrobat file icon Fitting MARSS models.pdf262K Download View Introduction to MARSS models without covariates.
Adobe Acrobat file icon Fitting MARSS models with covariates.pdf253K Download View Introduction to MARSS models with covariates. This is the material that you will need for homework #4.
R script file icon Fitting MARSS models with covariates-..nt.R10K Download just the R code for the above
Adobe Acrobat file icon multivariate-ss-with-cov-hw-key.pdf245K Download View

Week 5

This week we will learn how to fit Dynamic Linear Models (DLMs) in R with MARSS. The homework questions for this week are at the end of the vignette. You will need the Rdata file below to complete the HW.
Adobe Acrobat file icon Fitting DLMs with MARSS358K Download View
R script file icon Fitting DLMs with MARSS.R7K Download
Compressed gzip archive icon KvichakSockeye.RData1K Download

Week 6

This week we will see how to fit models for dynamic factor analysis (DFA).
HTML file icon Intro to DFA.html3.4M Download View source
Unknown file type icon Intro_to_DFA.Rmd25K Download
R script file icon Intro_to_DFA.R9K Download

Week 7 Fitting Bayesian models with STAN

This week you'll learn how to fit times series models in a Bayesian framework with R & STAN (or JAGS).

No HW this week.
Adobe Acrobat file icon Lab 7 - Fitting models with JAGS.pdf394K Download View This is for mainly for reference if you are more familiar with JAGS. Note using JAGS requires install JAGS. Use STAN above if you have never used JAGS. This is included who want to compare STAN and JAGS code.
Adobe Acrobat file icon Lab 7 - Fitting models with STAN.pdf285K Download View STAN version of the above.
Unknown file type icon Lab 7 - More fitting models with STAN.Rmd13K Download

Weeks 8-10 (no lab)

Discussion of individual projects & student presentations.


No announcements

Send questions about this workspace to Eli Holmes.