Comp Statistics Using R
Comp Statistics Using R
Statistics is a branch of mathematics that deals with the collection, analysis, interpretation,
presentation, and organization of data. It plays a vital role in various fields by providing valuable
insights and facilitating informed decision-making. Let's explore how statistics is applied in
different domains:
1. Business and Economics: In business and economics, statistics is used for market
research, forecasting sales, analyzing financial data, and making informed decisions about
investments. Statistical techniques like regression analysis help identify the relationship
between variables, enabling businesses to optimize their strategies and improve
efficiency.
2. Social Sciences: In fields like sociology, psychology, and anthropology, statistics helps
researchers study human behavior, attitudes, and trends. Surveys and experiments use
statistical methods to draw conclusions from data and test hypotheses. Statistical analysis
helps in understanding social phenomena and making evidence-based policy decisions.
3. Medicine and Healthcare: In the medical field, statistics is used in clinical trials to assess
the effectiveness of new treatments and drugs. Epidemiologists use statistical methods to
study disease patterns and risk factors in populations. Medical researchers also use
statistics to analyze patient data and draw conclusions about health outcomes.
4. Environmental Science: Environmental scientists use statistics to analyze data related to
climate change, pollution, and biodiversity. They study trends and patterns in
environmental data to understand the impact of human activities on the planet and
develop strategies for sustainable resource management.
5. Education: In the field of education, statistics is used to analyze test scores, evaluate
educational programs, and identify areas for improvement in teaching methods.
Researchers use statistical techniques to measure student performance and assess the
effectiveness of educational interventions.
6. Government and Public Policy: Governments use statistics to collect and analyze data
on demographics, economy, and public health. This information is crucial for policy
development and resource allocation. Census data and surveys help governments plan
and implement social and economic programs.
7. Sports Analytics: In sports, statistics are used to analyze player performance, team
strategies, and game outcomes. Sports analytics has become increasingly popular in
recent years, enabling teams to make data-driven decisions regarding player selection,
tactics, and training.
8. Quality Control and Manufacturing: Industries employ statistical quality control to
monitor and improve the production process. Statistical methods like Six Sigma help
identify and reduce defects, ensuring that products meet high-quality standards.
9. Market Research and Polling: Statistics is essential in market research to analyze
consumer behavior, preferences, and market trends. Polling organizations use statistical
sampling techniques to estimate public opinion on various issues.
10. Finance and Investment: In finance, statistics is used to analyze stock market data,
manage investment portfolios, and assess risk. Financial analysts use statistical models to
make predictions and guide investment decisions.
Installation of R
R programming is a very popular language and to work on that we have to install two
things, i.e., R and RStudio. R and RStudio works together to create a project on R.
The official site [Link] provides binary files for major operating
systems including Windows, Linux, and Mac OS. In some Linux distributions, R is
installed by default, which we can verify from the console by entering R.
Install R in Windows
There are following steps used to install the R in Windows:
Step 1:
Step 2:
When we click on Download R 3.6.1 for windows, our downloading will be started
of R setup. Once the downloading is finished, we have to run the setup of R in the
following way:
1) Select the path where we want to download the R and proceed to Next.
2) Select all components which we want to install, and then we will proceed to Next.
3) In the next step, we have to select either customized startup or accept the default,
and then we proceed to Next.
4) When we proceed to next, our installation of R in our system will get started:
RStudio IDE
RStudio is an integrated development environment which allows us to interact with R
more readily. RStudio is similar to the standard RGui, but it is considered more user-
friendly. This IDE has various drop-down menus, Windows with multiple tabs, and so
many customization processes. The first time when we open RStudio, we will see three
Windows. The fourth Window will be hidden by default. We can open this hidden
Window by clicking the File drop-down menu, then New File and then R Script.
Console Window Lower-left The location where commands are entered and
output is printed.
Help Tab Lower-right Output location for help commands and help
search Window.
Installation of RStudio
Installation on Windows/Linux
On Windows and Linux, it is quite simple to install RStudio. The process of installing
RStudio in both the OS is the same. There are the following steps to install RStudio in
our Windows/Linux:
Step 1:0s
In the first step, we visit the RStudio official site and click on Download RStudio.
Step 2:
In the next step, we will select the RStudio desktop for open-source license and click
on download.
Step 3:
In the next step, we will select the appropriate installer. When we select the installer,
our downloading of RStudion setup will start.
Step 4:
In the next step, we will run our setup in the following way:
1) Click on Next.
2) Click on Install.
3) Click on finish.
Pros
1) Open Source
An open-source language is a language on which we can work without any need for a
license or a fee. R is an open-source language. We can contribute to the development
of R by optimizing our packages, developing new ones, and resolving issues.
2) Platform Independent
R is a platform-independent language or cross-platform programming language
which means its code can run on all operating systems. R enables programmers to
develop software for several competing platforms by writing a program only once. R
can run quite easily on Windows, Linux, and Mac.
7) Statistics
R is mainly known as the language of statistics. It is the main reason why R is
predominant than other programming languages for the development of statistical
tools.
8) Continuously Growing
R is a constantly evolving programming language. Constantly evolving means when
something evolves, it changes or develops over time, like our taste in music and
clothes, which evolve as we get older. R is a state of the art which provides updates
whenever any new feature is added.
Cons
1) Data Handling
In R, objects are stored in physical memory. It is in contrast with other programming
languages like Python. R utilizes more memory as compared to Python. It requires the
entire data in one single place which is in the memory. It is not an ideal option when
we deal with Big Data.
2) Basic Security
R lacks basic security. It is an essential part of most programming languages such as
Python. Because of this, there are many restrictions with R as it cannot be embedded
in a web-application.
3) Complicated Language
R is a very complicated language, and it has a steep learning curve. The people who
don't have prior knowledge or programming experience may find it difficult to learn
R.
4) Weak Origin
The main disadvantage of R is, it does not have support for dynamic or 3D graphics.
The reason behind this is its origin. It shares its origin with a much older programming
language "S."
5) Lesser Speed
R programming language is much slower than other programming languages such as
MATLAB and Python. In comparison to other programming language, R packages are
much slower.
In R, algorithms are spread across different packages. The programmers who have no
prior knowledge of packages may find it difficult to implement algorithms.
R Packages
R packages are the collection of R functions, sample data, and compile codes. In the R
environment, these packages are stored under a directory called "library." During
installation, R installs a set of packages. We can add packages later when they are
needed for some specific purpose. Only the default packages will be available when
we start the R console. Other packages which are already installed will be loaded
explicitly to be used by the R program.
There is the following list of commands to be used to check, verify, and use the R
packages.
1. libPaths()
When the above code executes, it produces the following project, which may vary
depending on the local settings of our PCs & Laptops.
Pip 10s
[1] "C:/Users/ajeet/OneDrive/Documents/R/win-library/3.6"
[2] "C:/Program Files/R/R-3.6.1/library"
1. library()
When we execute the above function, it produces the following result, which may vary
depending on the local settings of our PCs or laptops.
Like library() function, R provides search() function to get all packages currently loaded
in the R environment.
1. search()
When we execute the above code, it will produce the following result, which may vary
depending on the local settings of our PCs and laptops:
The following command is used to get the packages directly from CRAN webpage and
install the package in the R environment. We may be prompted to choose the nearest
mirror. Choose the one appropriate to our location.
1. [Link]("Package Name")
The syntax of installing XML package is as follows:
1. [Link]("XML")
Output
Once the downloading has finished, we will use the following command:
1. [Link]("C:\Users\ajeet\OneDrive\Desktop\graphics\xml2_1.[Link]", re
pos = NULL, type = "source")
1. [Link]("C:\Users\ajeet\OneDrive\Desktop\graphics\xml2_1.[Link]", re
pos = NULL, type = "source")
List of R packages
R is the language of data science which includes a vast repository of packages. These
packages appeal to different regions which use R for their data purposes. CRAN has
10,000 packages, making it an ocean of superlative statistical work. There are lots of
packages in R, but we will discuss the important one.
There are some mostly used and popular packages which are as follows:
1) tidyr
The word tidyr comes from the word tidy, which means clear. So the tidyr package is
used to make the data' tidy'. This package works well with dplyr. This package is an
evolution of the reshape2 package.
2) ggplot2
R allows us to create graphics declaratively. R provides the ggplot package for this
purpose. This package is famous for its elegant and quality graphs which sets it apart
from other visualization packages. 10s
3) ggraph
R provides an extension of ggplot known as ggraph. The limitation of ggplot is the
dependency on tabular data is taken away in ggraph.
4) dplyr
R allows us to perform data wrangling and data analysis. R provides the dplyr library
for this purpose. This library facilitates several functions for the data frame in R.
5) tidyquant
The tidyquant is a financial package which is used for carrying out quantitative financial
analysis. This package adds to the tidyverse universe as a financial package which is
used for importing, analyzing and visualizing the data.
6) dygraphs
The dygraphs package provides an interface to the main JavaScript library which we
can use for charting. This package is essentially used for plotting time-series data in R.
7) leaflet
For creating interactive visualization, R provides the leaflet package. This package is
an open-source JavaScript library. The world's popular websites like the New York
Times, Github and Flicker, etc. are using leaflet. The leaflet package makes it easier to
interact with these sites.
8) ggmap
For delineating spatial visualization, the ggmap package is used. It is a mapping
package which consists of various tools for geolocating and routing.
9) glue
R provides the glue package to perform the operations of data wrangling. This
package is used for evaluating R expressions which are present within the string.
10) shiny
R allows us to develop interactive and aesthetically pleasing web apps by providing
a shiny package. This package provides various extensions with HTML widgets, CSS,
and JavaScript.
11) plotly
The plotly package provides online interactive and quality graphs. This package
extends upon the JavaScript library -[Link].
12) tidytext
The tidytext package provides various functions of text mining for word processing
and carrying out analysis through ggplot, dplyr, and other miscellaneous tools.
13) stringr
The stringr package provides simplicity and consistency to use wrappers for the
'stringi' package. The stringi package facilitates common string operations.
14) reshape2
This package facilitates flexible reorganization and aggregation of data using melt ()
and decast () functions.
15) dichromat
The R dichromat package is used to remove Red-Green or Blue-Green contrasts from
the colors.
16) digest
The digest package is used for the creation of cryptographic hash objects of R
functions.
17) MASS
The MASS package provides a large number of statistical functions. It provides
datasets that are in conjunction with the book "Modern Applied Statistics with S."
18) caret
R allows us to perform classification and regression tasks by providing the caret
package. CaretEnsemble is a feature of caret which is used for the combination of
different models.
19) e1071
The e1071 library provides useful functions which are essential for data analysis like
Naive Bayes, Fourier Transforms, SVMs, Clustering, and other miscellaneous functions.
20) sentimentr
The sentiment package provides functions for carrying out sentiment analysis. It is
used to calculate text polarity at the sentence level and to perform aggregation by
rows or grouping variables.
Syntax of R Programming
R Programming is a very popular programming language which is broadly used in data
analysis. The way in which we define its code is quite simple. The "Hello World!" is the
basic program for all the languages, and now we will understand the syntax of R
programming with "Hello world" program. We can write our code either in command
prompt, or we can use an R script file.
1. R Command Prompt
It is required that we have already installed the R environment set up in our system to
work on the R command prompt. After the installation of R environment setup, we can
easily start R command prompt by typing R in our Windows command prompt. When
we press enter after typing R, it will launch interpreter, and we will get a prompt on
which we can code our program.
In the above code, the first statement defines a string variable string, where we assign
a string "Hello World!". The next statement print() is used to print the value which is
stored in the variable string.
2. R Script File
The R script file is another way on which we can write our programs, and then we
execute those scripts at our command prompt with the help of R interpreter known
as Rscript. We make a text file and write the following code. We will save this file with
.R extension as:
Demo.R
To execute this file in Windows and other operating systems, the process will remain
the same as mentioned below.
Comments
In R programming, comments are the programmer readable explanation in the source
code of an R program. The purpose of adding these comments is to make the source
code easier to understand. These comments are generally ignored by compilers and
interpreters.
Single-line comment
4. Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand "
5. }
6. #My First program in R programming
7. string <-"Hello World!"
8. print(string)
In R, there are several data types such as integer, string, etc. The operating system
allocates memory based on the data type of the variable and decides what can be
stored in the reserved memory.
There are the following data types which are used in R programming:
Logical True, False It is a special data type for data with only two
possible values which can be construed as
true/false.
Integer 3L, 66L, 2346L Here, L tells R to store the value as an integer,
When we execute the following program, it will give us the following output:
1. Atomic vector
2. List
3. Array
4. Matrices
5. Data Frame
6. Factors
Vectors
A vector is the basic data structure in R, or we can say vectors are the most basic R data
objects. There are six types of atomic vectors such as logical, integer, character, double, and
raw. "A vector is a collection of elements which is most commonly of mode
character, integer, logical or numeric" A vector can be one of the following two types:
1. Atomic vector
2. Lists
List
In R, the list is the container. Unlike an atomic vector, the list is not restricted to be a single
mode. A list contains a mixture of data types. The list is also known as generic vectors
because the element of the list can be of any type of R object. "A list is a special type of
vector in which each element can be a different type."
We can create a list with the help of list() or [Link](). We can use vector() to create a required
length empty list.
Arrays
There is another type of data objects which can store data in more than two dimensions
known as arrays. "An array is a collection of a similar data type with contiguous
memory allocation." Suppose, if we create an array of dimension (2, 3, 4) then it creates
four rectangular matrices of two rows and three columns.
In R, an array is created with the help of array() function. This function takes a vector as an
input and uses the value in the dim parameter to create an array.
Matrices
A matrix is an R object in which the elements are arranged in a two-dimensional rectangular
layout. In the matrix, elements of the same atomic types are contained. For mathematical
calculation, this can use a matrix containing the numeric element. A matrix is created with
the help of the matrix() function in R.
Syntax
Data Frames
A data frame is a two-dimensional array-like structure, or we can say it is a table in which
each column contains the value of one variable, and row contains the set of value from each
column.
Factors
Factors are also data objects that are used to categorize the data and store it as levels. Factors
can store both strings and integers. Columns have a limited number of unique values so that
factors are very useful in columns. It is very useful in data analysis for statistical modeling.
Factors are created with the help of factor() function by taking a vector as an input
parameter.
Variables in R Programming
Variables are used to store the information to be manipulated and referenced in the R
program. The R variable can store an atomic vector, a group of atomic vectors, or a
combination of many R objects.
Language like C++ is statically typed, but R is a dynamically typed, means it check the
type of data type when the statement is run. A valid variable name contains letter,
numbers, dot and underlines characters. A variable name should start with a letter or
the dot not followed by a number.
var_name, Valid Variable can start with a dot, but dot should not be
[Link] followed by a number. In this case, the variable will
be invalid.
Assignment of variable
In R programming, there are three operators which we can use to assign the values to
the variable. We can use leftward, rightward, and equal_to operator for this purpose.
There are two functions which are used to print the value of the variable i.e., print() and
cat(). The cat() function combines multiples values into a continuous print output.
When we execute the above code in our R command prompt, it will give us the
following output:
We can check the data type of the variable with the help of the class() function. Let's
see an example:
1. variable_y<- 124
2. cat("The data type of variable_y is ",class(variable_y),"\n")
3.
4. variable_y<- "Learn R Programming"
5. cat(" Now the data type of variable_y is ",class(variable_y),"\n")
6.
7. variable_y<- 133L
8. cat(" Next the data type of variable_y becomes ",class(variable_y),"\n")
When we execute the above code in our R command prompt, it will give us the
following output:
Keywords in R Programming
In programming, a keyword is a word which is reserved by a program because it has a
special meaning. A keyword can be a command or a parameter. Like in C, C++, Java,
there is also a set of keywords in R. A keyword can't be used as a variable name.
Keywords are also called as "reserved names."
if else repeat
NaN NA NA_integer_
1) if
The if statement consists of a Boolean expression which is followed by one or more
statements. In R, if statement is the simplest conditional statement which is used to
decide whether a block of the statement will be executed or not.
Example:
1. a<-11
2. if(a<15)
3. + print("I am lesser than 15")
Output:
2) else
The R else statement is associated with if statement. When the if statement's condition
is false only then else block will be executed. Let see an example to make it clear:
Example:
1. a<-22
2. if(a<20){
3. cat("I am lesser than 20")
4. }else{
5. cat("I am larger than 20")
6. }
Output:
3) repeat
The repeat keyword is used to iterate over a block of code multiple numbers of times.
In R, repeat is a loop, and in this loop statement, there is no condition to exit from the
loop. For exiting the loop, we will use the break statement.
Example:
1. x <- 1
2. repeat {
3. cat(x)
4. x = x+1
5. if (x == 6){
6. break
7. }
8. }
Output:
4) while
A while keyword is used as a loop. The while loop is executed until the given condition
is true. This is also used to make an infinite loop.
Example:
1. a <- 20
2. while(a!=0){
3. cat(a)
4. a = a-2
5. }
Output:
5) function
A function is an object in R programming. The keyword function is used to create a
user-define function in R. R has some pre-defined functions also, such as seq, mean,
and sum.
Example:
1. [Link]<- function(n) {
2. for(i in 1:n) {
3. a <- i^2
4. print(a)
5. }
6. }
7. [Link](6)
Output:
6) for
The for is a keyword which is used for looping or iterating over a sequence (dictionary,
string, list, set or tuple).
We can execute a set of a statement once for each item in the iterator (list, set, tuple,
etc.) with the help of for loop.
Example:
1. v <- LETTERS[1:4]
2. for ( i in v) {
3. print(i)
4. }
Output:
7) next
The next keyword skips the current iteration of a loop without terminating it. When R
parser found next, it skips further evaluation and starts the new iteration of the loop.
Example:
1. v <- LETTERS[1:6]
2. for ( i in v) {
3. if (i == "D") {
4. next
5. }
6. print(i)
7. }
Output:
8) break
The break keyword is used to terminate the loop if the condition is true. The control
of the program firstly passes to the outer statement then passes to the body of the
break statement.
Example:
1. n<-1
2. while(n<10){
3. if(n==3)
4. break
5. n=n+1
6. cat(n,"\n")
7. }
8. cat("End of the program")
Output:
9) TRUE/FALSE
The TRUE and FALSE keywords are used to represent a Boolean true and Boolean false.
If the given statement is true, then the interpreter returns true else the interpreter
returns false.
10) NULL
In R, NULL represents the null object. NULL is used to represent missing and undefined
values. NULL is the logical representation of a statement which is neither TRUE nor
FALSE.
Example:
1. [Link](list(a = 1, b = "c"))
Output:
Inf and -Inf are positive and negative infinity. NaN stands for 'Not a Number.' NaN
applies on numeric values and real and imaginary parts of complex values, but it will
not apply to the values of integer vectors.
Usage
1. [Link](x)
2. [Link](x)
3. [Link](x)
4.
5. Inf
6. NaN
12) NA
Usage
1. NA
2. [Link](x)
3. anyNA(x, recursive = FALSE)
4.
5. ## S3 method for class '[Link]'
6. [Link](x)
7.
8. [Link](x) <- value
Operators in R
In computer programming, an operator is a symbol which represents an action. An
operator is a symbol which tells the compiler to perform
specific logical or mathematical manipulations. R programming is very rich in built-
in operators.
In R programming, there are different types of operator, and each operator performs
a different task. For data manipulation, There are some advance operators also such
as model formula and list indexing.
Arithmetic Operators
Arithmetic operators are the symbols which are used to represent arithmetic math
operations. The operators act on each and every element of the vector. There are
various arithmetic operators which are supported by R.
Relational Operators
A relational operator is a symbol which defines some kind of relation between two
entities. These include numerical equalities and inequalities. A relational operator
compares each element of the first vector with the corresponding element of the
second vector. The result of the comparison will be a Boolean value. There are the
following relational operators which are supported by R:
Logical Operators
The logical operators allow a program to make a decision on the basis of multiple
conditions. In the program, each operand is considered as a condition which can be
evaluated to a false or true value. The value of the conditions is used to determine the
overall value of the op1 operator op2. Logical operators are applicable to those
vectors whose type is logical, numeric, or complex.
The logical operator compares each element of the first vector with the corresponding
element of the second vector.
Assignment Operators
An assignment operator is used to assign a new value to a variable. In R, these
operators are used to assign values to vectors. There are the following types of
assignment