R - Statistical Programming Language | by Farhad Malik | Towards Data Science

We should take advantage of this time to learn a new skill if we can. I have been learning the programming language R during the past few weeks.
This article aims to provide an overview of the R programming language along with all of the main concepts which every data scientist must be familiar with
The field of data science and quantitative development requires us to constantly adapt and learn new skills because of its highly dynamic and demanding nature. There comes a time in a data scientist's professional life when it becomes important to learn more than one programming language. Subsequently, I have chosen to learn R.
This article will aim to outline all of the key main areas and will explain everything from the basics. It assumes that the reader is not familiar or has a beginner level understanding of the programming language R.
I highly recommend R for many reasons that I will highlight in this article
R is gaining popularity and it is one of the most popular programming languages. R was written for statisticians by statisticians. It integrates well with other programming languages such as C++, Java, SQL. Furthermore, R is mainly seen as a statistical programming language. As a result, a number of financial institutions and large quantitative organisations use the R programming language during their research and development.
Python is a general-purpose language and R can be seen as an analytical programming language.
This article will explain the following key areas about R:
What Is R?How To Install R?Where To Code R?What Is A R Package and R Script?What Are The Different R Data Types?How To Declare Variables And Their Scope In R?How To Write Comments?What Are Vectors?What Are Matrix?What Are Lists?What Are Data Frames?Different Logical Operations In RFunctions In RLoops In RRead And Write External Data In RPerforming Statistical Calculations In RPlotting In RObject-Oriented Programming in RFamous R LibrariesHow To Install External R LibrariesPlotting In R
What Is R?
How To Install R?
Where To Code R?
What Is A R Package and R Script?
What Are The Different R Data Types?
How To Declare Variables And Their Scope In R?
How To Write Comments?
What Are Vectors?
What Are Matrix?
What Are Lists?
What Are Data Frames?
Different Logical Operations In R
Functions In R
Loops In R
Read And Write External Data In R
Performing Statistical Calculations In R
Plotting In R
Object-Oriented Programming in R
Famous R Libraries
How To Install External R Libraries
Plotting In R
I will explain the programming language from the basics in a manner that would make the language easier to understand. Having said that, the key to advancing in programming is to always practice as much as possible.
This article should form a solid foundation for the readers
R is a free programming language under GNU license. In a nutshell, R is a statistical environment.
R is mainly used for statistical computing. It offers a range of algorithms which are heavily used in machine learning domain such as time series analysis, classification, clustering, linear modeling, etc.
R is also an environment that includes a suite of software packages that can be used for performing a numerical calculation to chart plotting to data manipulation.
R is heavily used in statistical research projects.
R is very similar to the S programming language.
R is compiled and runs on UNIX, Windows, MacOS, FreeBSD and Linux platforms.
R has a large number of data structures, operators and features. It offers from arrays to matrices to loops to recursion along with integration with other programming languages such as C, C++, and Fortran.
C programming language can be used to update R objects directly.
New R packages can be implemented to extend.
R interpreter
R was inspired by S+, therefore if you are familiar with S then it will be a straightforward task to learn R.
Along with the benefits listed above, R is:
Straightforward to learn
A large number of packages are available for statistical, analytics and graphics which are open-source and free
A wealth of academic papers with their implementation in R are available
World’s top universities teach their students the R programming language, therefore, it has now become an accepted standard and thus, R will continue to grow.
Integration capabilities with other languages
Plus there is a large community support
There are a handful of limitations too:
R isn’t as fast as C++, plus security and memory management is an issue too.
R has a large number of namespaces, sometimes that could appear to be too many. However, it is getting better.
R is a statistician language thus it is not as intuitive as Python. It’s not as straightforward to create OOP as it is with Python.
I will now be presenting the language R in quick-to-follow sections.
You can install R on:
Ubunto
Mac
Windows
Fedora
Debian
SLES
OpenSUSE
Open an internet browser
Go to www.r-project.org.
The latest R version at the point of writing this article is 3.6.3 (Holding the Windsock). It was released on 2020–02–29.
These are the links:
Download R for Linux
Download R for (Mac) OS X
Download R for Windows
There are multiple graphical interfaces available. I highly recommend R-Studio
Download RStudio Desktop:
Download RStudio from https://rstudio.com/products/rstudio/download/
RStudio Desktop Open Source License is Free
You can learn more here: https://rstudio.com/products/rstudio/#rstudio-desktop
RStudio requires R 3.0.1+.
It usually installs R Studio in the following location if you are using Windows:
C:\Program Files\RStudio
R package and R script are the two key components of R. This section will provide an overview of the concepts.
Since R is an open-source programming language, it is important to understand what packages are. A package essentially groups and organises code and other functions. A package is a library that can contain a large number of files.
Data scientists can contribute and share their code with others either by creating their own or enhancing others’ packages. Packages allow data scientists to reuse code and distribute it to others.
Packages are created to contain functions and data sets
A data scientist can create a package to organise code, documentation, tests, data sets etc. and the package can then be shared with others.
There are tens of thousands of R packages available on the internet. These packages are located in the central repository. There are a number of repositories available such as CRAN, Bioconductor, Github etc.
One repository worth mentioning is CRAN. It is a network of servers that store a large number of versions of code and documentation for R.
A package contains a description file where one would state the date, dependencies, author, and version of the package amongst other information. The description file helps the consumers get meaningful information about the package.
To load a package, type in:
library(name of the package)
To use a function of a package, type in the name of the package::name of the function.
For example, if we want to use the function “AdBCDOne” of the package “carat” then we can do:
library(carat)carat::AdBCDOne()
R script is where a data scientist can write the statistical code. It is a text file with an extension .R e.g. we can call the script as tutorial.R
We can create multiple R scripts in a package.
As an instance, if you have created two R scripts:
blog.Rpublication.R
blog.R
publication.R
And if you want to call the functions of publication.R in blog.R then you can use the source(“target R script”) command to import publication.R into blog.R:
source("publication.R")
The process is relatively simple. In a nutshell
Create a Description fileCreate the R.scripts and add any data sets, documentation, tests that are required for the packageWrite your functionality in R scriptsWe can use devtools and roxygen2 to create R packages by using the command:
Create a Description file
Create the R.scripts and add any data sets, documentation, tests that are required for the package
Write your functionality in R scripts
We can use devtools and roxygen2 to create R packages by using the command:
create_package("name of the package")
It is vital to understand the different data types and structures in R to be able to use the R programming language efficiently. This section will illustrate the concepts.
These are the basic data types in R:
character: such as “abc” or “a”integer: such as 5Lnumeric: such as 10.5logical: TRUE or FALSEcomplex: such as 5+4i
character: such as “abc” or “a”
integer: such as 5L
numeric: such as 10.5
logical: TRUE or FALSE
complex: such as 5+4i
We can use the typeof(variable) to find the type of a variable.
To find the metadata, such as attributes of a type, use the attributes(variable) command.
A number of data structures exist in R. The most important data structures are:
vector: the most important data structure that is essentially a collection of elements.matrix: A table-like structure with rows and columnsdata frame: A tabular data structure to perform statistical operationslists: A collection that can hold a combination of data types.factors: to represent categorical data
vector: the most important data structure that is essentially a collection of elements.
matrix: A table-like structure with rows and columns
data frame: A tabular data structure to perform statistical operations
lists: A collection that can hold a combination of data types.
factors: to represent categorical data
We can create a variable and assign it a value. A variable could be of any of the data types and data structures that are listed above. There are other data structures available too. Additionally, a developer can create their own custom classes.
A variable is used to store a value that can be changed in your code.
As a matter of understanding, it is vital to remember what an environment in R is. Essentially, an environment is where the variables are stored. It is a collection of pairs where the first item of the pair is the symbol (variable) and the second item is its value.
Environments are hierarchical (tree structure), hence an environment can have a parent and multiple children. The root environment is the one with an empty parent.
We have to declare a variable and assign it a value using →
x <- "my variable"print(x)
This will set the value of “my variable” to the variable x. The print() function will output the value of x, which is “my variable”.
Every time we declare a variable and call it, it is searched in the current environment and is recursively searched in the parent environments until the symbol is found.
To create a collection of integers, we can do:
coll <- 1:5print(coll)
1 is the first value and 5 is the last value of the collection.
This will print 1 2 3 4 5
Note, R-Studio IDE keeps track of the variables:
The ls() function can be used to show the variables and functions in the current environment.
Comments are added in the code to help the readers, other data scientists and yourself understand the code.
Note: Always ensure the comments are not polluting your scripts.
We can add a single line comment:
#This is a single line comment
We can add the multiline comments using the double quotes:
"This is a multi line comment"
Note: In R-Studio, select the code you want to comment and then press Ctrl+Shift+C
It will automatically comment on the code for you.
Vector is one of the most important data structures in R. Essentially, a vector is a collection of elements where each element is required to have the same data type e.g. logical (TRUE/FALSE), Numeric, character.
We can also create an empty vector:
x <- vector()
By default, the type of vector is logical, such as True/False. The line below will print logical as the type of vector:
typeof(x)
To create a vector with your elements, you can use the concatenate function (c):
x <- c("Farhad", "Malik", "FinTechExplained")print(x)
This will print:
[1] “Farhad” [2] “Malik” [3] “FinTechExplained”
If we want to find the length of a vector, we can use the length() function:
length(x) 
This will print 3 as there are three elements in the vector.
To add elements into a vector, we can combine an element with a vector.
For example, to add “world” at the start of a vector with one element “hello”:
x <- c("hello")x <- c("world", x)print(x)
This will print “world” “hello”
If we mix the types of elements then R will accommodate the type of the vector too. The type (mode) of the vector will become whatever it considers being the most suitable for the vector:
another_vec <- c("test", TRUE)print(typeof(another_vec))
Although the second element is a logical value, the type will be printed as “character”.
Operations can also be performed on vectors.
As an instance, to multiply a scalar to a vector:
x <- c(1,2,3)y <- x*2print(y)
This will print 2,4,6
We can also add two vectors together:
x <- c(1,2,3)y <- c(4,5,6)z <- x+yprint(z)
This will print 5 7 9
If the vectors are characters and we want to add them together:
x <- c("F","A","C")y <- c("G","E","D")z <- x+yprint(z)
It will output:
Error in x + y : non-numeric argument to binary operator
Matrix is also one of the most common data structures in R.
It can be considered as an extension of a vector. A matrix can have multiple rows and columns. All of the elements of a matrix must have the same data type.
To create a matrix, use the matrix() constructor and pass in nrow and ncol to indicate the columns and rows:
x <- matrix(nrow=4, ncol=4)
This will create a matrix variable, named x, with 4 rows and 4 columns.
A vector can be transformed into a matrix by passing a matrix in the constructor. The resultant matrix will be filled column-wise:
vector <- c(1, 2, 3)x <- matrix(vector)print(x)
This will create a matrix with 1 column and 3 rows (one for each element):
[,1][1,] 1[2,] 2[3,] 3
If we want to fill a matrix by row or column then we can explicitly pass in the number of rows and columns along with the byrow parameter:
vector <- c(1, 2, 3, 4)x <- matrix(vector, nrow=2, ncol=2, byrow=TRUE)print(x)
The above code created a matrix with 2 columns and rows. The matrix is filled by row.
[,1] [,2][1,] 1 2[2,] 3 4
If we want to create a collection that can contain elements of different types then we can create a list.
Lists are one of the most important data structures in R. To create a list, use the list() constructor:
my_list <- list("hello", 1234, FALSE)
The code snippet above illustrates how a list is created with three elements of different data types.
We can access any element by using the index e.g.:
item_one = my_list[1]
This will print “hello”
We can also give each element a name e.g.
my_named_list <- list(“A”=1, “B”=2)print(names(my_named_list))It prints “A” “B”print(my_named_list[‘A’])It prints 1
Factors are categorical data e.g. Yes, No or Male, Female or Red, Blue, Green, etc.
A factor data type can be created to represent a factor data set:
my_factor = factor(c(TRUE, FALSE, TRUE))print(my_factor)
Factors can be ordered too:
my_factor = factor(c(TRUE, FALSE, TRUE), levels = c(TRUE, FALSE))print(my_factor)
We can also print the factors in tabular format:
my_factor = factor(c(TRUE, FALSE, TRUE), levels = c(TRUE, FALSE))print(table(my_factor))
This will print:
TRUE FALSE     2     1
We have covered half of the article. Let’s move on to more statistical learning
Most, if not all of the data science projects require input data in tabular format. Data frames data structure can be used to represent tabular data in R. Each column can contain a list of elements and each column can be of a different type than each other.
To create a data frame of 2 columns and 5 rows each, simply do:
my_data_frame <- data.frame(column_1 = 1:5, column_2 = c("A", "B", "C", "D", E"))print(my_data_frame)column_1 column_21        1        A2        2        B3        3        C4        4        D5        5        E
This section provides an overview of the common operators:
This will check if one or two is true.
This will check if one and two are true.
This will return true if the input is False
Sometimes we want the code to perform a set of tasks. These tasks can be grouped as functions. The functions are essentially objects in R.
Arguments can be passed to a function in R and a function can return an object too.
R is shipped with a number of in-built functions such as length(), mean(), etc.
Every time we declare a function (or variable) and call it, it is searched in the current environment and is recursively searched in the parent environments until the symbol is found.
A function has a name. This is stored in the R environment. The body of the function contains the statements of a function.
A function can return value and can optionally accept a number of arguments.
To create a function, we need the following syntax:
name_of_function <- function(argument1...argumentN) {   Body of the function}
For example, we can create a function that takes in two integers and returns a sum:
my_function <- function(x, y) {  z <- x + y  return(z)}
To call the function, we can pass in the arguments:
z <- my_function(1,2)print(z)
This will print 3.
We can also set default values to an argument so that its value is taken if a value for an argument is not provided:
my_function <- function(x, y=2) {  z <- x + y  return(z)}output <- my_function(1)print(output)
The default value of y is 2, therefore, we can call the function without passing in a value for y.
The key to note is the use of the curly brackets {...}
Let’s look at a complex case whereby we will use a logical operator
Let’s consider that we want to create a function that accepts the following arguments: Mode, x and y.
If the Mode is True then we want to add x and y.
If the Mode is False then we want to subtract x and y.
my_function <- function(mode, x, y) {  if (mode == TRUE)  {    z <- x + y    return(z)  }  else  {    z <- x - y    return(z)  }}
To call the function to add the values of x and y, we can do:
output <- my_function(TRUE, 1, 5)print(output)
This will print 6
Let’s review the code below. In particular, see where print(z) is:
my_function <- function(mode, x, y) {  if (mode == TRUE)  {    z <- x + y    return(z)  }  else  {    z <- x - y    return(z)  }  #what happens if we try to print z  print(z)}
The key to note is that z is being printed after the brackets are closed.
Will the variable z be available there? This brings us to the topic of scope in functions!
A function can be declared within another function:
some_func <- function() {  some_func_variable <- 456  another_func <- function() {   another_func_variable <- 123  }}
In the example above, some_func and another_func are the two functions. another_func is declared inside some_func. As a result, another_func() is private to some_func(). Hence, it not accessible to the outside world.
If I execute another_func() outside of some_func as shown below:
another_func()
We will experience the error:
Error in another_func() : could not find function “another_func”
On the other hand, we can execute another_func() inside some_func() and it will work as expected.
Now consider this code to understand how scope works in R.
some_func_variable is accessible to both some_func and another_func functions.another_func_variable is only accessible to the another_func function
some_func_variable is accessible to both some_func and another_func functions.
another_func_variable is only accessible to the another_func function
some_func <- function() {  some_func_variable <- "DEF"  another_func <- function() {    another_func_variable <- "ABC"    print(some_func_variable)    print("Inside another func" + another_func_variable)  }  print(some_func_variable)  print("outside another func" + another_func_variable)  another_func()}some_func()
Running the above code will throw an exception in R-Studio:
> some_func()[1] “DEF”Error in print(“outside another func” + another_func_variable) :  object ‘another_func_variable’ not found
As the error states, another_func_variable is not found. We can see DEF was printed which was the value assigned to the variable some_func_variable.
If we want to access and assign values to a global variable, use the <<- operator. The variable is searched in the parent environment frame. If a variable is not found then a global variable is created.
To add an unknown number of arguments, use ...
my_func <- function(a, b, ...){   print(c)}my_func(a=1,b=2,c=3)
R supports control structures too. Data scientists can add logic to the R code. This section highlights the most important control structures:
Occasionally, we want to iterate over elements of a collection. The syntax is:
for (i in some_collection) {  print(i)}
In the example above, the iterator can be a list, vector, etc. The snippet of code above will print the elements of the collection.
We can also loop over a collection by using the seq_along() function. It takes a collection and generates a sequence of integers.
Occasionally, we have to loop until a condition is met. Once the condition is false then we can exit the loop.
We can use the while loops to achieve the desired features.
In the code below, we are setting the value of x to 3 and the value of z to 0. Subsequently, we are incrementing the value of z by 1 each time until the value of z is equal to or greater than x.
x <- 3z <- 0while(z < x) { z <- z + 1}
If Then Else are heavily used in programming.
In a nutshell, a condition is evaluated in the if control block. If it is true then its code is executed otherwise the next block is executed. The next block can either by Else If or Else.
if(condition is true) {
# execute statements
}
We can also have an optional else:
if(condition is true) {
# execute statements
}
else if (another condition is true) {
# execute statements
}
else {
# execute statement
}
x <- 3y <- 0if (x == 3) {   y <- 1}else if (x<3) {  y <- 2}else {  y <- 3}
If we want to repeat a set of statements for an unknown number of times (maybe until a condition is met or a user enters a value etc.) then we can the repeat/break statements. A break can end the iteration.
repeat {  print("hello")  x <- random()  if (x > 0.5)  {    break; #exit the loop  }}
If we want to skip an iteration, we can use the next statement:
repeat {  print("hello")  x <- random()  if (x > 0.5)  {    next #iterate again   }}
R offers a range of packages that can allow us to read/write to external data such as excel files or SQL tables. This section illustrates how we can achieve it.
library(openxlsx)path <-"C:/Users/farhadm/Documents/Blogs/R.xlsx"res <- read.xlsx(path, 'Sheet1')head(res)
This will display the top rows:
columnA <- runif(100,0.1,100)columnB <- runif(100,5,1000)df <- data.frame(columnA, columnB)write.xlsx(df, file = path, sheetName="NewSheet", append=TRUE)
It created a new excel file with sheet named as NewSheet:
We can read from a SQL table
library(RODBC)db <- odbcDriverConnect('driver={SQL Server};server=SERVERNAME;database=DATABASENAME;trusted_connection=true')res <- sqlQuery(db, 'SQL QUERY')
We can write to a SQL table
sqlSave(odbcConnection, some data frame, tablename="TABLE", append=TRUE, rownames=FALSE)
R is known to be one of the most popular statistical programming languages. It is vital to understand the in-built statistical functions in R. This section will outline the most common statistical calculations that are performed by data scientists.
One of the most common tasks in a data science project is to fill the missing values. We can use the is.na() to find the elements that are empty (NA or NAN):
vec <- c("test", NA, "another test")is.na(vec)
This will print FALSE TRUE FALSE, indicating that the second element in NA.
To understand them better, is.na() will return all of those elements/objects that are NA. is.nan() will return all of the NaN objects. It’s important to note that the NaN is an NA but an NA is not a NaN.
Note: Many statistical functions such as mean, median, etc, take in an argument: na.rm which indicates whether we want to remove the na (missing values).
The next few calculations will be based on the following two vectors:
A <- c(1,2,5,6.4,6.7,7,7,7,8,9,3,4,1.5,0,10,5.1,2.4,3.4, 4.5, 6.7)B <- c(4,4.1,0,1.4,2,1,6.7,7,5,5,8,9,3,2,2.5,0,10,5.1,4.3,5.7)print(length(A)) #20print(length(B)) #20
Both of the vectors, A and B, contain numerical values of 20 elements.
Mean is computed by summing the values in a collection and then dividing by the total number of values:
my_mean <- mean(A)print(my_mean)
Median is the middle value in a sorted collection. If there are an even number of values then it’s the average of the two middle values:
my_median <- median(A)print(my_median)
A mode is the most frequent value. R is missing a standard built-in function to calculate the mode. However, we can create a function to calculate it as shown below.
distinct_A <- unique(A)matches <- match(A, distinct_A)table_A <- tabulate(matches)max_A <- which.max(table_A)mode<-distinct_A[max_A]print(mode)
The function performs the following steps:
Computes the distinct values of the collectionThen it finds the frequency of each of the item and creates a table out of itLastly, it finds the index of the term that has the highest occurrence and returns it as the mode.
Computes the distinct values of the collection
Then it finds the frequency of each of the item and creates a table out of it
Lastly, it finds the index of the term that has the highest occurrence and returns it as the mode.
Standard deviation is the deviation of the values from the mean.
sd <- sd(A)print(sd)
Variance is the square of the standard deviation:
var <- var(A)print(var)
Correlation helps us understand whether the collections have a relationship with each other and whether they co-move with each other along with the strength of the relationship:
cor <- cor(A, B)print(cor)
We can pass in a specific correlation method such as Kendall or Spearman. Pearson is the default correlation method.
Pass in the correlation method in the method argument.
Covariance is created to inform us about the relationship between variables.
covariance <- cov(A, B)print(covariance)
We are often required to normalise data such as by using min-max normalisation or calculate the z-score using the standardisation mechanism.
Standardising data means having a data set with mean = 0 and standard deviation = 1. It requires subtracting the mean from each of the observation and then dividing it by the standard deviation.
We can use the scale function. If we want to subtract the mean from each of the observation then set its center parameter to True.
If we want to standardise data then we need to set its scale parameter to True.
normal_A <- scale(A, center=TRUE, scale = FALSE)print(normal_A)standard_A <- scale(A, center=TRUE, scale = TRUE)print(standard_A)
A regression model is gaining popularity in machine learning solutions due to its simplicity and explainability. Essentially, the regression models help us understand the relationship between different variables.
Usually, the coefficients are computed for one or more variables. These variables are regressors. They are used to estimate and predict another variable, the dependent variable. The dependent variable is also known as the response variable.
The data for regressors is gathered through sampling and they are used to predict an outcome:
bn are the coefficients which the linear models can estimate.
xn are the independent variables. We are going to gather data for these variables and feed to the model.
As an instance, let’s assume we gathered data set for temperature and want to predict the rainfall. We can use a linear model as shown below:
Temperature <- c(1,2,5,6.4,6.7,7,7,7,8,9,3,4,1.5,0,10,5.1,2.4,3.4, 4.5, 6.7)Rainfall <- c(4,4.1,0,1.4,2,1,6.7,7,5,5,8,9,3,2,2.5,0,10,5.1,4.3,5.7)model <- lm(Rainfall~Temperature)
Note, if there were multiple variables used to predict a variable such as using humidity and temperature to predict rainfall, we can use the lm() function and set the formula as:
Temperature <- c(1,2,5,6.4,6.7,7,7,7,8,9,3,4,1.5,0,10,5.1,2.4,3.4, 4.5, 6.7)Rainfall <- c(4,4.1,0,1.4,2,1,6.7,7,5,5,8,9,3,2,2.5,0,10,5.1,4.3,5.7)Humidity <- c(4,4.1,0,1.4,2,1,6.7,7,5,5,8,9,3,2,2.5,0,10,5.1,4.3,5.7)model <- lm(Rainfall~Temperature+Humidity)
We can then print out the summary of the model.
R will inform us about the residuals, coefficients, their standard deviation error, t-value, F statistics and so on:
print(summary(model))
It will print the following statistics:
Call:lm(formula = Rainfall ~ Temperature)Residuals:    Min      1Q  Median      3Q     Max -4.2883 -2.2512 -0.2897  1.8661  5.4124Coefficients:            Estimate Std. Error t value Pr(>|t|)   (Intercept)   4.8639     1.3744   3.539  0.00235 **Temperature  -0.1151     0.2423  -0.475  0.64040   ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 2.933 on 18 degrees of freedomMultiple R-squared:  0.01239, Adjusted R-squared:  -0.04248 F-statistic: 0.2258 on 1 and 18 DF,  p-value: 0.6404
Based on the example above, the rainfall is -0.1151 * temperature + 4.8639
If we want to use the model to estimate a new value, we can use the predict() function whereby the first parameter is the model and the second parameter is the value of the temperature for which we want to predict the rainfall:
temperature_value <- data.frame(Temperature = 170)rainfall_value <-  predict(model,temperature_value)print(rainfall_value)
Bayesian model uses probability to represent the unknowns. The aim is to feed in the data to estimate the unknown parameters.
As an instance, let’s assume we want to determine how much stock of a company will be worth tomorrow. Let’s also consider that we use the variable of company’s sales to estimate the stock price.
In this instance, the stock price is the unknown and we are going to use the value of company’s sales to calculate the price of the stock.
We can gather a sample of historic sales and stock price and use it to find the relationship between the two variables. In a real-world project, we would add more variables to estimate the stock price.
The key concepts to understand are conditional probability and Bayes theorem. These concepts were explained in the article:
towardsdatascience.com
towardsdatascience.com
Essentially, what we are trying to do is to use the prior probability of stock price to predict the posterior using the likelihood data and the normalizing constant.
install.packages("BAS")library(BAS)StockPrice <- c(1,2,5,6.4,6.7,7,7,7,8,9,3,4,1.5,0,10,5.1,2.4,3.4, 4.5, 6.7)Sales <- c(4,4.1,0,1.4,2,1,6.7,7,5,5,8,9,3,2,2.5,0,10,5.1,4.3,5.7)model <- bas.lm(StockPrice~Sales)print(summary(model))
Notice that we installed BAS package and then used the BAS library. The results are then displayed:
             P(B != 0 | Y) model 1     model 2Intercept      1.00000000  1.0000  1.00000000Temperature    0.08358294  0.0000  1.00000000BF                     NA  1.0000  0.09120622PostProbs              NA  0.9164  0.08360000R2                     NA  0.0000  0.01240000dim                    NA  1.0000  2.00000000logmarg                NA  0.0000 -2.39463218
Different prior distributions on the regression coefficients may be specified using the prior the argument, and include
“BIC”
“AIC
“g-prior”
“hyper-g”
“hyper-g-laplace”
“hyper-g-n”
“JZS”
“ZS-null”
“ZS-full”
“EB-local”
“EB-global”
To generate random numbers between a range, use the runif function. This will print 100 random numbers between 0.1 and 10.0
random_number <- runif(100, 0.1, 10.0)print(random_number)
We can also use the sample() function to generate items and numbers with or without replacement.
We can use the Poisson distribution and use the glm model where the family is Poisson:
output <-glm(formula = Temperature ~ Rainfall+Humidity,             family = poisson)print(summary(output))
The results will be printed:
Deviance Residuals:     Min       1Q   Median       3Q      Max  -3.2343  -0.8547  -0.1792   0.8487   1.8781Coefficients: (1 not defined because of singularities)            Estimate Std. Error z value Pr(>|z|)    (Intercept)  1.69807    0.17939   9.466   <2e-16 ***Rainfall    -0.02179    0.03612  -0.603    0.546    Humidity          NA         NA      NA       NA    ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for poisson family taken to be 1)Null deviance: 35.460  on 19  degrees of freedomResidual deviance: 35.093  on 18  degrees of freedomAIC: InfNumber of Fisher Scoring iterations: 5
There are several ways to generate data with normal distribution. The most common way is to call the rnorm function with the sample size, mean and standard deviation:
y <- rnorm(100, 0, 1)
Forward substitution is a common process used to solve a system of linear equations: Lx = y
In this instance, L is a lower triangular coefficient matrix L with non zero diagonal elements.
There are two functions that help us with forward and backward substitution.
R has forwardsolve(A,b) for forward substitution on lower triangular A, and backsolve(A,b) for backward substitution on upper triangular A.
Specifically, they are:
backsolve(r, x, k = ncol(r), upper.tri = TRUE,             transpose = FALSE)forwardsolve(l, x, k = ncol(l), upper.tri = FALSE,             transpose = FALSE)
r: an upper triangular matrix: R x = bl: a lower triangular matrix: L x = b
Both of these trianglar matrix gives us the coefficients which we are attempting to solve.
x: This is the matrix whose columns give the right-hand sides for the equations.
k: It is the number of columns of r and rows of x that we are required to use.
upper.tri: TRUE then use the upper triangular part of r.
transpose: If True then we are attempting to solve r’ * y = x for y
The output will be the same type as x therefore if x is a vector then the output will be a vector, otherwise, if x is a matrix then the output will be a matrix.
t-tests can be performed by using the t.test() function.
As an instance, one-sample t-test in R can be run by using t.test(y, mu = 0) where y is the variable which we want to test and mu is the mean as specified by the null hypothesis:
Humidity <- c(4,4.1,0,1.4,2,1,6.7,7,5,5,8,9,3,2,2.5,0,10,5.1,4.3,5.7)t.test(Humidity, mu = 5) 
The code above tests if the Humidity ≤ the mean of 5. This is the null hypothesis.
The results are:
One Sample t-test
data: Humidityt = -1.1052, df = 19, p-value = 0.2829alternative hypothesis: true mean is not equal to 595 percent confidence interval: 2.945439 5.634561sample estimates:mean of x  4.29
This section will explain how easy it is to plot charts in R.
I have generated the following data:
x<-runif(200, 0.1, 10.0)y<-runif(200, 0.15, 20.0)print(x)print(y)
The code snippet will plot the chart
plot(x, y, main='Line Chart')
install.packages('ggcorrplot')library(ggcorrplot)x<-runif(200, 0.1, 10.0)y<-runif(200, 0.15, 20.0)z<-runif(200, 0.15, 20.0)data <- data.frame(x,y,z)corr <- round(cor(data), 1)ggcorrplot(corr)
x<-runif(200, 0.1, 10.0)hist(x)
This section will outline the concept of object-oriented programming in R. It is vital to understand how objects are created in R because it can help you implement scalable and large applications in an easier manner.
The foremost concept to understand in R programming language is that everything is an object.
A function is an object too, as explained in the functions section. Therefore, we have to define a function to create objects. The key is to set the class attribute on an object.
R supports object-oriented concepts such as inheritance. A class can be a vector.
There are several ways to create a class in R. I will demonstrate the simplest form which revolves around creating S3 classes. This involves creating a list of properties.
Before I explain how to build a full-fledged class, let’s go over the steps in a simplified manner:
The first step is to create a named list, where each element has a name. The name of each element is a property of the class. As an example, this is how we can create a Human class in R:
farhad <- list(firstname="Farhad", lastname="Malik")class(farhad) <- append(class(farhad), "Human")
We have created an instance of a Human class with the following properties: firstname set to Farhad and lastname set to Malik.
To print the firstname property of the instance of Human object, we can do:
print(farhad$firstname)
Now, let’s move on to another important concept. How do we create an instance method for the object?
The key is to use the command: UseMethod
This command informs the R system to search for a function. An object can have multiple classes, the UseMethod uses the class of the instance to determine which method to execute.
Let’s create GetName function that returns the concatenated string of first and last name:
#This is how you create a new generic functionGetName <- function(instance){  UseMethod("GetName", instance)}#This is how you provide a function its bodyGetName.Human <- function(instance){  return(paste(instance$firstname,instance$lastname))}GetName(farhad)
To wrap it up, let’s create a class Human with properties firstname and lastname. It will have a function: GetName() which will return the first and last name.
Trick: create a function that returns a list and pass in the properties as the arguments to the function. Then use the UseMethod command to create the methods.
Human <- function(firstname, lastname){  instance <- list(firstname=firstname, lastname=lastname)  class(instance) <- append(class(instance), "Human")  return(instance)  }GetName <- function(instance){  UseMethod("GetName", instance)}GetName.Human <- function(instance){  return(paste(instance$firstname,instance$lastname))}farhad <- Human(firstname="farhad", lastname="malik")print(farhad)name <- GetName(farhad)print(name)
This will print:
> print(farhad)$firstname[1] "Farhad"$lastname[1] "Malik"attr(,"class")[1] "list"  "Human"> > > name <- GetName(farhad)> print(name)[1] "Farhad Malik"
What if we want to create a new class OfficeWorker that inherits from the Human class and provides different functionality to the GetName() method?
We can achieve it by:
Human <- function(firstname, lastname){  instance <- list(firstname=firstname, lastname=lastname)  class(instance) <- append(class(instance), "Human")  return(instance)  }OfficeWorker <- function(firstname, lastname){    me <- Human(firstname, lastname)    # Add the class  class(me) <- append(class(me),"OfficeWorker")  return(me)}
If we create an instance of an office worker and print the instance, it will print the following contents:
worker = OfficeWorker(firstname="some first name", lastname="some last name")print(worker)> print(worker)$firstname[1] "some first name"$lastname[1] "some last name"attr(,"class")[1] "list"         "Human"        "OfficeWorker"
Note, the classes of the instance are list, Human and OfficeWorker
To create a different function for the office worker then we can override it:
GetName <- function(instance){  UseMethod("GetName", instance)}GetName.Human <- function(instance){  return(paste(instance$firstname,instance$lastname))}GetName.OfficeWorker <- function(instance){  return(paste("Office  Worker",instance$firstname,instance$lastname))}
This will print:
> GetName(worker)[1] "some first name some last name"
It’s extremely straightforward to install an R package.
All we have to do is to type in the following command:
install.packages("name of package")
To install multiple packages, we can pass in a vector to the install.packages command:
install.packages(c("package1","package2"))
As an instance, CARAT is one of the most popular machine learning packages.
R-Studio makes it extremely easy to install a package. To install CARAT, click on the Packages tab at the bottom right and then click on the install button.
Enter “carat” and click on Install
The dialog boxes will appear to indicate that the package is being installed:
Once the package is installed, you can see it in the Console Window:
The downloaded binary packages are in C:\Users\AppData\Local\Temp\Rtmp8q8kcY\downloaded_packages
To remove a package, type in:
remove.packages("package name")
Other than the R libraries that have been mentioned in this article along with the built-in R functions, there are a large number of useful R packages which I recommend:
Prophet: For forecasting, data science and analytical projectsPlotly: For graphsJanitor: For data cleaningCaret: For classification and regression trainingMlr: For machine learning projectsLubridate: For time series dataGgpolot2: For visualisationDplyr: For manipulating and cleaning dataForcats: When working with categorical dataDplyr: For data manipulation
Prophet: For forecasting, data science and analytical projects
Plotly: For graphs
Janitor: For data cleaning
Caret: For classification and regression training
Mlr: For machine learning projects
Lubridate: For time series data
Ggpolot2: For visualisation
Dplyr: For manipulating and cleaning data
Forcats: When working with categorical data
Dplyr: For data manipulation
This article explained the following key areas about R:
What Is R?How To Install R?Where To Code R?What Is A R Package and R Script?What Are The Different R Data Types?How To Declare Variables And Their Scope In R?How To Write Comments?What Are Vectors?What Are Matrix?What Are Lists?What Are Data Frames?Different Logical Operations In RFunctions In RLoops In RRead And Write External Data In RPerforming Statistical Calculations In RPlotting In RObject-Oriented Programming in RFamous R LibrariesHow To Install External R LibrariesPlotting In R
What Is R?
How To Install R?
Where To Code R?
What Is A R Package and R Script?
What Are The Different R Data Types?
How To Declare Variables And Their Scope In R?
How To Write Comments?
What Are Vectors?
What Are Matrix?
What Are Lists?
What Are Data Frames?
Different Logical Operations In R
Functions In R
Loops In R
Read And Write External Data In R
Performing Statistical Calculations In R
Plotting In R
Object-Oriented Programming in R
Famous R Libraries
How To Install External R Libraries
Plotting In R
I explained the programming language R from the basics in a manner that would make the language easier to understand. Having said that, the key to advancing in programming is to always practice as much as possible.
Please let me know if you have any feedback or comments.