Module 1 Lecture Notes-2
Module 1 Lecture Notes-2
MODULE 1
1
Overview of Engineering Statistics and Data Analytics
Modern society is increasingly data-driven. Algorithms and the data they operate on now
control a vast and ever-increasing array of processes and systems. These data-intensive systems
rely on statistical methods to process the data that drive them and to provide actionable insights
to support effective decision making, design, manufacturing and engineering processes.
The tools generated by Engineering Statistics and Data Analytics find application in practically
all aspects of modern life including, but not limited to, Aviation and Aerospace Engineering,
Space Exploration, Banking and Financial Markets, Medicine and Healthcare, Robotics and
Automation, Quantum Mechanics and Applications as well as research and development in
science and engineering and other aspects of society.
In creating the innovative groundbreaking three-dimensional multilayer
electroencephalography (EEG) systems, also known as Ekpar Electroencephalography (Ekpar
EEG) systems with far-reaching implications for a broad range of fields from medicine to
computing and beyond, Professor Frank Edughom Ekpar relied in part on statistical methods
in correlation analysis and data visualization and reporting to demonstrate the effectiveness of
Ekpar EEG systems.
Subject A: Scatter plot of EEG data (in microvolts) for SE1-SE2 electrode pair.
Correlation coefficient, r = 0.2951.
Source: Frank Edughom Ekpar. A Novel Three-dimensional Multilayer
Electroencephalography Paradigm. Fortune Journal of Health Sciences, 7 (3), (2024): 466-
480. URL (FREE PDF): [Link]
[Link]
2
Topics To Be covered In The Entire Engineering Statistics And Data Analytics Course
3
Course Outline (Modules)
4
Module 6: Introduction to Data Analytics
1. Overview of data analytics in engineering
2. Introduction to big data analytics
3. Applications of cloud computing in data analytics
Mode of Delivery
• Lectures: Theoretical concepts of statistics, probability, and analytics
• Laboratory/Practical Sessions: Hands-on statistical computation and data analytics in R
• Tutorials & Problem-Solving Sessions: Worked examples and case studies
• Assignments & Group Projects: Application of concepts to engineering data
Assessment Methods
• Mid-Semester Examination (30%) – Covers substantially all key aspects of the course
• Final Examination (70%) – Comprehensive coverage of all modules
5
Introduction to the R Programming Language
What is R?
The R programming language is a cross-platform, open source and currently free programming
language suitable for statistical computing, graphics and data visualization. R is an interpreted
language.
Users can type in R source code at the prompt within the R Console and press ENTER to have
the source code interpreted and executed. Results are typically displayed within the R Console
or in a separate window as required.
6
Development can be simplified using the R Studio Integrated Development Environment (IDE)
available from: [Link]
The R Studio IDE combines the tools required to make the use of the R programming language
easier.
R Keywords
R keywords are special or reserved words or symbols with predefined meanings. Keywords
should not be used as names of variables, functions or other identifiers in R programming. Note
that since R is a case-sensitive language, variations in case can be utilized to create acceptable
identifiers that are similar to keywords, although this practice is discouraged. For example,
although TRUE is a keyword representing a logical constant, True or true could be used as a
variable name. However, to avoid confusion with keywords, the use of such variations is
discouraged.
7
if, else, repeat, while, for, in, next, break, function, return
TRUE, FALSE, NULL, Inf, NaN, NA
NA_integer_, NA_real_, NA_complex_, NA_character_
Variables
Variables are names or symbols used to store data, values and the results of expressions in R.
Variables are case-sensitive. They can start with alphanumeric characters and contain a mixture
of alphanumeric characters and numbers without spaces between them. Keywords cannot be
used as variables.
Examples
X <- 5
first_name <- "Ebiye"
last_name <- "Sekibo"
Here, the variable named X is assigned a value of 5. The assignment operator typically
utilized in R programming is <- (less minus) although the more common = can also be used.
The variable named first_name is assigned the value "Ebiye" while the variable named
last_name is assigned the value "Sekibo".
new_number <- 7
Z <- X + new_number
#Z equals 12
8
#The print() command can be used to print output to the R console.
print(full_name) #Prints "Ebiye Sekibo"
Comments In R
The # symbol is used to include comments in R programs in a single line of text. Information
supplied after the # sign is ignored and not used to evaluate the program. The # symbol can be
repeated on multiple lines to simulate multiline comments in R.
Data Types and Structures in R: Vectors, Matrices, Factors, Data Frames, Arrays and
Other R Collections (Lists)
Vectors
Vectors are used to represent a collection of data items of the same type (homogenous) in R.
The items themselves could be numeric, character or logical types.
Vectors are one-dimensional (1D) data structures in R. Vectors are defined using the c
command followed by paratheses or brackets containing a listing of the elements of the vector.
Examples:
my_vector <- c(8, 5, 7, 19, 25)
string_vector <- c("Banana", "Mango", "Udara", "Guava")
9
Accessing Vector Elements
Square brackets – [] – are used to access vector elements by index. The index of the desired
element is placed in the square braces appearing immediately after the variable name
representing the vector. Vector element indices start from 1, that is, R uses 1-based indexing.
my_vector[3] refers to the third element of the my_vector vector which has a value of 7.
string_vector[2] refers to the second element of the string_vector vector which has a value of
"Mango".
my_vector[3] <- 10 #Changes the third element of the my_vector vector to a value of 10.
Matrices
A matrix in R is a two-dimensional (2D) data structure with rows and columns that is used to
store items of the same type (homogenous). Matrices are created using the matrix command.
1 3 5
2 4 6
The vector contains the elements or data in the rows and columns of the matrix.
nrow represents the number of rows in the matrix while ncol represents the number of columns
in the matrix.
byrow is used to specify whether the matrix should be filled by row (TRUE) or by column
(FALSE). The default value of byrow is FALSE.
dimnames is a list that specifies the names of the rows and columns. It is optional.
10
Here, we change the value of the byrow argument to TRUE for the same matrix data given
above and note that the arrangement of elements in the matrix changes accordingly.
my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3,
byrow = TRUE)
1 2 3
4 5 6
Arrays
Arrays represent a generalization of matrices to more than two dimensions. All elements within
an array must be of the same data type.
my_array <- array(1:24, dim = c(2, 3, 4)) # A 2x3x4 array containing elements from 1 to 24.
Accessing elements within an R array is accomplished using square brackets [] and specifying
the indices for each dimension. R uses 1-based indexing. This means that the first element is at
index 1, the second element is at index 2, and so on.
11
Accessing A Specific Element.
To access the element in the first row, third column of the second matrix:
my_array <- array(1:24, dim = c(2, 3, 4)) # A 2x3x4 array containing elements from 1 to 24.
element <- my_array[1, 3, 2]
print(element) #Prints out 11
Factors
Factors are one-dimensional (1D) data structures used to store categorical data with predefined
levels. Internally, factors are stored as integers associated with labels.
gender_factor <- factor(c("Male", "Female", "Male", "Female"), levels = c("Male", "Female"))
Data Frames
In R, a data frame is similar to a table in a spreadsheet. A data frame can hold different types
of data across multiple columns. Each column in a data frame is essentially a vector, and all
columns must have the same number of rows.
my_dataframe above has 3 columns (Name, Language, and Age) each of which has 3 rows.
12
Accessing Elements In A Data Frame
Accessing Columns:
Using the dollar sign ($): This is a common and intuitive way to access a column by its name.
Using double square brackets [[]]: This also accesses a column by its name and returns it as
a vector.
Using single square brackets []: This can access a column by its index or name, but it returns
a data frame (even if it is a single column).
Accessing Rows:
Using single square brackets []: Here row indices can be specified before the comma within
the brackets.
13
# Example: Accessing a specific row (e.g., the third row)
my_dataframe[3, ]
Using logical conditions: Rows can be selected based on conditions applied to a column.
# Example: Accessing the element in the second row and third column
my_dataframe[2, 3]
my_r_list <- list("name" = "Ebiye Sekibo", "level" = 300, "age" = 25, "scores" = c(77, 85, 67,
95, 80))
The list above is designed to represent a student aged 25 years in 300 level with a set of scores
for a given semester and contains a string named name with a value of Ebiye Sekibo, a number
named level with a value of 300, a number named age with a value of 25 and a vector named
scores with the sequence: 77, 85, 67, 95 and 80.
14
Accessing Elements In R Lists
There are several methods for access to the elements in a list.
# Access and print the first element in the list given earlier
print(my_r_list[[1]]) #Prints "Ebiye Sekibo"
# Access and print the fourth element
print(my_r_list[[4]]) #Prints the vector: 77, 85, 67, 95, 80
# Access and print the element named "name" in the list given earlier
print(my_r_list$name) #Prints "Ebiye Sekibo"
# Access and print the element named "scores"
print(my_r_list$scores) #Prints the vector: 77, 85, 67, 95, 80
15
R As A Calculator – Basic Arithmetic
The order of arithmetic operations complies with the PEDMAS (Parenthesis, Exponents,
Division, Multiplication, Addition and Subtraction) convention.
7+6*5
results in 37 while
(7 + 6) * 5
results in 65
x <- 4
cube_of_x <- x ^ 3
print(cube_of_x) #Prints out 64
Iteration and looping or repetition are achieved by repeatedly executing a block of code. This
technique can be used to efficiently process multiple elements of a data structure or to perform
a given task a specified number of times.
for Loop
This can be used to iterate over the elements of a sequence such as a vector, list or data frame
or to repeat a block of code for a known number of iterations.
Examples:
data_vector <- c(1, 2, 3, 4, 5, 6, 7)
for(item in data_vector)
{
16
print(item)
}
while Loop in R
while Loop:
The while loop repeatedly executes a block of code as long as a specified condition remains
TRUE. The while loop can be used when the number of iterations is not known beforehand and
depends on a condition being met.
Examples:
counter <- 1
maximum_count <- 10
while (counter <= maximum_count)
{
print(paste("Current counter value: ", counter))
counter <- counter + 1
}
17
#Compute sum of first 10 non-zero whole numbers.
sum <- 0
current_number <- 1
last_number <- 10
while (current_number <= last_number)
{
sum <- sum + current_number
current_number <- current_number + 1
}
print(paste("Sum of first 10 non-zero whole numbers: ", sum))
repeat Loop:
The repeat loop executes a block of code indefinitely until an explicit break statement is
encountered within the loop body. The repeat loop is used when a loop needs to run at least
once and the termination condition is checked inside the loop.
Example:
x <- 1
max_x <- 20
repeat
{
print(x)
x <- x + 1
if (x >= max_x)
{
break
}
}
Conditional statements are used to control the flow of execution of a program in R. Conditional
statements in R programming allow for the execution of different code blocks based on whether
a specified condition evaluates to TRUE or FALSE, enabling decision-making within programs
and controlling the flow of execution.
if Statement:
The if statement executes a block of code only if the given condition is TRUE.
Example
x <- 20
if (x < 100)
{
print("x is less than 100")
}
18
Generic form of if statement:
if(condition_is_true)
{
code_block
}
if-else Statement:
The if-else statement provides an alternative code block to execute when the if condition is
FALSE.
y <- 45
if (y > 20)
{
print("y is greater than 20")
}
else
{
print("y is not greater than 20 ")
}
else if Ladder:
For multiple conditions, the else if ladder allows for checking subsequent conditions if the
preceding if or else if conditions are FALSE.
score <- 67
if (score >= 70)
{
print("Grade A")
}
else if (score >= 60)
{
print("Grade B")
}
else if(score >= 50)
{
print("Grade C")
}
else if(score >= 45)
{
print("Grade D")
}
else if(score >= 40)
{
print("Grade E")
}
else
{
19
print("Grade F")
}
switch() Statement:
The switch() statement is used when there is a need to select one of several code blocks to
execute based on the value of a single expression.
Generic Form:
switch(expression, case1, case2, case3, ...)
expression:
This is the value or variable that switch() will evaluate. It can be either a character string or a
number.
Key characteristics:
• No break statements: Unlike switch statements in some other languages, switch() in R
does not require break statements. Only the matching case is executed.
• No explicit default: There is no dedicated default case. However, an unnamed argument
can serve as a default if no other match is found when expression is a character string.
If expression is numeric and no match is found, NULL is returned.
• Return value: The switch() function returns the value of the selected case.
Examples:
choice <- 3
result <- switch(choice,
"First option",
20
"Second option",
"Third option"
)
print(result)
Functions are blocks of code that can enable efficient organization, maintenance and reuse of
code in R programs.
They are created as named entities using the function keyword followed by parentheses
(circular brackets) which may contain a list of parameters.
The function body is typically delineated by curly {} brackets and may contain any number of
statements.
Function names can start with alphanumeric characters and may contain a combination of
alphanumeric characters and numbers without spaces. R keywords cannot be used as function
names.
Additionally, R comes bundled with a set of built-in functions. Examples of in-built R functions
include, but are not limited to, the following:
21
o Functions for probability distributions (e.g., dnorm, pnorm, qnorm, rnorm for
normal distribution; dbinom, pbinom, qbinom, rbinom for binomial
distribution).
• Data Manipulation Functions:
o c(...): Combines values into a vector.
o factor(x): Creates a factor.
o sort(x), order(x): Sorts and orders data.
o unique(x): Returns unique elements.
o sample(x, size): Takes a random sample.
o apply(), lapply(), sapply(), tapply(), mapply(): Functions for applying
operations across data structures.
• String Manipulation Functions:
o nchar(x): Returns the number of characters in a string.
o toupper(x), tolower(x): Converts strings to uppercase or lowercase.
o paste(...): Concatenates strings.
o substring(x, first, last): Extracts substrings.
o grep(pattern, x), grepl(pattern, x): Pattern matching.
• Input/Output Functions:
o [Link](), [Link](): Reads data from files.
o [Link](), [Link](): Writes data to files.
A function may explicitly return a value or object (or multiple values or objects) using the
return keyword. If no explicit return statement is included, then the last expression within the
body of the function is returned.
Anatomy Of A Function In R
The parameters represent entities that can be passed to the function when it is called or invoked.
The parameter list can be empty and it can contain as many parameters as required. In the
xample above, function_name is the name of the function while parameter1, parameter2,…,
parameterN are parameters.
Default parameters are special parameters that are assigned default values in the function
definition and need not be supplied when the function is actually called or invoked, in which
case the default values are used within the function.
When a function is actually called or invoked, the values or objects passed to the function in
place of the parameters are called arguments.
22
The body of the function is typically enclosed within curly braces and can contain as many
statements as required to perform the task the function is designed to carry out.
Examples
Call or Invocation
Inline Functions
These are special functions for which the body is defined directly on a single line without curly
braces.
Functions may be extended within the context of object-oriented programming and in particular
class hierarchies related to inheritance in the R programming language.
23
The Apply Family Of Functions (apply, lapply, sapply, tapply, mapply)
With the apply family of functions, R offers an efficient and concise way to apply a function
to the elements of various data structures, such as matrices, data frames, and lists. This
approach often provides faster execution than traditional for loops. The Apply Family of
functions offers a powerful and efficient way to perform repetitive operations in R, especially
when working with larger datasets.
The main functions in this family include apply(), lapply(), sapply(), tapply(), and mapply().
sapply() : Similar to lapply(), but attempts to simplify the output to a vector or matrix if
possible.
24
my_list <- list(a = 1:3, b = 4:6, c = 7:9)
# Apply mean to each element, simplifying the output to a vector
vector_means <- sapply(my_list, mean)
print(vector_means)
tapply() : Applies a function to subsets of a vector, where the subsets are defined by factors.
25
# Print the result
print(result)
Output:
[1] 5 7 9
26
Data Visualization And Reporting In R
R can be used for data visualization to graphically present data in order to enable the acquisition
of actionable insights, a clearer understanding of relationships and to enhance data-driven
decision making.
Basic functions such as plot, barplot, pie, hist (for histograms), and so on, in R can be used for
data visualization and reporting. Additionally, the powerful ggplot2 package can be utilized,
permitting the construction of complex visualizations layer by layer, offering extensive control
over aesthetics, geometries, facets, and themes.
Here, the focus is on the use of the built-in R visualization and reporting features.
Bar Chart In R
Bar charts are used to visually display items in specific categories using bars. The bars can be
vertical or horizontal.
Output:
27
Custom Bar Chart
Custom bar charts can be created by supplying custom arguments to the barplot() function.
Output:
28
Pie Chart in R
Pie charts can be used for proportionate representation of items using slices in a circle.
Output:
Custom Pie Chart with separate colors and slices for the number of students in each of four (4)
groups.
29
Histogram In R Using In-built Data
Histograms can serve as graphical representations of the density of the underlying distribution
of data. The following example uses built-in data from the R programming language.
data(airquality)
30
Visualize World Map
R Source Code: (Acceptance of the list of countries presented and an Internet connection are
required to run this R program)
[Link]("maps")
library(maps)
map(database = "world")
Output:
31
Scatter Plot Using Two R Vectors
Scatter plots can be used to visually represent the correlations between two sets of data.
Example:
x_data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y_data <- c(2, 4, 5, 4, 6, 7, 8, 9, 10, 12)
Output:
32
xlim, ylim: Sets the limits for the x and y axes.
Code:
plot(x_data, y_data,
main = "My Custom Scatterplot",
xlab = "X-axis Label",
ylab = "Y-axis Label",
col = "darkred",
pch = 16, # Filled circles
cex = 1.5, # Larger points
xlim = c(0, 12),
ylim = c(0, 15))
Output:
33