SlideShare una empresa de Scribd logo
1 de 1
Descargar para leer sin conexión
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

Más contenido relacionado

Destacado (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
 

Último

ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxolyaivanovalion
 
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.pptxolyaivanovalion
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
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% SecurePooja Nehwal
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
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
 
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.pdfadriantubila
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxolyaivanovalion
 
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
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
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 BarshaAroojKhan71
 
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.pptxolyaivanovalion
 
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
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...amitlee9823
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
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
 

Último (20)

ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
 
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
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
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
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
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...
 
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
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptx
 
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...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
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
 
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
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
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
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
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...
 
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
 

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