0% found this document useful (0 votes)
9 views75 pages

R Programming

Chapter 3 covers statistics and probability, introducing key concepts such as mean, median, variance, and various probability distributions, particularly focusing on the Bernoulli and Binomial distributions. It explains the importance of data visualization in statistical analysis and provides formulas for calculating probabilities, including the probability mass function. The chapter emphasizes the application of these statistical concepts in real-world scenarios and the use of R programming for analysis.

Uploaded by

Afreed Shahid
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)
9 views75 pages

R Programming

Chapter 3 covers statistics and probability, introducing key concepts such as mean, median, variance, and various probability distributions, particularly focusing on the Bernoulli and Binomial distributions. It explains the importance of data visualization in statistical analysis and provides formulas for calculating probabilities, including the probability mass function. The chapter emphasizes the application of these statistical concepts in real-world scenarios and the use of R programming for analysis.

Uploaded by

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

Chapter – 3

Statistics & probability


Statistics Introduction:
Statistics is the science of analyzing, reviewing and conclude data.

Some basic statistical numbers include:

• Mean, median and mode


• Minimum and maximum value
• Percentiles
• Variance and Standard Devation
• Covariance and Correlation
• Probability distributions

The R language was developed by two statisticians. It has many built-in


functionalities, in addition to libraries for the exact purpose of statistical
analysis.

probability Introduction: R has an abbreviated name for every


probability distribution. This name is used to identify the functions associated with
the distribution

The Probability of an Event


The probability of an event is:The number of ways the event can happen / The
number of possible [Link] = # of Ways / Outcomes

Tossing Coins:When tossing a coin, there are two possible outcomes:

Way Probability

Heads 1/2 = 0.5

1|Page
Tails 1/2 = 0.5

P(A) - The Probability

The probability of an event A is often written as P(A).

When tossing two coins, there are 4 possible outcomes:

Event P(A)

Heads + Heads 1/4 = 0.25

Tails + Tails 1/4 = 0.25

Heads + Tails 1/4 = 0.25

Tails + Heads 1/4 = 0.25

Basic data visualization


Data visualization is the practice of translating information into a visual context, such as a
map or graph, to make data easier for the human brain to understand and pull insights
from.

Data visualization is an important part of a statistical analysis. The visualization tools


appropriate for a given data set are dependent upon the types of variables for each you
have made observations.

2|Page
probability

The probability that an event occurs k times in n trials is described by the


formula (nk)pkqn−k where p is the probability of the event occurring in a single trial, q is
the probability of the event not occurring (1−p) and (nk) is the binomial expansion “n
choose k” the formula for which is n!k! (n−k)!

Types of probability

Theotical probability

Experimental probability

Axiomatic probability

Common Probability Distributions


Common Probability Distributions In this chapter, we’ll look at a number of standard
probability distributions that exist for dealing with commonly occurring random phenomena in
statistical modelling. and they’re useful because their properties are well understood and documented.
In fact, they are so ubiquitous that most statistical software packages have corresponding built-in
functionality for their evaluation, and R is no exception. Several of these distributions represent an
essential ingredient in traditional statistical hypothesis testing, which is explored in Chapters 17 and 18.
Just like the random variables they model, the common distributions you’ll examine here are broadly
categorized as either discrete or continuous. Each distribution has four core R functions tied to it—a d-
function, providing specific mass or density function values; a p-function, providing cumulative
distribution probabilities; a q-function, providing quantiles; and an rfunction, providing random variate
generation

Defination: Probability distributions are functions that calculates the probabilities of the outcomes
of random variables.

Typical examples of random variables are coin tosses and dice rolls.

Example 1

Here is an graph showing the results of a growing number of coin tosses and the expected values of the
results (heads or tails). The expected values of the coin toss is the probability distribution of the coin
toss. Notice how the result of random coin tosses gets closer to the expected values (50%) as the
number of tosses increases.

3|Page
Example 2
Similarly, here is a graph showing the results of a growing number of dice rolls and the expected values
of the results (from 1 to 6). Notice again how the result of random dice rolls gets closer to the expected
values (1/6, or 16.666%) as the number of rolls increases.

Example 3
When the random variable is a sum of dice rolls the results and expected values take a different shape.
The different shape comes from there being more ways of getting a sum of near the middle, than a small
or large sum. Common Probability distribution functions.

• Binomial distribution

• Poisson distribution

• Uniform distribution

• Bernoulli distribution

• Rademacher distribution

• Normal distribution

• Exponential distribution

• Gamma distribution

Common probability mass funcation


In probability and statistics, a probability mass function is a function that gives the probability
that a discrete random variable is exactly equal to some value.[1] Sometimes it is also known as
the discrete probability density function. The probability mass function is often the primary
means of defining a discrete probability distribution, and such functions exist for
either scalar or multivariate random variables whose domain is discrete.

A probability mass function differs from a probability density function (PDF) in that the latter is
associated with continuous rather than discrete random variables. A PDF must
be integrated over an interval to yield a probability.[2]

What is Probability Mass Function?


Probability mass function (pmf) and cumulative distribution function (CDF) are two
functions that are needed to describe the distribution of a discrete random
4|Page
variable. The cumulative distribution function can be defined as a function that
gives the probabilities of a random variable being lesser than or equal to a
specific value. The CDF of a discrete random variable up to a particular value,
x, can be obtained from the pmf by summing up the probabilities associated
with the variable up to x.

Probability Mass Function Definition


Probability mass function can be defined as the probability that a discrete
random variable will be exactly equal to some particular value. In other words,
the probability mass function assigns a particular probability to every possible
value of a discrete random variable.

Probability Mass Function Example


Suppose a fair coin is tossed twice and the sample space is recorded as S =
[HH, HT, TH, TT]. The probability of getting heads needs to be determined.
Let X be the random variable that shows how many heads are obtained. X
can take on the values 0, 1, 2. The probability that X will be equal to 1 is 0.5.
Thus, it can be said that the probability mass function of X evaluated at 1 will
be 0.5.

Bernoulli

What is Probability Mass Function?


Probability mass function (pmf) and cumulative distribution function (CDF) are two
functions that are needed to describe the distribution of a discrete random
variable. The cumulative distribution function can be defined as a function that
gives the probabilities of a random variable being lesser than or equal to a
specific value. The CDF of a discrete random variable up to a particular value,
x, can be obtained from the pmf by summing up the probabilities associated
with the variable up to x.

Probability Mass Function Definition


Probability mass function can be defined as the probability that a discrete
random variable will be exactly equal to some particular value. In other words,
the probability mass function assigns a particular probability to every possible
value of a discrete random variable.
5|Page
Probability Mass Function Example
Suppose a fair coin is tossed twice and the sample space is recorded as S =
[HH, HT, TH, TT]. The probability of getting heads needs to be determined.
Let X be the random variable that shows how many heads are obtained. X
can take on the values 0, 1, 2. The probability that X will be equal to 1 is 0.5.
Thus, it can be said that the probability mass function of X evaluated at 1 will
be 0.5.

What is Probability Mass Function?


Probability mass function (pmf) and cumulative distribution function (CDF) are two
functions that are needed to describe the distribution of a discrete random
variable. The cumulative distribution function can be defined as a function that
gives the probabilities of a random variable being lesser than or equal to a
specific value. The CDF of a discrete random variable up to a particular value,
x, can be obtained from the pmf by summing up the probabilities associated
with the variable up to x.

Probability Mass Function Definition


Probability mass function can be defined as the probability that a discrete
random variable will be exactly equal to some particular value. In other words,
the probability mass function assigns a particular probability to every possible
value of a discrete random variable.

Probability Mass Function Example


Suppose a fair coin is tossed twice and the sample space is recorded as S =
[HH, HT, TH, TT]. The probability of getting heads needs to be determined.
Let X be the random variable that shows how many heads are obtained. X
can take on the values 0, 1, 2. The probability that X will be equal to 1 is 0.5.
Thus, it can be said that the probability mass function of X evaluated at 1 will
be 0.5.

BERNOULLI DISTRIBUTION

6|Page
The Bernoulli distribution is a discrete probability distribution that models a random experiment with
two possible outcomes: success (coded as 1) and failure (coded as 0). It is commonly used to represent
situations where an experiment results in a binary outcome, such as flipping a coin (heads or tails) or the
success or failure of a single trial in a binary experiment.

Probability Mass Function (PMF): The probability mass function (PMF) of a Bernoulli distribution
describes the probability of observing a particular outcome. For a random variable X with a Bernoulli
distribution: Here, p is the probability of success, and q is the probability of failure.

Key Properties:

Parameter p: The parameter p represents the probability of success in a single trial. Its range is
between 0 and 1.

Expectation and Variance:

• The expected value (mean) of a Bernoulli distribution is given by E(X)= p.

• The variance is given by Var(X)=p⋅(1−p). R Programming

Applications of Bernoulli Distribution:

1. The Bernoulli distribution is frequently used in machine learning and data analysis for binary
classification tasks. It can simulate the likelihood that a sample will fall into a specific class or category.

2. The Bernoulli distribution is used to model click-through rates (CTR) in online advertising and
marketing. The distribution can be used to calculate the likelihood that a user will click on an
advertisement or perform a particular activity.

3. To ascertain if a product or procedure complies with specific criteria, the Bernoulli distribution is used
in quality control. It is possible to utilize it to simulate the occurrence of flaws or failures.

4. When analyzing survey data, binary replies to questions with true/false or yes/no alternatives can be
modeled using the Bernoulli distribution. This makes it possible to estimate response probabilities and
compare proportions.

5. Clinical trials and epidemiological research are two examples of biological investigations that make
use of the Bernoulli distribution. It can simulate things like the onset of an illness, the efficacy or
inefficacy of treatments, or patient reactions.

6. In reliability engineering, binary events linked to system dependability, such as component failure or
system downtime, can be analyzed using the Bernoulli distribution.

7. The Bernoulli distribution is used to represent the probability of uncommon events or insurance
claims in risk assessment and insurance studies.

Summary:

7|Page
The Bernoulli distribution is a simple yet fundamental concept in probability theory. Understanding its
properties and implementing it in R enables you to model and analyze binary outcomes in various
applications. The provided R code snippets and visualizations should assist you in exploring and working
with the Bernoulli distribution effectively.

Binomial Distribution
The Binomial Distribution model is an essential part of statistical analysis that
provides powerful insights into probabilities and events. It is a discrete probability
distribution of the number of successes in independent experiments. We can
easily manipulate, analyze, and visualize these distributions by harnessing the
power of R, a popular language among statisticians and data scientists. This article
will delve into the theoretical underpinnings of the Binomial Distribution and its
applications and illustrate how to leverage R programming for implementing and
visualizing it.

What is Binomial Distribution?


A binomial distribution represents a probability distribution that describes the
outcome of a fixed number of independent and identically distributed Bernoulli
trials, each with a constant probability of success. In simpler terms, it’s the
probability of a SUCCESS or a FAILURE outcome in an experiment or survey that is
repeated multiple times.

The binomial is a distribution type with two possible outcomes (the prefix ‘bi’
represents two or twice). For example, a coin toss has two possible outcomes:
heads or tails.

Key properties of a binomial distribution are:

8|Page
[Link] observations’ number or trials is fixed. In other words, you can only figure
out the probability of something happening if you do it a certain number of times.

[Link] observation or trial is independent. In other words, none of your trials


affect the probability of the next trial.

These characteristics make the binomial distribution suitable for a wide range of
real-world scenarios and problems.

[Link] probability of success on a single trial is equal to p and remains the same
from trial to trial. The probability of failure is 1 – p = q.

[Link] outcomes of the trials are independent.

[Link] random variable X is the number of successes in n trials.

We have seen that the number of ways of obtaining x successes in n trials is given
by:(n/x) =n! /x! (n-x) !.

Here are some real-life scenarios where the binomial


distribution is applicable:
[Link] Tossing: Suppose you toss a fair coin ten times. The binomial distribution
can be used to determine the probability of getting a certain number of heads (or
tails). Each coin toss is an independent event, the probability of getting a head or
tail is the same for each toss (0.5), and you are conducting a fixed number of trials
(10 coin tosses).

[Link] Control in Manufacturing: A factory produces items and each item may
be defective or not defective. If you randomly select a certain number of items
(fixed number of trials), the binomial distribution can be used to calculate the
probability of finding a specific number of defective items. Each selection is an
independent event and the probability of selecting a defective item is the same
for each selection.

9|Page
[Link] Trials: Suppose a drug has a 70% chance of curing a certain disease. If
the drug is given to 50 patients (fixed number of trials), the binomial distribution
can help estimate the probability of the drug curing a certain number of patients.
Each patient’s outcome is independent of the others and the probability of
success (curing the disease) is the same for each trial.

[Link] Sampling: If you’re conducting a survey and you know that 60% of a
population will choose option A (based on past data or a larger sample), a
binomial distribution can help you determine the probability that a certain
number out of a smaller sample will choose option A. Each survey response is an
independent event, the probability of each person choosing option A is the same,
and you’re surveying a fixed number of people.

[Link]: If a basketball player makes a free throw 80% of the time, you can use
the binomial distribution to calculate the probability of that player making a
certain number of free throws out of a fixed number of attempts. Each free throw
is an independent event, and the probability of success is the same for each shot.

[Link] of Fraudulent Transactions:Banks use the binomial distribution to


model the probability that a certain number of credit card transactions are
fraudulent.

[Link] of Spam Emails per Day:Email companies use the binomial distribution
to model the probability that a certain number of spam emails land in an inbox
per day.

These examples demonstrate the wide applicability of the binomial distribution in


different domains.

Formula of Binomial Distribution:

10 | P a g e
The binomial distribution is a discrete probability distribution that models the
number of successes in a fixed number of independent Bernoulli trials. A Bernoulli
trial is an experiment or process that results in a binary outcome, often termed
“success” or “failure”.

The probability mass function (PMF) of the binomial distribution is given by:

[ P(X = k) = \binom{n}{k} p^k (1-p)^{n-k} ]

Where:

• ( P(X = k) ) is the probability of observing ( k ) successes in ( n ) trials.

• ( \binom{n}{k} ) is the binomial coefficient, representing the number of


ways to choose ( k ) successes from ( n ) trials. It’s calculated as (
\frac{n!}{k!(n-k)!} ), where ( ! ) denotes factorial.

• ( p ) is the probability of success on a single trial.

• ( 1-p ) is the probability of failure on a single trial.

• ( n ) is the number of trials.

• ( k ) is the number of successes.

In this formula, \( k \) can take any value from 0 to ( n ).

Functions used In Binomial Distribution:


11 | P a g e
• dbinom()

• pbinom()

• qbinom()

• rbinom()

dbinom()
The dbinom() function in R is a powerful tool for computing a binomial
distribution’s probability density (mass) function. It allows users to calculate the
probability of obtaining a specific number of “successes” in a fixed number of
Bernoulli trials, given a certain probability of success. By leveraging dbinom(),
users can enhance their statistical analysis, offering a clearer understanding of
binomial distributions in practical scenarios.

Here's how you can use the dbinom() function in R:

# Here we’re looking at a situation with 5 trials (let’s say flipping a coin 5 times),

# and we want to know the probability of getting 3 successes (let’s say 3 heads).

# Assuming a fair coin, the probability of success on each trial is 0.5.

# Syntax: dbinom(x, size, prob)

prob_three_heads <- dbinom(3, size=5, prob=0.5)

print(prob_three_heads)

12 | P a g e
The output for this code might look something like this:

[1] 0.3125

This suggests that the probability of getting exactly 3 heads in 5 coin tosses is
0.3125, assuming a fair coin (where the probability of getting a head in each toss
is 0.5).

Here’s another example using pbinom(), which gives us the cumulative probability
distribution, i.e., the probability of getting ‘x’ successes or fewer:

# Let’s say we’re interested in the probability of seeing 5 or fewer successes in 20


trials,

# with the probability of success on each trial being 0.25.

prob_five_or_less <- pbinom(5, size=20, prob=0.25)

print(prob_five_or_less)

The output for this code might look like this:

[1] 0.8823515

This indicates that the probability of getting 5 or fewer successes in 20 trials, with
the probability of success on each trial being 0.25, is approximately 0.882.

pbinom()

13 | P a g e
The pbinom() function in R calculates the cumulative probability of a binomial
distribution. This function is incredibly helpful when we need to compute the
probability of having a certain number of successes or fewer in a given number of
independent trials.

Here's an example of how to use pbinom():

# In this example, we have 10 trials (e.g., flipping a coin 10 times),

# and we want to know the cumulative probability of getting 4 successes (e.g., 4


heads) or less.

# Assuming a fair coin, the probability of success on each trial is 0.5.

# Syntax: pbinom(q, size, prob)

prob_four_or_less <- pbinom(4, size=10, prob=0.5)

print(prob_four_or_less)

The 'q' parameter is the number of successes we're interested in, 'size' represents
the number of trials, and 'prob' is the probability of success on each trial.

Running this code snippet, you might see an output like:

[1] 0.6230469

This suggests that the probability of getting 4 heads or fewer in 10 coin tosses is
approximately 0.623, assuming a fair coin (where the probability of getting a head
in each toss is 0.5).

qbinom()

14 | P a g e
The qbinom() function in R provides the inverse of the pbinom() function. It
returns the smallest number of successes in a set of Bernoulli trials for which the
cumulative probability is greater than or equal to a specified probability level. In
other words, qbinom() is used for a binomial distribution to find the quantile
function, or the number of successes at a given percentile.

Here's an example of how to use qbinom():

# Let’s say we have 10 trials (e.g., flipping a coin 10 times),

# and we want to know the number of successes (e.g., heads) we’d expect to see at
the 70th percentile.

# Assuming a fair coin, the probability of success on each trial is 0.5.

# Syntax: qbinom(p, size, prob)

num_successes <- qbinom(0.7, size=10, prob=0.5)

print(num_successes)

In this code, ‘p’ is the percentile we’re interested in (expressed as a probability),


‘size’ is the number of trials, and ‘prob’ is the probability of success on each trial.

Running this code snippet, you might see an output like:

[1] 6

This suggests that in 10 coin tosses, we’d expect to see 6 or fewer heads 70% of
the time, assuming a fair coin (where the probability of getting a head in each toss
is 0.5).

rbinom()
R’s rbinom() function allows us to generate random numbers following a binomial
distribution. This can be incredibly useful in various situations, such as simulating
experiments, bootstrapping, or validating statistical models.

Here’s how you can use rbinom():

15 | P a g e
# In this example, we want to generate 100 random numbers (let’s say 100
experiments of flipping a coin 10 times),

# and we’re interested in each experiment’s number of successes (e.g., heads).

# Assuming a fair coin, the probability of success on each trial is 0.5.

# Syntax: rbinom(n, size, prob)

random_numbers <- rbinom(100, size=10, prob=0.5)

print(random_numbers)

In this code, ‘n’ is the number of random numbers we want to generate, ‘size’ is
the number of trials, and ‘prob’ is the probability of success on each trial.

Running this code snippet, you might see an output like:

[1] 5 6 5 4 5 4 6 7 5 5 4 5 7 5 4 6 5 5 7 5 4 6 4 5 6 4 5 6 5 5 7 4 5 4 4 5 6 4 6 5 6 6
75745676546556655645676446565466557546675764
47565566567675764765556

These are the number of successes in each of the 100 experiments. For instance, in
the first experiment, we got 5 heads, in the second experiment, we got 6 heads, and
so on.

Conclusion:
[Link] binomial distribution is an essential statistical concept that provides insights
into the probability of a certain number of successes in a given number of
independent trials. Its applications are wide and varied, spanning numerous fields
from biology to finance.

2.R programming language has several functions for performing operations related
to the binomial distribution, such as dbinom(), pbinom(), qbinom(), and rbinom(),
each serving its unique purpose.

[Link]() function provides the exact probability of observing a specified number


of successes in a certain number of Bernoulli trials.

16 | P a g e
[Link]() function gives the cumulative probability of a specified or fewer
number of successes, while qbinom() helps to find the quantile, or the number of
successes at a given percentile.

[Link] rbinom() function is an effective tool for generating random numbers that
follow a binomial distribution, which can be particularly useful in scenarios like
simulations or data analysis involving bootstrapping.

By harnessing these functions in R, users can conduct robust and efficient


statistical analyses around the binomial distribution.

Hence, in this document we have discussed binomial distribution in R. We have


simulated using various examples in R studio and R snippets and also described the
built-in functions helps in generating binomial calculations. Therefore, a binomial
distribution helps in finding probability and random search using a binomial
variable.

This is a guide to Binomial distribution in R. Here we have discuss an introduction


and its functions associated with Binomial distribution along with the syntax and
appropriate examples.

Poisson Distribution
The Poisson distribution represents the probability of a provided number of cases
happening in a set period of space or time if these cases happen with an identified
constant mean rate (free of the period since the ultimate event). Poisson
distribution has been named after Siméon Denis Poisson(French Mathematician).

Many probability distributions can be easily implemented in R language with the


help of R’s inbuilt functions.

There are four Poisson functions available in R:

• dpois()
• ppois()
• qpois()

17 | P a g e
• rpois()

dpois ()
The dpois function finds the probability that a certain number of successes occur
based on an average rate of success, using the following syntax:

dpois(x, lambda)

where:

• x: number of successes
• lambda: average rate of success

Here’s an example of when you might use this function in practice:

18 | P a g e
It is known that a certain website makes 10 sales per hour. In a given hour, what is
the probability that the site makes exactly 8 sales?

dpois(x=8, lambda=10)

#0.112599

The probability that the site makes exactly 8 sales is 0.112599.

ppois()
The ppois function finds the probability that a certain number of successes or less
occur based on an average rate of success, using the following syntax:

ppois(q, lambda)

where:

• q: number of successes
• lambda: average rate of success

Here’s are a couple examples of when you might use this function in practice:

It is known that a certain website makes 10 sales per hour. In a given hour, what is
the probability that the site makes 8 sales or less?

ppois(q=8, lambda=10)

#0.3328197

The probability that the site makes 8 sales or less in a given hour is 0.3328197.

It is known that a certain website makes 10 sales per hour. In a given hour, what is
the probability that the site makes more than 8 sales?

ppois(q=8, lambda=10)

#0.6671803

The probability that the site makes more than 8 sales in a given hour is 0.6671803.
19 | P a g e
qpois()
The qpois function finds the number of successes that corresponds to a certain
percentile based on an average rate of success, using the following syntax:

qpois(p, lambda)

where:

• p: percentile
• lambda: average rate of success

Here’s an example of when you might use this function in practice:

It is known that a certain website makes 10 sales per hour. How many sales would
the site need to make to be at the 90th percentile for sales in an hour?

qpois(p=.90, lambda=10)

#14

A site would need to make 14 sales to be at the 90th percentile for number of sales
in an hour.

rpois()
The rpois function generates a list of random variables that follow a Poisson
distribution with a certain average rate of success, using the following syntax:

rpois(n, lambda)

where:

• n: number of random variables to generate


• lambda: average rate of success

Here’s an example of when you might use this function in practice:

20 | P a g e
Generate a list of 15 random variables that follow a Poisson distribution with a rate
of success equal to 10.

rpois(n=15, lambda=10) # [1] 13 8 8 20 8 10 8 10 13 10 12 8 10 10 6

Since these numbers are generated randomly, the rpois() function will produce
different numbers each time. If you want to create a reproducible example, be sure
to use the [Link]() command.

Common probability density functions


Probability - Number of outcomes

Probability density function:


It is a function that describe a continuous probability i.e probability of all values in an array In
probability random numbers are defined by using choice () method of the random module.

Eg: from numpy import random

x= [Link][(3,5,7,9],p=[0.1,0.3,0.6,0.0],size=(10))

print(x)

Output : 5 7 3 5 7 7 5 7 7 5

In this function we are use another module to visualise random distribution graphically. i.e
Seaborn module

Common probability density functions are:

● Uniform distribution

● Normal distribution

● Student -t distribution

● Standard Normal distribution

● Chi-square distribution

[Link] distribution: It is one of the common probability density functions where every
event has equal chances of occurring.

21 | P a g e
Eg: Generation of random numbers.

It has 3 parameters:

a- lower bound - default 0.0

b- upper bound - default 1.0

Size- The shape of the returned array

Eg: Visualization of uniform distribution

[Link] distribution:
It is one of the most important distribution.

It is also called as the Gaussian distribution after the German Mathematician Carl Friedrich
Gauss. It fits the probability distribution of many events

Eg: IQ scores, Heartbeat etc:

By using [Link]() method to get Normal Data Distribution.

It also has 3 parameters

● loc-(mean) where the peak of the bell exists.

● Scale-(standard deviation) how flat the Graph distribution should be.

● Size-The shape of the returned array.

Statistics Student T-Distribution

student t-Distribution
The student's t-distribution is similar to a normal distribution and used in
statistical inference to adjust for uncertainty.
Or

22 | P a g e
Student t-Distribution is a probability distribution that is used to calculate
population parameters when the sample size is small and when the
population variance is unknown.

When to use t-Distribution

Students t-Distribution was given by W. S Gosset, but he has published his


studies under the name "student", that's way it is called as students t-test.

types of t-test with formulas.


• One sample t-test
• Two t-test (pair t-test )
• Two t-test (independent t-test )

T-Distribution with graphical representation.


Student's T Distribution

The t-distribution is used for estimation and hypothesis testing of a


population mean (average).

The t-distribution is adjusted for the extra uncertainty of estimating


the mean.

23 | P a g e
If the sample is small, the t-distribution is wider. If the sample is big,
the t-distribution is narrower.

The bigger the sample size is, the closer the t-distribution gets to the
standard normal distribution.

T distribution

Notice how some of the curves have bigger tails.

This is due to the uncertainty from a smaller sample size.

The green curve has the smallest sample [Link] the t-distribution this is
expressed as 'degrees of freedom' (df), which is calculated by subtracting 1 from
the sample size (n).

For example a sample size of 30 will make 29 degrees of freedom for the t-
distribution.

The t-distribution is used to find critical t-values and p-values (probabilities) for
estimation and hypothesis testing.

24 | P a g e
Example for finding t-values and p-values
Find the t-values of a p-value by using a t-table or with programming.

Ex:program

import [Link] as stats

print([Link](0.75, 29))

Result :

0.6830438592467808

Find the p-values of a t-value by using a t-table or with programming.

Ex:program

import [Link] as stats

print([Link](2.1, 29))

25 | P a g e
Result :

0.9777290209818548

26 | P a g e
27 | P a g e
Chapter-4
Statistical Testing and Modelling
Statistical testing:-
• It involves using functions and packages to perform hypothesis test analyse data and raw conclusion
based on statistical significance.

• Testing functions like:- [Link]() , [Link]() , [Link]() etc..,.

Example:

g1<-rnorm(30,mean=50,sd=10)

g2<- rnorm(30,mean=55,sd=12)

t_test_result<- [Link](g1,g2)

print(t_test_result)

Types of statistical testing


Parametric Tests:

These tests assume that the data follows a specific distribution (e.g., normal distribution). t-test:
Compares means of two groups to determine if they are significantly different.

ANOVA (Analysis of Variance): Tests the differences among means of three or more groups.

Pearson correlation: Measures the strength and direction of the linear relationship between two
continuous variables.

28 | P a g e
Here's an example:

# Generate two sets of data

group1 <- c(25, 30, 32, 28, 35)

group2 <- c(20, 22, 25, 18, 30)

# Perform a two-sample t-test

t_result <- [Link](group1, group2)

# View the result

print(t_result)

This code performs a two-sample t-test comparing group1 and group2 and displays the test results,
including the t-statistic, degrees of freedom, and p-value.

Non-Parametric Tests:
These tests are distribution-free and don’t require assumptions about the data distribution

. Mann-Whitney U test: Compares medians of two independent groups.

Kruskal-Wallis test: Compares medians of three or more independent groups. Spearman correlation:
Assesses the strength and direction of monotonic association between variables.

Here's an example:

# Generate two sets of paired data

before <- c(12, 15, 18, 20, 22)

after <- c(10, 14, 17, 21, 20)

# Perform Wilcoxon signed-rank test

wilcox_result <- [Link](before, after, paired = TRUE

) # View the result

print(wilcox_result)

This code performs a Wilcoxon signed-rank test comparing the before and after data and displays the
test results, including the test statistic, the p-value, and information about the alternative hypothesis.

Chi-Square Tests:

29 | P a g e
Chi-Square test of independence: Examines whether there's a significant association between
categorical variables in a contingency table.

Chi-Square goodness of fit test: Determines if the observed categorical data matches the expected
Distribution.

ANOVA (Analysis of Variance) :


It is a method used to compare the means of three or more groups to determine if there are statistically
significant differences among them. In R, you can conduct ANOVA using functions like aov() or lm() for
more complex models, and then analyse the results to understand the variability between groups and
within groups, aiding in determining if the group means are significantly different from each other.

Correlation test:
To measure relationship between 2 variables

Statistical modelling:- Statistical modelling in R involves using mathematical and computational


techniques to describe and analyse relationships within data. R offers a vast array of packages and
functions specifically designed for statistical modelling.

Some types of statistical models in R include:


I. Linear Regression: It explores the relationship between a dependent variable and one or more
independent variables.
II. Logistic Regression: It models the probability of a binary outcome based on predictor
variables.
III. Time Series Analysis: It deals with analysing sequences of data points collected and indexed in
time order.
IV. Decision Trees and Random Forests: These are used for classification and regression by
creating a tree-like model of decisions.
V. Cluster Analysis: It groups similar objects into clusters, identifying patterns or structures
within data.
VI. Survival Analysis: It models time until an event of interest occurs, often used in medical and
social sciences.
VII. Each type serves different purposes and has its own set of assumptions and methods for
analysing data patterns.

Example:

data(mtcars)

model<-lm(mpg~wt,data=mtcars)

summary(model)

30 | P a g e
Statistical testing and modelling in R offer several advantages.
• R provides a vast array of statistical packages and libraries, making it versatile for various
analyses. Its advantages include:
• Comprehensive Statistical Tools: R offers a wide range of statistical tests and modelling
techniques, allowing users to conduct complex analyses, regression, ANOVA, etc.
• Graphical Capabilities: R has powerful visualization libraries like ggplot2, allowing users to
create detailed, customizable plots and graphs to better understand data distributions and
relationships.
• Community Support: Being an open-source language, R has a large and active community.
Users can access numerous resources, forums, and packages shared by statisticians and data
scientists.
• Reproducibility: R scripts enable the reproduction of analyses. Others can replicate your work,
enhancing transparency and credibility.
• Integration and Compatibility: R easily integrates with other languages, databases, and
platforms. It can interact with various data sources, enhancing its flexibility. VII. Free and
Open Source: R is free to use, making it accessible to researchers, students, and professionals
regardless of budget constraints.
• These advantages make R a preferred choice for statistical analysis and modelling in many
fields.

Statistical testing and modelling in R come with a few disadvantages.


• Steep Learning Curve: R can be challenging for beginners due to its complex syntax and
diverse functions, requiring time and effort to become proficient.
• Resource Intensive: Some statistical models and simulations in R can be computationally
intensive, demanding powerful hardware or longer processing times.
• Debugging Challenges: Debugging errors in R code might be difficult, especially when dealing
with complex models or large datasets.
• Assumptions and Limitations: Misapplication of statistical tests or models due to assumptions
not being met can lead to inaccurate results or misleading conclusions.
• Interpretation Complexity: Communicating the findings from statistical models to a non-
technical audience might be challenging due to their complexity.
• Package Dependency: R's reliance on packages can lead to version compatibility issues or
reliance on packages that might become obsolete over time.
• While these challenges exist, R remains a powerful tool for statistical analysis and modelling,
offering extensive functionalities and a vast community for support and development.

Sampling distribution

31 | P a g e
Sampling distribution refers to the distribution of sample statistic. Like the mean Or standard
deviation, computed from multiple samples of the same size taken from a population.

functions and packages like sample, replicate and boot.

Types of sampling distribution


I. Sampling distribution of the sample mean(mean()) :this represents the distribution of
sample means taken from multiple samples of the same size from a population.
II. Sampling distribution of the sample proportion (propotest()) : it refers to the
distribution of sample proportions from multiple samples taken from population.
III. Sampling distribution of the sample variance (var()) :this involves the distribution of
sample variance. Calculated from multiple samples of the same size from a population.
IV. Bootstrap sampling distribution (boot()) :it uses resampling with replacement to create
multiple datasets from a single sample, allowing the estimation of sampling distribution for
various statistics.

Example:-

data(iris)

head(iris)

[Link](123)

sample_means<-replicate (1000, mean(sample (iris$Sepal, Length, 30,replace=TRUE))


hist(sample_means, break s=30, col="lightblue", main="sampling distribution of sample
mean([Link]) ", xlab=" sample mean ")

Sampling distributions in R offer several advantages:

i) Statistical Inference: They allow you to perform various statistical inferences, such as
estimating parameters, constructing confidence intervals, and conducting hypothesis tests
based on samples.
ii) Simulation Studies: Sampling distributions in R enable the simulation of various scenarios,
helping researchers understand the behavior of statistics under different conditions.
iii) Visualizations: R provides robust tools for visualizing sampling distributions, allowing for clear
and intuitive representations that aid in understanding the underlying statistical concepts.
iv) Analyzing Sampling Variability: It helps in understanding the variability inherent in samples,
crucial for making generalizations about populations based on sample data.
v) Modeling and Analysis: R facilitates the creation and analysis of complex statistical models,
allowing for a deeper exploration of sampling distributions and their implications in real-
world applications.

Sampling distributions in R, like any statistical method, have certain limitations.

32 | P a g e
Some disadvantages include:

I. Assumptions: They often assume the data comes from a particular distribution, and if this
assumption is violated, the results may be inaccurate.
II. Sample Size: Small sample sizes might not accurately represent the population, leading to
biased estimates
III. Complexity: Some sampling methods might be complex to implement or understand,
especially for beginners.
IV. Representativeness: If the sampling method used isn't random or representative, it might not
reflect the true characteristics of the population.
V. Interpretation: Sometimes interpreting the results from sampling distributions can be
challenging, especially when dealing with complex statistical models or parameters.

HYPOTHESIS TESTING
➢ A hypothesis is a claim about a population parameter.

➢ A hypothesis test is a formal procedure to check if a hypothesis is true or not.

Examples of claims that can be checked:

▪ The average height of people in Denmark is more than 170 cm.

▪ The share of left handed people in Australia is not 10%.

▪ The average income of dentists is less the average income of lawyers.

COMPONENTS OF HYPOTHESIS TESTING


1. The Null and Alternative Hypothesis
Hypothesis testing is based on making two different claims about a population parameter. The null
hypothesis (H0) and the alternative hypothesis (H1) are the claims. The two claims needs to be
mutually exclusive, meaning only one of them can be true. The alternative hypothesis is typically
what we are trying to prove. For example, we want to check the following claim:

"The average height of people in Denmark is more than 170 cm." In this case, the parameter is the
average height of people in Denmark (µ). The null and alternative hypothesis would be:

Null hypothesis: The average height of people in Denmark is 170 cm.

Alternative hypothesis: The average height of people in Denmark is more than 170 cm. The claims are
often expressed with symbols like this: H0:µ=170cm H1:µ>170cm If the data supports the alternative

33 | P a g e
hypothesis, we reject the null hypothesis and accept the alternative hypothesis. If the data does not
support the alternative hypothesis, we keep the null hypothesis.

[Link] Significance Level


The significance level (α) is the uncertainty we accept when rejecting the null hypothesis in the
hypothesis test

The significance level is a percentage probability of accidentally making the wrong conclusion.

Typical significance levels are:

• α=0.1(10%)

• α= 0.05 (5%)

• α=0.01 (1%)

A lower significance level means that the evidence in the data needs to be stronger to reject the null
hypothesis.

There is no "correct" significance level - it only states the uncertainty of the conclusion.

3. The Test Statistic


The test statistic is used to decide the outcome of the hypothesis test. The test statistic is a standardized
value calculated from the sample.

Standardization means converting a statistic to a well known probability distribution.

The type of probability distribution depends on the type of test. Common examples are:

• Standard Normal Distribution (Z): used for Testing Population Proportions

• Student's T-Distribution (T): used for Testing Population Means

The Critical Value and P-Value Approach


There are two main approaches used for hypothesis tests:

• The critical value approach compares the test statistic with the critical value of the significance level.

• The p-value approach compares the p-value of the test statistic and with the significance level.

4. The Critical Value Approach


The critical value approach checks if the test statistic is in the rejection region.

34 | P a g e
The rejection region is an area of probability in the tails of the distribution.

The size of the rejection region is decided by the significance level (α).

The value that separates the rejection region from the rest is called the critical value.

If the test statistic is inside this rejection region, the null hypothesis is rejected.

For example, if the test statistic is 2.3 and the critical value is 2 for a significance level (α=0.05):

We reject the null hypothesis (H0) at 0.05 significance level (α)

5. The P-Value Approach


The p-value approach checks if the p-value of the test statistic is smaller than the significance level (α).

The p-value of the test statistic is the area of probability in the tails of the distribution from the value of
the test statistic.

If the p-value is smaller than the significance level, the null hypothesis is rejected.

The p-value directly tells us the lowest significance level where we can reject the null hypothesis.

For example, if the p-value is 0.03:

We reject the null hypothesis (H0) at a 0.05 significance level (α)

We keep the null hypothesis (H0) at a 0.01 significance level (α)

Steps for a Hypothesis Test


The following steps are used for a hypothesis test:

1. Check the conditions

2. Define the claims

3. Decide the significance level

4. Calculate the test statistic

5. Conclusion

One condition is that the sample is randomly selected from the population.

The other conditions depends on what type of parameter you are testing the hypothesis for.

35 | P a g e
Common parameters to test hypotheses are:

• Proportions (for qualitative data)

• Mean values (for numerical data)

Testing means
Hypothesis testing in R,A formal statistical test called a hypothesis test is used to confirm or Disprove a
statistical hypothesis.

The following steps are used for a hypothesis test:

1. Check the conditions

2. Define the claims

3. Decide the significance level

4. Calculate the test statistic

5. Conclusion

1. Checking the Conditions The conditions for calculating a confidence interval for
a proportion are:
• The sample is randomly selected
• And either

A moderately large sample size, like 30, is typically large enough.

In the example, the sample size was 30 and it was randomly selected, so the conditions are fulfilled.

2. Defining the Claims

"The average age of Nobel Prize winners when they received the prize is more than 55"

In this case, the parameter is the mean age of Nobel Prize winners when they received the prize
(u)
The null and alternative hypothesis are then:
Null hypothesis: The average age was 55.
Alternative hypothesis: The average age was more than 55.
Which can be expressed with symbols as:
𝐻0 : 𝜇 = 55
𝐻1 : 𝜇 > 55

36 | P a g e
This is a 'right tailed' test, because the alternative hypothesis claims that the proportion is more
than in the null hypothesis.
3. Deciding the Significance Level
The significance level (𝛼) is the uncertainty we accept when rejecting the null hypothesis in a
hypothesis test.

The significance level is a percentage probability of accidentally making the wrong conclusion.

Typical significance levels are:

▪ 𝛼 = 0.1(10%)
▪ 𝛼 = 0.5(5%)
▪ 𝛼 = 0.01(1%)

A lower significance level means that the evidence in the data needs to be stronger to reject the null
hypothesis.

There is no "correct" significance level - it only states the uncertainty of the conclusion.

4. Calculating the Test Statistic


The test statistic is used to decide the outcome of the hypothesis test.

The formula for the test statistic (TS) of a population mean is: 𝑥 − 𝜇 𝑠 ⋅ √𝑛

𝑥 − 𝜇 is the difference between the sam3ple mean (𝑥) and the claimed population mean (𝜇 ).

𝑠 is the sample standard deviation.

𝑛 is the sample size.

The claimed (𝐻0 ) population mean (𝜇) was 55

The sample mean (𝑥) was 62.1 The sample standard deviation (𝑠) was 13.46

The sample size ( 𝑛) was So the test statistic (TS) is then:

62.1 − 55 13.46 ⋅ √30 = 7.1 13.46 ⋅ √30 ≈ 0.528 ⋅ 5.477 = 2.889 ―

Example

With R use built-in math and statistics functions to calculate the test statistic.?.

37 | P a g e
# Specify the sample mean (x_bar), the sample standard deviation (s), the mean claimed in the null-
hypothesis (mu_null), and the sample size (n)

x_bar <- 62.1

s <- 13.46
mu_null <- 55 n <- 30

output 2.8891754519217536

[Link]
• The critical value approach compares the test statistic with the critical value of the
significance level
• The P-value approach compares the P-value of the test statistic and with the significance
level.

Proportion
A population proportion is the share of a population that belongs to a particular category
Hypothesis tests are used to check a claim about the size of that population proportion.

Hypothesis Testing a Proportion


The following steps are used for a hypothesis test:
1. Check the conditions
2. Define the claims
3. Decide the significance level
4. Calculate the test statistic
5. Conclusion

1. Checking the Conditions


The conditions for calculating a confidence interval for a proportion are:

The sample is randomly selected

There is only two options:

• Being in the category

38 | P a g e
• Not being in the category

The sample needs at least:

• 5 members in the category


• 5 members not in the category

In our example, we randomly selected 10 people that were born in the US.

The rest were not born in the US, so there are 30 in the other category.

The conditions are fulfilled in this case.

2. Defining the Claims


"The average age of Nobel Prize winners when they received the prize is more than 55"

In this case, the parameter is the mean age of Nobel Prize winners when they received the prize (u)

The null and alternative hypothesis are then:

Null hypothesis: The average age was 55.

Alternative hypothesis: The average age was more than 55.

Which can be expressed with symbols as:

𝐻0 : 𝜇 = 55

𝐻1 : 𝜇 > 55

This is a 'right tailed' test, because the alternative hypothesis claims that the proportion is more than in
the null hypothesis.

3. Deciding the Significance Level


The significance level (𝛼) is the uncertainty we accept when rejecting the null hypothesis in a hypothesis
test.

The significance level is a percentage probability of accidentally making the wrong conclusion.

Typical significance levels are:

𝛼 = 0.1(10%)

𝛼 = 0.5(5%)

𝛼 = 0.01(1%)

39 | P a g e
A lower significance level means that the evidence in the data needs to be stronger to reject the null
hypothesis.

There is no "correct" significance level - it only states the uncertainty of the conclusion.

4. Calculating the Test Statistic


The test statistic is used to decide the outcome of the hypothesis test.

The formula for the test statistic (TS) of a population mean is:

𝑥 − 𝜇 𝑠 ⋅ √𝑛

𝑥 − 𝜇 is the difference between the sam3ple mean (𝑥) and the claimed population mean (𝜇 ).

𝑠 is the sample standard deviation.

𝑛 is the sample size.

The claimed (𝐻0 ) population mean (𝜇) was 55

The sample mean (𝑥) was 62.1

The sample standard deviation (𝑠) was 13.46

The sample size ( 𝑛) was

So the test statistic (TS) is then:

0.25− 0.20 √0.2(1 −0.2) ⋅ √40 = 0.05 √0.2(0.8) ⋅ √40 = 0.05 √0.16 ⋅ √40 ≈ 0.05 0.4 ⋅ 6.325 = 0.791 ―

You can also calculate the test statistic using programming language functions:

Example

With R use the built-in [Link]() function to calculate the test statistic for a proportion.

# Specify the sample occurrences (x), the sample size (n), and the null-hypothesis claim (p)

x <- 10

n <- 40

p <- 0.20

# Calculate the sample proportion

p_hat = x/n

# Calculate and print the test statistic

40 | P a g e
(p_hat-p)/(sqrt((p*(1-p))/(n)

Output

0.7905694150420945

5. Concluding
There are two main approaches for making the conclusion of a hypothesis test:

• The critical value approach compares the test statistic with the critical value of the
significance level.
• The P-value approach compares the P-value of the test statistic and with the significance
level.

Categorical Variables
Categorical variables in R are stored into a factor. Let’s check the code below to convert a character
variable into a factor variable in R. Characters are not supported in machine learning algorithm, and
the only way is to convert a string to an integer.

Syntax

factor(x = character(), levels, labels = levels, ordered = [Link](x))

Arguments:

• not decimal.
• Levels: A vector of possible values taken by x. This argument is optional. The default value is
the unique list of items of the vector x.
• Labels: Add a label to the x categorical data in R. For example, 1 can take x: A vector of
categorical data in R. Need to be a string or integer, the label `male` while 0, the label
`female`.
• ordered: Determine if the levels should be ordered in categorical data in R.

Example

• Race
• Age group
• Educational level

Errors and power

41 | P a g e
Error

Defination:
errors typically occur when the code you're trying to run can't be executed due to some reasons.

ype I and Type II Errors in Hypothesis Tests

We have not yet discussed the fact that we are not guaranteed to make the correct decision by this
process of hypothesis testing. Maybe you are beginning to see that there is always some level of
uncertainty in statistics.

Let’s think about what we know already and define the possible errors we can make in hypothesis
testing. When we conduct a hypothesis test, we choose one of two possible conclusions based upon our
data.

If the p-value is smaller than your pre-specified significance level (α, alpha), you reject the null
hypothesis and either

You have made the correct decision since the null hypothesis is false

OR

You have made an error (Type I) and rejected Ho when in fact Ho is true (your data happened to be a
RARE EVENT under Ho)

If the p-value is greater than (or equal to) your chosen significance level (α, alpha), you fail to reject the
null hypothesis and either

You have made the correct decision since the null hypothesis is true

OR

You have made an error (Type II) and failed to reject Ho when in fact Ho is false (the alternative
hypothesis, Ha, is true)

The following summarizes the four possible results which can be obtained from a hypothesis test. Notice
the rows represent the decision made in the hypothesis test and the columns represent the (usually
unknown) truth in reality.

mod12-errors1

42 | P a g e
Although the truth is unknown in practice – or we would not be conducting the test – we know it must
be the case that either the null hypothesis is true or the null hypothesis is false. It is also the case that
either decision we make in a hypothesis test can result in an incorrect conclusion!

A TYPE I Error occurs when we Reject Ho when, in fact, Ho is True. In this case, we mistakenly reject a
true null hypothesis.

P(TYPE I Error) = P(Reject Ho | Ho is True) = α = alpha = Significance Level

A TYPE II Error occurs when we fail to Reject Ho when, in fact, Ho is False. In this case we fail to reject a
false null hypothesis.

P(TYPE II Error) = P(Fail to Reject Ho | Ho is False) = β = beta

When our significance level is 5%, we are saying that we will allow ourselves to make a Type I error less
than 5% of the time. In the long run, if we repeat the process, 5% of the time we will find a p-value <
0.05 when in fact the null hypothesis was true.

In this case, our data represent a rare occurrence which is unlikely to happen but is still possible. For
example, suppose we toss a coin 10 times and obtain 10 heads, this is unlikely for a fair coin but not
impossible. We might conclude the coin is unfair when in fact we simply saw a very rare event for this
fair coin

Power

Defination
Power is the probability of avoiding a Type II error. The higher the statistical power of a test, the lower
the risk of making a Type II error.

Power is usually set at 80%. This means that if there are true effects to be found in 100 different studies
with 80% power, only 80 out of 100 statistical tests will actually detect them.

Power of a Hypothesis Test

It is often the case that we truly wish to prove the alternative hypothesis. It is reasonable that we would
be interested in the probability of correctly rejecting the null hypothesis. In other words, the probability
of rejecting the null hypothesis, when in fact the null hypothesis is false. This can also be thought of as
the probability of being able to detect a (pre-specified) difference of interest to the researcher.

Factors Affecting the Power of a Hypothesis Test


The power of a hypothesis test is affected by numerous quantities (similar to the margin of error in a
confidence interval).

43 | P a g e
Assume that the null hypothesis is false for a given hypothesis test. All else being equal, we
have the following:

• Larger samples result in a greater chance to reject the null hypothesis which means an increase
in the power of the hypothesis test.
• If the effect size is larger, it will become easier for us to detect. This results in a greater chance
to reject the null hypothesis which means an increase in the power of the hypothesis test. The
effect size varies for each test and is usually closely related to the difference between the
hypothesized value and the true value of the parameter under study.
• From the relationship between the probability of a Type I and a Type II error (as α (alpha)
decreases, β (beta) increases), we can see that as α (alpha) decreases, Power = 1 – β = 1 – beta
also decreases.
• There are other mathematical ways to change the power of a hypothesis test, such as changing
the population standard deviation; however, these are not quantities that we can usually
control so we will not discuss them here.

Analysis of variance
ANOVA tests whether there is a difference in means of the groups at each level of the
independent variable. The null hypothesis (H0) of the ANOVA is no difference in means, and the
alternative hypothesis (Ha) is that the means are different from one another.

The standard R anova function calculates sequential ("type-I") tests. These rarely test interesting
hypotheses in unbalanced designs. A MANOVA for a multivariate linear model (i.e., an object of
class "mlm" or "manova" ) can optionally include an intra-subject repeated-measures design.

Introduction to ANOVA in R
ANOVA in R is a mechanism facilitated by R programming to carry out the

implementation of the statistical concept of ANOVA, i.e. analysis of variance, a technique

that allows the user to check if the mean of a particular metric across a various

population is equal or not, through the formulation of the null and alternative

hypothesis, with R programming providing effective functionalities to implement the

concept through various functions and packages.

44 | P a g e
Why ANOVA?
• This technique is used to answer the hypothesis while analyzing multiple groups

of data. There are multiple statistical approaches; however, the ANOVA in R is

applied when comparison needs to be done on more than two independent

groups, as in our previous example, three different age groups.

• ANOVA technique measures the mean of the independent groups to provide

researchers with the result of the hypothesis. In order to get accurate results,

sample means, sample size, and standard deviation from each individual group

must be taken into account.

• It is possible to observe the mean individually for each of the three groups for

comparison. However, this approach has limitations and may prove incorrect

because these three comparisons don’t consider total data and thus may lead to

type 1 error. R provides us with the function to conduct the ANOVA analysis to

examine variability among the independent groups of data. There are five stages

of conducting the ANOVA analysis. In the first stage, data is arranged in csv

format, and the column is generated for each variable. One of the columns would

be a dependent variable, and the remaining is the independent variable. In the

second stage, the data is read in R studio and named appropriately. In the third

stage, a dataset is attached to individual variables and read by the memory.

Finally, the ANOVA in R is defined and analyzed. In the below sections, I’ve

45 | P a g e
provided a couple of case study examples in which ANOVA techniques should be

used.

• Six insecticides were tested on 12 fields each, and the researchers counted the

number of bugs that remained in each field. Now the farmers need to know if the

insecticides make any difference and which one they best use. You answer this

question by using the aov() function to perform an ANOVA.

• Fifty patients received one of five cholesterol-reducing drug treatments (trt).

Three of the treatment conditions involved the same drug administered as 20 mg

once per day (1 time), 10mg twice per day (2 times) 5 mg four times per day (4

times). The two remaining conditions (drugD and drugE) represented competing

drugs. Which drug treatment produced the greatest cholesterol reduction

(response)?

Example program:
[Link]('multcomp')
library(multcomp)
str(cholesterol)
attach(cholesterol)
aov_model <- aov(response ~ trt)

46 | P a g e
chapter -5
linear regression :
it is the basic and commonly used type for predictive [Link] is a statistical
approach for modeling and relationship betweena dependent variable and a given
set of independent variables

Advantages Disadvantages

On the other hand in linear regression


Linear Regression is simple to
technique outliers can have huge effects on the
implement and easier to interpret
regression and boundaries are linear in this
the output coefficients.
technique.

When you know the relationship


Diversely, linear regression assumes a linear
between the independent and
relationship between dependent and
dependent variable have a linear
independent variables. That means it assumes
relationship, this algorithm is the
that there is a straight-line relationship
best to use because of it’s less
between them. It assumes independence
complexity compared to other
between attributes.
algorithms.

But then linear regression also looks at a


Linear Regression is susceptible to
relationship between the mean of the
over-fitting but it can be avoided
dependent variables and the independent
using some dimensionality
variables. Just as the mean is not a complete
reduction techniques,
description of a single variable, linear
regularization (L1 and L2)
regression is not a complete description of
techniques and cross-validation.
relationships among variables.

TYPES of linear regression


• Simple linear regression
• Multiple linear regression

47 | P a g e
Simple regression
The simple linear regression is used to predict a quantitative outcome y on the basis of
one single predictor variable x . The goal is to build a mathematical model (or formula)
that defines y as a function of the x variable.

Advantage

➢ It is easy to interpret: The slope and y-intercept of the regression line can be easily
understood and used to make predictions.
➢ It requires a small amount of data: Simple linear regression can be used with a small
number of observations.
➢ It is computationally efficient: Simple linear regression can be calculated using basic
algebra, making it easy to implement and run.
➢ It is a good starting point: Simple linear regression can be used as a starting point
for more complex models and can help identify trends in the data.
➢ It is relatively robust to outliers: Simple linear regression is relatively robust to the
presence of outliers in the data.
➢ It can handle non-linear relationship by using non-linear transformation of
predictors.
➢ It's good for understanding the relationship between a single independent variable
and a single dependent variable

MULTIPLE LINEAR REGREESION:


it is the most common form of linear regression,basically describes how a single response variable Y
depends linearly on a number of predictor variable X.

(or)

It is an extension of simple linear regression used to predict an outcome variable Y on the basis of
multiple distinct predictor variable X.

General mathematical equation for multiple linear regression

y=a+b1x1+b2x2+bn

• Y is the response variable

• A,b1,b2……bn are the coefficients

• X1,x2,……xn are the predictor variable

48 | P a g e
EXAMPLE: selling price of the a house can depend on the desirability of the location,number of
rooms,the year the house was built,the square footage of the lot and number of other factors.

Real-life applications:

■ To analyze the employee performance: Use to anaylze the determinants of employee


performance,such as productivity,satisfaction or turnover.

For ex you can a model that predicts productivity based on variables such as
education,experience,skills,motivation,feedback and incentives.

■ Marketing campaigns: used to evaluate the effectiveness of different marketing campaigns on


sales,customer satisfaction ,brand awareness.

■ Health outcomes: used to investigate the Influence of various factors on health outcomes,such as
blood pressure,cholestero,diabetes.

Advantages

➢ It has the ability to determine the relative influence of one or more predictor variables to the
critierion value.
➢ It also has the ability to identify outliers or anomalies.

Disadvantage

It need high level mathematics to analyze the data and is required in the statistical program.

It is difficult for researchers to interpret the resultsof the multiple regression analysis on the basis
ofassumptions as it has a requirementof a large sample odata to get the effective result.

■ Example program

Consider the data set “mtcars” avilable in the R environment .it gives a comparision between different
car models in terms of mileage per gallon(mpg),cylinder displacement(disp),hose power(hp),weight of
the car(wt) and some more parameters.

Input<- mtcars[,c(“mpg” , ”disp” , ”hp” , ”wt”)]

Print(head(input))

Output: Mpg disp hp wt Mazda RX4 21.0 160 110 2.620

Mazada RX4 Wag 21.0 160 110 2.875

Datsun 710 22.8 108 93 2 .320

49 | P a g e
Hornet 4 Drive 21.4 258 110 3.215

Hornet Sportabout 18.7 360 175 3.440

Valiant 18.1 225 105 3.460

Linear model selection and diagnostics


Understand Forward and Backward Stepwise Regression

Running a regression model with many variables including irrelevant ones will lead to a
needlessly complex model. Stepwise regression is a way of selecting important variables to get a
simple and easily interpretable model.

Below we discuss how forward and backward stepwise selection work, their advantages, and
limitations and how to deal with them.

Forward stepwise

Forward stepwise selection (or forward selection) is a variable selection method which:

o Begins with a model that contains no variables (called the Null Model)
o Then starts adding the most significant variables one after the other
o Until a pre-specified stopping rule is reached or until all the variables under
consideration are included in the model

In order to fully understand how forward selection works, we need to know:

o How to determine the most significant variable at each step


o How to choose a stopping rule

[Link] the most significant variable to add at each stepThe most significant variable can
be chosen so that, when added to the model:

• It has the smallest p-value, or


• It provides the highest increase in R 2 , or
• It provides the highest drop in model RSS (Residuals Sum of Squares) compared
to other predictors under consideration.

2. Choose a stopping rule

The stopping rule is satisfied when all remaining variables to consider have a p-value larger
than some specified threshold, if added to the [Link] we reach this state, forward

50 | P a g e
selection will terminate and return a model that only contains variables with p-values <
threshold threshold.

Program to write forward stepwise regression

define intercept-only model

intercept_only <- lm(mpg ~ 1, data=mtcars)

#define model with all predictors

all <- lm(mpg ~ ., data=mtcars)

#perform forward stepwise regression

forward <- step(intercept_only, direction='forward', scope=formula(all), trace=0)

#view results of forward stepwise regression

forward$anova

forward$coefficients

Backward stepwise

Backward stepwise selection (or backward elimination) is a variable selection method which:

o Begins with a model that contains all variables under consideration (called the
Full Model)
o Then starts removing the least significant variables one after the other
o Until a pre-specified stopping rule is reached or until no variable is left in the
model

Here’s an example of backward elimination with 5 variables:

[Link] the least significant variable to remove at each step

The least significant variable is a variable that:

• Has the highest p-value in the model, or

• Its elimination from the model causes the lowest drop in R 2 , or

• Its elimination from the model causes the lowest increase in RSS (Residuals Sum of
Squares) compared to other predictors

51 | P a g e
2. Choose a stopping rule

The stopping rule is satisfied when all remaining variables in the model have a p-value smaller
than some pre-specified [Link] we reach this state, backward elimination will
terminate and return the current step’s model.

Program to write backward stepwise regression

#define intercept-only model

intercept_only <- lm(mpg ~ 1, data=mtcars)

#define model with all predictors

all <- lm(mpg ~ ., data=mtcars)

#perform backward stepwise regression

backward <- step(all, direction='backward', scope=formula(all), trace=0)

#view results of backward stepwise regression

backward$anova

#view final model

backward$coefficient

Linear Regression Diagnostics in R Linear regression diagnostics Linear


regression diagnostics in R are essential for assessing the validity and reliability of the linear
regression model’s assumptions and for detecting potential issues that may affect the model’s
performance. Below is a theoretical explanation of some common linear regression diagnostics
in R.

Certainly, let’s delve into diagnostic procedures for linear regression in R. We’ll focus on the key
diagnostic checks:

Residual Analysis: Check for patterns in the residuals.

Outlier Detection: Identify potential outliers. Influence and Cook’s Distance: Identify influential
observations.

52 | P a g e
Multicollinearity: Check for high correlation between predictors.

Let’s provide R code examples for each of these diagnostics using a hypothetical dataset.

# Example: Residual analysis Program to write diagnostics

#model <- lm(Y ~ X, data = data)

# Create diagnostic plots (residuals vs. fitted values, residuals vs. normal quantiles,

#and a histogram of residuals)

par(mfrow = c(2, 2))

plot(model)

Advanced graphics
R also has a higher-level set of graphics functions which make it possible to produce
complex graphics with a single function call. The high level function which produces
graphs is called plot.R Programming. R includes at least three graphical systems, the
standard graphics package, the lattice package for Trellis graphs and the grammar-of-
graphics ggplot2 package. R has good graphical capabilities but there are some
alternatives like gnuplot. R Programming.

Advanced Graph Types:The lattice package provides a comprehensive system for


visualizing multivariate data, including the ability to create plots conditioned on one or
more variables. The ggplot2 package offers a elegant systems for generating univariate
and multivariate graphs based on a grammar of graphics.

PLOTTING REGIONS AND MARGINS


About Plotting

In R Programming Language the plot() function is used to draw points(markers)in diagram

different types of Plotting Regions

➢ Box Plotting
➢ Histograms
➢ Bar Plotting
➢ Scatter Plot

53 | P a g e
The R programming graphics available to make up the image

There are three regions that make up the image.

• ➢ Plot region
• ➢ Figure region
• ➢ Outer region

The reason to define lines using vectors ?

In this, we can explicitly measure and set margin space.

The number lines of text that can fit on top of one another parallel to each edge. We can specify these
as vectors of length 4 in a particular order; each of the four elements corresponds to one of the four
sides; c(bottom, left, top, right).

About margins

Margins are the the index number representing either the row (1) or the column (2)

OR

All the plots in R have margins surrounding them that separate the main plotting space from the area
where the axes, labels and addition text lie

The graphical parameters available in the R

There are 2 Graphical Parameters available in the R. Such as

• ➢ oma (outer margin)


• ➢ mar (figure margin) call par()

The graphical parameters oma (outer margin) and mar (figure margin) are used to control these
amounts; like mfrow, they are initialized through a call to par before you begin to draw any new plot.

R> par()$oma

[output] 0 0 0 0

R> par()$mar

[output] 5.1 4.1 4.1 2.

54 | P a g e
➢ Plot Region:-

The plot region is all you’ve dealt with so far. This is where your actual plot appears and where you’ll
usually be drawing your points, lines, text, and so on. The plot region uses the user coordinate system,
which reflects the value and scale of the horizontal and vertical axes.

Example:-

# Create some example data

x <- seq(-5, 5, [Link] = 100)

y <- x^2

# Create a plot of the curve

plot(x, y, type = "l", col = "blue", lwd = 2, main = "Example Plot")

# Define a region to shade

(e.g., between x = -2 and x = 2)

x_region <- c(-2, 2)

y_region <- c(0, 4)

output:-

55 | P a g e
➢ Figure Region:-

The figure region is the area that contains the space for your axes, their labels, and any titles. These
spaces are also referred to as the figure margins.

Example:-

# Create a figure region with specified dimensions

plot(1, type = "n", xlab = "", ylab = "", xlim = c(0, 10), ylim = c(0, 5))

# Add a rectangle to represent the figure region

rect(xleft = 2, ybottom = 1, xright = 8, ytop = 4, col = "lightblue", border = "blue")

# Add some points to the figure region

points(x = c(3, 5, 7), y = c(2, 3, 2), col = "red", pch = 16)

# Add a title

title(“Figure Region in R)

Output:-

➢ Outer Region:-

The outer region, also referred to as the outer margins, is additional space around the figure region that
is not included by default but can be specified if it’s needed.

56 | P a g e
# Example of Outer Product in R

# Function to iterate through the outer region of a matrix

iterate_outer_region <- function(matrix) {

rows <- nrow(matrix)

cols <- ncol(matrix)

# Extract the outer region using array indexing

outer_region <- matrix[c(1, rows), c(1, cols)]

# Iterate through the extracted outer region

for (i in 1:nrow(outer_region)) {

for (j in 1:ncol(outer_region)) {

cat("Processing element at (", i, ", ", j, "): ", outer_region[i, j], "\n", sep = "")

# Example matrix

example_matrix <- matrix (1:12, nrow = 3, ncol = 4)

# Call the function with the example matrix

iterate_outer_region(example_matrix)

A. Example program to illustrate margins in plotting


# Sample data

x <- 1:10

y <- c(3, 5, 2, 8, 7, 4, 6, 9, 1, 3)

# Create a scatter plot plot

(x, y, pch = 16, main = "Scatter Plot with Margins", xlab = "X-axis", ylab = "Y-axis")

57 | P a g e
# Define margins (you can modify these values according to your data)

margin_left <- 3

margin_right <- 7

margin_bottom <- 2

margin_top <- 8

# Add text labels to represent the margins

mtext

58 | P a g e
R Program to illustrate plotting and margin ?
# Sample data

x <- 1:10

y <- c(3, 5, 2, 8, 7, 4, 6, 9, 1, 3)

# Create a scatter plot

plot(x, y, pch = 16, main = "Scatter Plot with Margins", xlab = "X-axis", ylab = "Y-axis")

# Define margins (you can modify these values according to your data)

margin_left <- 3

margin_right <- 7

margin_bottom <- 2

margin_top <- 8

# Add rectangles to represent the margins

rect(margin_left, margin_bottom, margin_right, margin_top, border = "red", lwd = 2, lty = "dashed")

# Add text labels to represent the margins

mtext("Margin Left", side = 1, line = 2, at = margin_left, col = "red", font = 2)

mtext("Margin Right", side = 1, line = 2, at = margin_right, col = "red", font = 2)

mtext("Margin Bottom", side = 2, line = 2, at = margin_bottom, col = "red", font = 2)

mtext("Margin Top", side = 2, line = 2, at = margin_top, col = "red", font = 2)

Output:-

59 | P a g e
R program using figure margins and outer margins.

We are used the ‘par()’ function to set both figure margins (‘mar’) and outer margins (‘oma’)

Example:-

# Create a sample plot with outer margin

plot(1:10, main = "Example Plot")

# Adjust outer margins

par(oma = c(2, 2, 2, 2)) # c(bottom, left, top, right)

# Add some content to the outer margin

mtext("Outer Margin Content", side = 3, line = 2)

# Adjust figure margins

par(mar = c(5, 4, 4, 2) + 0.1) # c(bottom, left, top, right)

# Add some content to the figure margin

text(5, 5, "Figure Margin Content", col = "blue")

60 | P a g e
# Reset par settings to default

par(oma = c(0, 0, 0, 0))

par(mar = c(5, 4, 4, 2) + 0.1)

In the given example we are used both figure margin and outer margin.

mtext()

In R, the ‘mtext()’ function is used to add text to the margins of a plot. It stands for “Margin Text” and is
commonly used to label the axes, add titles, or provide additional annotations to a plot. The ‘mtext()’
function allows you to specify the text, side (which margin to place the text on), line , and other
parameters for customizing the appearance of the text.

Here's a brief example of how to use ‘mtext()’

Example program:-

# Create a sample plot

plot(1:10, main = "Example Plot", xlab = "X-axis", ylab = "Y-axis")

# Add text to the outer margins

mtext("Top Margin", side = 3, line = 1, col = "blue")

mtext("Bottom Margin", side = 1, line = 1, col = "red")

mtext("Left Margin", side = 2, line = 1, col = "green")

mtext("Right Margin", side = 4, line = 1, col = "purple")

# Add text to the figure region

mtext("Figure Region", side = 3, line = 2, col = "orange")

# Add text with rotation

mtext("Rotated Text", side = 1, line = 2, col = "brown", adj = 0, padj = 0.5, srt = 90)

# Add text with font style and size

mtext(expression(paste(italic("Italic Text"), " and ", bold("Bold Text"))),

side = 2, line = 2, col = "darkblue", font = 2, cex = 1.2)

# Add a title to the outer margin

mtext("Overall Title", side = 3, line = 3, outer = TRUE, col = "black")

61 | P a g e
Output:-

In this example, ‘mtext(“X-axis label”, side = 1,line =3)’ adds the text “X-axis label” to the bottom
margin(side=1) at line 3. Similarly, ‘mtext(“Y-axis label”, side = 2, line = 3)’ adds the text “Y-axis label” to
the left margin (side = 2) at line 3.

We can customize the appearance of the text further by using additional parameters, such as ‘col’ for
color, ‘cex’ for text size, and ‘font’ for text font. The ‘mtext()’ function provides flexiblility in annotating
and enhancing the visual presentation of plots in R.

Point and click , co-ordinate interaction


Point and click:
They are one of the action of a computer users moving a pointer to a contain location on a
screen.

62 | P a g e
Co-ordinate:
Typically refers to a pair of values that represent a point in a multi-dimension space.

Or

The co-ordinate like (x,y) they are the numeric value representing position along respective area.

Interaction: action peformed

Application

Data exploration and analysis:

• Exploration: data sets by allowing users to click on pointers of interest in plotes


• Analysis: relationships B/w variable through interactive scatter plots

Data visulatization: representation of information in the form of chart,diagram,picture ect.

Finance and stack market analysis: helps traders to gain on insight into economy stack
market

Educational tools: based on grades

Advantages:

• Data validation: checking and confirming values ensuring accurancy.


• Collaboration: like partnership
• Educational tools: helping students and learness gresp complex concepts through
visual exploration.

Disadvantages:

• Limited flexibility
• Limited scripiting visibility
• Security

Where we use point & click co-ordinate interaction:

• Data exploration: idea about structure of the dataset.


• Game development
• User interface for stistical models

63 | P a g e
Program

# create a scatter plot

Plot(1,type=”n”,xlab=”x”,ylab=”y”,main=”point_and_click interaction”)

# allow the user to click on plot

Points<-locator()

# display the clicked co-ordinates

if( ! Is null(points $ x))

Cat(“clicked at (“points$x , points$y,)\n”)

Else

Cat(“no pointer clicked \n”)

Customizing traditional R plots


• Composition of traditional R plot

The method is described in the Embedding base graphics plots in grid viewports section of the
gridBase vignette.
The gridBase package contains functions to set sensible parameters for the plotting region of
the base plot. So we need these packages

R plot: The plot() function is used to draw points (markers) in a diagram. The function takes
parameters for specifying points in the diagram. Parameter 1 specifies points on the x-axis.
Parameter 2 specifies points on the y-axis.

64 | P a g e
• Types of traditional R plot

These include density plots (histograms and kernel density plots), dot plots, bar charts (simple,
stacked, grouped), line charts, pie charts (simple, annotated, 3D), boxplots (simple, notched,
violin plots, bagplots) and Scatterplots (simple, with fit lines, scatterplot matrices, high density
plots, and 3D plots).

Scatterplots: A “scatter plot” is a type of plot used to display the relationship between two
numerical variables, and plots one dot for each observation.

Histogram: histogram is a graphical representation commonly used to visualize the distribution


of numerical data. It divides the values within a numerical variable into “bins”, and counts the
number of observations that fall into each bin.

Line chart:

A line chart is a graph that connects a series of points by drawing line segments between them. These
points are ordered in one of their coordinate (usually the x-coordinate) value. Line charts are usually
used in identifying the trends in data. The plot() function in R is used to create the line graph.

Bar chart:

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with
heights or lengths proportional to the values that they represent. The bars can be plotted vertically or
horizontally. A vertical bar chart is sometimes called a column chart.

The bagplot(x, y) function in the aplpackpackage provides a bivariate


Bag plot:
version of the univariate boxplot. The bag contains 50% of all points. The
bivariate median is approximated. The fence separates points in the fence
from points outside.
65 | P a g e
Examples plots graphs and charts in using R traditional

66 | P a g e
Specialized text and label notation.
Font: -

1) What are the 4-type font?


• Serif,
• Sans Serif
• Display
• Script
2) How many types generic families?
• Sans
• Serif
• Mono
3) What are the possible various of fonts? Types
• Normal text
• Bold
• italic
• bold and italic
4) how many set graphical parameters?
We can set two graphical parameters universally for a device using par.

5) What are the graphical parameters?


• Family for the specific font

67 | P a g e
• Integer selector for controlling bold and italic typeface

Greek symbols: -

1) What are Greek symbols used for?


Greek letters are used in mathematics, science, engineering, and other areas

2) How many Greek symbols are there?


here are 24 letters in the Greek alphabet
.
3) What does ∑ mean?
The symbol ∑ indicates summation and is used as a shorthand notation for the sum of terms that
follow a pattern.

4) How do you text Greek symbols?


Alt shortcuts — for example, to type ύ, ϋ or ΰ, hold Alt and press U one, two or three times.
5) What is the 7 Greek symbol?
• Alpha
• Beta
• Eta
• Theta
• Nu xi lambda
• Psi
• Tau

Mathematical expression:

- 1) What is the mathematical form of R?

a variable used to represent a real number or any other mathematical quantity.

2) What is R in mathematical functions?

Recall the notation that R stands for the real numbers. Similarly, R2 is a two-dimensional vector, and R3 is
a three-dimensional vector.

3) What is the mathematical symbol R?

Real numbers.

4) What is a mathematical function in R?

a rule which takes some. inputs and delivers a definite output.

5) How to use mathematical functions in R?

68 | P a g e
• Simple Math. In R, you can use operators to perform common mathematical operations on
numbers. .
• Built-in Math Functions. R also has many built-in math functions that allows you to perform
mathematical tasks on numbers.
• sqrt() The sqrt() function returns the square root of a number
• abs()
• ceiling() and floor()

PLOTTING IN HIGHER DIMENSION

Defining Colors
A colour palette is a combination of colours used in UI designs when designing and interface when used
correctly colours platform from a visual foundation of your brand help to maintain the consistency and
to make your user interface aesthetically pleasing and enjoyable to use.

Colour symbolism art , color in a computer programs are represented by combining 3 “pigments”, color
can be specified by col keyword

HEX Value:-
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff (same as decimal 0-
255). For example, #ff0000 is displayed as red, because red is set to its highest value (ff) and the others
are set to the lowest value (00). To display black, set all values to 00, like this: #000000.

Hex color codes start with a pound sign or hashtag (#) and are followed by six letters and/or numbers.
The first two letters/numbers refer to red, the next two refer to green, and the last two refer to blue.
The color values are defined in values between 00 and FF (instead of from 0 to 255 in RGB). # We need
this line of code to show graphs in our compiler bitmap(file="[Link]") plot(1:10, col="red")

69 | P a g e
Plot() :-
The plot() function is used to draw points (markers) in a diagram. The function takes parameters for
specifying points in the diagram. Parameter 1 specifies points on the x-axis. Parameter 2 specifies points
on the y-axis.

Example:-

# We need this line of code to show graphs in our compiler bitmap(file="[Link]")

plot(1:10, pch=25, cex=2, col="purple")

Dimensions :-
Dimensions are represented by dim() function a dimension statement defines the array and sets up
the number of elements within the dimensions.

A measurement of something in a particular direction such as height , length , width.

The dim function of the R programming language returns the dimension (e.g. the number of columns
and rows) of a matrix, array or data frame. Above, you can see the R code for the application of dim in R.

An array is a three-dimensional (r × c × h) object (think a bunch of stacked r × c matrices). All elements in


an array must be of the same data type (character > numeric > logical). A dataframe is a two-
dimensional (r × c × h) object (like a matrix). A list is a set of objects.

# An array with one dimension with values ranging from 1 to 24


thisarray <- c(1:9)

# An array with more than one dimension multiarray <- array(thisarray,


dim = c(4, 3, 2)) multiarray

70 | P a g e
Plotting in higher dimensions by representing and
using colors
High-dimensional data are defined as data in which the number of features
(variables observed), p, are close to or larger than the number of observations (or data points), n. The
opposite is low-dimensional data in which the number of observations, n, far outnumbers the number of
features.

HIGHER DIMENSIONS :-

A dimensions beyond the three dimensions of space that we experience in our everyday lives. Higher
dimensions typically refer to array or matrices with more then two dimensions.

We can also work higher dimensions data using functions like array() to create and manipulate the
structures. Example 1:- 3D dimension scatterplot library(scatterplot3d) [Link](42) num_points<-100
x<-runif(num_points) y<-runif(num_points) z<-runif(num_points) w<-runif(num_points)
color_palette<-colorRampPalette(c("pink","black","red","orange")) colors<-
color_palette(100)[cut(w,100)]
scatterplot3d(x ,y , z, color=colors, main="3D scatterplot with color mapping") Output:-

71 | P a g e
Example 2:- 2D dimensions scatterplot

[Link](42) num_points<-100 x<-


runif(num_points) y<-runif(num_points)
z<-runif(num_points)
color_palette<-colorRampPalette(c("red","green","blue","yellow")) colors<-color_palette(100)
[cut(z,100)]
plot(x, y, col=colors, pch=16, main="2D scatterplot with color mapping") Output:-

72 | P a g e
3D Scatter plot

2 marks questions.

1. What is 3d scatter plot?


3d scatterplots are used to plot data points on there axes in the attempt to show the
relationship between three variables.
2. What is the use of 3d scatterplot?
3d scatterplots are used to plot data points on three axes in the attempt to show the
relationship between three variables.
3. How to install and load Scatterplot 3d? [Link](“Scatterplot 3d”) library(“Scatterplot
3d”)
4. What is the function of 3d Scatterplot and what is XYZ in r?
• Scatterplot3d(x,y=Null,z=Null)
• XYZ are the coordinates of points to be [Link] arguments Y and Z can be optional
depending can optional depending on the structure of of x.
5. Write the basic 3d scatterplots. Scatterplot3d(iris[ ,1:3])
6. Change the shape and the colour of points.
Scatterplot3d(iris[ ,1:3],pch=16,color=”steelblue”)
7. Write the arguments for global appearance in graph.
• Grid:a logical [Link] TRUE,a grid is drawn on the plot.
• Box: a logical value. If TRUE, a box is drawn around the plot.

5/7 marks questions.

8. Write the modification of 3d Scatterplot output.


Scatterplot3d returns a list of function closures which can be used to add elements on a existing
plot.

The returned functions are:


• [Link](): to convert 3d coordinates to the 2d parallel projection of the existing
scatterplot3d. It can be used to add arbitrary elements, such as legend,into the plot.

73 | P a g e
• Points3d(): to add points or lines into existing plot.
• Plane3d(): to ass a plane into the existing plot.
• Box3d(): to add or refresh a box around the plot.
9. Specifying the legend position using keywords.
• Bty=”n”: to remove the box around the legend .
• Bg=”transparent”: to change the background colour of the legend box to transparent
colour.
• Inset: to modify the distance (s) between plot margins and the legend box.
• Horiz: a logical value; if TRUE, set the legend horizontally rather than vertically.
• Xpd: a logical values; if enables the legend items to be drawn outside the plot.
10. Write the regression plane and supplementary points functions.
• The result of scatterplot3d () is assigned to s3d.
• A linear model is calculated as follow:ln(zvar~xvar+yvar)
• Assumption:zvar depends on xvar and yvar.
• The function s3d$plane3d() is used to add the regression plane.
• Supplementary points are added using the function s3d$points3d().
11. Write the example program for 3d Scatterplot.
#install and load the required package

[Link](“scatterplot3d”)

Library (scatterplot3d)

#sample data(random values for demonstration)

X<-rnorm(100)

Y<-rnorm(100)

Z<-rnorm(100)

#create a 3d scatter plot

Scatter3d(x,y,z,main=”3d Scatterplot”,pch=19,color=”blue”,xlab=”Xaxis”,ylab=”Y-axis”,zlab=”Z-
axis”)

12. Change the main title and axis labels.

Scatterplot3d(iris[ ,1:3], Main=”3d


Scatterplot”, xlab=”sepal length(cm)”,

74 | P a g e
ylab=”sepal width(cm)”, zlab=”petal
length (cm)”)
13. Change the points shapes in group.
shapes = c(16, 17, 18)

shapes <- shapes[[Link](iris$Species)]

scatterplot3d(iris[,1:3], pch = shapes)

14. Changing points colours in group.


colors <- c("#999999", "#E69F00", "#56B4E9") colors <-
colors[[Link](iris$Species)] scatterplot3d(iris[,1:3], pch = 16,
color=colors)
15. Removing the box around the plot and adding a bars to point in plot.
• scatterplot3d(iris[,1:3], pch = 16, color = colors, grid=TRUE, box=FALSE)#this is for
removing box.
• scatterplot3d(iris[,1:3], pch = 16, type="h", color=colors)#this is for adding bars.

75 | P a g e

You might also like