Overview of Data Objects in R

This tutorial gives an overview of data objects in R.

1  Mode, class, and typeof

  • The mode, class, and type of a R object can be returned by functions mode(), class(), mode()/storage.mode(), respectively.
  • Mode is a mutually exclusive classification of objects according to their basic structure. The 'atomic' modes are numeric, complex, character, and logical. Recursive objects have modes such as list or function or a few others. An object has one and only one mode.
    When we work with data in R, one of common problems is due to mismatch between the operation on the object and the object's mode.
  • Class is a property assigned to an object that determines how generic functions operate with it. It is not a mutually exclusive classification. If an object has no specific class assigned to it, such as a simple numeric vector, it's class is usually, by convention, the same as its mode.
  • The mode() and storage.mode() are the old-style types where storage.mode() is more accurate, while typeof() is the newer, even more accurate version. Indeed, typeof() gives the 'type' of object from R's point of view, while mode() gives the 'type' from the view point of Becker, Chambers & Wilks (1998), which may be more compatible with other S implementation.
  • Modes have the same set of names as typeof except that
    • Types integer and double have mode returned as numeric.
    • Types special and builtin have mode returned as function.
    • Type symbol have mode returned as name.
    • Type language has mode returned as "(" or "call".
  • The mode of an object tells us how it is stored. It could happen that two different objects of different classes are stored in same mode. However, how these two objects, for example, are printed using the print() command, is determined by their classes.
  • The data type of a R object can be tested by functions is.numeric(), is.vector()is.character(), is.logical(), is.list(), is.factor(), is data.frame(), and so on.
  • The mode of a factor object, even when it is displayed in characters, is always numeric, so special care is needed when working with factors.

  •  The mode of a matrix is simply the mode of its constituents; the class of a matrix is returned as matrix.

Read more ›

Posted in Courses, How-to, R Tagged with: , ,

Install R package from local zipped file

Sometimes it is necessary to install R package from locally stored zipped binary file. This tutorial tells you how to do this quickly and easily, in two steps.

  1. Download the ZIP file of the package,save it to local drive of your computer.
  2. Run following R commans

That's it!


  • The file.choose() command will show a window allowing you to choose the .zip file or the tar.gz file where you downloaded it. This command is very useful when you don't have enough rights on a Windows machine and run R from a flash drive like myself.
  • It is also useful before running this command to RENAME the zip file you are going to install into the package name that you intend to use.
Posted in How-to, R Tagged with:

Stationarity, Autocorrelation, White Noise, and Linear Time Series

This tutorial introduces basic concepts about stationarity, autocorrelation, white noise, and linear time series.

1  Stationarity

1.1  Strict stationarity

A time series {r_t} is said to be strictly stationary if the joint distribution of (t_{t_1},\cdots,r_{t_k})\,is identical to  (t_{t_1+l},\cdots,r_{t_k+l})\,for all t, where k is an arbitrary positive integer and (t_1,\cdots,r_k) is a collection of k positive integers.

In other words, strict stationarity requires that the joint distribution of (r_{t_1},\cdots,r_{t_k}) is invariant under time shift. This is a very hard condition that is hard to verify empirically. Read more ›

Posted in Courses Tagged with: , , , ,

Asset Return and Distributions

This post talks about asset return and distributions, covering various definitions of returns and the relationship among them, return distributions and tests of returns.

1  Asset returns

Most financial studies involves returns, instead of prices, for two reasons:

  1. Return is a complete and scale-free summary of investment opportunity;
  2. Return has more attractive statistical properties than price.

Read more ›

Posted in Courses Tagged with: , , , ,

Download financial data using R's quantmod package

This tutorial gives a short intruduction about how to use R's Quantmod package to retrieve financial time series data from internet.

1  Overview of quantmod package

The quantmod package for R is designed to assist the quantitative trader in the development, testing, and deployment of statistically based trading models. It provides a rapid prototyping environment, where quant traders can quickly and cleanly explore and build trading models. Quantmod makes modelling easier by removing the repetitive workflow issues surrounding data management, modelling interfaces, and performance analysis.

However, quantmod is not a replacement for anything statistical. It has no 'new' modelling routines or analysis tool to speak of. It does now offer charting not currently available elsewhere in R, but most everything else is more of a wrapper to what you already know and love about the language and packages you currently use.

Read more ›

Posted in Courses, How-to, R Tagged with: , , ,

Arrays and Matrices in Python

This tutorial gives introduction about arrays and matrices in Python, provided by the Numpy module.

1  Array

1.1  Initialization of arrays

Arrays are initialized from lists or tuples using the numpy.array() function. Two dimentional arrays are initialized using list of lists, or tuples of lists, or list of tuples, etc. Higher dimensional arrays can be initialized by further nesting lists or tuples. Read more ›

Posted in How-to, Python Tagged with: ,

Shallow and deep copies in Python

This tutorial describes the differences between shallow and deep copies in Python.

The difference between shallow and deep copying is only relevant for compound objects, i.e. objects containing other objects, like lists or class instances. Python creates real copies only if it has to, i.e. if the user, the programmer, explicitly demands it.  Read more ›

Posted in How-to, Python Tagged with:

A Quick Tutorial on Python 3

After learning this quick tutorial on Python 3, you will accummulate confidence in writting your own Python codes.

1  Short Introduction

1.1  Numbers

  • '/' always returns a float; '//' does floor division and returns an integer; '%' calculates the remainder; power calculation is done by '**'
  • In interactive mode, the last printed expression is assigned to a special variable '_' (underscore), similar as 'ans' in Matlab.

    The '_' variable shall be treated as read-only. Assigning a value to it will invalidate its embedded magic behavior.

Read more ›

Posted in How-to, Python Tagged with:

R in Time Series: Linear Regression With Harmonic Seasonality

This tutorial talks about linear regression with harmonic seasonality.

1  Underlying mathematics

In regression modeling with seasonality, we can use one parameter for each season. For instance, 12 parameters for 12 months in one year. However, seasonal effects often vary smoothly over the seasons, so that it may be more parameter-efficient to use a smooth function instead of separate indices. Sine and cosine functions can be used to build smooth variationinto a seasonal model. Read more ›

Posted in How-to, Knowledge, Mathematics, R Tagged with: , , ,

R in Time Series: Linear Regression with Seasonal Variables

This tutorial gives a short introduction about linear regression with seasonal variables.

A time series are observations measured sequentially in time, seasonal effects are often present in the data, especially annual cycles caused directly or indirectly by the Earth's movement around the sun. Here we will present linear regression model with additive seasonal indicator variables included.

Suppose a time series contains s seasons. For example

  • For time series measured over each calendar month, s = 12.
  • For time series measured in six-month intevals, corresponding to summer and winter, s = 2.

Read more ›

Posted in How-to, Knowledge, Mathematics, R Tagged with: , , ,