# Introduction to Autoregressive models

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

### 1  AR(1) model

#### 1.1  Model definition

[r_t=phi_0+phi_1r_{t-1}+a_t]

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

Notes:

• 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.

# 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 be a non-empty set, and be a collection of subsets of . We say  is a -algebra or -field if:

1. The empty set
2. For any set
3. Whenever a sequence of sets .

Note:

• If we have a -algebra of sets, then all operations of the sets will give other sets that are still in the -algebra.
• The whole space since .

# 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

# 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?

# 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.

### 1  By RODBC package

Refer to this post for details.

### 2  By RJDBC package

Refer to this post for details

### 3  By RSQLSERVER package

This tutorial briefly talks about saving and loading R data objects. By saving R data object into R’s internal binary format, prepared or manipulated data set can be easily shared between different R sessions, and under different operation system platform.

There are two ways of saving R data objects: Read more Saving and Loading R Data Objects

# 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.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.