0% found this document useful (0 votes)
14 views33 pages

Basicsof Statistics

r programming bca 5 nep 2020

Uploaded by

cleanculture469
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)
14 views33 pages

Basicsof Statistics

r programming bca 5 nep 2020

Uploaded by

cleanculture469
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

 Statistics is the practice of turning data

into information to identify trends and


understand features of populations.
 Often, the first thing statistical analysts are faced
with raw data, the records or observations that
make up a sample.
 Depending on the nature of the intended analysis,
these data could be stored in a specialized R
object, often a data frame ,possibly read in from
an external file using techniques.
 Before you can begin summarizing or modelling
your data, however, it is important to clearly
identify your available variables.
 A variable is a characteristic of an individual in a population,
the value of which can differ between entities within that
population.
 You recorded the age, Gender, and humor level for a sample
of people. These characteristics are your variables.
 The values measured will differ between the individuals.
Variables can take on a number of forms, determined by the
nature of the values they may take.
 A numeric variable is one whose observations are naturally
recorded as numbers.
 There are two types of numeric variables:
1. Continuous
2. Discrete.
 A continuous variable can be recorded as any value in some
interval, up to any number of decimals (which technically
gives an infinite number of possible values, even if the
continuum is restricted in range).
 For example, if you were observing rainfall amount, a value
of 15 mm would make sense, but so would a value of
15.42135 mm. Any degree of measurement precision gives a
valid observation.
 A discrete variable, may take on only distinct numeric values
and if the range is restricted, then the number of possible
values is finite.
 For example, if you were observing the number of heads in 20
flips of a coin, only whole numbers would make sense. It
would not make sense to observe 15.42135 heads, the
possible outcomes are restricted to the integers from 0 to 20 .
 Though numeric observations are common for many
variables, it’s also important to consider categorical variables.
 There are two types of categorical variables.
1. Nominal
2. Ordinal
 Those that cannot be logically ranked are called nominal.
 An example of a categorical-nominal variable is gender. In
most data sets, it has two fixed possible values, male and
female, and the order of these categories is irrelevant.
 Categorical variables that can be naturally ranked are called
ordinal.
 An example of a categorical ordinal variable would be the
dose of a drug, with the possible values low, medium, and
high. These values can be ordered in either increasing or
decreasing amounts, and the ordering might be relevant to the
research.
 When discussing or analyzing data related to only one
dimension, then it is called univariate data.
 For example, the weight variable is univariate since
each measurement can be expressed with one
component—a single number.
 Data with respect to variables that exist in more than one
dimension or with more than one component or
measurement associated with each observation, your data
are considered multivariate.
 Example is that of spatial coordinates, which must be
considered in terms of at least two components a
horizontal x-coordinate and a vertical y-coordinate.
 Statistics as a discipline is concerned with understanding
features of an overall population, defined as the entire
collection of individuals or entities of interest. The
characteristics of that population are referred to as
parameters.
 Because researchers are rarely able to access relevant data on
every single member of the population of interest, they
typically collect a sample of entities to represent the
population and record relevant data from these entities. They
may then estimate the parameters of interest using the sample
data—and those estimates are the statistics.
 Measures of centrality are commonly used to explain large
collections of data by describing where numeric observations are
centred.
 One of the most common measures of centrality is arithmetic mean.
It is considered to be the central “balance point” of a collection of
observations.
 The measure of central tendency in R Language represents the
whole set of data by a single value. It gives us the location of the
central points.
 There are three main measures of central tendency: -
1. Mean
2. Median
3. Mode
 It is the sum of observations divided by the total number of observations.
It is also defined as average which is the sum divided by count.
 Syntax:
mean(x, [Link])
 Parameters:
• x: Numeric Vector
• [Link]: Boolean value to ignore NA value
Example:
# Taking a list of elements
list = c(2, 40, 2, 502, 177, 7, 9)
Output:
#Compute the mean value
[1] 105.5714
print(mean(list))
 It is the middle value of the data set. It splits the data into
two halves.
 If the number of elements in the data set is odd then the
center element is median and if it is even then the median
would be the average of two central elements.
 Syntax:
median(x, [Link] = False)
Where, X is a vector and [Link] is used to remove missing
value.

Example
X=c(2, 4.4, 3, 3, 2, 2.2, 2, 4) Output:
print(median(X)) [1] 2.6
 The mode is a statistical measure used to identify the value that
occurs most frequently in a dataset.
 Unlike the mean and median, which represent the central
tendencies, the mode highlights the most commonly recurring
value, making it a useful measure for identifying patterns and
understanding the distribution of categorical or discrete data.
 There is no inbuilt function for finding mode in R, so we can create
our own function for finding the mode or we can use the package
called modeest.
# Create the function.
getmode <- function(v) {
uniqv <- unique(v)
uniqv[[Link](tabulate(match(v, uniqv)))]}
# Create the vector with numbers. Output:
v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3) [1] 2
# Calculate the mode using the user function.
result <- getmode(v)
print(result)
[Link]("modeest")
# Install the package if you haven't already
library(modeest)
vec <- c(1, 2, 3, 4, 5, 3, 7, 8, 9, 3)
#mlv: Most likely values.
#mfv: Most frequent values
mlv(vec, method = "mfv")

Output:
[1] 3
 Variance is the sum of squares of differences
between all numbers and means.
 The mathematical formula for variance is as
follows:
 Variance can be calculated by using var() function in R.
Syntax: var(x)
 Parameters:
x: numeric vector
 Example:
# R program to get variance of a list
# Taking a list of elements Output:
list = c(2, 4, 4, 4, 5, 5, 7, 9) [1] 4.571429
# Calculating variance using var()
print(var(list))
 Standard Deviation is the square root of variance. It is a
measure of the extent to which data varies from the mean.
 Calculate the standard deviation by using sd() function in R.
 Syntax:
sd(x)
 Parameters:
x: numeric vector
 Example:
# R program to get standard deviation of a list
# Taking a list of elements Output:
list = c(2, 4, 4, 4, 5, 5, 7, 9) [1] 2.13809
# Calculating standard deviation using sd()
print(sd(list))
 The interquartile range defines the difference between the third and the
first quartile.
 First Quartile is denoted by Q1 known as the lower quartile(25%), the
second Quartile is denoted by Q2 (50%), and the third Quartile is
denoted by Q3 (75%) known as the upper quartile. Therefore, the
interquartile range is equal to the upper quartile minus lower quartile.
 The formula for the interquartile range is given below-

Interquartile range = Upper Quartile – Lower Quartile


= Q3 – Q1
Syntax:
IQR(x)
Parameters:
x: Data set

# R program to calculate IQR value


# Defining vector
x <- c(5, 5, 8, 12, 15, 16) Output:
# Print Interquartile range [1] 8.5
print(IQR(x))
 The covariance expresses how much two numeric variables
“change together” and the nature of that relationship, whether it is
positive or negative.
 Suppose for n individuals you have a sample of observations for
two variables, labeled x = {x1, x2,. . , xn} and y = {y1, y2,. . , yn },
where xi corresponds to yi for i = 1,. . . ,n.
 The sample covariance rxy is computed with the following, where
x¯ and y¯ represent the respective sample means of both sets of
observations.
 Syntax:
cov(x, y)
where,
x and y represents the data vectors
 Correlation allows you to interpret the covariance further by
identifying both the direction and the strength of any
association.
 There are different types of correlation coefficients, but the
most common of these is Pearson’s product-moment
correlation coefficient, the default implemented by R.
 Pearson’s sample correlation coefficient pxy is computed by
dividing the sample covariance by the product of the
standard deviation of each data set. Formally, where rxy
corresponds to sx and sy which ensures that −1 ≤ pxy ≤ 1.
# Create sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
# Calculate covariance
covariance <- cov(x, y)
print(paste("Covariance:", covariance)) Output
"Covariance: 5"
# Calculate correlation "Correlation: 1"
correlation <- cor(x, y)
print(paste("Correlation:", correlation))
 Outliers are the data points that lie away from the other
points of the dataset.
 An outlier is an observation that does not appear to “fit” with
the rest of the data.
 Outlier detection is a statistical approach used to find outliers
in datasets. Measurement errors, incorrect data entry, or
really anomalous data values are a few of the causes of
outliers.
list1 <- c(0.1,0.3,1.3,0.6,0.2,-1.7,0.8,0.9,-0.8,-1.0)
list2 <- c(-0.3,0.9,2.8,2.3,1.2,-4.1,-0.4,4.1,-2.3,100.0)
plot(list1,list2,axes=T, cex=2,[Link]=1.5,[Link]=1.5)
arrows(-0.5,-80,-0.94,-97,lwd=2)
text(-0.45,-74,labels="outlier?“, cex=3)

You might also like