SlideShare una empresa de Scribd logo
1 de 20
Descargar para leer sin conexión
STATISTICAL DATA ANALYSIS USING R
Additional R Programming Examples
Dennis
Friday 2
nd
and Saturday 3
rd
May, 2014.
Dennis Introduction to R Statistical Programming.
Topics covered
Vector and Matrix operation.
File Operations.
Evaluation of Probability Density Functions.
Evaluation of Probability Distribution Functions, i.e.
Binomial, Poisson, Normal and Uniform Distributions.
Testing of Hypothesis, i.e. t, z, F, Chi-square.
Linear Regression.
Prediction.
Sum of Squared Residuals.
Residual Plots.
Multiple Regression.
Dennis Introduction to R Statistical Programming.
Vectors I
A numeric vector is a list of numbers.
The three common functions that are used to create vectors
in various situations are:- c(), seq() and rep().
The function ”c()”
The letter ”c” means concatenate, i.e. to join together.
> c(200, 270, 250, 300, 210) # Creating a numeric vector
> Saturday.sales < −c(200, 300, 320)
> Sunday.sales < −c(120, 230, 200)# Saving a vector in a variable.
> weekend.sales < −c(Saturday.sales, Sunday.sales)# Combining
two vectors.
> cities < −c(”Kerala”, ”Delhi”, ”Chennai”) # Creating a vector
of characters.
Dennis Introduction to R Statistical Programming.
Vectors II
The functions ”cbind()”, ”rbind()” and ”seq()”
> rows.sales < −cbind(Saturday.sales, Sunday.sales) # Binds
Rows
> columns.sales < −rbind(Saturday.sales, Sunday.sales) # Binds
Columns
> colnames(column.sales) < −cities # Adds titles into an array of
vectors.
The abbreviation ”seq” means sequence, i.e. it is used for
equidistant series of numbers.
> seq(4, 9) # Creates a sequence of numbers from 4 to 9 at interval
of 1. # creates a sequence of numbers from 3 to 10 at interval of 2.
> seq(3, 10)
Dennis Introduction to R Statistical Programming.
Vectors III
The function ”rep()”
The abbreviation ”rep” means replicate, i.e. it is used to generate
repeated values.It is used in two variants, depending on whether the
second argument is a vector or a single number, for example; to
repeat a series of numbers;
> x = c(4, 5)
> rep(x, 3) # This will repeat ”x” three times.
Consider the following r-code;
> rep(1 : 2, c(10, 15)) # This means repeat ”1” 10 times and ”2”
15 times.
Dennis Introduction to R Statistical Programming.
Matrices I
A matrix is a two-dimensional array of numbers.
Example
> x < −1 : 12
> dim(x) < −c(3, 4); x
Alternatively, the function ”matrix()” can be used as follows;
> matrix(1 : 12, nrow = 3, byrow = T)
The dim assignment function sets or changes the dimension
attribute of x, causing R to treat the vector of 12 numbers as
a 3 x 4 matrix.
Useful functions that operate on matrices include rownames(),
colnames(), and the transposition function t().
Dennis Introduction to R Statistical Programming.
Matrices II
Example
> x < −matrix(1 : 12, nrow = 3, byrow = T)
> rownames(x) < −LETTERS[1 : 3]; x
> colnames(x) < −letters[1 : 3]; x # Naming rows and columns.
The character vector LETTERS is a built-in variable that
contains the capital letters A−Z. Similar useful vectors are
letters, month.name and month.abb, which refers to lowercase
letters a−z, month names, and abbreviated month names
respectively.
Finding the Transpose and the Inverse of a matrix.
> y < −t(x); y # Gives the transpose of a matrix.
> z < −solve(x); z # Gives the inverse of a matrix.
Dennis Introduction to R Statistical Programming.
Matrices III
Create two matrices, say, A and B.
Matrix addition, subtraction and multiplication
> C = c(rep(0, 3), seq(1, 6, 1), rep(seq(1, 6, 1), 2))
> A = matrix(sample(C, 12), nrow = 3, byrow = T); A
> B = matrix(sample(C, 12), nrow = 3, byrow = T); B
> D = A + B; D # Adds the matrix.
> E = A − B; E # Subtracts matrix B form matrix A.
> F = A ∗ B; F # Multiplies matrix A form matrix B element-wise.
> G = A% ∗ %t(B); G # Computes matrix multiplication.
Dennis Introduction to R Statistical Programming.
File operations
Reading Data.
The two common functions used in reading data are;
scan() and read.table(), i.e.
> Sales = scan(file = ”filepath”)
> SALES = read.table(file = ”filepath”, header = FALSE)
To merge two or more files according to the column
names or row names, we use the function merge();
> AB = merge(x, y, z) # merging files.
To write an output into a file, use the function write(),
i.e.
> output.file = write(x, file =
”file path to save the data”, ncolumns = 1, append = FALSE)
Dennis Introduction to R Statistical Programming.
Some probability distributions I
Normal distribution
Full list and options are found in > help(Normal) command.
dnorm
> x < −seq(−20, 20, by = .1)
> y < −dnorm(x)
> plot(x, y)
pnorm
> x < −seq(−20, 20, by = .1)
> y < −pnorm(x, mean = 3, sd = 4)
> plot(x, y)
Dennis Introduction to R Statistical Programming.
Some probability distributions II
qnorm
The next function we look at is qnorm which is the inverse of
pnorm. The idea behind qnorm is that you give it a probability,
and it returns the number whose cumulative distribution matches
the probability.
> x < −seq(0, 1, by = .05)
> y < −qnorm(x)
> plot(x, y)
> y < −qnorm(x, mean = 3, sd = 2)
> plot(x, y)
rnorm
> y < −rnorm(200, mean = −2)
> hist(y)
Dennis Introduction to R Statistical Programming.
t-distribution I
dt, Help at > help(TDist)
> x < −seq(−20, 20, by = .5)
> y < −dt(x, df = 10)
pt
> x = c(−3, −4, −2, −1)
> pt((mean(x) − 2)/sd(x), df = 20)
qt
> v < −c(0.005, .025, .05)
> qt(v, df = 253)
rt
> rt(3, df = 10)
Dennis Introduction to R Statistical Programming.
Chi-square I
dchisq, Help at > help(Chisquare)
> x < −seq(−20, 20, by = .5)
> y < −dchisq(x, df = 10)
pchisq
> x = c(2, 4, 5, 6)
> pchisq(x, df = 20)
qchisq
> v < −c(0.005, .025, .05); qchisq(v, df = 253)
rchisq
> rchisq(3, df = 20)
Dennis Introduction to R Statistical Programming.
Testing of Hypotheses I
In the one-sample t-test, we are comparing a sample mean to
a known or hypothesized population mean. The null
hypothesis is that the expected mean difference between the
sample mean and the population mean is zero, or in other
words, that the expected value of the sample mean is equal to
the population mean.
t-test example 1.
> rnorm1 = rnorm(50, 500, 100); rnorm1 # Generate random
numbers from a normal distribution with µ = 500, σ = 100.
> summary(rnorm1) # Gives the descriptive summary.
The mean of 517.3 is higher than µ = 500, but is it significantly
higher? To test this hypotheses, we use the function t.test().
> t.test(rnorm1, mu = 500)
Dennis Introduction to R Statistical Programming.
Testing of Hypotheses II
Load the in-built library datasets.
t-test example 2.
> library(datasets)
> head(mtcars)
Quiz
Assuming that the data in mtcars follows the normal distribution,
find the 95% confidence interval estimate of the difference between
the mean gas mileage of manual and automatic transmissions.
Dennis Introduction to R Statistical Programming.
Testing of Hypotheses III
Solution
> L = mtcars$am == 0
> mpg.auto = mtcars[L, ]$mpg; mpg.auto # automatic
transmission mileage.
> mpg.manual = mtcars[!L, ]$mpg; mpg.manual # manual
transmission mileage.
> t.test(mpg.auto, mpg.manual)
Answer
In mtcars, the mean mileage of automatic transmission is 17.147
mpg and the manual transmission is 24.392 mpg. The 95%
confidence interval of the difference in mean gas mileage is between
3.2097 and 11.2802 mpg.
Dennis Introduction to R Statistical Programming.
Testing of Hypotheses IV
Chi-squared test
Consider the following example;
> female = c(18, 102)
> male = c(10, 110)
> migraine = cbind(female, male); migraine
> chisq.test(migraine)
From the results we find that;- We determine that the chi-square
test fails to reject the null hypothesis that gender and migraine
susceptibility are independent. If the ratio of female to male
migraine sufferers is indeed 18:6, then our result is not what we had
hoped for. We may have an unusual sample or we may simply need
a larger sample to obtain statistical significance.
Dennis Introduction to R Statistical Programming.
Regression I
Simple Linear Regression
An example of grade point averages (GPAs) of 20 students
along with the number of hours each student studies per
week. We then use the lm() function to find the slope and
intercept terms.
Hours 10 12 10 15 14 12 13 15 16 14 13 12 11 10 13 13
14 18 17 14
GPA 3.33 2.92 2.56 3.08 3.57 3.31 3.45 3.93 3.82 3.70
3.26 3.00 2.74 2.85 3.33 3.29 3.58 3.85 4.00 3.50
> results < lm(GPA ∼ Hours); summary(results)
The correct interpretation of the regression equation is that a
student who did not study would have an estimated GPA of
1.3728, and that for every 1-hour increase in study time, the
estimated GPA would increase by 0.1489 points.
Dennis Introduction to R Statistical Programming.
Regression II
Example
Consider the dataframe on cars, ”mtcars” in library(datasets).
Test if ”mpg”-miles per gallon is affected by
”disp”-displacement and ”hp”-horsepower.
> myvariables = c(”mpg”, ”disp”, ”hp”)
> mycars = mtcars[myvariables]
> mymodel = lm(mpg ∼ disp + hp, data = mycars)
> summary(mymodel)
From the summary of the model, we can clearly see that the
independent variables; ”disp”-Engine displacement &
”hp”-Horsepower negatively affects the ”dependent variable”,
i.e. for an increase in either displacement or horsepower, it
will result in a decrease in the miles per gallon .
Dennis Introduction to R Statistical Programming.
Regression III
Another important graphical way to assess relationship
between variables is by using the ”conditioning plot” as shown
below.
conditioning plot
> coplot(mpg disp|factor(am, levels = 0 : 1, labels =
c(”Automatic”, ”Manual”)), data = mycars, panel =
panel.smooth, rows = 1)
Regression model prediction
> new = data.frame(disp = c(100, 200, 199), hp =
c(121, 124, 100))
> mpg.predicted = predict(mymodel, new); mpg.predicted
> data.frame(mpg.predicted, new)
Dennis Introduction to R Statistical Programming.

Más contenido relacionado

La actualidad más candente

Vectors data frames
Vectors data framesVectors data frames
Vectors data framesFAO
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Parth Khare
 
RDataMining slides-regression-classification
RDataMining slides-regression-classificationRDataMining slides-regression-classification
RDataMining slides-regression-classificationYanchang Zhao
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
RDataMining slides-time-series-analysis
RDataMining slides-time-series-analysisRDataMining slides-time-series-analysis
RDataMining slides-time-series-analysisYanchang Zhao
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data framekrishna singh
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factorskrishna singh
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In RRsquared Academy
 
RDataMining slides-clustering-with-r
RDataMining slides-clustering-with-rRDataMining slides-clustering-with-r
RDataMining slides-clustering-with-rYanchang Zhao
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with RShareThis
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data StructureSakthi Dasans
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting SpatialFAO
 

La actualidad más candente (20)

R language introduction
R language introductionR language introduction
R language introduction
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
RDataMining slides-regression-classification
RDataMining slides-regression-classificationRDataMining slides-regression-classification
RDataMining slides-regression-classification
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
RDataMining slides-time-series-analysis
RDataMining slides-time-series-analysisRDataMining slides-time-series-analysis
RDataMining slides-time-series-analysis
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
R language
R languageR language
R language
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
RDataMining slides-clustering-with-r
RDataMining slides-clustering-with-rRDataMining slides-clustering-with-r
RDataMining slides-clustering-with-r
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data Structure
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
 
R Basics
R BasicsR Basics
R Basics
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting Spatial
 
R교육1
R교육1R교육1
R교육1
 

Destacado

R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
StatXact 10 Infosheet
StatXact 10 InfosheetStatXact 10 Infosheet
StatXact 10 InfosheetCytel
 
Renewable energy
Renewable energyRenewable energy
Renewable energyDennis
 
20110419 healthcare patiëntenlogistiek pim zuid
20110419 healthcare patiëntenlogistiek pim zuid20110419 healthcare patiëntenlogistiek pim zuid
20110419 healthcare patiëntenlogistiek pim zuidRicoh Nederland
 
Estimation Theory Class (Handout 11)
Estimation Theory Class (Handout 11)Estimation Theory Class (Handout 11)
Estimation Theory Class (Handout 11)Ahmad Gomaa
 
Version Control With GitHub & RStudio
Version Control With GitHub & RStudioVersion Control With GitHub & RStudio
Version Control With GitHub & RStudioRsquared Academy
 
Step by Step Guidance to Study Abroad
Step by Step Guidance to Study AbroadStep by Step Guidance to Study Abroad
Step by Step Guidance to Study AbroadSamaa Hazem Hosny
 
De Europese digitale interne markt: hope or hype
De Europese digitale interne markt: hope or hypeDe Europese digitale interne markt: hope or hype
De Europese digitale interne markt: hope or hypeRicoh Nederland
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First StepsRsquared Academy
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming FundamentalsRagia Ibrahim
 
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...Revolution Analytics
 
Estimation and hypothesis testing 1 (graduate statistics2)
Estimation and hypothesis testing 1 (graduate statistics2)Estimation and hypothesis testing 1 (graduate statistics2)
Estimation and hypothesis testing 1 (graduate statistics2)Harve Abella
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRsquared Academy
 

Destacado (20)

R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
StatXact 10 Infosheet
StatXact 10 InfosheetStatXact 10 Infosheet
StatXact 10 Infosheet
 
Renewable energy
Renewable energyRenewable energy
Renewable energy
 
20110419 healthcare patiëntenlogistiek pim zuid
20110419 healthcare patiëntenlogistiek pim zuid20110419 healthcare patiëntenlogistiek pim zuid
20110419 healthcare patiëntenlogistiek pim zuid
 
Estimation Theory Class (Handout 11)
Estimation Theory Class (Handout 11)Estimation Theory Class (Handout 11)
Estimation Theory Class (Handout 11)
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter2
Chapter2Chapter2
Chapter2
 
Version Control With GitHub & RStudio
Version Control With GitHub & RStudioVersion Control With GitHub & RStudio
Version Control With GitHub & RStudio
 
Step by Step Guidance to Study Abroad
Step by Step Guidance to Study AbroadStep by Step Guidance to Study Abroad
Step by Step Guidance to Study Abroad
 
De Europese digitale interne markt: hope or hype
De Europese digitale interne markt: hope or hypeDe Europese digitale interne markt: hope or hype
De Europese digitale interne markt: hope or hype
 
R program
R programR program
R program
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First Steps
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Estimation and hypothesis testing 1 (graduate statistics2)
Estimation and hypothesis testing 1 (graduate statistics2)Estimation and hypothesis testing 1 (graduate statistics2)
Estimation and hypothesis testing 1 (graduate statistics2)
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar a R programming intro with examples

statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..Kamarudheen KV
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Dr. Volkan OBAN
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docxGEETHAR59
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxJohnWilliam111370
 
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.irnabati
 
Fundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLABFundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLABAli Ghanbarzadeh
 

Similar a R programming intro with examples (20)

Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
bobok
bobokbobok
bobok
 
proj1v2
proj1v2proj1v2
proj1v2
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
R programming
R programmingR programming
R programming
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
Regression
RegressionRegression
Regression
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
ARIMA Models - [Lab 3]
ARIMA Models - [Lab 3]ARIMA Models - [Lab 3]
ARIMA Models - [Lab 3]
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
 
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
 
Fundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLABFundamentals of Image Processing & Computer Vision with MATLAB
Fundamentals of Image Processing & Computer Vision with MATLAB
 
Fst ch3 notes
Fst ch3 notesFst ch3 notes
Fst ch3 notes
 

Último

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...amitlee9823
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...amitlee9823
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsJoseMangaJr1
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachBoston Institute of Analytics
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...amitlee9823
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...karishmasinghjnh
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 

Último (20)

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning Approach
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 

R programming intro with examples

  • 1. STATISTICAL DATA ANALYSIS USING R Additional R Programming Examples Dennis Friday 2 nd and Saturday 3 rd May, 2014. Dennis Introduction to R Statistical Programming.
  • 2. Topics covered Vector and Matrix operation. File Operations. Evaluation of Probability Density Functions. Evaluation of Probability Distribution Functions, i.e. Binomial, Poisson, Normal and Uniform Distributions. Testing of Hypothesis, i.e. t, z, F, Chi-square. Linear Regression. Prediction. Sum of Squared Residuals. Residual Plots. Multiple Regression. Dennis Introduction to R Statistical Programming.
  • 3. Vectors I A numeric vector is a list of numbers. The three common functions that are used to create vectors in various situations are:- c(), seq() and rep(). The function ”c()” The letter ”c” means concatenate, i.e. to join together. > c(200, 270, 250, 300, 210) # Creating a numeric vector > Saturday.sales < −c(200, 300, 320) > Sunday.sales < −c(120, 230, 200)# Saving a vector in a variable. > weekend.sales < −c(Saturday.sales, Sunday.sales)# Combining two vectors. > cities < −c(”Kerala”, ”Delhi”, ”Chennai”) # Creating a vector of characters. Dennis Introduction to R Statistical Programming.
  • 4. Vectors II The functions ”cbind()”, ”rbind()” and ”seq()” > rows.sales < −cbind(Saturday.sales, Sunday.sales) # Binds Rows > columns.sales < −rbind(Saturday.sales, Sunday.sales) # Binds Columns > colnames(column.sales) < −cities # Adds titles into an array of vectors. The abbreviation ”seq” means sequence, i.e. it is used for equidistant series of numbers. > seq(4, 9) # Creates a sequence of numbers from 4 to 9 at interval of 1. # creates a sequence of numbers from 3 to 10 at interval of 2. > seq(3, 10) Dennis Introduction to R Statistical Programming.
  • 5. Vectors III The function ”rep()” The abbreviation ”rep” means replicate, i.e. it is used to generate repeated values.It is used in two variants, depending on whether the second argument is a vector or a single number, for example; to repeat a series of numbers; > x = c(4, 5) > rep(x, 3) # This will repeat ”x” three times. Consider the following r-code; > rep(1 : 2, c(10, 15)) # This means repeat ”1” 10 times and ”2” 15 times. Dennis Introduction to R Statistical Programming.
  • 6. Matrices I A matrix is a two-dimensional array of numbers. Example > x < −1 : 12 > dim(x) < −c(3, 4); x Alternatively, the function ”matrix()” can be used as follows; > matrix(1 : 12, nrow = 3, byrow = T) The dim assignment function sets or changes the dimension attribute of x, causing R to treat the vector of 12 numbers as a 3 x 4 matrix. Useful functions that operate on matrices include rownames(), colnames(), and the transposition function t(). Dennis Introduction to R Statistical Programming.
  • 7. Matrices II Example > x < −matrix(1 : 12, nrow = 3, byrow = T) > rownames(x) < −LETTERS[1 : 3]; x > colnames(x) < −letters[1 : 3]; x # Naming rows and columns. The character vector LETTERS is a built-in variable that contains the capital letters A−Z. Similar useful vectors are letters, month.name and month.abb, which refers to lowercase letters a−z, month names, and abbreviated month names respectively. Finding the Transpose and the Inverse of a matrix. > y < −t(x); y # Gives the transpose of a matrix. > z < −solve(x); z # Gives the inverse of a matrix. Dennis Introduction to R Statistical Programming.
  • 8. Matrices III Create two matrices, say, A and B. Matrix addition, subtraction and multiplication > C = c(rep(0, 3), seq(1, 6, 1), rep(seq(1, 6, 1), 2)) > A = matrix(sample(C, 12), nrow = 3, byrow = T); A > B = matrix(sample(C, 12), nrow = 3, byrow = T); B > D = A + B; D # Adds the matrix. > E = A − B; E # Subtracts matrix B form matrix A. > F = A ∗ B; F # Multiplies matrix A form matrix B element-wise. > G = A% ∗ %t(B); G # Computes matrix multiplication. Dennis Introduction to R Statistical Programming.
  • 9. File operations Reading Data. The two common functions used in reading data are; scan() and read.table(), i.e. > Sales = scan(file = ”filepath”) > SALES = read.table(file = ”filepath”, header = FALSE) To merge two or more files according to the column names or row names, we use the function merge(); > AB = merge(x, y, z) # merging files. To write an output into a file, use the function write(), i.e. > output.file = write(x, file = ”file path to save the data”, ncolumns = 1, append = FALSE) Dennis Introduction to R Statistical Programming.
  • 10. Some probability distributions I Normal distribution Full list and options are found in > help(Normal) command. dnorm > x < −seq(−20, 20, by = .1) > y < −dnorm(x) > plot(x, y) pnorm > x < −seq(−20, 20, by = .1) > y < −pnorm(x, mean = 3, sd = 4) > plot(x, y) Dennis Introduction to R Statistical Programming.
  • 11. Some probability distributions II qnorm The next function we look at is qnorm which is the inverse of pnorm. The idea behind qnorm is that you give it a probability, and it returns the number whose cumulative distribution matches the probability. > x < −seq(0, 1, by = .05) > y < −qnorm(x) > plot(x, y) > y < −qnorm(x, mean = 3, sd = 2) > plot(x, y) rnorm > y < −rnorm(200, mean = −2) > hist(y) Dennis Introduction to R Statistical Programming.
  • 12. t-distribution I dt, Help at > help(TDist) > x < −seq(−20, 20, by = .5) > y < −dt(x, df = 10) pt > x = c(−3, −4, −2, −1) > pt((mean(x) − 2)/sd(x), df = 20) qt > v < −c(0.005, .025, .05) > qt(v, df = 253) rt > rt(3, df = 10) Dennis Introduction to R Statistical Programming.
  • 13. Chi-square I dchisq, Help at > help(Chisquare) > x < −seq(−20, 20, by = .5) > y < −dchisq(x, df = 10) pchisq > x = c(2, 4, 5, 6) > pchisq(x, df = 20) qchisq > v < −c(0.005, .025, .05); qchisq(v, df = 253) rchisq > rchisq(3, df = 20) Dennis Introduction to R Statistical Programming.
  • 14. Testing of Hypotheses I In the one-sample t-test, we are comparing a sample mean to a known or hypothesized population mean. The null hypothesis is that the expected mean difference between the sample mean and the population mean is zero, or in other words, that the expected value of the sample mean is equal to the population mean. t-test example 1. > rnorm1 = rnorm(50, 500, 100); rnorm1 # Generate random numbers from a normal distribution with µ = 500, σ = 100. > summary(rnorm1) # Gives the descriptive summary. The mean of 517.3 is higher than µ = 500, but is it significantly higher? To test this hypotheses, we use the function t.test(). > t.test(rnorm1, mu = 500) Dennis Introduction to R Statistical Programming.
  • 15. Testing of Hypotheses II Load the in-built library datasets. t-test example 2. > library(datasets) > head(mtcars) Quiz Assuming that the data in mtcars follows the normal distribution, find the 95% confidence interval estimate of the difference between the mean gas mileage of manual and automatic transmissions. Dennis Introduction to R Statistical Programming.
  • 16. Testing of Hypotheses III Solution > L = mtcars$am == 0 > mpg.auto = mtcars[L, ]$mpg; mpg.auto # automatic transmission mileage. > mpg.manual = mtcars[!L, ]$mpg; mpg.manual # manual transmission mileage. > t.test(mpg.auto, mpg.manual) Answer In mtcars, the mean mileage of automatic transmission is 17.147 mpg and the manual transmission is 24.392 mpg. The 95% confidence interval of the difference in mean gas mileage is between 3.2097 and 11.2802 mpg. Dennis Introduction to R Statistical Programming.
  • 17. Testing of Hypotheses IV Chi-squared test Consider the following example; > female = c(18, 102) > male = c(10, 110) > migraine = cbind(female, male); migraine > chisq.test(migraine) From the results we find that;- We determine that the chi-square test fails to reject the null hypothesis that gender and migraine susceptibility are independent. If the ratio of female to male migraine sufferers is indeed 18:6, then our result is not what we had hoped for. We may have an unusual sample or we may simply need a larger sample to obtain statistical significance. Dennis Introduction to R Statistical Programming.
  • 18. Regression I Simple Linear Regression An example of grade point averages (GPAs) of 20 students along with the number of hours each student studies per week. We then use the lm() function to find the slope and intercept terms. Hours 10 12 10 15 14 12 13 15 16 14 13 12 11 10 13 13 14 18 17 14 GPA 3.33 2.92 2.56 3.08 3.57 3.31 3.45 3.93 3.82 3.70 3.26 3.00 2.74 2.85 3.33 3.29 3.58 3.85 4.00 3.50 > results < lm(GPA ∼ Hours); summary(results) The correct interpretation of the regression equation is that a student who did not study would have an estimated GPA of 1.3728, and that for every 1-hour increase in study time, the estimated GPA would increase by 0.1489 points. Dennis Introduction to R Statistical Programming.
  • 19. Regression II Example Consider the dataframe on cars, ”mtcars” in library(datasets). Test if ”mpg”-miles per gallon is affected by ”disp”-displacement and ”hp”-horsepower. > myvariables = c(”mpg”, ”disp”, ”hp”) > mycars = mtcars[myvariables] > mymodel = lm(mpg ∼ disp + hp, data = mycars) > summary(mymodel) From the summary of the model, we can clearly see that the independent variables; ”disp”-Engine displacement & ”hp”-Horsepower negatively affects the ”dependent variable”, i.e. for an increase in either displacement or horsepower, it will result in a decrease in the miles per gallon . Dennis Introduction to R Statistical Programming.
  • 20. Regression III Another important graphical way to assess relationship between variables is by using the ”conditioning plot” as shown below. conditioning plot > coplot(mpg disp|factor(am, levels = 0 : 1, labels = c(”Automatic”, ”Manual”)), data = mycars, panel = panel.smooth, rows = 1) Regression model prediction > new = data.frame(disp = c(100, 200, 199), hp = c(121, 124, 100)) > mpg.predicted = predict(mymodel, new); mpg.predicted > data.frame(mpg.predicted, new) Dennis Introduction to R Statistical Programming.