By the end of this section, you will be able to:
- Create a vector of data values for the R statistical analysis tool.
- Write basic statistical commands using the R statistical analysis tool.
Commands and Vectors in R
R is a statistical analysis tool that is widely used in the finance industry. It is available as a free program and provides an integrated suite of functions for data analysis, graphing, and statistical programming. R is increasingly being used as a data analysis and statistical tool as it is an open-source language and additional features are constantly being added by the user community. The tool can be used on many different computing platforms and can be downloaded at the R Project website.
Link to Learning
Using the R Statistical Tool
There are many resources for learning and using the R statistical tool, including the following:
How to install R on different computer operating systems
Once you have installed and started R on your computer, at the bottom of the R console, you should see the symbol >, which indicates that R is ready to accept commands.
Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. >
R is a command-driven language, meaning that the user enters commands at the prompt, which R then executes one at a time. R can also execute a program containing multiple commands. There are ways to add a graphic user interface (GUI) to R. An example of a GUI tool for R is RStudio.
The R command line can be used to perform any numeric calculation, similar to a handheld calculator. For example, to evaluate the expression $10+3\xc2\xb77,$ enter the following expression at the command line prompt and hit return:
> 10+3*7 [1] 31
Most calculations in R are handled via functions. For statistical analysis, there are many preestablished functions in R to calculate mean, median, standard deviation, quartiles, and so on. Variables can be named and assigned values using the assignment operator <-. For example, the following R commands assign the value of 20 to the variable named x and assign the value of 30 to the variable named y:
> x <- 20 > y <- 30
These variable names can be used in any calculation, such as multiplying x by y to produce the result 600:
> x*y [1] 600
The typical method for using functions in statistical applications is to first create a vector of data values. There are several ways to create vectors in R. For example, the c function is often used to combine values into a vector. The following R command will generate a vector called salaries that contains the data values 40,000, 50,000, 75,000, and 92,000:
> salaries <- c(40000, 50000, 75000, 92000)
This vector salaries can then be used in statistical functions such as mean, median, min, max, and so on, as shown:
> mean(salaries) [1] 64250 > median(salaries) [1] 62500 > min(salaries) [1] 40000 > max(salaries) [1] 92000
Another option for generating a vector in R is to use the seq function, which will automatically generate a sequence of numbers. For example, we can generate a sequence of numbers from 1 to 5, incremented by 0.5, and call this vector example1, as follows:
> example1 <- seq(1, 5, by=0.5)
If we then type the name of the vector and hit enter, R will provide a listing of numeric values for that vector name.
> salaries [1] 40000 50000 75000 92000 > example1 [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
Often, we are interested in generating a quick statistical summary of a data set in the form of its mean, median, quartiles, min, and max. The R command called summary provides these results.
> summary(salaries) Min. 1st Qu. Median Mean 3rd Qu. Max. 40000 47500 62500 64250 79250 92000
For measures of spread, R includes a command for standard deviation, called sd, and a command for variance, called var. The standard deviation and variance are calculated with the assumption that the data set was collected from a sample.
> sd(salaries) [1] 23641.42 > var(salaries) [1] 558916667
To calculate a weighted mean in R, create two vectors, one of which contains the data values and the other of which contains the associated weights. Then enter the R command weighted.mean(values, weights).
The following is an example of a weighted mean calculation in R:
Assume your portfolio contains 1,000 shares of XYZ Corporation, purchased on three different dates, as shown in Table 13.14. Calculate the weighted mean of the purchase price, where the weights are based on the number of shares in the portfolio.
Date Purchased | Purchase Price ($) | Number of Shares Purchased |
---|---|---|
January 17 | 78 | 200 |
February 10 | 122 | 300 |
March 23 | 131 | 500 |
Total | 1,000 |
Here is how you would create two vectors in R: the price vector will contain the purchase price, and the shares vector will contain the number of shares. Then execute the R command weighted.mean(price, shares), as follows:
> price <- c(78, 122, 131) > shares <- c(200, 300, 500) > weighted.mean(price, shares) [1] 117.7
A list of common R statistical commands appears in Table 13.15.
R Command | Result |
---|---|
mean( ) | Calculates the arithmetic mean |
median( ) | Calculates the median |
min( ) | Calculates the minimum value |
max( ) | Calculates the maximum value |
weighted.mean( ) | Calculates the weighted mean |
sum( ) | Calculates the sum of values |
summary( ) | Calculates the mean, median, quartiles, min, and max |
sd( ) | Calculates the sample standard deviation |
var( ) | Calculates the sample variance |
IQR( ) | Calculates the interquartile range |
barplot( ) | Plots a bar chart of non-numeric data |
boxplot( ) | Plots a boxplot of numeric data |
hist( ) | Plots a histogram of numeric data |
plot( ) | Plots various graphs, including a scatter plot |
freq( ) | Creates a frequency distribution table |
Graphing in R
There are many statistical applications in R, and many graphical representations are possible, such as bar graphs, histograms, time series plots, scatter plots, and others. The basic command to create a plot in R is the plot command, plot(x, y), where x is a vector containing the x-values of the data set and y is a vector containing the y-values of the data set.
The general format of the command is as follows:
>plot(x, y, main="text for title of graph", xlab="text for x-axis label", ylab="text for y-axis label")
For example, we are interested in creating a scatter plot to examine the correlation between the value of the S&P 500 and Nike stock prices. Assume we have the data shown in Table 13.13, collected over a one-year time period.
Note that data can be read into R from a text file or Excel file or from the clipboard by using various R commands. Assume the values of the S&P 500 have been loaded into the vector SP500 and the values of Nike stock prices have been loaded into the vector Nike. Then, to generate the scatter plot, we can use the following R command:
>plot(SP500, Nike, main="Scatter Plot of Nike Stock Price vs. S&P 500", xlab="S&P 500", ylab="Nike Stock Price")
As a result of these commands, R provides the scatter plot shown in Figure 13.10. This is the same data that was used to generate the scatter plot in Figure 13.8 in Excel.