0% found this document useful (0 votes)
11 views112 pages

Statistical Programming with R Course

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views112 pages

Statistical Programming with R Course

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A Course File

On

Statistical Programming with R

(III B. Tech VI– Semester-HR21)

Submitted to

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


(CSE/ET)
By

Mr. Shiva Kumar & Mr. Shanmukha Rao


(Dept. of CSE)

HYDERABAD INSTITUTE OF TECHNOLOGY


AND MANAGEMENT
Gowdavelly (V), Medchal Mandal, Hyderabad – 501 401
Hyderabad Institute of Technology and Management

CONTENTS

S. No. Particulars Page No.

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

4 Interview Questions 112

2
Hyderabad Institute of Technology and Management

Syllabus

[Link] III Year – VI Sem L T P C


Subject Code: 21ES6CS05 3 1 0 4

Statistical Programming with R

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

1. List motivation for learning a programming language


2. Import, review, manipulate and summarize data-sets in R
3. Explore data-sets to create testable hypotheses and identify appropriate statistical tests
4. Perform appropriate statistical tests using R Create and edit visualizations with

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

CO-PO &PSO Mapping:

Course Name - Course 1 PSO PSO PSO


1 2 3 4 5 6 7 8 9 11 12
Outcomes / Program Outcomes 0 1 2 3
CO1 M L L

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

Name of the Faculty: [Link] DEPT:ET Sem: VI SEM Regulation:HR21


Kumar & A. Shanmukha Rao
SUB: Statistical Programming with R

[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

15 02-04- Doing Math and 3 Chalk & Math Apply mathematical


2024 Simulation in R, talk,Brainstor Simulation, functions and
Math Function, ming,Coding. Probability simulations in R,
Extended Example calculate probabilities
Calculating using extended
Probability. examples
16 03-04- LAB(1-2) Programs 2 PPT & Coding
2024
17 10-04- LAB(3-4) Programs 2 PPT & Coding
2024
18 16-04- Cumulative Sums 3 Chalk & Cumulative Compute cumulative
2024 and Products- talk,Brainstor Sums, Calculus, sums and products,
Minima and ming,Coding. Statistical apply calculus
Maxima- Calculus, Distribution, concepts, utilize
Functions Fir Sorting statistical
Statistical distributions and
Distribution, sorting algorithms
Sorting.
19 17-04- LAB(5-6) Programs 2 PPT & Coding
2024
20 23-04- Linear Algebra 3 Chalk & Linear Algebra, Perform linear
2024 Operation on talk,Brainstor Vectors, algebra operations,
Vectors and ming,Coding. Matrices, Cross understand vector
Matrices, Extended Product, cross products,
Example,Vector Markov Chains analyze Markov
cross Product- chains for stationary
Extended Example, distribution
Finding Stationary
Distribution of
Markov Chains.
21 24-04- LAB(7-8) Programs 2 PPT & Coding
2024
22 30-04- Set Operation, Set 3 Chalk & Set Operations, Perform set
2024 Operation, Input talk,Brainstor Input/Output, operations, manage
/out put, Accessing ming,Coding. Keyboard/Moni input/output
the Keyboard and tor, Files operations, handle
Monitor,Reading files effectively in R
and writer Files.
23 01-05- LAB(9-10) 2 PPT & Coding
2024 Programs
22

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

29 22-05- LAB(7-9) Programs 2 PPT & Coding


2024
15

UNIT-V

30 11-06- Probability 3 Chalk & Probability Understand various


2024 Distributions, talk,Brainstor Distributions, probability
Normal ming,Coding. Normal distributions,
Distribution. Distribution particularly the
normal distribution
31 12-06- LAB(1-3) Programs 2 PPT & Coding
2024
32 18-06- Binomial 3 Chalk & Binomial Analyze situations
2024 Distribution, Poisson talk,Brainstor Distribution, using binomial and
Distributions. ming,Coding. Poisson Poisson distributions,
Distribution calculate probabilities

33 19-06- LAB(4-6) Programs 2 PPT & Coding


2024
34 25-06- Other Distribution, 3 Chalk & Other Understand various
2024 Basic Statistics, talk,Brainstor Distributions, distributions, perform
Correlation and ming,Coding. Basic Statistics, basic statistical
Covariance. Correlation, analysis, calculate
Covariance correlation and
covariance
35 26-06- LAB(7-9) Programs 2 PPT & Coding
2024
36 T-Tests, ANOVA. 2 Chalk & T-Tests, Perform hypothesis
talk,Brainstor ANOVA testing using T-tests
ming,Coding. and ANOVA in R,
interpret results

17

Total No of Hours = 89
Course Outcomes:
After completion of this course the student is able to

CO1: Proficiency in R programming for statistical analysis.


CO2: Application of advanced statistical techniques using R.
CO3: Development of data manipulation and visualization skills. - PO1, PO2, PO3, PO5, PSO1
CO4: Ability to solve complex statistical problems effectively.. - PO1, PO2, PO3, PO5, PSO
Projects:
[Link] Market Trend Analysis
[Link] Outcome Prediction
[Link] Segmentation and Behavior Analysis

TEXT BOOKS:
T1. The Art of R Programming, A K Verma, Cengage Learning.

T2. The Art of R Programming, Norman Matloff, No starch Press.

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:

➢ Proficient utilization of R programming for comprehensive statistical analysis and


data manipulation.
➢ Mastery in implementing advanced statistical techniques and structures within R for
enhanced data interpretation and problem-solving.
➢ Development of robust data analysis skills, encompassing mathematical modeling,
simulation, and distribution analysis using R
➢ Competency in generating and customizing graphical representations of data, and
conducting statistical inference and hypothesis testing using R.

CO-PO &PSO Mapping:

O-PO/PSO Mapping Chart


(3/2/1 indicates Strength of Correlation)
3 – High, 2-Medium, 1- Low

Course Name - Course 1 PSO PSO PSO


1 2 3 4 5 6 7 8 9 11 12
Outcomes / Program Outcomes 0 1 2 3
CO1 M L L

CO2 H H M M
CO3 M M
CO4 M L L L

Course Projects:

1. Stock Market Trend Analysis


2. Healthcare Outcome Prediction
3. Customer Segmentation and Behavior Analysis
Notes:

Module I : Introduction
The R environment

R is a programming language for statistical computing and data visualization. R is an integrated


suite of software facilities for data manipulation, calculation and graphical display. Among
other things it has

• an effective data handling and storage facility,


• a suite of operators for calculations on arrays, in particular matrices,
• a large, coherent, integrated collection of intermediate tools for data analysis,
• graphical facilities for data analysis and display either directly at the computer or on hard-
copy, and
• a well-developed, simple and effective programming language (called ‘S’) which includes
conditionals, loops, user defined recursive functions and input and output facilities.
(Indeed most of the system supplied functions are themselves written in the S language.)

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 introductory session and functions

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).

Some other introductory function are listed below

ls() See which R objects are now in the R workspace.

rm(x, y) Remove objects no longer needed. (Clean up)

attach(df)Make the columns in the data frame visible as variables.

detach() Remove data frame from the search path.

q() Quit the R program.

c() Combine values into a vector or List.

getwd() Get working directory.

setwd() Set working directory.

apropos() function searches for objects, including functions, directly accessible in the
current R session that have names that include a specified character string.

find() returns where objects of a given name can be found.

Using R as a Calculator/Basic Math

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

Variables are objects in R that you can use to store values.

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...)

> name = "Shanmukha"


> x = 4
> x1 = 20
> X = 23
> x_1 = 40
> CityName = “Hyderabad”
> .x = 99

Basic Data type


Generally, while doing programming in any programming language, you need to use various
variables to store various information. Variables are nothing but reserved memory locations to
store values. This means that, when you create a variable, you reserve some space in memory.

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

All strings, alphabets, text etc.. are classified as character type.

> 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"

Advanced Data Structures

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.

> fruits <- c("banana", "apple", "orange") #character vector


> logical <- c(TRUE, FALSE, TRUE, FALSE) #logical vector
> numbers <- c(1, 2, 3) #numeric vector
> #numerical vectors with sequences
> A = 1:10
> B = 1.5:15.5
#Vector indexing
> fruits[1]
[1] "banana"

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.

Factor objects can be created with the factor() function.

> pain <- c(0,3,2,2,1)


> fpain <- factor(pain, levels=c(0:3))
> levels(fpain) <- c("none", "mild", "medium", "severe")

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.

The result should be apparent from the following:

> 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.

> ranks = c(1,2,3,1,4,2,3,1,4,2,3,2,1,4,1,2,3,3,4,1)


> franks = factor(ranks, levels = c(4:1), ordered = TRUE)
> franks
[1] 1 2 3 1 4 2 3 1 4 2 3 2 1 4 1 2 3 3 4 1
Levels: 4 < 3 < 2 < 1
Lists

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.

> x <- list(1, "a", TRUE, 1 + 4i)


> x
[[1]]
[1] 1

[[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:

> x <- 1:12


> dim(x) <- c(3,4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

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.

A convenient way to create matrices is to use the matrix function:

> 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.

> cbind(A=1:4, B=5:8, C=9:12)


A B C
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> rbind(A=1:4,B=5:8,C=9:12)
[,1] [,2] [,3] [,4]
A 1 2 3 4
B 5 6 7 8
C 9 10 11 12

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:

> x <- matrix(1:12,nrow=3,byrow=T)


> rownames(x) <- LETTERS[1:3]
> x
[,1] [,2] [,3] [,4]
A 1 2 3 4
B 5 6 7 8
C 9 10 11 12
> t(x)
A B C
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

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.

The only difference between vectors, matrices, and arrays are

• Vectors are uni-dimensional arrays

• Matrices are two-dimensional arrays

• Arrays can have 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

[,1] [,2] [,3] [,4]


[1,] 25 26 31 24
[2,] 28 27 32 26
[3,] 30 29 33 27

, , 2

[,1] [,2] [,3] [,4]


[1,] 28 32 29 26
[2,] 29 30 31 25
[3,] 30 28 27 24

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.

The data frames in R can be created using [Link]( ).

> std_data = [Link]( id = c(1:5), gender = c("Male", "Female",


"Male", "Female", "Male"), age = c(25, 26, 31, 24, 29))
> std_data
id gender age
1 1 Male 25
2 2 Female 26
3 3 Male 31
4 4 Female 24
5 5 Male 29

We can create data frames from pre-existing variables:


> gender = c("Male", "Female", "Male", "Female", "Male", "Male",
"Female", "Male", "Female", "Male")
> age=c(25, 26, 31, 24, 29, 30, 28, 27, 24, 22)
> [Link] = [Link]( id, gender, age)
> [Link]
id gender age
1 1 Male 25
2 2 Female 26
3 3 Male 31
4 4 Female 24
5 5 Male 29
6 6 Male 30
7 7 Female 28
8 8 Male 27
9 9 Female 24
10 10 Male 22

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,

Module II : R Programming Structures


LOOP IN R

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.

Different ways to write loop in R

• For Loop
• While Loop
• Apply Family of Functions such as apply, lapply, sapply, etc.

Apply Family of Functions/ Looping Over Non-vector Sets in R

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:

• apply( ) : Applies a function over the margins of an array or matrix.


• lapply( ) : Applies a function over a list or a vector.
• sapply( ) : Similar to lapply( ) but with simplified results.
• tapply( ) : Applies a function over a ragged array.
• mapply( ) : Multivariate version of lapply( ).

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.

The basic syntax for the apply() function is as follows:

apply(X, MARGIN, FUN)

• X is the name of the matrix or data frame.


• MARGIN indicates which dimension to perform an operation across (1 = row, 2 = column).
• FUN is the specific operation you want to perform (e.g. min, max, sum, mean, etc.).

Consider a sample data set mtcars

Motor Trend Car Road Tests: It is a data frame with 32 observations on 11 (numeric)
variables.

Example 1 : Find Maximum value of each column and round up it to 1 decimal.

> round(apply(mtcars, 2, max),1)


mpg cyl disp hp drat wt qsec vs am gear carb
33.9 8.0 472.0 335.0 4.9 5.4 22.9 1.0 1.0 5.0 8.0

In the second parameter of apply function, 2 denotes the function to be applied at column
level.

Example 2 : Find Maximum value of last 4 row

> apply(tail(mtcars,4), 1, max)


Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E
351 175 335 121

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.

The basic syntax for the lapply() function is as follows:

lapply(X, FUN)

• X is the name of the list, vector, or data frame.


• FUN is the specific operation you want to perform.
• Example 1 : Calculate Median of each of the variables

> lapply( mtcars, function(x) median(x, [Link] = TRUE) )

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

Example 2 : Apply a custom function

> lapply(mtcars, function(x) x - mean(x))

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)

• X is the name of the list, vector, or data frame.


• FUN is the specific operation you want to perform.
• Example 1 : Number of Missing Values in each Variable

> sapply(mtcars, function(x) sum([Link](x)))

The above function returns 0 for all variables in data frame mtcars.

Example 2 : Extract names of all numeric variables in IRIS dataset

> 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.

lapply( ) and sapply( ) together

In this example, we would show you how both lapply() and sapply() are used simultaneously to
solve the problem.

Converting Factor Variables to Numeric

The following code would convert all the factor variables of data frame iris to numeric types
variables.

> index <- sapply(iris, [Link])


> lapply(iris[index], function(x) [Link](x))
$Species
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3

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.

This function uses the following basic syntax:

The basic syntax for the tapply() function is as follows:

tapply(X, INDEX, FUN)

• X is the name of the object, typically a vector.


• INDEX is a list of one or more factors.
• FUN is the specific operation you want to perform.

Example 1 : The max [Link] of each of the three Species

> tapply(iris$[Link], iris$Species, max)

setosa versicolor virginica


5.8 7.0 7.9

Example 2 : The minimum [Link] of each of the three Species

> tapply(iris$[Link], iris$Species, min)

setosa versicolor virginica


0.1 1.0 1.4

5. mapply( )

The mapply( ) function in R can be used to apply a function to multiple list or vector arguments.

This function uses the following basic syntax:

mapply(FUN, …, MoreArgs = NULL, SIMPLIFY = TRUE, [Link] = TRUE)

• FUN : The function to apply.


• …: Arguments to vectorize over.
• MoreArgs : A list of other arguments to FUN.
• SIMPLIFY : Whether or not to reduce the result to a vector.
• [Link] : Whether or not to use names if the first … argument has names.

Example 1 : Use mapply( ) to create a matrix

The following code shows how to use mapply() to create a matrix by repeating the values c(1, 2, 3)
each 5 times:

> mapply(rep, 1:3, times=5)


[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
[4,] 1 2 3
[5,] 1 2 3
Notice how this is much more efficient than typing out the following:

create same matrix as previous example

> matrix(c(rep(1, 5), rep(2, 5), rep(3, 5)), ncol=3)

[,1] [,2] [,3]


[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
[4,] 1 2 3
[5,] 1 2 3

Example 2 : Use mapply() to find max value of corresponding elements in vectors

The following code shows how to use mapply() to find the max value for corresponding elements in
two vectors:

> vector1 <- c(1, 2, 3, 4, 5)


> vector2 <- c(2, 4, 1, 2, 10)
> mapply(max, vector1, vector2)

[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.

Example 3 : Use mapply() to multiply corresponding elements in vectors

The following code shows how to use mapply() to find multiply the corresponding elements in
several vectors:

> vec1 <- c(1, 2, 3, 4)


> vec2 <- c(2, 4, 6, 8)
> vec3 <- c(3, 6, 9, 12)
> mapply(function(val1, val2, val3) val1*val2*val3, vec1, vec2,
vec3)

[1] 6 48 162 384

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.

Example 1 : Maximum value of each column

> 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 -

> x = vector("double", ncol(dat))


> for (i in seq_along(dat)){
x[i]= max(dat[i], [Link] = TRUE)}
> x

The vector function can be used to create an empty vector. The seq_along finds out what to
loop over.

Example 2 : Split IRIS data based on unique values in "species" variable

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.

Combine / Append data within loop

In the example below, we are combining / appending rows in iterative process.

Method 1 : Use [Link]() with rbind()

[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.

> temp =list()


> for (i in 1:length(unique(iris$Species))) {
series = [Link](Species = [Link]( unique( iris$Species
))[i])
temp[[i]] = series
}
> output = [Link](rbind, temp)
> output
Species
1 setosa
2 versicolor
3 virginica

Method 2 : Use standard looping technique

In this case, we are first creating an empty table (data frame). Later we are appending data to
empty data frame.

> dummydt = [Link](matrix(ncol=0,nrow=0))


> for (i in 1:length(unique(iris$Species))) {
series = [Link](Species = [Link](unique(iris$Species
))[i])
if (i==1) {
output = rbind(dummydt, series)}
else {
output = rbind(output, series)}
}
> output
For loop and sapply( ) together

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.

> for (i in which(sapply(dat, [Link]))) {


dat[[Link](dat[, i]), i] <- median(dat[, i], [Link] = TRUE)
}

Create new columns in Loop

Suppose you need to standardise multiple variables. To accomplish this task, we need to
execute the following steps -

Identify numeric variables

Calculate Z-score i.e. subtracting mean from original values and then divide it by standard
deviation of the raw variable.

Run Step2 for all the numeric variables

Make names of variables based on original names. For example x1_scaled.

Create dummy data

> mydata = [Link](x1 = sample(1:100,100), x2=sample(letters,


100, replace = TRUE), x3 = rnorm(100))

Standardize Variables

> lst=list()
> for (i in which(sapply(mydata, [Link]))) {
[Link] = (mydata[,i] - mean(mydata[,i])) /sd(mydata[,i])
lst[[i]] = [Link]
}

> names(lst)=paste(names(sapply(mydata, [Link])), "_scaled" ,


sep="")
> [Link] = [Link]([Link](cbind, lst))

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.

In the example below, we are checking whether a number is an odd or even,


> i=1
> while(i<7)
{
if(i%%2==0)
print(paste(i, "is an Even number"))
else if(i%%2>0)
print(paste(i, "is an Odd number"))
i=i+1
}

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:

[1] "1 is an Odd number"


[1] "2 is an Even number"
[1] "3 is an Odd number"
[1] "4 is an Even number"
[1] "5 is an Odd number"
[1] "6 is an Even number"

Loop Concepts : Break and Next

Break Keyword

When a loop encounters 'break' it stops the iteration and breaks out of loop.

> for (i in 1:3) {


for (j in 3:1) {
if ((i+j) > 4) {
break }
else {
print(paste("i=", i, "j=", j))
}
}
}
Output :

[1] "i= 1 j= 3"


[1] "i= 1 j= 2"
[1] "i= 1 j= 1"

In this case, as condition i+j >4 is met, it 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 :

[1] "i= 1 j= 3"


[1] "i= 1 j= 2"
[1] "i= 1 j= 1"
[1] "i= 2 j= 2"
[1] "i= 2 j= 1"
[1] "i= 3 j= 1"

If you get confused between 'break' and 'next', compare the output of both and see the
difference.

R if else conditions

R has two different if else statements: if-else and ifelse().

ifelse()

The ifelse() function in base R can be used to write quick if-else statements. This function uses
the following syntax:

ifelse(test, yes, no)

• test : A logical test.


• yes : The value to return if the logical test is True.
• no : The value to return if the logical test is False.

Example 1 : Create a new column, if mpg is more than 20 then “high” else “low”.

> mt= mtcars #coping mtcars data, so original data remains


> mt$mpg.c = ifelse(mt$mpg > 20,"High","Low")
> head(mt)
mpg cyl disp hp drat wt qsec vs am gear
carb mpg.c
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4
4 High
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4
4 High
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4
1 High
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3
1 High
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3
2 Low
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3
1 Low

Example 2 : Using nested ifelse() to code values of cylinders as 1, 2 and 3.

> mt$cyl.c = ifelse(mt$cyl == 4,1,ifelse(mt$cyl == 6, 2, 3))


> head(mt)
mpg cyl disp hp drat wt qsec vs am gear
carb mpg.c cyl.c
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4
4 High 2
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4
4 High 2
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4
1 High 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3
1 High 2
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3
2 Low 3
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3
1 Low 2

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

• test : A logical test.


• yes : The value to return if the logical test is True.
• no : The value to return if the logical test is False.

Example 1 : Check whether the user is eligible for vote or not.

> age = [Link](readline("Enter your age: "))


Enter your age: 20
> if(age<18)
+ {
+ "You cannot vote this year"
+ } else
+ {
+ "Congratulations! You can vote"
+ }
[1] "Congratulations! You can vote"

Example 2 : Check whether the number is even or odd.

> num = [Link](readline("Enter any number: "))


Enter any number: 44
> if(num %% 2 == 0)
+ {
+ "Even number"
+ } else
+ {
+ "Odd number"
+ }
[1] "Even number"

Ifelse() vs if-else statement

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.

Operators in R can be organized into the following groups:

• Arithmetic operators for traditional mathematical evaluations such as addition and


subtraction.
• Assignment operators for assigning values to variables.
• Comparison operators for testing equality between values.
• Logical operators for evaluating the “truthiness” of values against one another.
• Miscellaneous operators for various tasks including vectors and sequencing.

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:

• Addition, +, which returns the sum of two numbers.


• Subtraction, -, which returns the difference between two numbers.
• Multiplication, *, which returns the product of two numbers.
• Division, /, which returns the quotient of two numbers.
• Exponents, ** or ^, which returns the value of one number raised to the power of another.
• Modulus, %%, which returns the remainder of one number divided by another.
• Integer Division, %/%, which returns the integer quotient of two numbers.

> 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

Comparison/ Relational 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:

• Equal, ==, which returns TRUE if two values are equal.


• Not equal, !=, which returns TRUE if two values are not equal.
• Less than, <, which returns TRUE if left value is less than right value.
• Less than or equal to, <=, which returns TRUE if left value is less than or equal to right
value.
• Greater than, >, which returns TRUE if left value is greater than right value.
• Greater than or equal to, >=, which returns TRUE if left value is greater than or equal to
right value.

> "india" == "india"


[1] TRUE
> 29 > 29
[1] FALSE
> 29 >= 29
[1] TRUE
> 29 < 29
[1] FALSE
> 29 <= 29
[1] TRUE
> 29 != 29
[1] FALSE

Logical/ Boolean 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.

> x = c(TRUE, FALSE, TRUE);x


[1] TRUE FALSE TRUE
> y = c(TRUE, TRUE, FALSE)
[1] TRUE TRUE FALSE
> x & y
[1] TRUE FALSE FALSE
> x[1] && y[1]
[1] TRUE
> x && y # looks at just the first elements of each vector
[1] TRUE
> if (x[1] && y[1]) print("both TRUE")
[1] "both TRUE"
> if (x & y) print("both TRUE")
[1] "both TRUE"
Warning message:
In if (x & y) print("both TRUE") :
the condition has length > 1 and only the first element will be
used

Miscellaneous operators

R uses the following 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.

Default Values for Arguments

Now consider the function head(). We know that, by default the head() gives first 6
records from the data frame.

> head(mtcars, n=10)

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.

If we observe the syntax of the head():

> head(x, n = 6L, ...)

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:

oddcount <- function(x) {


k <- 0
for (n in x) {
if (n %% 2 == 1) k <- k+1
}
k
}

Deciding whether to explicitly call return()

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.

Consider our second example from the preceding section:

oddcount <- function(x) {


k <- 0
for (n in x) {
if (n %% 2 == 1) k <- k+1
}
k }

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
}

Functions are objects

Let’s create a vector

> v <- c(1,3,7,2,6)


At any point of time, if we want to see the content of v, we simply type “v”and the contents
are displayed.

> 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:

> x <- c(13,5,12)


> sort(x)
[1] 5 12 13
> x
[1] 13 5 12

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:

> x <- sort(x)


>x
[1] 5 12 13
Recursion

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.

Example 1 : Factorial of a number using recursion.

> fac <- function(n)


+ {
+ if(n==0)
+ {
+ fa=1
+ }
+ else
+ {
+ fa=n*fac(n-1)
+ }
+ fa
+ }
> fac(5)
[1] 120

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().

> qs <- function(x) {


+ if (length(x) <= 1) return(x)
+ pivot <- x[1]
+ therest <- x[-1]
+ sv1 <- therest[therest < pivot]
+ sv2 <- therest[therest >= pivot]
+ sv1 <- qs(sv1)
+ sv2 <- qs(sv2)
+ return(c(sv1,pivot,sv2))
+ }
> x=c(2,21,3,4,13,55,8,89,1,57)
> qs(x)
[1] 1 2 3 4 8 13 21 55 57 89

Note carefully the termination condition: if (length(x) <= 1) return(x)

Without this, the function would keep calling itself repeatedly on empty vectors, executing
forever.

Extended Example: A Binary Search Tree

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.

Remember that allocating space for a matrix in R is a time-consuming activity. In an effort to


amortize the memory-allocation time, we allocate new space for a tree’s matrix several rows
at a time, instead of row by row. The number of rows allocated each time will be given in the
variable inc. As is common with tree traversal, we implement our algorithm with recursion.

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 <- newtree(8,3)


What happened here? First, the command containing our call newtree(8,3) creates a new
tree, assigned to x, storing the number 8. The argument 3 specifies that we allocate storage
room three rows at a time. The result is that the matrix component of the list x is now as
follows:

> x
$mat
[,1] [,2] [,3]
[1,] NA NA 8
[2,] NA NA NA
[3,] NA NA NA

$nxt
[1] 2

$inc
[1] 3

> x <- ins(1,x,5)


Three rows of storage are indeed allocated, and our data now consists just of the number 8.
The two NA values in that first row indicate that this node of the tree currently has no
children.

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.

> x <- ins(1,x,6)


> x
$mat
[,1] [,2] [,3]
[1,] 2 NA 8
[2,] NA 3 5
[3,] NA NA 6

$nxt
[1] 4

$inc
[1] 3

> x <- ins(1,x,2)


> x
$mat
[,1] [,2] [,3]
[1,] 2 NA 8
[2,] 4 3 5
[3,] NA NA 6
[4,] NA NA 2
[5,] NA NA NA
[6,] NA NA NA

$nxt
[1] 5

$inc
[1] 3

> x <- ins(1,x,20)


The session continues in this manner. Note that when our initial allotment of three rows is
full, ins() allocates three new rows, for a total of six. In the end, the matrix is as follows:

> 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

# the tree is represented as a list (mat,nxt,inc), where mat is


the
# matrix, nxt is the next empty row to be used, and inc is the
number of
# rows of expansion to be allocated whenever the matrix becomes
full

# print sorted tree via in-order traversal


printtree <- function(hdidx,tr) {
left <- tr$mat[hdidx,1]
if (![Link](left)) printtree(left,tr)
print(tr$mat[hdidx,3]) # print root
right <- tr$mat[hdidx,2]
if (![Link](right)) printtree(right,tr)
}
# initializes a storage matrix, with initial stored value firstval
newtree <- function(firstval,inc) {
m <- matrix(rep(NA,inc*3),nrow=inc,ncol=3)
m[1,3] <- firstval
return(list(mat=m,nxt=2,inc=inc))
}
# inserts newval into the subtree of tr, with the subtree's root
being
# at index hdidx; note that return value must be reassigned to tr
by the
# caller (including ins() itself, due to recursion)
ins <- function(hdidx,tr,newval) {
# which direction will this new node go, left or right?
dir <- if (newval <= tr$mat[hdidx,3]) 1 else 2
# if null link in that direction, place the new node here,
otherwise recurse
if ([Link](tr$mat[hdidx,dir])) {
newidx <- tr$nxt # where new node goes
# check for room to add a new element
if (tr$nxt == nrow(tr$mat) + 1) {
tr$mat <-
rbind(tr$mat,
matrix(rep(NA,tr$inc*3),nrow=tr$inc,ncol=3))
}
# insert new tree node
tr$mat[newidx,3] <- newval
# link to the new node
tr$mat[hdidx,dir] <- newidx
tr$nxt <- tr$nxt + 1 # ready for next insert
return(tr)
} else tr <- ins(tr$mat[hdidx,dir],tr,newval)
}

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.

Module III : DOING MATH AND SIMULATIONS IN R


Math Functions

R includes an extensive set of built-in math functions. Here is a partial list:

• exp() : Exponential function, base e


• log() : Natural logarithm
• log10() : Logarithm base 10
• sqrt() : Square root
• abs() : Absolute value
• sin(), cos(), and so on : Trigonometric functions
• min() and max() : Minimum value and maximum value within a vector
• [Link]() and [Link]() : Index of the minimal element and maximal element
of a vector
• pmin() and pmax() : Element-wise minima and maxima of several vectors
• sum() and prod() : Sum and product of the elements of a vector
• cumsum() and cumprod() : Cumulative sum and product of the elements of a vector
• round(), floor(), and ceiling() : Round to the closest integer, to the closest
integer below, and to the closest integer above
• factorial() : Factorial function

Extended Example: Calculating a Probability

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:
𝑛

∑ 𝑝𝑖 (1 − 𝑝1 ) … (1 − 𝑝𝑖−1 )(1 − 𝑝𝑖+1 ) … (1 − 𝑝𝑛 )


1=1

(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 𝑝:

> exactlyone <- function(p) {


notp <- 1 - p
tot <- 0.0
for (i in 1:length(p))
tot <- tot + p[i] * prod(notp[-i])
return(tot)
}

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.

Cumulative Sums and Products


As mentioned, the functions cumsum() and cumprod() return cumulative sums and
products.

> x <- c(12,5,13)


> cumsum(x)
[1] 12 17 30
> cumprod(x)
[1] 12 60 780

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.

Minima and Maxima

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).

> nlm(function(x) return(x^2-sin(x)),8)


$minimum
[1] -0.2324656
$estimate
[1] 0.4501831
$gradient
[1] 4.024558e-09
$code [1] 1
$iterations
[1] 5

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.

Here's an example of using the nlm() function to minimize a 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.

optim(): The optim() function is a more general optimization function in R. It provides


a variety of optimization algorithms, including Nelder-Mead, BFGS, and simulated annealing.
The function can handle both constrained and unconstrained optimization problems.

Here's an example of using the optim() function to minimize a function:

> result <- optim(par = 0, f)


$par
[1] -1.4

$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.

> D(expression(exp(x^2)),"x") # derivative


exp(x^2) * (2 * x)
> integrate(function(x) x^2,0,1)
0.3333333 with absolute error < 3.7e-15

Here, R reported

𝑑 𝑥2 2
𝑒 = 2𝑥𝑒 𝑥
𝑑𝑥

and
1
∫ 𝑥 2 𝑑𝑥 ≈ 0.333333
0

Functions for Statistical Distributions

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:

• With d for the density or probability mass function (pmf)


• With p for the cumulative distribution function (cdf)
• With q for quantiles
• With r for random number generation

The rest of the name indicates the distribution.

Common R Statistical Distribution Functions

Distribution pdf/pmf cdf Quantiles Random Numbers


Normal dnorm() pnorm() qnorm() rnorm()
Binomial dbinom() pbinom() qbinom() rbinom()
Chi square dchisq() pchisq() qchisq() rchisq()

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:

> x <- c(13,5,12,5)


> sort(x)
[1] 5 51213
> x
[1]13 512 5

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.

> x <- c(13,5,12,5)


> rank(x)
[1] 4.0 1.5 3.0 1.5
This says that 13 had rank 4 in x; that is, it is the fourth smallest. The value 5 appears twice in
x, with those two being the first and second smallest, so the rank 1.5 is assigned to both.
Optionally, other methods of handling ties can be specified.

Linear Algebra Operations on Vectors and Matrices

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

The function computed 1 · 5 + 2 · 12 + 3 · 13 = 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

Here’s the code:

> 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

Here’s the code:

> a <- matrix(c(1,1,-1,1),nrow=2,ncol=2)


> b <- c(2,4)
> solve(a,b)
[1] 3 1
> solve(a)
[,1] [,2]
[1,] 0.5 0.5
[2,] -0.5 0.5

In that second call to solve(), the lack of a second argument signifies that we simply wish
to compute the inverse of the matrix.

Here are a few other linear algebra functions:

• t() : Matrix transpose


• qr() : QR decomposition
• chol() : Cholesky decomposition
• det() : Determinant
• eigen() : Eigenvalues/eigenvectors
• diag() : Extracts the diagonal of a square matrix (useful for obtaining variances from a
covariance matrix and for constructing a diagonal matrix).
• sweep() : Numerical analysis sweep operations

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).

Extended Example : Vector Cross Product

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

Our need to calculate submatrices—fits perfectly with R, which excels at specifying


submatrices. This suggests calling det() on the proper submatrices, as follows:

xprod <- function(x, y) {


m <- rbind(rep(NA,3),x, y)
xp <- vector(length=3)
for (i in 1:3)
xp[i] <- -(-1)^i * det(m[2:3,-i])
return(xp)
}

Note that even R’s ability to specify values as NA came into play here to deal with the
“placeholders” mentioned above.

Extended Example: Finding Stationary Distributions of Markov Chains

A Markov chain is a random process in which we move among various states, in a


“memoryless” fashion, whose definition need not concern us here. The state could be the
number of jobs in a queue, the number of items stored in inventory, and so on. We will assume
the number of states to be finite.

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.

We are interested in calculating the vector 𝜋 = (𝜋1 , . . . , 𝜋𝑠 ), where 𝜋𝑖 is the long-run


proportion of time spent at state i, over all states i. Let P denote the transition probability
matrix whose ith row, jth column element is pij . Then it can be shown that 𝜋 must satisfy

𝜋 = 𝜋𝑃

which is equivalent to Equation

(𝐼 − 𝑃𝑇 )𝜋 = 0

Here I is the identity matrix and PT denotes the transpose of P.

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.

But note that there is also the constraint shown in equation.

∑ 𝜋𝑖 = 1
𝑖

In matrix terms, this is as follows:

1𝑇𝑛 𝜋 = 1

where 1n is a vector of n 1’s. So, in the modified version of equation (𝐼 − 𝑃𝑇 )𝜋 = 0, we replace


the removed row with a row of all 1’s and, on the right-hand side, replace the removed 0 with
a 1. We can then solve the system.

All this can be computed with R’s solve() function, as follows:

> findpi1 <- function(p) {


n <- nrow(p)
imp <- diag(n) - t(p)
imp[n,] <- rep(1,n)
rhs <- c(rep(0,n-1),1)
pivec <- solve(imp,rhs)
return(pivec)
}

Here are the main steps:

• 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():

• It may have negative components. If so, we multiply by −1.


• It may not satisfy equation (∑𝑖 𝜋𝑖 = 1) . We remedy this by dividing by the length of the
returned vector.

Here is the code:

1 findpi2 <- function(p) {


2 n <- nrow(p)
3 # find first eigenvector of P transpose
4 pivec <- eigen(t(p))$vectors[,1]
5 # guaranteed to be real, but could be negative
6 if (pivec[1] < 0) pivec <- -pivec
7 # normalize to sum to 1
8 pivec <- pivec / sum(pivec)
9 return(pivec)
10 }

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

R includes some handy set operations, including these:


• union(x,y): Union of the sets x and y
• intersect(x,y): Intersection of the sets x and y
• setdiff(x,y): Set difference between x and y, consisting of all elements of x that are
not in y
• setequal(x,y): Test for equality between x and y
• c %in% y: Membership, testing whether c is an element of the set y
• choose(n,k): Number of possible subsets of size k chosen from a set of size n

Here are some simple examples of using these functions:

> x <- c(1,2,5)


> y <- c(5,1,8,9)
> union(x,y)
[1] 1 2 5 8 9
> intersect(x,y)
[1] 1 5
> setdiff(x,y)
[1] 2
> setdiff(y,x)
[1] 8 9
> setequal(x,y)
[1] FALSE
> setequal(x,c(1,2,5))
[1] TRUE
> 2 %in% x
[1] TRUE
> 2 %in% y
[1] FALSE
> choose(5,2)
[1] 10

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))
}

Let’s try it.


> x
[1] 1 2 5
> y
[1] 5 1 8 9
> symdiff(x,y) [1] 2 8 9

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:

> "%subsetof%" <- function(u,v) {


+ return(setequal(intersect(u,v),u)) +}
> c(3,8) %subsetof% 1:10
[1] TRUE
> c(3,8) %subsetof% 5:10
[1] FALSE

The function combn() generates combinations. Let’s find the subsets of {1,2,3} of size 2.

> c32 <- combn(1:3,2)


> c32
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 2 3 3
> class(c32)
[1] "matrix"

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

The first subset, {1, 2}, has a sum of 3, and so on.

INPUT/OUTPUT

Accessing the Keyboard and Monitor


R provides several functions for accessing the keyboard and monitor. Here, we’ll look at the
scan(), readline(), print(), and cat() functions.

Using the scan() Function

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

The [Link] file contents are as follows:

123
4.2 5
6

The [Link] file contains this:

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:

> v <- scan("[Link]")

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:

> x1 <- scan("[Link]",what="")


Read 4 items
> x2 <- scan("[Link]",what="",sep="\n")
Read 3 items
> x1
[1] "abc" "de" "f" "g"
> x2
[1] "abc" "de f" "g"
> x1[2]
[1] "de"
> x2[2]
[1] "de f"

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:

> v <- scan("")


1: 12 5 13
4: 3 4 5
7: 8
8:
Read 7 items
> v
[1]12 513 3 4 5 8

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.

Using the readline() Function

If you want to read in a single line from the keyboard, readline() is very handy.

> w <- readline()


abc de f
> w
[1] "abc de f"
Typically, readline() is called with its optional prompt, as
follows:
> inits <- readline("type your initials: ")
type your initials: NM
> inits
[1] "NM"

Printing to the Screen

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:

> x <- 1:3


> print(x^2)
[1] 1 4 9

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.

The arguments to cat() will be printed out with intervening spaces:

> 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

You can even set sep to be a vector of strings, like this:

> x <- c(5,12,13,8,88)


> cat(x,sep=c(".",".",".","\n","\n"))
[Link]
88

Reading and Writing Files

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.

Reading a Data Frame or Matrix from a File

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:

> z <- [Link]("z", header=TRUE)


> z
name age
1 John 25
2 Mary 28
3 Jim 19

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

We can read it into a matrix this way:

> x <- matrix(scan("x"), nrow=5, byrow=TRUE)

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:

> [Link] <- function(filename) {


[Link]([Link](filename))
}

Reading Text Files

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

We can read the file all at once, like this:

> z1 <- readLines("z1")


> z1
[1] "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.

> kids <- c("Jack", "Jill")


> ages <- c(12,10)
> d <- [Link](kids, ages, stringsAsFactors=FALSE)
>d
kids ages
1 Jack 12
2 Jill 10
> [Link](d,"kds")
The file kds will now have these contents:
"kids" "ages"
"1" "Jack" 12
"2" "Jill" 10

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

You can write multiple fields as well. So:

> cat(file="v",1,2,"xyz\n")

would produce a file v consisting of a single line:

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:

> c <- file("www","w")


> writeLines(c("abc","de","f"),c)
> close(c)

The file www will be created with these contents:

abc
de
f

Note the need to proactively close the file.

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 Workhorse of R Base Graphics: The plot() Function

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.

> plot(c(1,2,3), c(1,2,4))

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:

> plot(c(-3,3), c(-1,5), type = "n", xlab="x", ylab="y")

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:

> x <- c(1,2,3)


> y <- c(1,3,8)
> plot(x,y)
> lmout <- lm(y ~ x)
> abline(lmout)

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)

Starting a New Graph While Keeping the Old Ones

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:

• On Linux systems, call X11().


• On a Mac, call macintosh().
• On Windows, call windows().

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)

Adding Points: The points() Function

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)

Adding a Legend: The legend() Function

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)

Adding Text: The text() Function

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:

> text(46.7,0.02,"Exam 1")


> text(12.3,0.008,"Exam 2")

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.)

Here is a simple example:

> 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.

To use this information to place text, combine it with text():

> 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.

Changing Character Sizes: The cex Option

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:

> text(2.5,4,"abc",cex = 1.5)

This prints the same text as in our earlier example but with characters 1.5 times the normal
size.

Changing the Range of Axes: The xlim and ylim Options

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)

Data: testscores$Exam1 (39 obs.); Bandwidth 'bw' = 6.967

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:

> plot(c(0, 100), c(0, 0.03), type = "n", xlab="score",


ylab="density")
> lines(d2)
> lines(d1)

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.

> f <- function(x) return(1-exp(-x))


> curve(f,0,2)
> polygon(c(1.2,1.4,1.4,1.2),c(0,0,f(1.3),f(1.3)),col="gray")

The result is shown in figure.

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)

Smoothing Points: The lowess() and loess() Functions

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.

Graphing Explicit Functions

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:

> g <- function(t) { return (t^2+1)^0.5 } # define g()


> x <- seq(0,5,length=10000) #x =[0.0004,0.0008,0.0012,..., 5]
> y <- g(x) # y = [g(0.0004), g(0.0008), g(0.0012), ..., g(5)]
> plot(x,y,type="l")

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.

You can also use plot(), as follows:

> f <- function(x) return((x^2+1)^0.5)


> plot(f,0,5) # the argument must be a function name

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.

Saving Graphs to Files

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

Let’s open a file:

> 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?

Saving the Displayed Graph

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.

Module V : Probability Distributions

A random variable is a numerical description of the outcome of an experiment. In effect, a


random variable associates a numerical value with each possible experimental outcome. The
particular numerical value of the random variable depends on the outcome of the experiment.
A random variable can be classified as being either discrete or continuous depending on the
numerical values it assumes.

Discrete Random Variable

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.

Continuous Random Variables

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.

Discrete Probability Distributions

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.

The classical method of assigning probabilities to values of a random variable is applicable


when the experimental outcomes generate values of the random variable that are equally
likely. for instance, consider the experiment of rolling a die and observing the number on the
upward face. It must be one of the numbers 1, 2, 3, 4, 5, or 6 and each of these outcomes is
equally likely. Thus, if we let x = number obtained on one roll of a die and P (x) = the
probability of x, the probability distribution of x is given in Table.

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.

Now, we have to find out the probability of x successes in n trails.

Let us suppose that a particular order of outcomes of x successes in n repetitions be as


follows

SSSSSFFFSSFS………FS(x number of successes and n-x failures)

Since, the trails are all independent the probability for the joint occurrence of the event is

pppppqqqppqp……..qp

= (pppppp…..x times)(qqqqqq…… (n-x) times)

= 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

Probability of x successes in n trails is


x n− x
P(X=x) = c x p q , x = 0,1,2,........, n
n

This is called probability distribution of Binomial random variable X or simply Binomial


distribution. Symbolically this can be written as B(X; n, p)

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

Where n and p are called parameters of the binomial distribution.

Mean : np
Variance : npq

Uses of Binomial Distribution:

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.

• When n is very indefinitely very large


• Probability of success is very small.
• 𝑛𝑝 = 𝜆 is finite, 𝜆 𝜖 𝑅 +

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!

Where e = 2.7183 and   0

Here  is called the parameter of the Poisson distribution.

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

1. The number of blinds born per year in a large city.


2. The number of printing mistakes per page in a large volume of a book.
3. The number of air pockets in a glass sheet.
4. The number of accidents occurred annually at a busy crossing of city.
5. The number of defective articles produced by a quality machine.
6. This is widely used in waiting lines or queuing problems in management studies.
7. It has wide applications in industrial quality control.
8. In determining the number of deaths in a given period by a rare disease.

Continuous Probability Distributions

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

Normal distribution is an approximation of binomial distribution under certain conditions.

• n, the number of trails is indefinitely large, i.e. n → 


• neither p nor q is very small.

Definition: A continuous random variable X is said to have a normal distribution with


parameters  and  if its density function is given by the probability law
2

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, Variables, and Observations

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.

Categorical and Quantitative Data

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.

A categorical variable is a variable with categorical data, and a quantitative variable is a


variable with quantitative data. The statistical analysis appropriate for a particular variable
depends upon whether the variable is categorical or quantitative. If the variable is categorical,
the statistical analysis is limited. We can summarize categorical data by counting the number
of observations in each category or by computing the proportion of the observations in each
category. However, even when the categorical data are identified by a numerical code,
arithmetic operations such as addition, subtraction, multiplication, and division do not provide
meaningful results.

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

The sample mean class size is 44 students.

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.

Arrange the data in ascending order (smallest value to largest value).

(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.

Q1 = first quartile, or 25th percentile

Q2 = second quartile, or 50th percentile (also the median)

Q3 = third quartile, or 75th percentile

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,

Q1 = 3850 + .25(3880 – 3850) = 3850 + .25(30) = 3857.5


3 3
For Q3, 𝑄3 = (𝑛 + 1) = (12 + 1) = 9.75
4 4

The third quartile is .75 of the way between the value in position 9(3950) and the value in
position 10 (4050). Thus,

Q3 = 3950 + .75(4050 – 3950) = 3950 + .75(100) = 4025.

Measures of Variability or Dispersion

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:

“Dispersion is the measure of variation of the variables about a central value”.

“Dispersion is a measure of the extent to which the individual items vary”.


Some commonly used measures of variability.

Range

The simplest measure of variability is the range.

Range = Largest value - Smallest value

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

Number Of Deviation About Squared Deviation


Mean Class
Students In The Mean About The Mean
Size (𝑥̅ )
Class (𝑥𝑖 ) (𝑥𝑖 − 𝑥̅ ) (𝑥𝑖 − 𝑥̅ )2
46 44 2 4
54 44 10 100
42 44 -2 4
46 44 2 4
32 44 -12 144
0 256
∑(𝑥𝑖 − 𝑥̅ ) ∑(𝑥𝑖 − 𝑥̅ )2
To illustrate the computation of the sample variance, we will use the data on class size for the
sample of five college classes. A summary of the data, including the computation of the
deviations about the mean and the squared deviations about the mean, is shown in Table. The
sum of squared deviations about the mean is ∑(𝑥𝑖 − 𝑥̅ )2 = 256. hence, with n − 1 = 4, the
sample variance is

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:

∑(𝑥𝑖 − 𝑥̅ )(𝑦𝑖 − 𝑦̅)


𝑠𝑥𝑦 =
𝑛−1

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

Week xi yi (𝒙𝒊 − 𝒙̅) (𝒚𝒊 − 𝒚̅) ̅)(𝒚𝒊 − 𝒚


(𝒙𝒊 − 𝒙 ̅)
1 2 50 -1 -1 1
2 5 57 2 6 12
3 1 41 -2 -10 20
4 3 54 0 3 0
5 4 54 1 3 3
6 1 38 -2 -13 26
7 5 63 2 12 24
8 3 48 0 -3 0
9 4 59 1 8 8
10 2 46 -1 -5 5
30 510 0 0 99

∑(𝑥𝑖 − 𝑥̅ )(𝑦𝑖 − 𝑦̅) 99


𝑠𝑥𝑦 = = = 11
𝑛−1 9

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

𝑐𝑜𝑣(𝑋, 𝑌) 𝑠𝑥𝑦
𝑟𝑥𝑦 = =
𝑠𝑑(𝑋) 𝑠𝑑(𝑌) 𝑠𝑥 𝑠𝑦

• 𝑟𝑥𝑦 = sample correlation coefficient


• sxy = sample covariance
• sx = sample standard deviation of x
• sy = sample standard deviation of y

The correlation coefficient is independent of the units of measurement of X and Y . For


example, if someone measures the height and weight in metres and kilograms respectively and
another person measures them in centimetres and grams, respectively, then the correlation
coefficient between the two sets of data will be the same. The correlation coefficient is
symmetric, i.e. r(X,Y) = r(Y, X). The limits of r are −1 ≤ r ≤ 1. If all the points in a scatter plot
lie exactly on a straight line, then the linear relationship between X and Y is perfect and |r| =
1.

If the relationship between X and Y is

(i) perfectly linear and increasing, then r = +1 and

(ii) perfectly linear and decreasing, then r = −1.

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:

A statistical hypothesis is an assumption that we make about a population parameter, which


may or may not be true concerning one or more variables.

According to Prof. Morris Hamburg “A hypothesis in statistics is simply a quantitative


statement about a population”.

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.

Hypotheses are two types

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.

The null hypothesis would be

H 0 :  = 100

Then the alternative hypothesis could be any one of the statements.

H 1 :   100
(or ) H 1 :   100
(or ) H 1 :   100

Errors in testing of hypothesis:

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.

• The hypothesis is true but our test rejects it.(type-I error)


• The hypothesis is false but our test accepts it. .(type-II error)
• The hypothesis is true and our test accepts it.(correct)
• The hypothesis is false and our test rejects it.(correct)

The first two decisions are called errors in testing of hypothesis.

• 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  .

𝛼 = P (Committing Type-I error)

= P (H0 is rejected when it is true)

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 −
𝛽.

Power of the test = P (H0 is rejected when it is false)

= 1- P (H0 is accepted when it is false)

= 1- P (Committing Type-II error) = 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.

One tailed and two tailed tests:

A test with the null hypothesis 𝐻0 : 𝜃 = 𝜃0 against the alternative hypothesis𝐻0 : 𝜃 ≠ 𝜃0 , it is


called a two tailed test. In this case the critical region is located on both the tails of the
distribution.

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.

𝐻0 : 𝜃 = 𝜃0 against 𝐻0 : 𝜃 > 𝜃0 ------- right tailed test

𝐻0 : 𝜃 = 𝜃0 against 𝐻0 : 𝜃 < 𝜃0 ------- left tailed test

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.

i.e. 𝑆𝐸(𝑡) = √𝑣𝑎𝑟(𝑡)

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

is an unbiased, estimate of the population variance 𝜎 2 .

t-Test lor single Mean

Suppose we want to test:

(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.

Under the null hypothesis Ho :

(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.

Assumptions for Student's t-test

The following assumptions are made in the Student's t-test:

(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.

Solution : Given that

𝜇 = 0.700 inches, 𝑥̅ = 0.742 inches, s = 0.040 inches and n = 10

Null Hypothesis, H0 : 𝜇 = 0.700, i.e.. the product is conforming to specifications.

Alternative Hypothesis, H1 : 𝜇 ≠ 0.700

Test Statistic. Under H0, the test statistic is :

𝑥̅ − 𝜇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.

Alternative hypothesis, H1 : 𝜇 ≠ 100.

Test Statistic. Under H0, the test statistic is :

𝑥̅ − 𝜇0
𝑡=
√𝑆 2 ⁄𝑛

Where 𝑥̅ and S2 are to be computed from the sample values of I.Q.’s

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

|97.2 − 100| 2.8 2.8


|𝑡| = = = = 0.62
√203.73/10 √20.37 4.514

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.

t-Test for Difference of Means.

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

follows Student's t-distribution with (n1 + n2 - 2) d.f.

Assumptions of t-test for difference of means.

(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

Sample means 𝑥̅1 = 1,234 ℎ𝑟𝑠. 𝑥̅2 = 1,036 ℎ𝑟𝑠.

Sample S.D.’s 𝑠̅1 = 36 ℎ𝑟𝑠. 𝑠̅2 = 40 ℎ𝑟𝑠.

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.

Alternative Hypothesis, H1 : 𝜇1 > 𝜇2 , type I is superior to type II,

Test statistic. Under H0, the test statistic is:

𝑥̅ − 𝑦̅
𝑡=
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.

Paired t-test For Difference of Means.

Let us now consider the case when

(i) the sample sizes are equal, i.e., n1 = n2 = n (say), and

(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.

Here we consider the increments. di = xi – yi , (i =1, 2, ..., n).

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

follows Student's t-distribution with (n - 1) d.f.

Example : A certain stimulus administered to each of the 12 patients resulted in the following
increase of blood pressure:

5, 2, 8, -1, 3, 0, -2, 1, 5, 0, 4 and 6

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).

Null Hypothesis, H0 : 𝜇1 = 𝜇2 , i.e., there is no significant difference in the blood pressure


readings of the patients before and after the drug. In other words. the given increments are
just by chance (fluctuations of sampling) and not due to the stimulus.

Alternative Hypothesis, H1 : 𝜇1 < 𝜇2 , i.e., the stimulus results is an increase in blood pressure.

Test Statistic. Under H0, the test statistic is :

𝑑̅
𝑡=
𝑆
√𝑛

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
√𝑛

Tabulated t0.05 for 11 d.f. for right-tailed test is 1.80.

Conclusion. Since calculated t > t0.05 , H0 is rejected at 5% level of significance. Hence we


conclude that the stimulus will, in general, be accompanied by an increase in blood pressure.
Analysis of Variance(ANOVA)

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 .

Assumptions for Analysis of Variance

Three assumptions are required to use analysis of variance.

• For each population, the response variable is normally distributed.


• The variance of the response variable, denoted 𝜎 2 , is the same for all of the populations.
• The observations must be independent.

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 = ⋯ = 𝜇𝑘

Alternative Hypothesis, At least two of the means 𝜇1 , 𝜇2 , … , 𝜇𝑘 are different.

Sum of squares

𝑆𝑇2 = ∑𝑖 ∑𝑗(𝑦𝑖𝑗 − 𝑦̅.. )2, is known as total sum of squares(T.S.S.)

𝑆𝐸2 = ∑𝑖 ∑𝑗(𝑦𝑖𝑗 − 𝑦̅𝑖. )2, is called within sum of squares or S.S. due to error (S.S.E.)

𝑆𝑡2 = ∑𝑖 𝑛𝑖 (𝑦̅𝑖. − 𝑦̅.. )2 , is called S.S. due to treatments (S.S.T.)

Then, T.S.S. = S.S.E. + S.S.T.

Mean Sum of Squares(M.S.S.)

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

is the M.S.S. due to treatments, and

𝑆𝐸2 𝑆. 𝑆. 𝐸.
= = 𝑠𝐸2
𝑁−𝑘 𝑁−𝑘

is the M.S.S. due to error.

ANOVA table

Sources of Sum of Mean Sum of


d.f. Variance Ration
Variation Squares Squares
Treatment 𝑆𝑡2 k-1 𝑠𝑡2 𝑠𝑡2
𝐹=
𝑠𝐸2
Error 𝑆𝐸2 N-k 𝑠𝐸2
= 𝐹𝑘−1, 𝑁−𝑘
Total 𝑆𝑇2 N-1
Thus if an observed value of F obtained from is greater than the tabulated value of F for (k-1,
N-k) d.f. at specified level of significance, then H0 is rejected at that level, otherwise we fail to
reject H0.

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

Groups Count Sum Average Variance


Females 5 2950 590 2250
Males 5 2750 550 2250
ANOVA table

Source of Variation SS df MS F F crit


Between Groups 4000 1 4000 1.78 5.32
Within Groups 18000 8 2250
Total 22000 9
The implication is that MSST would have to be 5.32 times greater than MSSE before we could
conclude at the 0.05 level of significance that the two population treatment means differ.
Because the data yielded F = 1.78, there is insufficient information to conclude that the mean
SAT scores differ for the populations of female and male high school seniors.
Sample Question Bank
Subject: Statistics with R Programming

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.

You might also like