SlideShare a Scribd company logo
1 of 1
Download to read offline
Analytics With RIn R-Studio
CORRELATION
#One should have the basic concept in statistics and R to understand this and the different terms associated with this work
sheet.
# Correlation gives us the strength of association between the variables.
#############################################
# Example 1
############################################
data(trees) # We will be using the inbuilt R dataset “trees”.
View(trees)
names(trees)
attach(trees)
?cor.test # Test for association between paired samples, using one of Pearson’s product moment correlation coefficient, Ken-
dall’s tau or Spearman’s rho.
plot(Girth, Height, main = “Scatterplot”, las = 1) # Plotting the variable girth & height.
cor(Girth, Height, method = “pearson”) # We are calculating the correlation between Girth and Height based on the method
of Pearson correlation. The value comes to 0.52 which is the R value which means there is a A moderate uphill (positive)
relationship between variables.
cor(Girth, Height, method = “spearman”) # We are calculating searmans Rank Correlation coefficient.
cor(Girth, Height, method = “kendall”) # We are calculating searmans Rank Kendalls coefficient.
# Cor.test - here we will use this function to test that null hypothesis(H0) = 0 or correlation = 0.
cor.test(Girth, Height, method = “pearson”) # By default it is a two sided test
# We get the correlation - Sample estimate: cor = 0.519
# We get the 95% confidence interval for the correlation
# We get the Test statistics - 3.2722 and P value - 0.0027 that the correlation = 0
cor.test(Girth, Height, method = “pearson”, alt = “greater”, conf.level = 0.99) # We change it to a one tailed test with greater
than value and also change the confidence level.
cov(Girth, Height) # Co-variance
#############################################
# Example 2
############################################
# Calculating correlation matrix and confidence intervals, i.e. correlation between many variables in a matrix view.
# We will use the data set “swiss”.
data(swiss)
?swiss
cor(swiss) # Correlation matrix
round(cor(swiss), 2) # Rounding of to two decimal places
cor.test(swiss$Fertility, swiss$Education)
# To get probability value for an entire matrix we need to install a package to do it
install.packages(“Hmisc”)
require(Hmisc)
# We need to take the data frame and convert into a matrix to get the correlation matrix and p values.
as.matrix(swiss) # converting data frame into matrix.
rcorr(as.matrix(swiss)) # gives correlation between each variable and the probability.
For queries and more information
feel free to contact me :
jeromegomes89@hotmail.com
in.linkedin.com/in/jeromegomes89
By:
Jerome
Gomes
Packages needed:
1. ggplot2
2. Hmisc

More Related Content

Viewers also liked

Viewers also liked (15)

Introduction To R
Introduction To RIntroduction To R
Introduction To R
 
Moving Data to and From R
Moving Data to and From RMoving Data to and From R
Moving Data to and From R
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R at Microsoft (useR! 2016)
R at Microsoft (useR! 2016)R at Microsoft (useR! 2016)
R at Microsoft (useR! 2016)
 
Installing R and R-Studio
Installing R and R-StudioInstalling R and R-Studio
Installing R and R-Studio
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
 
R at Microsoft
R at MicrosoftR at Microsoft
R at Microsoft
 
R-Studio Vs. Rcmdr
R-Studio Vs. RcmdrR-Studio Vs. Rcmdr
R-Studio Vs. Rcmdr
 
R at Microsoft
R at MicrosoftR at Microsoft
R at Microsoft
 
Language R
Language RLanguage R
Language R
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 
Geoestadística con R
Geoestadística con RGeoestadística con R
Geoestadística con R
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
機率統計 -- 使用 R 軟體
機率統計 -- 使用 R 軟體機率統計 -- 使用 R 軟體
機率統計 -- 使用 R 軟體
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 

Recently uploaded

Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
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
Abortion pills in Riyadh +966572737505 get cytotec
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
JoseMangaJr1
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
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...
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
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
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
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
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 

Correlation with R in R-Studio

  • 1. Analytics With RIn R-Studio CORRELATION #One should have the basic concept in statistics and R to understand this and the different terms associated with this work sheet. # Correlation gives us the strength of association between the variables. ############################################# # Example 1 ############################################ data(trees) # We will be using the inbuilt R dataset “trees”. View(trees) names(trees) attach(trees) ?cor.test # Test for association between paired samples, using one of Pearson’s product moment correlation coefficient, Ken- dall’s tau or Spearman’s rho. plot(Girth, Height, main = “Scatterplot”, las = 1) # Plotting the variable girth & height. cor(Girth, Height, method = “pearson”) # We are calculating the correlation between Girth and Height based on the method of Pearson correlation. The value comes to 0.52 which is the R value which means there is a A moderate uphill (positive) relationship between variables. cor(Girth, Height, method = “spearman”) # We are calculating searmans Rank Correlation coefficient. cor(Girth, Height, method = “kendall”) # We are calculating searmans Rank Kendalls coefficient. # Cor.test - here we will use this function to test that null hypothesis(H0) = 0 or correlation = 0. cor.test(Girth, Height, method = “pearson”) # By default it is a two sided test # We get the correlation - Sample estimate: cor = 0.519 # We get the 95% confidence interval for the correlation # We get the Test statistics - 3.2722 and P value - 0.0027 that the correlation = 0 cor.test(Girth, Height, method = “pearson”, alt = “greater”, conf.level = 0.99) # We change it to a one tailed test with greater than value and also change the confidence level. cov(Girth, Height) # Co-variance ############################################# # Example 2 ############################################ # Calculating correlation matrix and confidence intervals, i.e. correlation between many variables in a matrix view. # We will use the data set “swiss”. data(swiss) ?swiss cor(swiss) # Correlation matrix round(cor(swiss), 2) # Rounding of to two decimal places cor.test(swiss$Fertility, swiss$Education) # To get probability value for an entire matrix we need to install a package to do it install.packages(“Hmisc”) require(Hmisc) # We need to take the data frame and convert into a matrix to get the correlation matrix and p values. as.matrix(swiss) # converting data frame into matrix. rcorr(as.matrix(swiss)) # gives correlation between each variable and the probability. For queries and more information feel free to contact me : jeromegomes89@hotmail.com in.linkedin.com/in/jeromegomes89 By: Jerome Gomes Packages needed: 1. ggplot2 2. Hmisc