Chapter 5. How To : Simple Linear Regression on asset or FOREX quotes versus time

5.1. Introduction

Technical and quantitative analysts have applied statistical principles to the financial market since its inception. Some attempts have been very successful while some have been anything but. The key is to find a way to identify price trends without the fallibility and bias of the human mind. One approach that can be successful for investors and is available in most charting tools is Simple linear regression.

Linear regression analyzes two separate variables in order to define a single relationship. In our case, we will use the two variables available on an asset or FOREX quotes timeseries, hence the price and time variables.

Describing the detailed statistical and mathematical approach behind computing a Simple linear regression is beyond the scope of this document. But we will walk through the various steps required within AirXCell to compute such a simple linear regression on asset or FOREX quotes.

We will cover here two different methods for computing and charting a Simple Linear Regression on asset or FOREX quotes:

5.2. Use 1. Manage Asset to load asset

One should thoroughly follow How-to How To : Load Asset or FOREX quotes within AirXCell (3) before running the next sections.

The remainder of this chapter assumes the user has execute that previous How-To and continues here right at its end.

5.3. Compute 'Simple Linear Regression' manually

The procedure is pretty straightforward with the help of the fRegression package and its regFit method.

The R code we will be using is as follows:


# use function regFit from fRegression package
require("fRegression")

# get timeseries from IBM (AXCAsset)
IBM.ts <- getTimeSeries (IBM)

# Fit linear reression model using standard lm method
IBM.fit <- regFit (IBM.Adjusted ~ time (IBM.Adjusted), data = IBM.ts, use = "lm")

# Plot responses + fitted values
plot(IBM.fit, which = 1)

# Plot residuals
plot(IBM.fit, which = 2)
        
        

It's quite sefl-explanatory. Let's just underline the function we are using : IBM.Adjusted ~ time (IBM.Adjusted) where IBM.Adjusted is the title of the column of the timeseries that contains adjusted prices.

The formula hence means that we eant to perform a simple linear regression of the asset adjusted quotes against the time of the quotes.

5.3.1. Open "R Ĉode Editor"

Once the IBM Asset quotes are loaded, we should open an R Code Editor and type in the code above.

The result is shown on Figure 5.1, “Regression calculation code in editor”:

Figure 5.1. Regression calculation code in editor

Regression calculation code in editor


One can execute the code using either Ctrl + A typed in the editor or the "Run All" button on the toolbar.

5.3.2. Responses + Fitted values

The result from the execution is shown on Figure 5.2, “Responses + Fitted values”:

Figure 5.2. Responses + Fitted values

Responses + Fitted values


5.3.3. Residuals

Let's now change the code in the editor a little bit to chart the residuals in addition to the fitted values.

The R code we will be using for this is :


# Plot residuals
plot(IBM.fit, which = 2)

            

The new code is shown on Figure 5.3, “Regression calculation code in editor (2)”:

Figure 5.3. Regression calculation code in editor (2)

Regression calculation code in editor (2)


The result from the execution is shown on Figure 5.4, “Residuals”:

Figure 5.4. Residuals

Residuals


5.4. Notes

With the manual way, any kind of regression can be computed within AirXCell, the user just needs the data loaded within AirXCell as timeseries and define the right formula.

5.5. Compute 'Simple Linear Regression' with AirXCell form

As stated previously, a simple linear regression on asset quotes can be computed automatically with the help of the 5 analyze assets form.

One simply needs to open such a form, select the desired asset and run SMP as shown on Figure 5.5, “Automatically running SMP”:

Figure 5.5. Automatically running SMP

Automatically running SMP