Statistical Programming with R Course
Statistical Programming with R Course
On
Submitted to
CONTENTS
1 Syllabus 3-4
Lesson Plan
a) Schedule as per template
2 b) Course outcomes 5-9
c) CO-PO mapping
d) Course Projects (PBL)
a) Notes
3 10-111
b) Question bank
2
Hyderabad Institute of Technology and Management
Syllabus
Course Objective
1. Use R for statistical programming, computation, graphics, and modeling,
2. Write functions and use R in an efficient way
3. Be able to expand their knowledge of R on their own.
Course Outcomes:
At the end of the course student will be able to
Module-I Introduction, How to run R, R Sessions and Functions, Basic Math, Variables, Data Types, Vectors,
Conclusion, Advanced Data Structures, Data Frames, Lists, Matrices, Arrays, Classes.
Module–II R Programming Structures, Control Statements, Loops, - Looping Over Non vector Sets,- If-Else,
Arithmetic and Boolean Operators and values, Default Values for Argument, Return Values, Deciding Whether
to explicitly call return- Returning Complex Objects, Functions are Objective, No Pointers in R, Recursion, A
Quick sort Implementation-Extended Extended Example: A Binary Search Tree.
Module-III Doing Math and Simulation in R, Math Function, Extended Example Calculating Probability
Cumulative Sums and Products-Minima and Maxima- Calculus, Functions Fir Statistical Distribution, Sorting,
Linear Algebra Operation on Vectors and Matrices, Extended Example: Vector cross Product- Extended
Example: Finding Stationary Distribution of Markov Chains, Set Operation, Input /output, Accessing the
Keyboard and Monitor, Reading and writer Files.
Module-IV Graphics, Creating Graphs, The Workhorse of R Base Graphics, the plot() Function – Customizing
Graphs, Saving Graphs to Files.
Module–V Probability Distributions, Normal Distribution- Binomial Distribution- Poisson Distributions Other
Distribution, Basic Statistics, Correlation and Covariance, T-Tests,-ANOVA.
3
Text Books:
1) The Art of R Programming, A K Verma, Cengage Learning.
3) The Art of R Programming, Norman Matloff, No starch Press.
Reference Books:
1) R Cookbook, Paul Teetor, Oreilly.
2) R in Action, Rob Kabacoff, Manning
2) R for Everyone, Lander, Pearson
CO2 H H M M
CO3 M M
CO4 M L L L
Hyderabad Institute of Technology and Management
Gowdavelly (V), Medchal (D)
Department of CSE/CSM
[Link] Planne Topic Textbo No. Exec Pedagogy Topic Level Topic Level Outcomes Remark Sign of
d date ok Of u Keywords s Verifier
classes ted
UNIT-I
1 13-02- Introduction ,How to T1 3 Chalk & R, R Sessions, Understand the basics
2024 run R, R Sessions talk,Brainstor Functions of R, run R sessions,
and Functions. ming,PPT comprehend R
functions
2 14-02- LAB (1-3) Programs 2 PPT & Coding
2024
3 20-02- Basic Math, Variables, 3 Chalk & Basic Math, Understand basic
2024 Data Types, Vectors, talk,Brainstor Variables, Data mathematical
Conclusion. ming,Coding. Types, Vectors operations,
different data
types, and vectors
in R
4 21-02- LAB (4-6) Programs T1 2 PPT & Coding
2024
5 27-02- Advanced Data T1 3 Chalk & Advanced Data Understand advanced
2024 Structures, Data talk,Brainstor Structures, Data data structures like lists,
Frames, Lists, ming,Coding. Frames matrices, arrays, and
Matrices, Arrays, classes in R
Classes.
6 28-02- LAB(7-9) Programs T1 2 PPT & Coding
2024
15
UNIT-II
7 05-03- R Programming T1 3 Chalk & R Programming Understand
2024 Structures, Control talk,Brainstor Structures, programming structures
Statements, Loops, ming,Coding. Control and control flow in R,
Looping Over Statements, Loops utilize loops for efficient
Nonvector Sets. data manipulation
8 06-03- LAB(1-3) Programs T1 2 PPT & Coding
2024
9 12-03- If-Else, Arithmetic T1 3 Chalk & If-Else, Apply conditional
2024 and Boolean talk,Brainstor Arithmetic statements and
Operators and ming,Coding. Operators, arithmetic operations,
values, Default Boolean understand default
Values for Operators argument values in R
Argument.
10 13-03- LAB(4-5) T1 2 PPT &
2024 Programs Coding
11 19-03- Return Values, T1 3 Chalk & Return Values, Implement functions
2024 Recursion, talk,Brainstor Recursion with return values,
Deciding ming,Coding. utilize recursion
Whether to effectively, handle
explicitly call complex objects in R
return- Returning
Complex Objects.
12 20-03- LAB(6-8) Programs 2 PPT & Coding
2024
13 26-03- Functions are T2 3 Chalk & Functions, Understand functions
2024 Objective, No talk,Brainstor Pointers, Quick as objects, grasp the
Pointers in R, A ming,Coding. sort concept of pointers,
Quick sort Implementation implement sorting
Implementation- algorithms in R
Extended Example:
A Binary Search
Tree.
14 27-03- LAB(9-10) 2 PPT & Coding
2024 Programs
20
UNIT-III
UNIT-IV
24 07-05- Graphics, Creating 3 Chalk & Graphics, Generate graphics,
2024 Graphs, The talk,Brainstor Graph Creation create various types of
Workhorse of R. ming,Coding. graphs using R as a
tool for data
visualization
25 08-05- LAB(1-3) Programs 2 PPT & Coding
2024
26 14-05- Base Graphics, 3 Chalk & Base Graphics, Utilize base graphics
2024 the plot() Function. talk,Brainstor plot() Function functions, understand
ming,Coding. and use the plot()
function for creating
plots in R
27 15-05- LAB(4-6) Programs 2 PPT & Coding
2024
28 21-05- Saving Graphs to 3 Chalk & Saving Graphs, Save generated graphs
2024 Files, Customizing talk,Brainstor Graph to files, customize
Graphs. ming,Coding. Customization graphs according to
specific requirements
UNIT-V
17
Total No of Hours = 89
Course Outcomes:
After completion of this course the student is able to
TEXT BOOKS:
T1. The Art of R Programming, A K Verma, Cengage Learning.
REFERENCE BOOKS:
R1. R Cookbook, Paul Teetor, Oreilly.
R2. R in Action, Rob Kabacoff, Manning
R3. R for Everyone, Lander, Pearson
FACULTY HOD
Course Outcomes:
CO2 H H M M
CO3 M M
CO4 M L L L
Course Projects:
Module I : Introduction
The R environment
The term “environment” is intended to characterize it as a fully planned and coherent system,
rather than an incremental accretion of very specific and inflexible tools, as is frequently the
case with other data analysis software.
R is very much a vehicle for newly developing methods of interactive data analysis. It has
developed rapidly, and has been extended by a large collection of packages. However, most
programs written in R are essentially ephemeral, written for a single piece of data analysis.
History
First came S, the first statistical computing language that was a real computer language. It was
invented at Bell Labs by John Chambers and co-workers. Development started in 1975. It was
first distributed outside of Bell Labs in 1980. Before 1988, S did not have real functions (it had
macros to do the same job, but nowhere near as well). A version of S with real functions was
released in 1988.
S always was and still is proprietary software. It is not free as in free beer nor free as in free
speech. After purchasing it, one can only use it as the license allows. The name S is a one-letter
name like C (another language from Bell Labs).
In 1988 another company was founded to market S, with additions, as a proprietary product
called S-PLUS (or S+). After several changes of ownership, the current owner (TIBCO
Software) does not seem to be selling it any more, at least not as a product by itself.
Then came R, an implementation of a language very like S. It was invented at the University
of Auckland by Ross Ihaka and Robert Gentleman. The name R is a one-letter name like S but
is also for Ross and Robert. Development started in 1992. The first stable release was in 2000.
R is now developed and maintained by the “R Development Core Team.”
R is available as Free Software under the terms of the Free Software Foundation’s GNU General
Public License in source code form. It compiles and runs on a wide variety of UNIX platforms
and similar systems (including FreeBSD and Linux), Windows and MacOS.
In 2011 a company called RStudio provided a product of the same name that is an integrated
development environment for R (and they also provide other tools for use with R). RStudio is
free software.
R and statistics
Our introduction to the R environment did not mention statistics, yet many people use R as a
statistics system. We prefer to think of it of an environment within which many classical and
modern statistical techniques have been implemented. A few of these are built into the base R
environment, but many are supplied as packages. There are about 25 packages supplied with
R (called “standard” and “recommended” packages) and many more are available through the
CRAN family of Internet sites (via [Link] and elsewhere.
There is an important difference in philosophy between S (and hence R) and the other main
statistical systems. In S a statistical analysis is normally done as a series of steps, with
intermediate results being stored in objects. Thus whereas SAS and SPSS will give copious
output from a regression or discriminant analysis, R will give minimal output and store the
results in a fit object for subsequent interrogation by further R functions.
Why R?
R is the language of choice for statistical computing, at least among research statisticians and
new companies (start-ups and companies that were recently start-ups like Google, Facebook,
Amazon, and the like). The reasons are that R is a really developer friendly programming
language. If anyone anywhere in the world wants to make an R package to do some problem,
CRAN will make it available to everyone everywhere.
R does not make changes that break existing code (hardly ever). So such packages are easy to
maintain. There is nothing like CRAN for competing proprietary statistical computing
languages. Since R is free software, it is hackable by anyone who is a good programmer. It can
be made to do anything. So again, if anyone anywhere in the world has solved your problem,
there is likely a CRAN package to do what you want. Or if you are a company with great
engineers, they can make R do whatever the company needs.
In older companies in which statistics has been important for many years (drug companies, for
example) SAS may be what they use. They’ve been using it for decades and don’t want to
change. In other academic disciplines besides statistics, other computing languages may be
used for statistics (SAS, SPSS, Stata, for example), but R is displacing them more and more. The
reason is that there is so much available in R that is not available anywhere else. For example,
the R package aster ([Link] does many forms of life history
analysis that nothing else can do. So if hundreds of biologists want to analyze their data, they
have to learn R. The ones that become good at it then become R advocates. Similar stories
could be told about many kinds of statistical analysis that are available only in R.
You also hear Python, Perl, Julia, and other dynamic programming languages recommended
for statistics or “data science” but none of these languages actually have much real statistics
available, not even 1% of what is available on CRAN. So they are useful if the statistics being
done is very simple, but not otherwise.
R has an inbuilt help facility similar to the man facility of UNIX. To get more information on
any specific named function, for example solve, the command is
> help(solve)
An alternative is
> ?solve
For a feature specified by special characters, the argument must be enclosed in double or single
quotes, making it a “character string”: This is also necessary for a few words with syntactic
meaning including if, for and function.
> help("[[")
Either form of quote mark may be used to escape the other, as in the string "It’s important".
Our convention is to use double quote marks for preference. On most R installations help is
available in HTML format by running
> [Link]()
which will launch a Web browser that allows the help pages to be browsed with hyperlinks.
On UNIX, subsequent help requests are sent to the HTML-based help system. The ‘Search
Engine and Keywords’ link in the page loaded by [Link]() is particularly useful as it is
contains a high-level concept list which searches though available functions. It can be a great
way to get your bearings quickly and to understand the breadth of what R has to offer.
The [Link] command (alternatively ??) allows searching for help in various ways. For
example,
> ??solve
Try ?[Link] for details and more examples. The examples on a help topic can
normally be run by example(topic).
apropos() function searches for objects, including functions, directly accessible in the
current R session that have names that include a specified character string.
You can use the R console as a calculator where you just type in an expression you want
calculated, press Enter, and R gives you the result.
> 10+20 #Addition of two numbers
[1] 30
> sum(10,20) #Both performs same operation
[1] 30
> 100-99 #Subtraction of two numbers
[1] 1
> 19*2 #Multiplication of two numbers
[1] 38
> 23/3 #floating-point division
[1] 7.666667
> 23 %/% 3 #integer division
[1] 7
> 23 %% 3 #remainder of a division
[1] 2
> 2^3 #power of a number
[1] 8
> 2**3 #both performs same
[1] 8
Variable
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for R variables are:
• A variable name must start with a letter and can be a combination of letters, digits, period(.)
and underscore(_). If it starts with period(.), it cannot be followed by a digit.
• A variable name cannot start with a number or underscore (_)
• Variable names are case-sensitive (age, Age and AGE are three different variables)
• Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
A data type in a programming language is a set of data with predefined values. Examples of
data types are: integer, floating point, unit number, character, string, etc. In contrast to other
programming languages like C and java in R, the variables are not declared as some data type.
The variables are assigned with R-Objects and the data type of the R-object becomes the data
type of the variable. R has 5 basic data types: character, numeric, integer, complex, logical.
The frequently used Advanced Data Types/ Data Structures are – Vectors, Lists, Matrices,
Arrays, Factors, Data Frames.
Character
> A = "shanmukha"
> class(A)
[1] "character"
> class("Hello")
[1] "character"
Numbers(Numeric, Integer)
Numbers in R are generally treated as numeric objects (i.e. double precision real numbers).
This means that even if you see a number like “1” or “2” in R, which you might think of as
integers, they are likely represented behind the scenes as numeric objects (so something like
“1.00” or “2.00”). This isn’t important most of the time...except when it is.
If you explicitly want an integer, you need to specify the L suffix. So entering 1 in R gives you
a numeric object; entering 1L explicitly gives you an integer object.
> a = 17
> class(a)
[1] "numeric"
> a = 17L
> class(a)
[1] "integer"
Complex
Complex number is a combination of a real number and an imaginary number.
A complex number is written with an "i" as the imaginary part:
> x = 1 + 2i
> class(x)
[1] "complex"
Logical
In R logical/Boolean values are TRUE and FALSE. We can evaluate any expression in R, and
get one of two answers, TRUE or FALSE.
> class(TRUE)
[1] "logical"
Based on the discussion above, once we have data in variables, we need some mechanism for
manipulating that data lo solve problems. Data structure is a particular way of storing and
organizing data in a computer so that it can be used efficiently. A data structure is a special
format for organizing and storing data.
There many types advanced structures are available in programming languages. The frequently
used data structures in R are: Vector, Factor, List, Matrix, Array, Data Frame.
Vector
A vector is a collection same data elements. For example numeric vectors, character vectors
and logical vectors. The function c( ) is used to create an vector.
Factors
Factors are used to represent categorical data and can be unordered or ordered. One can think
of a factor as an integer vector where each integer has a label. Factors are important in
statistical modelling and are treated specially by modelling functions like lm( ) and glm( ).
Using factors with labels is better than using integers because factors are self-describing.
Having a variable that has values “Male” and “Female” is better than a variable that has values
1 and 2.
The first command creates a numeric vector pain, encoding the pain levels of five patients. We
wish to treat this as a categorical variable, so we create a factor fpain from it using the function
factor. This is called with one argument in addition to pain, namely levels = 0:3, which
indicates that the input coding uses the values 0–3. The latter can in principle be left out since
R by default uses the values in pain, suitably sorted, but it is a good habit to retain it; see below.
The effect of the final line is that the level names are changed to the four specified character
strings.
> fpain
[1] none severe medium medium mild
Levels: none mild medium severe
> [Link](fpain)
[1] 1 4 3 3 2
> levels(fpain)
[1] "none" "mild" "medium" "severe"
The function [Link]( ) extracts the numerical coding as numbers 1–4 and levels
extracts the names of the levels. Notice that the original input coding in terms of numbers 0–
3 has disappeared; the internal representation of a factor always uses numbers starting at 1.
R also allows you to create a special kind of factor in which the levels are ordered. This type
of variables are referred as ordinal variables. By including ordered = TRUE inside factor( ),
ordered factor variable can be created. This is also done using the ordered function, which
works similarly to factor.
Lists are a special type of vector that can contain elements of different classes. Lists are a very
important data type in R and you should get to know them well.
Lists can be explicitly created using the list() function, which takes an arbitrary number of
arguments.
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+4i
Matrices
A matrix in mathematics is just a two-dimensional array of numbers. Matrices are used for
many purposes in theoretical and practical statistics.
In R, the matrix notion is extended to elements of any type, so you could have, for instance, a
matrix of character strings. Matrices and arrays are represented as vectors with dimensions:
The dim assignment function sets or changes the dimension attribute of x, causing R to treat
the vector of 12 numbers as a 3 × 4 matrix. Notice that the storage is column-major; that is, the
elements of the first column are followed by those of the second, etc.
> matrix(1:12,nrow=3,byrow=T)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
Notice how the byrow=T switch causes the matrix to be filled in a row wise fashion rather
than column wise.
You can “glue” vectors together, column wise or row wise, using the cbind() and rbind()
functions.
Useful functions that operate on matrices include rownames, colnames, and the
transposition function t (notice the lowercase t as opposed to uppercase T for TRUE), which
turns rows into columns and vice versa:
The character vector LETTERS is a built-in variable that contains the capital letters A–Z.
Similar useful vectors are letters, [Link], and [Link] with lowercase letters,
month names, and abbreviated month names.
Operations on matrix
> A = matrix(c(2, 4, 6, 8), byrow = TRUE, nrow = 2)
> B = matrix(c(1, 3, 5, 7), byrow = TRUE, nrow = 2)
> A + B #Matrix addition
[,1] [,2]
[1,] 3 7
[2,] 11 15
> A – B #Matrix Subtraction
[,1] [,2]
[1,] 1 1
[2,] 1 1
> A * B #Hadamard product/Element wise product
[,1] [,2]
[1,] 2 12
[2,] 30 56
The actual matrix multiplication is dot product, which is performed using %*% operator from
dplyr package.
> library(dplyr)
> A %*% B #Matrix multiplication/Dot product
[,1] [,2]
[1,] 22 34
[2,] 46 74
> det(A) #Determinant of matrix
[1] -8
> solve(A) #Inverse of matrix A
[,1] [,2]
[1,] -1.00 0.50
[2,] 0.75 -0.25
R Array
An Array is a data structure which can store data of the same type in more than two
dimensions.
> temperatures <- c(25, 28, 30, 26, 27, 29, 31, 32, 33, 24, 26,
27, 28, 29, 30, 32, 30, 28, 29, 31, 27, 26, 25, 24)
> temp_array <- array(data = temperatures, dim = c(3, 4, 2))
> temp_array
, , 1
, , 2
Data Frame
A Data Frame is a data structure that organizes data into a 2-dimensional table of rows and
columns, much like a spreadsheet.
Data frames are represented as a special type of list where every element of the list has to have
the same length. Each element of the list can be thought of as a column and the length of each
element of the list is the number of rows.
Unlike matrices, data frames can store different classes of objects in each column. Matrices
must have every element be the same class (e.g. all integers or all numeric).
In addition to column names, indicating the names of the variables or predictors, data frames
have a special attribute called [Link] which indicate information about each row of the
data frame.
Class
R possesses a simple generic function mechanism which can be used for an object-oriented
style of programming. Method dispatch takes place based on the class of the first argument to
the generic function. The function class prints the vector of names of classes an object inherits
from. Correspondingly, class<- sets the classes an object inherits from. Assigning an empty
character vector or NULL removes the class attribute,
What is Loop?
Loop helps you to repeat the similar operation on different variables or on different columns
or on different datasets. For example, you want to multiple each variable by 5. Instead of
multiply each variable one by one, you can perform this task in loop. Its main benefit is to
bring down the duplication in your code which helps to make changes later in the code.
• For Loop
• While Loop
• Apply Family of Functions such as apply, lapply, sapply, etc.
In R, there are several ways to perform repetitive tasks without using traditional for loops.
These alternatives are particularly useful when dealing with large datasets or when working
interactively on a command line. Here are some of the methods commonly used for looping
over non-vector sets in R:
They are the hidden loops in R. They make loops easier to read and write. But these concepts
are very new to the programming world as compared to For Loop and While Loop.
1. apply( )
It is used when we want to apply a function to the rows or columns of a matrix or data frame.
It cannot be applied on lists or vectors.
Motor Trend Car Road Tests: It is a data frame with 32 observations on 11 (numeric)
variables.
In the second parameter of apply function, 2 denotes the function to be applied at column
level.
In the second parameter of apply function, 1 denotes the function to be applied at row level.
2. lapply( )
When we apply a function to each element of a data structure and it returns a list.
lapply(X, FUN)
The function(x) is used to define the function we want to apply. The [Link]=TRUE is
used to ignore missing values and median would now be calculated on non-missing values.
This gives a list with medians of all the variables in mtcars.
$mpg
[1] 19.2
$cyl
[1] 6
$disp
[1] 196.3
$hp
[1] 123
$drat
[1] 3.695
$wt
[1] 3.325
$qsec
[1] 17.71
$vs
[1] 0
$am
[1] 0
$gear
[1] 4
$carb
[1] 2
In this case, we are subtracting mean of each variable from its each value of variables and the
final output would be a list and output is shown below.
$mpg
[1] 0.909375 0.909375 2.709375 1.309375 -1.390625 -1.990625
-5.790625 4.309375 2.709375 -0.890625 -2.290625 -3.690625
[13] -2.790625 -4.890625 -9.690625 -9.690625 -5.390625 12.309375
10.309375 13.809375 1.409375 -4.590625 -4.890625 -6.790625
[25] -0.890625 7.209375 5.909375 10.309375 -4.290625 -0.390625
-5.090625 1.309375
$cyl
[1] -0.1875 -0.1875 -2.1875 -0.1875 1.8125 -0.1875 1.8125 -
2.1875 -2.1875 -0.1875 -0.1875 1.8125 1.8125 1.8125 1.8125
[16] 1.8125 1.8125 -2.1875 -2.1875 -2.1875 -2.1875 1.8125
1.8125 1.8125 1.8125 -2.1875 -2.1875 -2.1875 1.8125 -0.1875
[31] 1.8125 -2.1875
Note : The complete output is not listed here since output is long.
3. sapply( )
sapply() is a user friendly version of lapply() as it returns a vector when we apply a function to
each element of a data structure.
The basic syntax for the sapply() function is as follows:
sapply(X, FUN)
The above function returns 0 for all variables in data frame mtcars.
> colnames(iris)[which(sapply(iris,[Link]))]
[1] "[Link]" "[Link]" "[Link]" "[Link]"
In this example, sapply(iris, [Link]) returns TRUE/FALSE against each variable. If the
variable is numeric, it would return TRUE otherwise FALSE. Later, which function returns the column
position of the numeric variables . Try running only this portion of the code
which(sapply(iris,[Link])). Adding colnames function would help to return the
actual names of the numeric variables.
In this example, we would show you how both lapply() and sapply() are used simultaneously to
solve the problem.
The following code would convert all the factor variables of data frame iris to numeric types
variables.
Explanation :
index would return TRUE / FALSE whether the variable is factor or not. Converting only those
variables wherein index=TRUE.
4. tapply( )
The tapply() function in R can be used to apply some function to a vector, grouped by another vector.
5. mapply( )
The mapply( ) function in R can be used to apply a function to multiple list or vector arguments.
The following code shows how to use mapply() to create a matrix by repeating the values c(1, 2, 3)
each 5 times:
The following code shows how to use mapply() to find the max value for corresponding elements in
two vectors:
[1] 2 4 3 4 10
The max value of the elements in position 1 of either vector is 2. The max value of the elements in
position 2 of either vector is 4. The max value of the elements in position 3 of either vector is 3. And
so on.
The following code shows how to use mapply() to find multiply the corresponding elements in
several vectors:
The product of the elements in position 1 of each vector is 1 * 2 * 3 = 6. The product of the
elements in position 2 of each vector is 2 * 4 * 6 = 48. And so on.
6. For Loop
Like apply family of functions, For loop is used to repeat the same task on multiple data
elements or datasets. It is similar to For loop in other languages such as Python, Java etc. This
concept is not new and it has been in the programming field over many years.
> x = NULL
> for (i in 1:ncol(mtcars)){
x[i]= max(mtcars[i], [Link] = TRUE)}
> x
Prior to starting a loop, we need to make sure we create an empty vector. The empty vector is
defined by x=NULL. Next step is to define the number of columns for which loop over would
be executed. It is done with ncol() function. The length() function could also be used to
know the number of column.
The above For loop program can be written like the code below -
The vector function can be used to create an empty vector. The seq_along finds out what to
loop over.
The program below creates multiple data frames based on the number of unique values in
variable Species in IRIS dataset.
> require(dplyr)
> for (i in 1:length(unique(iris$Species))) {
> assign(paste("iris" ,i , sep = "."), filter(iris, Species ==
[Link](unique(iris$Species)[i])))
}
It returns three data frames named iris.1 iris.2 iris.3.
[Link]() applies a given function to the list as a whole. When it is used with rbind(), it
would bind all the list arguments. In other words, it converts list to matrix of multiple rows.
In this case, we are first creating an empty table (data frame). Later we are appending data to
empty data frame.
Suppose you are asked to impute Missing Values with Median in each of the numeric variable
in a data frame. It's become a daunting task if you don't know how to write a loop. Otherwise,
it's a straightforward task.
In the program below, which(sapply(dat, [Link])) makes sure loop runs only
on numeric variables.
Suppose you need to standardise multiple variables. To accomplish this task, we need to
execute the following steps -
Calculate Z-score i.e. subtracting mean from original values and then divide it by standard
deviation of the raw variable.
Standardize Variables
> lst=list()
> for (i in which(sapply(mydata, [Link]))) {
[Link] = (mydata[,i] - mean(mydata[,i])) /sd(mydata[,i])
lst[[i]] = [Link]
}
In this case, [Link]() with cbind() function helps to make data in matrix form from list.
7. While Loop in R
A while loop is more broader than a for loop because you can rescript any for loop as a while
loop but not vice-versa.
The double percent sign (%%) indicates mod. Read i%%2 as mod(i,2). The iteration would
start from 1 to 6 (i.e. i<7). It stops when condition is met.
Output:
Break Keyword
When a loop encounters 'break' it stops the iteration and breaks out of loop.
Next Keyword
When a loop encounters 'next', it terminates the current iteration and moves to next iteration.
for (i in 1:3) {
for (j in 3:1) {
if ((i+j) > 4) {
next
} else {
print(paste("i=", i, "j=", j))
}
}
}
Output :
If you get confused between 'break' and 'next', compare the output of both and see the
difference.
R if else conditions
ifelse()
The ifelse() function in base R can be used to write quick if-else statements. This function uses
the following syntax:
Example 1 : Create a new column, if mpg is more than 20 then “high” else “low”.
if-else statement
This style of writing If-Else is mostly used when we use conditional statements in loop and R
functions. In other words, it is used when we need to perform various actions based on a
condition. This function uses the following syntax:
if(test) {
yes
}
else {
no
Below are the main differences between the ifelse() and the if-else statement in R.
Usage: ifelse() function is commonly used when applying a condition to an entire vector or
column of data. whereas, if-else statement is typically used when dealing with more complex
conditional logic.
Vectorized vs Scalar: The ifelse() function can work with entire vectors or columns of data at
once, while the if-else statement operates on individual values.
Output Length: The ifelse() function returns a vector with the same length as the input. On
the other hand, the if-else statement returns a single value or executes blocks of code.
R Operators
Operators are used in R to perform various operations on variables and values. Among the
most commonly used ones are arithmetic and assignment operators.
Arithmetic operators
In R, standard mathematical rules apply throughout and follow the usual left-to-right order of
operations: parentheses, exponents, multiplication, division, addition, subtraction (PEMDAS).
R supports the following arithmetic operators:
Comparison operators are used to compare two variables and values, and it returns Boolean
values TRUE or FALSE. R has the following comparison operators:
Logical operators are used to combine conditional statements. R has the following logical
operators:
• Boolean AND for vectors, &, for comparing each element and returning TRUE if both
elements are TRUE.
• Boolean AND for scalars, &&, which returns TRUE if both values are TRUE, only evaluates
as many elements as necessary.
• Boolean OR for vectors, |, for comparing each element and returning TRUE if either
element is TRUE.
• Boolean OR for scalars, ||, which returns TRUE if either value is TRUE, only evaluates as
many elements as necessary.
• Logical NOT, !, which returns TRUE if the associated statement is FALSE.
Note: The long form of AND and OR (&& and ||) are preferred for if statements as the short
form can produce a vector value.
Miscellaneous operators
• The : operator creates a sequence of numbers from the left argument to the right one.
• The %in% operator returns TRUE if the left argument is in the vector to the right.
• The %*% operator performs matrix multiplication on two matrices.
Boolean values
The Boolean values TRUE and FALSE can be abbreviated as T and F (both must be capitalized).
These values change to 1 and 0 in arithmetic expressions:
> 1 < 2
[1] TRUE
> (1 < 2) * (3 < 4)
[1] 1
> (1 < 2) * (3 < 4) * (5 < 1)
[1] 0
> (1 < 2) == TRUE
[1] TRUE
> (1 < 2) == 1
[1] TRUE
In the second computation, for instance, the comparison 1 < 2 returns TRUE, and 3 < 4 returns
TRUE as well. Both values are treated as 1 values, so the product is 1.
Now consider the function head(). We know that, by default the head() gives first 6
records from the data frame.
Since n is set to 10, it returns first 10 records from the mtcars data frame. This is an example
of the use of named arguments.
Where x is an object of data frame, vector, etc.. n is the number of elements should be returned.
The n = 6L field means that this argument is optional, and if we don’t specify it, the default
value will be 6. If we don’t want the default value, we must name the argument in our call: >
head(mtcars, n=10).
Return values
The return value of a function can be any R object. Although the return value is often a list, it
could even be another function.
You can transmit a value back to the caller by explicitly calling return(). Without this call, the
value of the last executed statement will be returned by default. For instance, consider the
oddcount() example.
oddcount <- function(x) {
k <- 0
for(n in x) {
if(n%%2 == 1)k <- k+1
}
return(k)
}
This function returns the count of odd numbers in the argument. We could slightly simplify
the code by eliminating the call to return(). To do this, we evaluate the expression to be
returned, k, as our last statement in the code:
The prevailing R idiom is to avoid explicit calls to return(). One of the reasons cited for this
approach is that calling that function lengthens execution time. However, unless the function
is very short, the time saved is negligible, so this might not be the most compelling reason to
refrain from using return(). But it usually isn’t needed nonetheless.
Here, we simply ended with a statement listing the expression to be returned—in this case, k.
A call to return() wasn’t necessary.
Good software design, however, should be mean that you can glance through a function’s code
and immediately spot the various points at which control is returned to the caller. The easiest
way to accomplish this is to use an explicit return() call in all lines in the middle of the
code that cause a return. (You can still omit a return() call at the end of the function if you
wish.)
Returning complex objects
There are situations where we need to return complex objects(not simple number or strings).
These can be added to a list and can be returned back to calling function. If your function has
multiple return values, place them in a list or other container.
In the below example, we are going to return both matrix and a vector through return.
M <- function()
{
V <- c(1,2,5,3,8)
m <- matrix(1:8, ncol = 4)
v1 <- mean(v)
m1 <- min(m)
L <- list(vec = v1, mat = m1)
L
}
> v
[1] 1 3 7 2 6
In the same way, R treats functions as objects. So, if we type the function name, the code inside
the function is displayed.
> M
function()
{
v<-c(1,2,5,3,8)
m<-matrix(1:8,ncol=4)
v1<-mean(v)
m1<-min(m)
L<-list(vec=v1,mat=m1)
L
}
And functions can be assigned to another functions too. As like assigning a value to a variable
objects.
No Pointers in R
R does not have variables corresponding to pointers or references like those of, say, the C
language. This can make programming more difficult in some cases.
For example, you cannot write a function that directly changes its arguments. In Python, for
instance, you can do this:
>>> x = [13,5,12]
>>> [Link]()
>>> x
[5, 12, 13]
Here, the value of x, the argument to sort(), changed. By contrast, here’s how it works in R:
The argument to sort() does not change. If we do want x to change in this R code, the
solution is to reassign the arguments:
Recursion is a concept that occurs when the definition of a concept or process depends on a
simpler version of itself. It is used in various disciplines, including mathematics and computer
science. In programming, recursion refers to the technique of defining a function that calls
itself within its own definition. This allows for the solution of complex problems by breaking
them down into smaller, more manageable subproblems. Simply recursion is a recursive
function calls itself.
Recursive functions call themselves. That is, they break down the problem into the smallest
possible components and the function() calls itself within the original function() on each of
the smaller components. Afterward, the results are put together to solve the original problem.
A Quicksort Implementation
A classic example is Quicksort, an algorithm used to sort a vector of numbers from smallest to
largest. For instance, suppose we wish to sort the vector (5,4,12,13,3,8,88). We first compare
everything to the first element, 5, to form two sub-vectors: one consisting of the elements less
than 5 and the other consisting of the elements greater than or equal to 5. That gives us sub-
vectors (4,3) and (12,13,8,88). We then call the function on the sub-vectors, returning (3,4)
and (8,12,13,88). We string those together with the 5, yielding (3,4,5,8,12,13,88), as desired.
R’s vector-filtering capability and its c() function make implementation of Quicksort quite
easy
Note : This example is for the purpose of demonstrating recursion. R’s own sort function,
sort().
Without this, the function would keep calling itself repeatedly on empty vectors, executing
forever.
Treelike data structures are common in both computer science and statis- tics. In R, for
example, the rpart library for a recursive partitioning approach to regression and classification
is very popular. Trees obviously have applications in genealogy, and more generally, graphs
form the basis of analysis of social networks.
However, there are real issues with tree structures in R, many of them related to the fact that
R does not have pointer-style references. Yet trees can be implemented in R itself, and if
performance is not an issue, using this approach may be more convenient.
For the sake of simplicity, our example here will be a binary search tree, a classic computer
science data structure that has the following property:
In each node of the tree, the value at the left link, if any, is less than or equal to that of
the parent, while the value at the right link, if any, is greater than that of the parent.
5 20
2 6
We’ve stored 8 in the root—that is, the head of the tree. Its two child nodes contain 5 and 20,
and the former itself has two child nodes, which store 2 and 6.
Note that the nature of binary search trees implies that at any node,
all of the elements in the node’s left subtree are less than or equal to the value stored in this
node, while the right subtree stores the elements that are larger than the value in this mode.
In our example tree, where the root node contains 8, all of the values in the left subtree—5, 2
and 6—are less than 8, while 20 is greater than 8.
Specifically, we’ll represent each node by a row in a three-column matrix. The node’s stored
value will be in the third element of that row, while the first and second elements will be the
left and right links. For instance, if the first element in a row is 29, it means that this node’s
left link points to the node stored in row 29 of the matrix.
NOTE : If you anticipate that the matrix will become quite large, you may wish to double its
size at each allocation, rather than grow it linearly as we have here. This would further reduce
the number of time-consuming disruptions.
Before discussing the code, let’s run through a quick session of tree building using its routines.
> x
$mat
[,1] [,2] [,3]
[1,] NA NA 8
[2,] NA NA NA
[3,] NA NA NA
$nxt
[1] 2
$inc
[1] 3
We then make the call ins(1,x,5) to insert a second value, 5, into the tree x. The argument 1
specifies the root. In other words, the call says, “Insert 5 in the subtree of x whose root is in
row 1.” Note that we need to reassign the return value of this call back to x. Again, this is due
to the lack of pointer variables in R. The matrix now looks like this:
> x
$mat
[,1] [,2] [,3]
[1,] 2 NA 8
[2,] NA NA 5
[3,] NA NA NA
$nxt
[1] 3
$inc
[1] 3
The element 2 means that the left link out of the node containing 8 is meant to point to row
2, where our new element 5 is stored.
$nxt
[1] 4
$inc
[1] 3
$nxt
[1] 5
$inc
[1] 3
> x
$mat
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 4 3 5
[3,] NA NA 6
[4,] NA NA 2
[5,] NA NA 20
[6,] NA NA NA
$nxt
[1] 6
$inc
[1] 3
# routines to create trees and insert items into them are included
below;
# a deletion routine is left to the reader as an exercise
# storage is in a matrix, say m, one row per node of the tree; if
row
# i contains (u,v,w), then node i stores the value w, and has left
and
# right links to rows u and v; null links have the value NA
There is recursion in both printtree() and ins(). The former is definitely the easier of
the two, so let’s look at that first. It prints out the tree, in sorted order.
Recall our description of a recursive function f() that solves a problem of category X: We have
f() split the original X problem into one or more smaller X problems, call f() on them, and
combine the results. In this case, our problem’s category X is to print a tree, which could be a
subtree of a larger one. The role of the function on line 13 is to print the given tree, which it
does by calling itself in lines 15 and 18. There, it prints first the left subtree and then the right
subtree, pausing in between to print the root.
This thinking—print the left subtree, then the root, then the right subtree—forms the
intuition in writing the code, but again we must make sure to have a proper termination
mechanism. This mechanism is seen in the if() statements in lines 15 and 18. When we come
to a null link, we do not continue to recurse.
The recursion in ins() follows the same principles but is considerably more delicate. Here,
our “category X” is an insertion of a value into a sub- tree. We start at the root of a tree,
determine whether our new value must go into the left or right subtree (line 33), and then call
the function again on that subtree. Again, this is not hard in principle, but a number of details
must be attended to, including the expansion of the matrix if we run out of room (lines 40–
41).
One difference between the recursive code in printtree() and ins() is that the former
includes two calls to itself, while the latter has only one. This implies that it may not be
difficult to write the latter in a non-recursive form.
As our first example, we’ll work through calculating a probability using the prod() function.
Suppose we have n independent events, and the ith event has the probability pi of occurring.
What is the probability of exactly one of these events occurring?
Suppose first that n = 3 and our events are named A, B, and C. Then we break down the
computation as follows:
P(exactly one event occurs) = P(A and not B and not C) + P(not A and B and not C) + P(not A
and not B and C)
P(A and not B and not C) would be 𝑝𝐴 (1 − 𝑝𝐵 )(1 − 𝑝𝐶 ), and so on. For general n, that is
calculated as follows:
𝑛
(The ith term inside the sum is the probability that event i occurs and all the others do not
occur.)
Here’s code to compute this, with our probabilities 𝑝𝑖 contained in the vector 𝑝:
creates a vector of all the “not occur” probabilities 1 − pj , using recycling. The expression
notp[-i] computes the product of all the elements of notp, except the ith—exactly what we
need.
In x, the sum of the first element is 12, the sum of the first two elements is 17, and the sum of
the first three elements is 30.
The function cumprod() works the same way as cumsum(), but with the product instead
of the sum.
There is quite a difference between min() and pmin(). The former simply combines all its
arguments into one long vector and returns the minimum value in that vector. In contrast, if
pmin() is applied to two or more vectors, it returns a vector of the pair-wise minima, hence
the name pmin.
Here’s an example:
> z
[,1] [,2]
[1,] 1 2
[2,] 5 3
[3,] 6 2
> min(z[,1],z[,2])
[1] 1
> pmin(z[,1],z[,2])
[1] 1 3 2
In the first case, min() computed the smallest value in (1,5,6,2,3,2). But the call to pmin()
computed the smaller of 1 and 2, yielding 1; then the smaller of 5 and 3, which is 3; then finally
the minimum of 6 and 2, giving 2. Thus, the call returned the vector (1, 3, 2).
You can use more than two arguments in pmin(), like this:
> pmin(z[1,],z[2,],z[3,])
[1] 1 2
The 1 in the output is the minimum of 1, 5, and 6, with a similar computation leading to the
2.
The max() and pmax() functions act analogously to min() and pmin().
Function minimization/maximization can be done via nlm() and optim(). For example,
let’s find the smallest value of f (x) = x2 − sin(x).
Here, the minimum value was found to be approximately −0.23, occurring at x = 0.45. A
Newton-Raphson method (a technique from numerical analysis for approximating roots) is
used, running five iterations in this case. The second argument specifies the initial guess, which
we set to be 8. (This second argument was picked pretty arbitrarily here, but in some problems,
you may need to experiment to find a value that will lead to convergence.)
In R, the nlm() and optim() functions are commonly used for optimization, including
finding the minimum of a function. These functions are part of the base R package and provide
different optimization algorithms.
nlm() : The nlm() function is used for non-linear minimization. It implements a Newton-
type algorithm for optimization. The function takes as input the objective function to be
minimized and an initial parameter value. It returns the optimized parameter values that
minimize the objective function.
f <- function(x) {
return(x^2 + 3*x + 2)
}
> nlm(f, p = 0)
$minimum
[1] -0.25
$estimate
[1] -1.5
$gradient
[1] 0
$code
[1] 1
$iterations
[1] 2
The output will be -1.5, which is the value that minimizes the function.
$value
[1] -0.24
$counts
function gradient
12 NA
$convergence
[1] 0
$message
NULL
The output will be -1.4, which is the value that minimizes the function.
It's important to note that the choice between nlm() and optim() depends on the specific
optimization problem and the characteristics of the objective function. The nlm() function is
often used for local optimization of smooth functions, while optim() provides more
flexibility and a wider range of optimization algorithms.
Calculus
R also has some calculus capabilities, including symbolic differentiation and numerical
integration, as you can see in the following example.
Here, R reported
𝑑 𝑥2 2
𝑒 = 2𝑥𝑒 𝑥
𝑑𝑥
and
1
∫ 𝑥 2 𝑑𝑥 ≈ 0.333333
0
Simulation is an important (and big) topic for both statistics and for a variety of other areas
where there is a need to introduce randomness. Sometimes you want to implement a statistical
procedure that requires random number generation or sample (i.e. Markov chain Monte Carlo,
the bootstrap, random forests, bagging) and sometimes you want to simulate a system and
random number generators can be used to model random inputs.
R comes with a set of pseudo-random number generators that allow you to simulate from well-
known probability distributions like the Normal, Poisson, and binomial.
R has functions available for most of the famous statistical distributions. Prefix the name as
follows:
As an example, let’s simulate 1,000 chi-square variates with 2 degrees of freedom and find their
mean.
> mean(rchisq(1000,df=2))
[1] 1.938179
The r in rchisq specifies that we wish to generate random numbers— in this case, from the
chi-square distribution. As seen in this example, the first argument in the r-series functions is
the number of random variates to generate.
These functions also have arguments specific to the given distribution families. In our example,
we use the df argument for the chi-square family, indicating the number of degrees of freedom.
Let’s also compute the 95th percentile of the chi-square distribution with two degrees of
freedom:
> qchisq(0.95,2)
[1] 5.991465
Here, we used q to indicate quantile—in this case, the 0.95 quantile, or the 95th percentile.
The first argument in the d, p, and q series is actually a vector so that we can evaluate the
pdf/pmf, cdf, or quantile function at multiple points. Let’s find both the 50th and 95th
percentiles of the chi-square distribution with 2 degrees of freedom.
> qchisq(c(0.5,0.95),df=2)
[1] 1.386294 5.991465
Sorting
Ordinary numerical sorting of a vector can be done with the sort() function, as in this example:
Note that x itself did not change, in keeping with R’s functional language philosophy.
If you want the indices of the sorted values in the original vector, use the order() function.
Here’s an example:
> order(x)
[1] 2 4 3 1
This means that x[2] is the smallest value in x, x[4] is the second smallest, x[3] is the
third smallest, and so on.
You can use order(), together with indexing, to sort data frames, like this:
>y
V1 V2
1 def 2
2 ab 5
3 zzzz 1
> r <- order(y$V2)
> r
[1] 3 1 2
> z <- y[r,] >z
V1 V2
3 zzzz 1
1 def 2
2 ab 5
You can use order() to sort according to character variables as well as numeric ones, as
follows:
> d
kids ages
1 Jack 12
2 Jill 10
3 Billy 13
> d[order(d$kids),]
kids ages
3 Billy 13
1 Jack 12
2 Jill 10
> d[order(d$ages),]
kids ages
2 Jill 10
1 Jack 12
3 Billy 13
A related function is rank(), which reports the rank of each element of a vector.
Multiplying a vector by a scalar works directly, as you saw earlier. Here’s an- other example:
> y
[1] 1 3 410
> 2*y
[1] 2 6 820
If you wish to compute the inner product (or dot product) of two vectors, use crossprod(),
like this:
> crossprod(1:3,c(5,12,13))
[,1]
[1,] 68
Note that the name crossprod() is a misnomer, as the function does not compute the vector
cross product.
For matrix multiplication in the mathematical sense, the operator to use is %*%, not *. For
instance, here we compute the matrix product:
1 2 1 −1 1 1
[ ][ ]= [ ]
3 4 0 1 3 1
> a
[,1] [,2]
[1,] 1 2
[2,] 3 4
> b
[,1] [,2]
[1,] 1 -1
[2,] 0 1
> a %*% b
[,1] [,2]
[1,] 1 1
[2,] 3 1
The function solve() will solve systems of linear equations and even find matrix inverses.
For example, let’s solve this system:
𝑥1 + 𝑥2 = 2
−𝑥1 + 𝑥2 = 4
Its matrix form is as follows:
1 1 𝑥1 2
( )( ) = ( )
−1 1 𝑥2 4
In that second call to solve(), the lack of a second argument signifies that we simply wish
to compute the inverse of the matrix.
Note the versatile nature of diag(): If its argument is a matrix, it returns a vector, and vice
versa. Also, if the argument is a scalar, the function returns the identity matrix of the specified
size.
> m
[,1] [,2]
[1,] 1 2
[2,] 7 8
> dm <- diag(m)
> dm
[1] 1 8
> diag(dm)
[,1] [,2]
[1,] 1 0
[2,] 0 8
> diag(3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
The sweep() function is capable of fairly complex operations. As a simple example, let’s take
a 3-by-3 matrix and add 1 to row 1, 4 to row 2, and 7 to row 3.
> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> sweep(m,1,c(1,4,7),"+")
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 8 9 10
[3,] 14 15 16
The first two arguments to sweep() are like those of apply(): the array and the margin,
which is 1 for rows in this case. The fourth argument is a function to be applied, and the third
is an argument to that function (to the "+" function).
Let’s consider the issue of vector cross products. The definition is very simple: The cross
product of vectors (𝑥1 , 𝑥2 , 𝑥3 ) and (𝑦1 , 𝑦2 , 𝑦3 ) in three-dimensional space is a new three-
dimensional vector.
(𝑥2 𝑦3 − 𝑥3 𝑦2 , −𝑥1 𝑦3 + 𝑥3 𝑦1 , 𝑥1 𝑦2 − 𝑥2 𝑦1 )
This can be expressed compactly as the expansion along the top row of the determinant, as
shown
_ _ _
(𝑥1 𝑥2 𝑥3 )
𝑦1 𝑦2 𝑦3
Here, the elements in the top row are merely placeholders.
Don’t worry about this bit of pseudo math. The point is that the cross product vector can be
computed as a sum of sub determinants. For instance, the first component in equation 𝑥2 𝑦3 −
𝑥3 𝑦2 is easily seen to be the determinant of the submatrix obtained by deleting the first row
and first column.
𝑥2 𝑥3
(𝑦 𝑦3 )
2
Note that even R’s ability to specify values as NA came into play here to deal with the
“placeholders” mentioned above.
As a simple example, consider a game in which we toss a coin repeatedly and win a dollar
whenever we accumulate three consecutive heads. Our state at any time i will the number of
consecutive heads we have so far, so our state can be 0, 1, or 2. (When we get three heads in a
row, our state reverts to 0.)
The central interest in Markov modelling is usually the long-run state distribution, meaning
the long-run proportions of the time we are in each state. In our coin-toss game, we can use
the code we’ll develop here to calculate that distribution, which turns out to have us at states
0, 1, and 2 in pro- portions 57.1%, 28.6%, and 14.3% of the time. Note that we win our dollar
if we are in state 2 and toss a head, so 0.143 × 0.5 = 0.071 of our tosses will result in wins.
Since R vector and matrix indices start at 1 rather than 0, it will be convenient to relabel our
states here as 1, 2, and 3 rather than 0, 1, and 2. For example, state 3 now means that we
currently have two consecutive heads.
Let pij denote the transition probability of moving from state i to state j during a time step. In
the game example, for instance, p23 = 0.5, reflecting the fact that with probability 1/2, we will
toss a head and thus move from having one consecutive head to two. On the other hand, if we
toss a tail while we are in state 2, we go to state 1, meaning 0 consecutive heads; thus p21 = 0.5.
𝜋 = 𝜋𝑃
(𝐼 − 𝑃𝑇 )𝜋 = 0
Any single one of the equations in the system of above equation is redundant. We thus
eliminate one of them, by removing the last row of I − P in above equation. That also means
removing the last 0 in the 0 vector on the right-hand side of equation.
∑ 𝜋𝑖 = 1
𝑖
1𝑇𝑛 𝜋 = 1
• Calculate (𝐼 − 𝑃𝑇 ) in line 3. Note again that diag(), when called with a scalar argument,
returns the identity matrix of the size given by that argument.
• Replace the last row of P with 1 values in line 4.
• Set up the right-hand side vector in line 5.
• Solve for 𝜋 in line 6.
Another approach, using more advanced knowledge, is based on eigen-values. Note from
equation (𝜋 = 𝜋𝑃) that 𝜋 is a left eigenvector of P with eigen-value 1. This suggests using R’s
eigen() function, selecting the eigenvector corresponding to that eigenvalue.
Since π is a left eigenvector, the argument in the call to eigen() must be P transpose rather
than P. In addition, since an eigenvector is unique only up to scalar multiplication, we must
deal with two issues regarding the eigenvector returned to us by eigen():
The return value of eigen() is a list. One of the list’s components is a matrix named vectors.
These are the eigenvectors, with the ith column being the eigenvector corresponding to the ith
eigenvalue. Thus, we take column 1 here.
Set Operations
Consider coding the symmetric difference between two sets— that is, all the elements
belonging to exactly one of the two operand sets. Because the symmetric difference between
sets x and y consists exactly of those elements in x but not y and vice versa, the code consists
of easy calls to setdiff() and union(), as follows:
> symdiff
function(a,b) {
sdfxy <- setdiff(x,y)
sdfyx <- setdiff(y,x)
return(union(sdfxy, sdfyx))
}
Here’s another example: a binary operand for determining whether one set u is a subset of
another set v. A bit of thought shows that this property is equivalent to the intersection of u
and v being equal to u. Hence we have another easily coded function:
The function combn() generates combinations. Let’s find the subsets of {1,2,3} of size 2.
The results are in the columns of the output. We see that the subsets of {1,2,3} of size 2 are
(1,2), (1,3), and (2,3).
The function also allows you to specify a function to be called by combn() on each
combination. For example, we can find the sum of the numbers in each subset, like this:
> combn(1:3,2,sum)
[1] 3 4 5
INPUT/OUTPUT
You can use scan() to read in a vector, whether numeric or character, from a file or the
keyboard. With a little extra work, you can even read in data to form a list.
Suppose we have files named [Link], [Link] and [Link]. The [Link] file contains the following:
123
4 5
6
123
4.2 5
6
abc
de f
g
Let’s see what we can do with these files using the scan() function.
> scan("[Link]")
Read 4 items
[1] 123 4 5 6
> scan("[Link]")
Read 4 items
[1] 123.0 4.2 5.0 6.0
> scan("[Link]")
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines,
[Link], : scan() expected 'a real', got 'abc'
> scan("[Link]",what="")
Read 4 items
[1] "abc" "de" "f" "g"
In the first call, we got a vector of four integers (though the mode is numeric). The second
time, since one number was nonintegral, the others were shown as floating-point numbers,
too.
In the third case, we got an error. The scan() function has an optional argument named
what, which specifies mode, defaulting to double mode. So, the nonnumeric contents of the
file z3 produced an error. But we then tried again, with what="". This assigns a character
string to what, indicating that we want character mode. (We could have set what to any
character string.)
Of course, in typical usage, we would assign the return value of scan() to a variable. Here’s
an example:
By default, scan() assumes that the items of the vector are separated by whitespace, which
includes blanks, carriage return/line feeds, and horizontal tabs. You can use the optional sep
argument for other situations. As example, we can set sep to the newline character to read
in each line as a string, as follows:
In the first case, the strings "de" and "f" were assigned to separate elements of x1. But in the
second case, we specified that elements of x2 were to be delineated by end-of-line characters,
not spaces. Since "de" and "f" are on the same line, they are assigned together to x[2].
More sophisticated methods for reading files will be presented later in this chapter, such as
methods to read in a file one line at a time. But if you want to read the entire file at once,
scan() provides a quick solution.
You can use scan() to read from the keyboard by specifying an empty string for the filename:
Note that we are prompted with the index of the next item to be input, and we signal the end
of input with an empty line.
If you do not wish scan() to announce the number of items it has read, include the argument
quiet=TRUE.
If you want to read in a single line from the keyboard, readline() is very handy.
At the top level of interactive mode, you can print the value of a variable or expression by
simply typing the variable name or expression. This won’t work if you need to print from
within the body of a function. In that case, you can use the print() function, like this:
Recall that print() is a generic function, so the actual function called will depend on the
class of the object that is printed. If, for example, the argument is of class "table", then the
[Link]() function will be called.
It’s a little better to use cat() instead of print(), as the latter can print only one expression and
its output is numbered, which may be a nuisance. Compare the results of the functions:
> print("abc")
[1] "abc"
> cat("abc\n")
abc
Note that we needed to supply our own end-of-line character, "\n", in the call to cat().
Without it, our next call would continue to write to the same line.
> x
[1] 1 2 3
> cat(x,"abc","de\n")
1 2 3 abc de
If you don’t want the spaces, set sep to the empty string "", as follows:
> cat(x,"abc","de\n",sep="")
123abcde
Any string can be used for sep. Here, we use the newline character:
> cat(x,"abc","de\n",sep="\n")
1
2
3
abc
de
Now that we’ve covered the basics of I/O, let’s get to some more practical applications of
reading and writing files. The following sections discuss reading data frames or matrices from
files, working with text files, accessing files on remote machines, and getting file and directory
information.
The use of the function [Link]() to read in a data frame. As a quick review, suppose
the file z looks like this:
name age
John 25
Mary 28
Jim 19
The first line contains an optional header, specifying column names. We could read the file
this way:
Note that scan() would not work here, because our file has a mixture of numeric and
character data (and a header).
There appears to be no direct way of reading in a matrix from a file, but it can be done easily
with other tools. A simple, quick way is to use scan() to read in the matrix row by row. You
use the byrow option in the function matrix() to indicate that you are defining the
elements of the matrix in a row-wise, rather than column-wise, manner.
For instance, say the file x contains a 5-by-3 matrix, stored row-wise:
1 0 1
1 1 1
1 1 0
1 1 0
0 0 1
This is fine for quick, one-time operations, but for generality, you can use [Link](),
which returns a data frame, and then convert via [Link](). Here is a general method:
In computer literature, there is often a distinction made between text files and binary files.
That distinction is somewhat misleading—every file is binary in the sense that it consists of 0s
and 1s. Let’s take the term text file to mean a file that consists mainly of ASCII characters or
coding for some other human language (such as GB for Chinese) and that uses newline
characters to give humans the perception of lines. The latter aspect will turn out to be central
here. Non-text files, such as JPEG images or executable program files, are generally called
binary files.
You can use readLines() to read in a text file, either one line at a time or in a single
operation. For example, suppose we have a file z1 with the following contents:
John 25
Mary 28
Jim 19
Since each line is treated as a string, the return value here is a vector of strings—that is, a
vector of character mode. There is one vector element for each line read, thus three elements
here.
Writing to a File
Given the statistical basis of R, file reads are probably much more common than writes. But
writes are sometimes necessary, and this section will present methods for writing to files.
The function [Link]() works very much like [Link](), except that it writes
a data frame instead of reading one.
In the case of writing a matrix to a file, just state that you do not want row or column names,
as follows:
> [Link](xc,"xcnew", [Link]=FALSE, [Link]=FALSE)
The function cat() can also be used to write to a file, one part at a time.
Here’s an example:
> cat("abc\n",file="u")
> cat("de\n",file="u",append=TRUE)
The first call to cat() creates the file u, consisting of one line with contents "abc". The
second call appends a second line. Unlike the case of using the writeLines() function
(which we’ll discuss next), the file is automatically saved after each operation. For instance,
after the previous calls, the file will look like this:
abc
de
> cat(file="v",1,2,"xyz\n")
1 2 xyz
You can also use writeLines(), the counterpart of readLines(). If you use a connection,
you must specify "w" to indicate you are writing to the file, not reading from it:
abc
de
f
Module IV : Graphics
Creating Graphs
To begin, we’ll look at the foundational function for creating graphs: plot(). Then we’ll
explore how to build a graph, from adding lines and points to attaching a legend.
The plot() function forms the foundation for much of R’s base graphing operations, serving
as the vehicle for producing many different kinds of graphs. plot() is a generic function, or
a placeholder for a family of functions. The function that is actually called depends on the class
of the object on which it is called.
Let’s see what happens when we call plot() with an X vector and a Y vector, which are
interpreted as a set of pairs in the (x,y) plane.
This will cause a window to pop up, plotting the points (1,1), (2,2), and (3,4), as shown in
figure. As you can see, this is a very plain-Jane graph.
Note : The points in the graph in Figure are denoted by empty circles. If you want to use a
different character type, specify a value for the named argument pch (for point character).
The plot() function works in stages, which means you can build up a graph in stages by issuing
a series of commands. For example, as a base, we might first draw an empty graph, with only
axes, like this:
This draws axes labelled x and y. The horizontal (x) axis ranges from −3 to 3. The vertical (y)
axis ranges from −1 to 5. The argument type="n" means that there is nothing in the graph itself.
Adding Lines: The abline() Function
We now have an empty graph, ready for the next stage, which is adding a line:
After the call to plot(), the graph will simply show the three points, along with the x- and
y- axes with hash marks. The call to abline() then adds a line to the current graph.
The result of the call to the linear-regression function lm() is a class instance containing the
slope and intercept of the fitted line, as well as various other quantities that don’t concern us
here. We’ve assigned that class instance to lmout. The slope and intercept will now be in
lmout$coefficients.
So, what happens when we call abline()? This function simply draws a straight line, with
the function’s arguments treated as the intercept and slope of the line. For instance, the call
abline(c(2,1)) draws this line on whatever graph you’ve built up so far: y = 2 + 1·x
But abline() is written to take special action if it is called on a regression object (though,
surprisingly, it is not a generic function). Thus, it will pick up the slope and intercept it needs
from lmout$coefficients and plot that line. It superimposes this line onto the current
graph, the one that graphs the three points. In other words, the new graph will show both the
points and the line, as in figure.
You can add more lines using the lines() function. Though there are many options, the two
basic arguments to lines() are a vector of x-values and a vector of y-values. These are
interpreted as (x, y) pairs representing points to be added to the current graph, with lines
connecting the points. For instance, if X and Y are the vectors (1.5, 2.5) and (3, 3), you could
use this call to add a line from (1.5, 3) to (2.5, 3) to the present graph:
> lines(c(1.5,2.5),c(3,3))
If you want the lines to “connect the dots,” but don’t want the dots them-selves, include
type="l" in your call to lines() or to plot(), as follows:
> plot(x,y,type="l")
You can use the lty parameter in plot() to specify the type of line, such as solid or dashed.
To see the types available and their codes, enter this command:
> help(par)
Each time you call plot(), directly or indirectly, the current graph window will be replaced
by the new one. If you don’t want that to happen, use the command for your operating system:
For instance, suppose you wish to plot two histograms of vectors X and Y and view them side
by side. On a Linux system, you would type the following:
> hist(x)
> x11()
> hist(y)
The points() function adds a set of (x, y) points, with labels for each, to the currently
displayed graph. For instance, in our first example, suppose we entered this command:
> points(testscores$Exam1,testscores$Exam3,pch="+")
The result would be to superimpose onto the current graph the points of the exam scores from
that example, using plus signs (+) to mark them. As with most of the other graphics functions,
there are many options, such as point color and background color. For instance, if you want a
yellow background, type this command:
> par(bg="yellow")
Now your graphs will have a yellow background, until you specify otherwise.
As with other functions, to explore the myriad of options, type this:
> help(par)
The legend() function is used, not surprisingly, to add a legend to a multi-curve graph. This
could tell the viewer something like, “The green curve is for the men, and the red curve
displays the data for the women.” Type the following to see some nice examples:
> example(legend)
Use the text() function to place some text anywhere in the current graph. Here’s an example:
text(2.5,4,"abc")
This writes the text “abc” at the point (2.5,4) in the graph. The center of the string, in this
case “b,” would go at that point.
To see a more practical example, let’s add some labels to the curves in our exam scores graph,
as follows:
The result is shown in figure. In order to get a certain string placed exactly where you want it,
you may need to engage in some trial and error. Or you may find the locator() function to
be a much quicker way to go, as detailed in the next section.
Pinpointing Locations: The locator() Function
Placing text exactly where you wish can be tricky. You could repeatedly try different x- and
y-coordinates until you find a good position, but the locator() function can save you a lot
of trouble. You simply call the function and then click the mouse at the desired spot in the
graph. The function returns the x- and y-coordinates of your click point. Specifically, typing
the following will tell R that you will click in one place in the graph:
locator(1)
Once you click, R will tell you the exact coordinates of the point you clicked. Call
locator(2) to get the locations of two places, and so on. (Warning: Make sure to
include the argument.)
> hist(c(12,5,13,25,16))
> locator(1)
$x
[1] 6.239237
$y
[1] 1.221038
This has R draw a histogram and then calls locator() with the argument 1, indicating we
will click the mouse once. After the click, the function returns a list with components x and
y, the x- and y-coordinates of the point where we clicked.
> text(locator(1),"nv=75")
Here, text() was expecting an x-coordinate and a y-coordinate, specifying the point at
which to draw the text “nv=75”. The return value of locator() supplied those coordinates.
Restoring a Plot
R has no “undo” command. However, if you suspect you may need to undo your next step
when building a graph, you can save it using recordPlot() and then later restore it with
replayPlot().
Less formally but more conveniently, you can put all the commands you’re using to build up
a graph in a file and then use source(), or cut and paste with the mouse, to execute them. If
you change one command, you can redo the whole graph by sourcing or copying and pasting
your file.
For our current graph, for instance, we could create file named examplot.R with the
following contents:
> d1 = density(testscores$Exam1,from=0,to=100)
> d2 = density(testscores$Exam2,from=0,to=100)
> plot(d1,main="",xlab="")
> lines(d2)
> text(46.7,0.02,"Exam 1")
> text(12.3,0.008,"Exam 2")
If we decide that the label for exam 1 was a bit too far to the right, we can edit the file and
then either do the copy-and-paste or execute the following:
> source("examplot.R")
Customizing Graphs
You’ve seen how easy it is to build simple graphs in stages, starting with plot(). Now you
can begin to enhance those graphs, using the many options R provides.
The cex (for character expand) function allows you to expand or shrink characters within a
graph, which can be very useful. You can use it as a named parameter in various graphing
functions. For instance, you may wish to draw the text “abc” at some point, say (2.5,4), in
your graph but with a larger font, in order to call attention to this particular text. You could
do this by typing the following:
This prints the same text as in our earlier example but with characters 1.5 times the normal
size.
You may wish to have the ranges on the x- and y-axes of your plot be broader or narrower
than the default. This is especially useful if you will be displaying several curves in the same
graph.
You can adjust the axes by specifying the xlim and/or ylim parameters in your call to
plot() or points(). For example, ylim=c(0,90000) specifies a range on the y-axis of
0 to 90,000.
If you have several curves and do not specify xlim and/or ylim, you should draw the tallest
curve first so there is room for all of them. Otherwise, R will fit the plot to the first one your
draw and then cut off taller ones at the top! We took this approach earlier, when we plotted
two density estimates on the same graph. Instead, we could have first found the highest values
of the two density estimates. For d1, we find the following:
> d1
Call:
[Link](x = testscores$Exam1, from = 0, to = 100)
X y
Min. : 0 Min. :1.423e-07
1st Qu.: 25 1st Qu.:1.629e-03
Median : 50 Median :9.442e-03
Mean : 50 Mean :9.844e-03
3rd Qu.: 75 3rd Qu.:1.756e-02
Max. :100 Max. :2.156e-02
So, the largest y-value is 0.022. For d2, it was only 0.017. That means we should have plenty
of room if we set ylim at 0.03. Here is how we could draw the two plots on the same picture:
First we drew the bare-bones plot—just axes without innards, as shown in figure. The first two
arguments to plot() give xlim and ylim, so that the lower and upper limits on the Y axis
will be 0 and 0.03. Calling lines() twice then fills in the graph, yielding following figures.
(Either of the two lines() calls could come first, as we’ve left enough room.)
Adding a Polygon: The polygon() Function
You can use polygon() to draw arbitrary polygonal objects. For example, the following code
draws the graph of the function f(x) = 1 − e−x and then adds a rectangle that approximates the
area under the curve from x = 1.2 to x = 1.4.
In the call to polygon() here, the first argument is the set of x-coordinates for the rectangle,
and the second argument specifies the y-coordinates. The third argument specifies that the
rectangle in this case should be shaded in solid gray.
As another example, we could use the density argument to fill the rectangle with striping. This
call specifies 10 lines per inch:
> polygon(c(1.2,1.4,1.4,1.2),c(0,0,f(1.3),f(1.3)),density=10)
Just plotting a cloud of points, connected or not, may give you nothing but an uninformative
mess. In many cases, it is better to smooth out the data by fitting a nonparametric regression
estimator such as lowess().
Let’s do that for our test score data. We’ll plot the scores of exam 2 against those of exam 1:
> plot(testscores)
> lines(lowess(testscores))
The result is shown in figure. A newer alternative to lowess() is loess(). The two
functions are similar but have different defaults and other options. You need some advanced
knowledge of statistics to appreciate the differences. Use whichever you find gives better
smoothing.
Say you want to plot the function g(t) = (t2 + 1)0.5 for t between 0 and 5. You could use the
following R code:
But you could avoid some work by using the curve() function, which basically uses the same
method:
> curve((x^2+1)^0.5,0,5)
If you are adding this curve to an existing plot, use the add argument:
> curve((x^2+1)^0.5,0,5,add=T)
The optional argument n has the default value 101, meaning that the function will be evaluated
at 101 equally spaced points in the specified range of x.
Use just enough points for visual smoothness. If you find 101 is not enough, experiment with
higher values of n.
Here, the call plot() leads to calling [Link](), the implementation of the generic
plot() function for the function class.
Again, the approach is your choice; use whichever one you prefer.
The R graphics display can consist of various graphics devices. The default device is the screen.
If you want to save a graph to a file, you must set up another device.
Let’s go through the basics of R graphics devices first to introduce R graphics device concepts,
and then discuss a second approach that is much more direct and convenient.
R Graphics Devices
> pdf("[Link]")
This opens the file [Link]. We now have two devices open, as we can confirm:
> [Link]()
X11 pdf
23
The screen is named X11 when R runs on Linux. (It’s named windows on Windows systems.)
It is device number 2 here. Our PDF file is device number 3. Our active device is the PDF file:
> [Link]()
pdf
3
All graphics output will now go to this file instead of to the screen. But what if we wish to save
what’s already on the screen?
One way to save the graph currently displayed on the screen is to re-establish the screen as
the current device and then copy it to the PDF device, which is 3 in our example, as follows:
> [Link](2)
X11
2
> [Link](which=3)
pdf
3
But actually, it is best to set up a PDF device as shown earlier and then rerun whatever analyses
led to the current screen. This is because the copy operation can result in distortions due to
mismatches between screen devices and file devices.
Closing an R Graphics Device
Note that the PDF file we create is not usable until we close it, which we do as follows:
> [Link](3)
pdf
3
> [Link]()
X11
2
You can also close the device by exiting R, if you’re finished working with it. But in future
versions of R, this behavior may not exist, so it’s probably better to proactively close.
A random variable that may assume either a finite number of values or an infinite sequence of
values such as 0, 1, 2, . . . is referred to as a discrete random variable. for example, consider the
experiment of an accountant taking the certified public accountant (CPA) examination. The
examination has four parts. we can define a random variable as x = the number of parts of the
CPA examination passed. It is a discrete random variable because it may assume the finite
number of values 0, 1, 2, 3, or 4.
As another example of a discrete random variable, consider the experiment of cars arriving at
a tollbooth. The random variable of interest is x = the number of cars arriving during a one-
day period. The possible values for x come from the sequence of integers 0, 1, 2, and so on.
Hence, x is a discrete random variable assuming one of the values in this infinite sequence.
A random variable that may assume any numerical value in an interval or collection of
intervals is called a continuous random variable. Experimental outcomes based on
measurement scales such as time, weight, distance, and temperature can be described by
continuous random variables.
For example, consider an experiment of monitoring incoming telephone calls to the claims
office of a major insurance company. Suppose the random variable of interest is x = the time
between consecutive incoming calls in minutes. This random variable may assume any value
in the interval x ≥ 0. Actually, an infinite number of values are possible for x, including values
such as 1.26 minutes, 2.751 minutes, 4.3333 minutes, and so on. As another example, consider
a 90-mile section of interstate highway I-75 north of Atlanta, Georgia. for an emergency
ambulance service located in Atlanta, we might define the random variable as x = number of
miles to the location of the next traffic accident along this section of I-75. In this case, x would
be a continuous random variable assuming any value in the interval 0 ≤ x ≤ 90. Additional
examples of continuous random variables are listed in Table 5.2. Note that each example
describes a random variable that may assume any value in an interval of values.
The probability distribution for a random variable describes how probabilities are distributed
over the values of the random variable. for a discrete random variable x, a probability function,
denoted by P(x), provides the probability for each value of the random variable.
Probability of x
Number Obtained x
P(x)
1 1/6
2 1/6
3 1/6
4 1/6
5 1/6
6 1/6
In the development of a probability function for any discrete random variable, the following
two conditions must be satisfied.
𝑃(𝑥) ≥ 0
∑ 𝑃(𝑥) = 1
Binomial Distribution
Suppose an experiment is repeated ‘n’ times and each trail is independent.
Let us assume that each trail results in two possible mutually exclusive and exhaustive
outcomes i.e., success and failure.
Let X is random variable represents total no. of successes in ‘n’ trails. Let the probability
of success in each trail is p and the probability of failure is q=1-p and p remain constant from
trail to trail.
Since, the trails are all independent the probability for the joint occurrence of the event is
pppppqqqppqp……..qp
= pxqn-x
n
Further in a series of n trails x successes and n-x failures can occur in c x ways. So, the required
probability is
Def: A random variable X is said to be follow a binomial distribution if its probability function
is given by
x n− x
P(X=x) = c x p q , x = 0,1,2,........, n
n
And p + q =1
Mean : np
Variance : npq
1. It has major application in the field of industrial quality control when items are classified
as defective and non defective.
2. This distribution is used when we like to know the opinion of the public when the voters
may be in favour of or against a candidate.
3. This distribution is also used in market researches where a consumer may prefer the
product of brand A or brand B
4. This distribution is used in medical research where a particular drug might cure a person
or not
5. This distribution also used in economic survey where respondents are in for or against a
certain economic policy of the govt.
Poisson Distribution
Poisson distribution is a discrete probability distribution, which is the limiting case of the
binomial distribution under certain conditions.
Def: A discrete random variable X is said to be follow a Poisson distribution if the probability
mass function is given by
e − x
p ( X = x) = P ( x; ) = , x = 0,1,2,3..........
x!
Examples where the Poisson distribution is used (or) Applications of Poisson distribution:
This distribution is used to describe the behavior of the rare events like
A fundamental difference separates discrete and continuous random variables in terms of how
probabilities are computed. For a discrete random variable, the probability function f (x)
provides the probability that the random variable assumes a particular value. with continuous
random variables, the counterpart of the probability function is the probability density
function, also denoted by f (x). The difference is that the probability density function does not
directly provide probabilities. however, the area under the graph of f (x) corresponding to a
given interval does provide the probability that the continuous random variable x assumes a
value in that interval. So when we compute probabilities for continuous random variables we
are computing the probability that the random variable assumes any value in an interval.
Normal Distribution
1 1 x − 2
f ( x / , ) = N ( , ) =
2 2
exp −
2 2
Where − X
− , 0
e = 2.7183 , = 3.1416
Here and are the mean and variance of the normal distribution respectively.
2
Note: A random variable X with mean and variance and following the normal law can
2
be expressed by X ~ N ( , )
2
Basic Statistics
Data are the facts and figures collected, analyzed, and summarized for presentation and
interpretation. All the data collected in a particular study are referred to as the data set for the
study. Table 1.1 shows a data set containing information for 60 nations that participate in the
World Trade Organization. The World Trade Organization encourages the free flow of
international trade and provides a forum for resolving trade disputes.
Elements are the entities on which data are collected. Each nation listed in Table is an element
with the nation or element name shown in the first column. With 60 nations, the data set
contains 60 elements.
A variable is a characteristic of interest for the elements. The data set in Table includes the
following five variables:
• WTO Status: The nation’s membership status in the World Trade Organization; this can
be either as a member or an observer.
• Per Capita GDP ($): The total market value ($) of all goods and services produced by the
nation divided by the number of people in the nation; this is commonly used to compare
economic productivity of the nations.
• Trade Deficit ($1000s): The difference between the total dollar value of the nation’s imports
and the total dollar value of the nation’s exports.
• Fitch Rating: The nation’s sovereign credit rating as appraised by the Fitch Group1; the
credit ratings range from a high of AAA to a low of F and can be modified by + or −.
• Fitch Outlook: An indication of the direction the credit rating is likely to move over the
upcoming two years; the outlook can be negative, stable, or positive.
Measurements collected on each variable for every element in a study provide the data. The
set of measurements obtained for a particular element is called an observation. Referring to
Table, we see that the first observation (Armenia) contains the following measurements:
Member, 5,400, 2,673,359, BB−, and Stable. The second observation (Australia) contains the
following measurements: Member, 40,800, −33,304,157, AAA, Stable, and so on. A data set
with 60 elements contains 60 observations.
Scales of Measurement
Data collection requires one of the following scales of measurement: nominal, ordinal, interval,
or ratio. The scale of measurement determines the amount of information contained in the
data and indicates the most appropriate data summarization and statistical analyses.
When the data for a variable consist of labels or names used to identify an attribute of the
element, the scale of measurement is considered a nominal scale. For example, referring to the
data in Table, the scale of measurement for the WTO Status variable is nominal because the
data “member” and “observer” are labels used to identify the status category for the nation. In
cases where the scale of measurement is nominal, a numerical code as well as a nonnumerical
label may be used. For example, to facilitate data collection and to prepare the data for entry
into a computer database, we might use a numerical code for the WTO Status variable by
letting 1 denote a member nation in the World Trade Organization and 2 denote an observer
nation. The scale of measurement is nominal even though the data appear as numerical values.
The scale of measurement for a variable is considered an ordinal scale if the data exhibit the
properties of nominal data and in addition, the order or rank of the data is meaningful. For
example, referring to the data in Table, the scale of measurement for the Fitch Rating is ordinal
because the rating labels which range from AAA to F can be rank ordered from best credit
rating AAA to poorest credit rating F. The rating letters provide the labels similar to nominal
data, but in addition, the data can also be ranked or ordered based on the credit rating, which
makes the measurement scale ordinal. Ordinal data can also be recorded by a numerical code,
for example, your class rank in school.
The scale of measurement for a variable is an interval scale if the data have all the properties
of ordinal data and the interval between values is expressed in terms of a fixed unit of measure.
Interval data are always numerical. College admission SAT scores are an example of interval-
scaled data. For example, three students with SAT math scores of 620, 550, and 470 can be
ranked or ordered in terms of best performance to poorest performance in math. In addition,
the differences between the scores are meaningful. For instance, student 1 scored 620 − 550 =
70 points more than student 2, while student 2 scored 550 − 470 = 80 points more than student
3.
The scale of measurement for a variable is a ratio scale if the data have all the properties of
interval data and the ratio of two values is meaningful. Variables such as distance, height,
weight, and time use the ratio scale of measurement. This scale requires that a zero value be
included to indicate that nothing exists for the variable at the zero point.
For example, consider the cost of an automobile. A zero value for the cost would indicate that
the automobile has no cost and is free. In addition, if we compare the cost of $30,000 for one
automobile to the cost of $15,000 for a second automobile, the ratio property shows that the
first automobile is $30,000/$15,000 = 2 times, or twice, the cost of the second automobile.
Data can be classified as either categorical or quantitative. Data that can be grouped by specific
categories are referred to as categorical data. Categorical data use either the nominal or ordinal
scale of measurement. Data that use numeric values to indicate how much or how many are
referred to as quantitative data. Quantitative data are obtained using either the interval or ratio
scale of measurement.
Arithmetic operations provide meaningful results for quantitative variables. For example,
quantitative data may be added and then divided by the number of observations to compute
the average value. This average is usually meaningful and easily interpreted.
Many situations require information about a large group of elements (individuals, companies,
voters, households, products, customers, and so on). But, because of time, cost, and other
considerations, data can be collected from only a small portion of the group. The larger group
of elements in a particular study is called the population, and the smaller group is called the
sample. Formally, we use the following definitions.
A population is the set of all elements of interest in a particular study. A sample is a subset of
the population.
Measures of Location
Mean
Perhaps the most important measure of location is the mean, or average value, for a variable.
The mean provides a measure of central location for the data. if the data are for a sample, the
mean is denoted by 𝑥̅ ; if the data are for a population, the mean is denoted by the Greek letter
𝜇.
in statistical formulas, it is customary to denote the value of variable x for the first observation
by x1, the value of variable x for the second observation by x2, and so on. in general, the value
of variable x for the ith observation is denoted by xi. For a sample with n observations, the
formula for the sample mean is as follows.
∑ 𝑥𝑖
𝑥̅ =
𝑛
let us consider the following class size data for a sample of five college classes.
46 54 42 46 32
∑ 𝑥𝑖 46 + 54 + 42 + 46 + 32
𝑥̅ = = = 44
𝑛 5
Median
The median is another measure of central location. The median is the value in the middle
when the data are arranged in ascending order (smallest value to largest value). With an odd
number of observations, the median is the middle value. An even number of observations has
no single middle value. in this case, we follow convention and define the median as the average
of the values for the middle two observations. For convenience the definition of the median is
restated as follows.
(a) For an odd number of observations, the median is the middle value.
(b) For an even number of observations, the median is the average of the two middle values.
Let us apply this definition to compute the median class size for the sample of five college
classes. Arranging the data in ascending order provides the following list.
32 42 46 46 54
Because n = 5 is odd, the median is the middle value. Thus the median class size is 46 students.
even though this data set contains two observations with values of 46, each observation is
treated separately when we arrange the data in ascending order.
Mode
Another measure of location is the mode. The mode is defined as follows. The mode is the
value that occurs with greatest frequency.
To illustrate the identification of the mode, consider the sample of five class sizes. The only
value that occurs more than once is 46. Because this value, occurring with a frequency of 2,
has the greatest frequency, it is the mode.
Quartiles
it is often desirable to divide a data set into four parts, with each part containing approximately
one-fourth, or 25%, of the observations. These division points are referred to as the quartiles
and are defined as follows.
Because quartiles are specific percentiles, the procedure for computing percentiles can be used
to compute the quartiles.
To illustrate the computation of the quartiles for a data set consisting of n observations, we
will compute the quartiles for the starting salary data. Previously we showed that the second
median for the starting salary data is 3905; thus, the second quartile is Q2 = 3905. To compute
the first and third quartiles,
1 1
For Q1, 𝑄1 = (𝑛 + 1) = (12 + 1) = 3.25
4 4
The first quartile is .25 of the way between the value in position 3 (3850) and the value in
position 4 (3880). Thus,
The third quartile is .75 of the way between the value in position 9(3950) and the value in
position 10 (4050). Thus,
The word dispersion means deviation or difference. In statistics refers to deviation if the values
of a variable from their central value. Measures of dispersion indicate the extent to which
individual observations vary from their averages i.e., mean, median or mode. It shows the
spread of items of a series from their central value. This is otherwise known as variation or
dispersion.
Definitions:
Range
Let us consider the data on starting salaries for business school graduates.
3850, 3950, 4050, 3880, 3755, 3710, 3890, 4130, 3940, 4325, 3920, 3880
The largest starting salary is 4325 and the smallest is 3710. The range is 4325 − 3710 = 615.
Although the range is the easiest of the measures of variability to compute, it is seldom used
as the only measure. The reason is that the range is based on only two of the observations and
thus is highly influenced by extreme values. Suppose the highest paid graduate received a
starting salary of $10,000 per month. in this case, the range would be 10,000 − 3710 = 6290
rather than 615. This large value for the range would not be especially descriptive of the
variability in the data because 11 of the 12 starting salaries are closely grouped between 3710
and 4130.
Interquartile Range
A measure of variability that overcomes the dependency on extreme values is the interquartile
range (IQR). This measure of variability is the difference between the third quartile, Q3, and
the first quartile, Q1. in other words, the interquartile range is the range for the middle 50%
of the data.
𝐼𝑄𝑅 = 𝑄3 − 𝑄1
For the data on monthly starting salaries, the quartiles are Q3 = 4000 and Q1 = 3865. Thus, the
interquartile range is 4025 − 3857 = 168.
Variance
The variance is a measure of variability that utilizes all the data. The variance is based on the
difference between the value of each observation (xi) and the mean. The difference between
each xi and the mean (𝑥̅ for a sample, 𝜇 for a population) is called a deviation about the mean.
For a sample, a deviation about the mean is written (xi − 𝑥̅ ); for a population, it is written (xi −
𝜇). in the computation of the variance, the deviations about the mean are squared.
if the data are for a population, the average of the squared deviations is called the population
variance. The population variance is denoted by the Greek symbol 𝜎 2 . For a population of N
observations and with 𝜇 denoting the population mean, the definition of the population
variance is as follows.
2
∑(𝑥𝑖 − 𝜇)2
𝜎 =
𝑁
In most statistical applications, the data being analyzed are for a sample. When we compute a
sample variance, we are often interested in using it to estimate the population variance 𝜎 2 .
Although a detailed explanation is beyond the scope of this text, it can be shown that if the
sum of the squared deviations about the sample mean is divided by n − 1, and not n, the
resulting sample variance provides an unbiased estimate of the population variance. For this
reason, the sample variance, denoted by s2, is defined as follows.
∑(𝑥𝑖 − 𝑥̅ )2
𝑠2 =
𝑛−1
2
∑(𝑥𝑖 − 𝑥̅ )2 256
𝑠 = = = 64
𝑛−1 4
The variance as a measure useful in comparing the amount of variability for two or more
variables. in a comparison of the variables, the one with the largest variance shows the most
variability. Further interpretation of the value of the variance may not be necessary.
Standard Deviation
The standard deviation is defined to be the positive square root of the variance. Following the
notation we adopted for a sample variance and a population variance, we use s to denote the
sample standard deviation and 𝜎 to denote the population standard deviation.
The standard deviation is measured in the same units as the original data. For this reason the
standard deviation is more easily compared to the mean and other statistics that are measured
in the same units as the original data.
Covariance
For a sample of size n with the observations (x1, y1), (x2, y2), and so on, the sample covariance
is defined as follows:
This formula pairs each xi with a yi. We then sum the products obtained by multiplying the
deviation of each xi from its sample mean x by the deviation of the corresponding yi from its
sample mean y; this sum is then divided by n − 1.
To measure the strength of the linear relationship between the number of commercials x and
the sales volume y in the stereo and sound equipment store problem,
Number of
Week Sales Volume ($100s) y
Commercials x
1 2 50
2 5 57
3 1 41
4 3 54
5 4 54
6 1 38
7 5 63
8 3 48
9 4 59
10 2 46
Correlation Coefficient
Suppose two variables X and Y are measured on a continuous scale and are linearly related.
The correlation coefficient r(X,Y) = r measures the degree of linear relationship between X and
Y using
𝑐𝑜𝑣(𝑋, 𝑌) 𝑠𝑥𝑦
𝑟𝑥𝑦 = =
𝑠𝑑(𝑋) 𝑠𝑑(𝑌) 𝑠𝑥 𝑠𝑦
The signs of r thus determine the direction of the association. If r is close to zero, then it
indicates that the variables are independent or the relationship is not linear. Note that if the
relationship between X and Y is nonlinear, then the degree of linear relationship may be low
and r is then close to zero even if the variables are clearly not independent. Note that r ( X , X
) = 1 and r(X, −X) = −1.
Testing of Hypothesis
Hypothesis:
Hypothesis testing:
Hypothesis testing is to test some hypothesis about parent population from which the sample
is drawn.
Example: A coin may be tossed 200 times and we may get heads 80 times and tails 120 times,
we may now be interested in testing the hypothesis that the coin is unbiased.
To take another example we may study the average weight of the 100 students of a particular
college and may get the result as 110lb. We may now be interested in testing the hypothesis
that the sample has been drawn from a population with average weight 115lb.
1. Null Hypothesis
2. Alternative hypothesis
Null hypothesis:
The hypothesis under verification is known as null hypothesis and is denoted by H0 and is
always set up for possible rejection under the assumption that it is true.
For example, if we want to find out whether extra coaching has benefited the students or not,
we shall set up a null hypothesis that “extra coaching has not benefited the students”. Similarly,
if we want to find out whether a particular drug is effective in curing malaria we will take the
null hypothesis that “the drug is not effective in curing malaria”.
Alternative hypothesis:
The rival hypothesis or hypothesis which is likely to be accepted in the event of rejection of
the null hypothesis H0 is called alternative hypothesis and is denoted by H1 or Ha.
For example, if a psychologist who wishes to test whether or not a certain class of people have
a mean I.Q. 100, then the following null and alternative hypothesis can be established.
H 0 : = 100
H 1 : 100
(or ) H 1 : 100
(or ) H 1 : 100
After applying a test, a decision is taken about the acceptance or rejection of null hypothesis
against an alternative hypothesis. The decisions may be four types.
• Type-I error
• Type-II error
Type-I error: The type-I error is said to be committed if the null hypothesis (H0) is true but
our test rejects it.
Type-II error: The type-II error is said to be committed if the null hypothesis (H0) is false but
our test accepts it.
Level of significance:
The maximum probability of committing type-I error is called level of significance and is
denoted by .
This can be measured in terms of percentage i.e. 5%, 1%, 10% etc…….
Power of the test:
The probability of rejecting a false hypothesis is called power of the test and is denoted by 1 −
𝛽.
• A test for which both 𝛼 and 𝛽 are small and kept at minimum level is considered desirable.
• The only way to reduce both 𝛼 and 𝛽 simultaneously is by increasing sample size.
• The type-II error is more dangerous than type-I error.
Critical region:
A statistic is used to test the hypothesis H0. The test statistic follows a known distribution. In
a test, the area under the probability density curve is divided into two regions i.e. the region
of acceptance and the region of rejection. The region of rejection is the region in which H 0 is
rejected. It indicates that if the value of test statistic lies in this region, H0 will be rejected. This
region is called critical region. The area of the critical region is equal to the level of significance
𝛼. The critical region is always on the tail of the distribution curve. It may be on both sides or
on one side depending upon the alternative hypothesis.
A test with the null hypothesis 𝐻0 : 𝜃 = 𝜃0 against the alternative hypothesis 𝐻0 : 𝜃 > 𝜃0 (right
tailed alternative) or 𝐻0 : 𝜃 < 𝜃0 (left tailed alternative) is called one tailed test. In this case
the critical region is located on one tail of the distribution.
Sampling distribution:
Suppose we have a population of size ‘N’ and we are interested to draw a sample of size ‘n’
from the population. In different time if we draw the sample of size n, we get different samples
N
of different observations i.e. we can get
c n possible samples. If we calculate some particular
N
statistic from each of the
c n samples, the distribution of sample statistic is called sampling
distribution of the statistic. For example if we consider the mean as the statistic, then the
distribution of all possible means of the samples is a distribution of the sample mean and it is
called sampling distribution of the mean.
Standard error:
Standard deviation of the sampling distribution of the statistic t is called standard error of t.
Student's 't'
Let 𝑥𝑖 , (𝑖 = 1,2, … 𝑛) be a random sample of size n from a normal population with mean 𝜇 and
variance 𝜎 2 , Then Student's t is defined by the statistic
𝑥̅ − 𝜇
𝑡=
𝑆⁄
√𝑛
Where
∑ 𝑥𝑖
𝑥̅ = ,is the sample mean and
𝑛
∑(𝑥𝑖 − 𝑥̅ )2
2
𝑆 =
𝑛−1
(i) if a random sample 𝑥𝑖 , (𝑖 = 1,2, … 𝑛) of size n· has been drawn from a normal population
with a specified mean, say 𝜇0 , or
(ii) if the sample mean differs significantly from the, hypothetical value 𝜇0 of the population
mean.
(i) The sample has been drawn from the population with mean 𝜇0 , or
(ii) There is no significant difference between the sample mean 𝑥̅ and the population mean 𝜇,
𝑥̅ − 𝜇0 𝑥̅ − 𝜇0
𝑡= =
√𝑆 2 ⁄𝑛 √𝑠 2 ⁄(𝑛 − 1)
∑ 𝑥𝑖 ∑(𝑥𝑖 −𝑥̅ )2
Where 𝑥̅ = and 𝑠 2 = follows Student's t-distribution with (n - 1) d.f.
𝑛 𝑛−1
We now compare the calculated value of t with the tabulated value at certain level of
significance. If calculated | t | > tabulated t, null hypothesis is rejected and if calculated | t | <
tabulated t, H0 may be accepted at the level of significance adopted.
(i) The parent population from which the sample is drawn is normal.
(ii) The sample observations are independent, i.e.. the sample is random.
(iii) The population standard deviation 𝜎 is unknown.
Example 1 : A machinist is making engine parts with axle diameters of 0·700 inch. A random
sample of 10 parts shows a mean diameter of 0·742 inch with a standard deviation of 0·040
inch. Compute the statistic you would use to test whether the work is meeting the
specifications. Also state how you would proceed further.
𝑥̅ − 𝜇0 0.742 − 0.700
𝑡= = = 3.15
√𝑠 2 ⁄(𝑛 − 1) √0.040⁄(10 − 1)
How to proceed further. Here the test statistic 't' follows Student's t- distribution with 10-1=9
d.f. We will now compare this calculated value with tabulated value of t for 9 d.f. and at certain
level of significance, say 5%. Let this tabulated value be denoted by t0.
(i) If calculated 't' , 3·15> t0, we say that the value of t is significant. This implies that 𝑥̅ differs
significantly from 𝜇 and H0 is rejected at this level of significance and we conclude that the
product is not meeting the specifications.
(ii) If calculated t < t0, we say that the value of t is not significant, i.e., there is no significant
difference between 𝑥̅ and 𝜇. In other words, the deviation (𝑥̅ − 𝜇) is just due to fluctuations
of sampling and null hypothesis H0 may be retained at 5% level of significance, i.e., we may
take the product conforming to specifications.
Example 2 : A random sample of 10 boys had the following I.Q.'s : 70, 120, 110, 101, 88, 83,
95, 98, 107, 100. Do these data support the assumption of a population mean I.Q. of 100 ?
Solution :
Null hypothesis, H0 : The data are consistent with the assumption of a mean I.Q. of100 in the
population, i.e., 𝜇 = 100.
𝑥̅ − 𝜇0
𝑡=
√𝑆 2 ⁄𝑛
xi xi - x̅ (xi - 𝑥̅ )2
70 -27.2 739.84
120 22.8 519.84
110 12.8 163.84
101 3.8 14.44
88 -9.2 84.64
83 -14.2 201.64
95 -2.2 4.84
98 0.8 0.64
107 9.8 96.04
100 2.8 7.84
972 1833.6
972 1833.6
Hence, n=10, x̅ = = 97.2 and 𝑆 2 = = 203.73
10 9
Tabulated t0.05 for (10 - 1) i.e., 9 d.f. for two-tailed test is 2·262.
Conclusion. Since calculated t is less than tabulated t0.05 for 9 d.f., H0 may be accepted at 5%
level of significance and we may conclude that the data are consistent with the assumption of
mean I.Q. of 100 in the population.
Suppose we want to test if two independent samples xi (i = 1, 2, .., n1) and yj (j =1, 2, ..., n2) of
sizes n1 and n2 have been drawn from two normal populations with means 𝜇1 and 𝜇2
respectively.
Under the null hypothesis (H0) that the samples have been drawn from the normal populations
with means 𝜇1 and 𝜇2 and under the assumption that the population variance are equal, i.e.,
𝜎12 = 𝜎22 = 𝜎 2 (say), the statistic
𝑥̅ − 𝑦̅
𝑡=
1 1
𝑆√𝑛 + 𝑛
1 2
1 1
Where 𝑆 2 = n1 + n2 − 2 [∑(𝑥1 − 𝑥̅1 )2 + ∑(𝑥2 − 𝑥̅2 )2 ] = [𝑛1 𝑛12 + 𝑛2 𝑛22 ]
n1 + n2 − 2
(i) Parent populations. from which the samples have been drawn are normally distributed.
(ii) The population variances are equal and unknown. i.e., 𝜎12 = 𝜎22 = 𝜎 2 (say), where 𝜎 2 is
unknown.
(iii) The two samples are random and independent of each other.
Example : Samples of two types of electric light bulbs were tested for length of life and
following data were obtained:
Type I Type II
Sample size n1 = 8 n2 = 7
Is the difference in the means sufficient to warrant that type I is superior to type II regarding
length of life?
Solution :
Null Hypothesis, H0 : 𝜇1 = 𝜇2 , i.e., the two types I and II of electric bulbs are identical.
𝑥̅ − 𝑦̅
𝑡=
1 1
𝑆√𝑛 + 𝑛
1 2
1 1
Where 𝑆 2 = n1 + n2 − 2 [∑(𝑥1 − 𝑥̅1 )2 + ∑(𝑥2 − 𝑥̅2 )2 ] = [𝑛1 𝑛12 + 𝑛2 𝑛22 ]
n1 + n2 − 2
1
= 13 [8 × 362 + 7 × 402 ] = 1659.08
1234−1036 198
𝑡= = = 9.39
1 1
√1659.08 ( + ) √1659.08 ×0.2679
8 7
Tabulated value of t for 13 d.f. at 5% level of significance for right(single) tailed test is 1·77.
Conclusion. Since calculated t is much greater than tabulated t, it is highly significant and H 0
is rejected. Hence the two types of electric bulbs differ significantly.
(il) the two samples are not independent but the sample observations are paired together. i.e.,
the pair of observations (xi , yi), (i =1, 2, ..., n) corresponds to the same (ith) sample unit. The
problem is to test if the sample means differ significantly or not.
For example, suppose we want to test the efficacy of a particular drug, for inducing sleep. Let
xi and yi (i =1, 2, ..., n) be the readings, in hours of sleep, on the ith individual, before and after
the drug is given respectively. We apply the paired t-test given below.
Under the null hypothesis, H0 that increments are due to fluctuations of sampling, i.e., the
drug is not responsible for these increments, the statistic.
𝑑̅
𝑡=
𝑆
√𝑛
1 1
Where 𝑑̅ = 𝑛 ∑𝑛𝑖=1 𝑑𝑖 and 𝑆 2 = 𝑛−1 ∑𝑛𝑖=1(𝑑𝑖 − 𝑑̅ )2
Example : A certain stimulus administered to each of the 12 patients resulted in the following
increase of blood pressure:
Can it be concluded that the stimulus will, in general. be accompanied by an increase in blood
pressure?
Solution. Here we are given the increments in blood pressure i.e., dij (= xi - yi).
Alternative Hypothesis, H1 : 𝜇1 < 𝜇2 , i.e., the stimulus results is an increase in blood pressure.
𝑑̅
𝑡=
𝑆
√𝑛
d : 5 2 8 -1 3 0 -2 1 5 0 4 6 : 31
d2 : 25 4 64 1 9 0 4 1 25 0 16 36 : 185
1 1 (∑ 𝑑) 2
2
𝑆 2 = 𝑛−1 ∑𝑛𝑖=1(𝑑𝑖 − 𝑑̅)2 = 𝑛−1 [∑ 𝑑 − 𝑛 ].
1 312 1
= 11 [185 − ] = 11 (185 − 80.08) = 9.5382.
12
∑𝑑 31
𝑑̅ = 𝑛 = 12 = 2.58.
𝑑̅ 2.58×√12
𝑡= 𝑆 = = 2.89.
√9.5382
√𝑛
If the means for the three populations are equal, we would expect the three sample means to
be close together. In fact, the closer the three sample means are to one another, the weaker
the evidence we have for the conclusion that the population means differ. Alternatively, the
more the sample means differ, the stronger the evidence we have for the conclusion that the
population means differ. In other words, if the variability among the sample means is “small,”
it supports H0; if the variability among the sample means is “large,” it supports H1.
If the null hypothesis, H0 : 𝜇1 = 𝜇2 = ⋯ = 𝜇𝑘 , is true, we can use the variability among the
sample means to develop an estimate of 𝜎 2 . First, note that if the assumptions for analysis of
variance are satisfied and the null hypothesis is true, each sample will have come from the
same normal distribution with mean 𝜇 and variance 𝜎 2 .
Null Hypothesis, we want to test the equality of the population means, i.e., the homogeneity
of different rations. Hence, null hypothesis is given by:
H0 : 𝜇1 = 𝜇2 = ⋯ = 𝜇𝑘
Sum of squares
𝑆𝐸2 = ∑𝑖 ∑𝑗(𝑦𝑖𝑗 − 𝑦̅𝑖. )2, is called within sum of squares or S.S. due to error (S.S.E.)
The sum of squares divided by its degress of freedom gives the corresponding variance or the
mean sum of squares (M.S.S.). Thus
𝑆𝑡2 𝑆. 𝑆. 𝑇.
= = 𝑠𝑡2
𝑘−1 𝑘−1
𝑆𝐸2 𝑆. 𝑆. 𝐸.
= = 𝑠𝐸2
𝑁−𝑘 𝑁−𝑘
ANOVA table
Example : suppose you select independent random samples of five female and five male high
school seniors and record their SAT scores. The data are shown in Table 9.1. A Minitab analysis
of the data, shown in Figure 9.5, reveals that the sample mean SAT scores (shaded) are 590 for
females and 550 for males. Can we conclude that the population of female high school students
scores higher, on average, than the population of male students?
Females Males
530 490
560 520
590 550
620 580
650 610
Module I
1. Explain about Sessions and Functions in R Language.
2. Discuss the Variables and rules for naming a variables with the help of examples.
3. What are the different Data Types in R-Programming.
4. Discuss about Vectors in R-Programming.
5. Demonstrate how to use data frames in R programming with an example.
6. Illustrate the concept of lists in R-programming with an example..
7. Provide an example of how to explain Matrices in R-Programming.
8. Explain Arrays in R-Programming with an example.
9. What is factor? Why it is important in R, Explain with the help of example.
10. What is Class in R Programming how it is different from other programming
languages.
Module II
1. Describe the various control statements in R.
2. Describe the while loop and provide an example using break and continue.
3. What are the various loop types in R programming?
4. Discus about Arithmetic and Boolean Operators in R.
5. Illustrate looping over non-vector sets with example.
6. Explain Functions in R.
7. Explain about Recursion in R programming
8. How R programming manages without pointers? Explain with examples.
9. Describe the implementation Quick sort in R.
10. Describe A Binary Search Tree Using R-Programming.
Module III
1. Define Simulation in R
2. Explain about Math Functions in R.
3. Describe the concepts Calculating Cumulative Sums and Products
4. Discuss Minima and Maxima using R.
5. Implement differentiation and integration with examples.
6. Explain about return values in R programming.
7. Explain the different linear Algebra Operations on Vectors
8. Perform Linear Algebra Operations on Matrices in R.
9. What are Functions For Statistical Distribution in R.
10. Explain Sorting in R-Programming.
11. What are Set Operations in R. Explain with examples.
12. Explain the Input /Output operations in R
13. Explain Stationary Distribution of Markov Chains in R.
14. Describe usage of scan() and readline() functions with examples.
15. Explain the differences between ifelse() and if-else statements.
16. Distinguish between lapply and sapply with suitable examples.
Module IV
1. Explain how to create Graphs.
2. Explain about scatter plot and histograms with examples. Explain its importance.
3. Explain the plot() Function in R.
4. What is points () function? Explain with an example.
5. What is legend () function? Explain with an example.
6. What is text () function? Explain with an example.
7. What is locator () function? Explain with an example.
8. How to plot multiple curves in same graph? Explain with example?
9. Explain the changing Character sizes concept in Customizing Graphs
10. Explain about the Customizing Graphs.
11. What is the procedure for Saving Graphs to Files.
Module V
1. Comment on Probability Distributions.
2. Implement the Normal Distribution in R
3. Give some applications of Binomial Distribution.
4. Explain the Poisson Distributions.
5. Define the functions rnorm(), pnorm(), qnorm(), dnorm() in R-Programming
6. Write R code to generate the probability distribution table for number of successes
from a binomial distribution where n = 5 and probability of success in each trial is
0.25
7. Distinguish between Correlation and Covariance.
8. Define mean, median and mode.
9. Comment on the results of one-sample T-test with example.
10. Implement paired T-Test with an example.
11. Discuss the concept of ANOVA in R-Programming.
12. Discuss about descriptive statistics? Write examples?
Interview Questions
1. Explain the process of running R and starting R sessions.
2. What are the basic data types in R? Provide examples.
3. How do you create and manipulate vectors in R?
4. Describe the concept of data frames in R and their significance.
5. Discuss the use of lists and their advantages in R.
6. Explain the difference between matrices and arrays in R.
7. How do you define and work with classes in R programming?
8. What are control statements in R? Provide examples of conditional and loop structures.
9. How does recursion work in R? Provide an example.
10. What are the arithmetic and boolean operators in R? Give examples.
11. Explain the concept of default values for function arguments in R.
12. How do you decide whether to explicitly call return in a function in R?
13. Discuss the process of sorting data in R. Provide examples.
14. Explain the concept of cumulative sums and products in R. Provide examples.
15. What are statistical distributions, and how are they used in R?
16. Describe the process of accessing input and output in R.
17. How do you create and customize graphs in R using the plot() function?
18. Explain the concept of probability distributions, focusing on the normal distribution,
binomial distribution, and Poisson distribution.
19. Discuss the importance of correlation and covariance in statistics and their calculation in R.
20. What is a t-test, and how is it performed in R? Provide an example.
21. Explain the concept of ANOVA (Analysis of Variance) and its implementation in R.
22. How do you perform linear algebra operations on vectors and matrices in R?
23. What is a binary search tree, and how is it implemented in R?
24. How do you perform simulations and mathematical computations in R?
25. Discuss the process of reading and writing files in R.