Introduction to Autoregressive models

This tutorial presents introduction of autoregressive models, and theoir implementation in R.

1  AR(1) model

1.1  Model definition


where ({a_t},) is a white noise series of mean zero and variance (sigma_a^2).


  • AR(1) model is widely used not only for returns, as shown with (r_t,) here, but also for volatility with (r_t,) replaced with (sigma_t).
  • Conditional on past return (r_{t-1}), we have conditional mean and variance as following[begin{gather*}mathbb{E}[r_t|r_{t-1}]=phi_0+phi_1r_{t-1}\text{Var}[r_t|r_{t-1}]=sigma_a^2end{gather*}]This is a Markov property in that, conditional on (r_{t-1}), the return (r_t,) is not correlated with (r_{t-i},) for i > 1.

Read more Introduction to Autoregressive models

Stochastic Calculus Notes 1: General Probability Theory

This note of stochastic calculus covers general probability theory.

1  Infinite probability spaces

1.1  Definition: σ-algebra

Let \Omega\, be a non-empty set, and \mathcal{F}\, be a collection of subsets of \Omega. We say \mathcal{F}\,  is a \sigma-algebra or \sigma-field if:

  1. The empty set \emptyset\in\mathcal{F}
  2. For any set A\in\mathcal{F}\,\Longrightarrow\,A^c\in\mathcal{F}
  3. Whenever a sequence of sets A_1,A_2,\cdots\in\mathcal{F}\,\Longrightarrow\,\bigcup_{n=1}^\infty A_n\in\mathcal{F}.


  • If we have a \sigma-algebra of sets, then all operations of the sets will give other sets that are still in the \sigma-algebra.
  • The whole space \Omega\in\mathcal{F}\, since \Omega=\emptyset^c.

Read more Stochastic Calculus Notes 1: General Probability Theory

Working with Lubridate Package in R

This tutorial gives you an introduction about working with the lubridate package in R.

This post is partly based on the following paper (PDF) published by the authors of the lubridate package:

Garrett Grolemund and Hadley Wickham, Dates and Times Made Easy with lubridate, Journal of Statistical Software, Vol 40, No. 3, April 2011

Read more Working with Lubridate Package in R

Introduction to Dates and Times in R

This tutorial gives a brief introduction to dates and times in R.

1  Overview

There are several ways of handling with date and time data in R:

  • The date class in base R — handles dates without time
  • The chron package — handles dates and times without control for time zones.
  • The POSIXt classes in base R — handle dates and times with control for time zone.
  • The lubridate package
  • The timeDate package
  • The zoo package
  • The xts package

General rule for choosing ways for handling dates/times:

  • I would choose date and POSIXt classes if they meet my requirements, since they are included in R base. Why bother with external (package) dependencies unless very necessary?

Read more Introduction to Dates and Times in R

Write R Output to Text File

This tutorial presents two ways to write R output to text file.

R provides two functions for writing objects to files in ASCII format:

  1. write() function — suitable for the same kind of data as read using scan() function;
  2. write.table() function — suitable for the types of data which would normally be read using read.table() function.

Note, the write.fwf() function in the gdata package can write R objects to a file using fixed-width fields, but it is not covered here due to its non-common usage.

Read more Write R Output to Text File

Generating and Working with Sequence Data in R

This tutorial covers some key points about generating and working with sequence data in R.

1  Generating sequence data

Generating vectors / sequences of data is commonly used for simulations or coding tests when ‘real’ data isn’t available.

1.1 Sequences

1.1.1  seq()

1.1.2  gl()

The gl() function can be used to generate factors by specifying the pattern of their levels. Read more Generating and Working with Sequence Data in R

Import Data into R from External File

This tutorial covers several ways to import data into R from external file

1  Import using scan() function

The scan() function is most appropriatee when all the data to be read is of same mode, so that it can be accommodated by a vector or matrix.

1.1  Argument — file

  • A quoted string or character variable containing the name of a file, or a URL, or a connection.
  • If file=”” or the argument omitted, then scan() will read from console, stopping when a completely blank line is input, for example, hit ENTER without typing anything.

Read more Import Data into R from External File