SlideShare a Scribd company logo
1 of 15
Join tables, create lag times, and
base correlation plots upon lag
times
Using the dplyr and corrplot packages in R
by Doug Loqa
Goal: Create a
correlation plot between
columns of 2 related
tables
Key Price1 Price2 Price3
1 .. .. ..
2 .. .. ..
3 .. .. ..
4 .. .. ..
Key Demand1 Demand2 Demand3
1 .. .. ..
2 .. .. ..
3 .. .. ..
4 .. .. ..
• Subgoal1: Combine
2 tables
• Subgoal2: Create lag times
• Subgoal3: Create correlation
table
• Subgaol4: Create a correlation
plot
Cost1 Demand1 Demand1 lag Demand1 2 lags
.. .. .. ..
.. .. .. ..
Cost1 Demand1 Demand1 lag
Cost1 1 .. ..
Demand1 .. 1 ..
Demand1 lag .. .. 1
Install your packages, and input your tables
• Call:
Install.Packages("dplyr")
Install.Packages(“corrplot
”)
• Call:
Library(dplyr)
Library(corrplot)
• CREATE 2 TABLES ONE BASED ON COST, AND
ONE BASED ON DEMAND:
COST<-READ.CSV(FIRST, HEADERS = TRUE,
STRINGSASF ACTORS = FALSE)
SET UP A DEMAND TABLE THE SAME WAY
Cost
• ……
• ……
Demand
• ……
• ……
Sub Goal 1: Extract relevant columns and join
2 tables
• From the dplyr package, use the
select() call to create 2 new
scaled down tables that only
have relevant columns.
• Use the merge() function to join
your scaled down tables.
In this case, a right-join is exemplified.
• SELECT() WORKS LIKE THE SELECT CLAUSE
IN SQL, BUT IN PLACE OF FROM, YOU JUST
ENTER TABLE SOURCE IN FIRST ARGUMENT,
AND THEN PLACE NUMBERS FOR EACH
SEQUENTIAL COLUMN YOU WANT.
• MERGE WORKS LIKE A SQL JOIN. THE TWO
TABLES JOINED ARE LISTED AS THE FIRST
TWO ARGUMENTS, THE COMMON FIELD OR
“KEY” IS LISTED THIRD AND SPECIFIED
WITH “by = KEY” AND YOU FINALLY
SPECIFY LEFT, RIGHT, OR INNER/OUTER
WITH THE “ALL.X”, “ALL.Y”,OR “ALL”.
IN THIS CASE, USE:
MERGE(COST, DEMAND, by = “KEY”,
all.x = TRUE)
Sub Goal 2: Create a new table tracking changes
over time in both cause and effect variables
Create vectors of
change for input
Get
difference
between
each interval
of the Cost
column
Create output
vector single
interval time lag
Get the
difference
between
each interval
of the
Demand
column
Create output vector
using a double
interval time lag
Create
difference
between
every other
value of the
Demand
column
Approach: Use the diff() function creating
vectors and bind all three vectors together
Use as.numeric()
and diff()
functions
one<-diff(
as.numeric(
cost,1))
Use the same
functions on the
Demand row
two<-diff(
as.numeric(
Demand,1))
Create a third similar
vector using a
difference of 2
three<-diff(
as.numeric(
Demand,2))
Pad each new vector with the appropriate
amount of “0”s and then combine them
Use pipes, and the concatenation to return new equally-sized vectors
• For the first 2 columns just add a single “0” like this:
one<-as.vector(c(one, 0))
two<-as.vector(c(two, 0))
• For every proceeding vector add one more 0 than the last:
three<-as.vector(c(three,0,0))
four<-as.vector(c(four,0,0,0))
……..
• Finally bind all of these columns together into a table:
Time_lag<-cbind(one, two, three, four)
Your table should have 0s at the end
Your vectors represent changes in
respective categorical information with 0s
at the end
Cost
Chng
Chng
Chng
…
…
0
Cbind all of these (which should be of
equal length due to the 0s) to get the
following table; call it “lag”
Cost Demand Demand2 Demand3
Chng Chng Chng2 Chng3
Chng Chng Chng2 Chng3
Chng Chng Chng2 Chng3
… … … 0
… … 0 0
0 0 0 0
Demand
Chng
Chng
Chng
…
…
0
Demand2
Chng2
Chng2
Chng2
…
0
0
Demand3
Chng3
Chng3
Chng3
0
0
0
Chng = change between single spots
Chng2 = change between 2 time spots
Chng3 = change between 3 time spots
Now, make sure these columns are numeric
and then add the “key” column from before
Apply to
columns
•Apply(lag, 2,
as.numeric())
Specify 2 to iterate across cols
Bind from
Cost
table
•lag<-cbind(cost[,1],lag)
Sub Goal 3: You might want to group data in
the Key and use the aggregate() function
Fact Key Cost Dem
and1
Dem
and2
1
1 Sum(
chng)
Sum(
chng)
Sum(
chng)2
3
4
5
2
6 Sum(
chng)
Sum(
chng)
Sum(
chng)7
8
9
10
… 11 .. .. ..
• Group your key
data by a
segment (say by
each 5 records).
Ex. Fact<-
ceiling(Lag$ke
y/5)
This sets up another
column that can be
factored and bind it to
the lag table.
• Then Use the
aggregate() function
which requires a list
and specify a sum()
as the function to
aggregate by. Here is
how that looks:
Ex. aggregate(lag,
list(lag$Fact),FU
N =sum)
Now create a correlation matrix
and remove Key if you aggregated in previous step
Select only the columns you need to
compare as a matrix (kicking out key)
• Now that you have the table
(aggregated or not), select the
cost, and demand lag columns
as a new matrix:
as.matrix(cbind(lag$cost,
lag$demand1, lag$demand2,
lag$demand3))
Use the Corr() function removing
the key column if you did aggregate
Cost Demand Demand2 Demand3
Chng_Agg Chng_Agg Chng2_Agg Chng3_Agg
Chng_Agg Chng_Agg Chng2_Agg Chng3_Agg
Chng_Agg Chng_Agg Chng2_Agg Chng3_Agg
This will look much like the initial lag table
when it was first created; call this Lag 2
Sub Goal 4: Now, create your correlation
matrix and a plot
Create correlation matrix and plot with corr() and
corrplot() functions
clag<-corr(lag2)
LagPlot<-corrplot(clag)
Finally, you can adapt your correlation plot to
see values and see shading better
Use arguments like method =
“shade”,
tl.srt=30, and
add.coef.col=“red”
to modify as seen here.
Explore other modifications and enjoy making
correlation plots!

More Related Content

What's hot

A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to RAngshuman Saha
 
Spark 4th Meetup Londond - Building a Product with Spark
Spark 4th Meetup Londond - Building a Product with SparkSpark 4th Meetup Londond - Building a Product with Spark
Spark 4th Meetup Londond - Building a Product with Sparksamthemonad
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting SpatialFAO
 
Excel macro for integration of a function
Excel macro for integration of a functionExcel macro for integration of a function
Excel macro for integration of a functionUpendra Lele
 
03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developer03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developerAndrea Antonello
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming languageLincoln Hannah
 
5. R basics
5. R basics5. R basics
5. R basicsFAO
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingMandeep Singh
 
Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat SheetLaura Hughes
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeRsquared Academy
 
support vector regression
support vector regressionsupport vector regression
support vector regressionAkhilesh Joshi
 

What's hot (20)

Lecture 2 f17
Lecture 2 f17Lecture 2 f17
Lecture 2 f17
 
R-Excel Integration
R-Excel IntegrationR-Excel Integration
R-Excel Integration
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
Spark 4th Meetup Londond - Building a Product with Spark
Spark 4th Meetup Londond - Building a Product with SparkSpark 4th Meetup Londond - Building a Product with Spark
Spark 4th Meetup Londond - Building a Product with Spark
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting Spatial
 
Excel macro for integration of a function
Excel macro for integration of a functionExcel macro for integration of a function
Excel macro for integration of a function
 
03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developer03 Geographic scripting in uDig - halfway between user and developer
03 Geographic scripting in uDig - halfway between user and developer
 
Excel/R
Excel/RExcel/R
Excel/R
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming language
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
svm classification
svm classificationsvm classification
svm classification
 
5. R basics
5. R basics5. R basics
5. R basics
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat Sheet
 
Windows 7
Windows 7Windows 7
Windows 7
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
 
support vector regression
support vector regressionsupport vector regression
support vector 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...
 

Similar to Create a correlation plot from joined tables and lag times

Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functionsjoellivz
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdfMariappanR3
 
Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programmingTim Essam
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 introRagu Nathan
 
lecture 23
lecture 23lecture 23
lecture 23sajinsc
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0PMILebanonChapter
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select TopicsJay Coskey
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsMukesh Kumar
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operationsSmee Kaem Chann
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Matlab 1
Matlab 1Matlab 1
Matlab 1asguna
 
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin HuaiA Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin HuaiDatabricks
 
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxCSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxfaithxdunce63732
 

Similar to Create a correlation plot from joined tables and lag times (20)

CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
tidyr.pdf
tidyr.pdftidyr.pdf
tidyr.pdf
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programming
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
lecture 23
lecture 23lecture 23
lecture 23
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select Topics
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin HuaiA Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
 
Matlab1
Matlab1Matlab1
Matlab1
 
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxCSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
 

Recently uploaded

Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 

Recently uploaded (20)

Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 

Create a correlation plot from joined tables and lag times

  • 1. Join tables, create lag times, and base correlation plots upon lag times Using the dplyr and corrplot packages in R by Doug Loqa
  • 2. Goal: Create a correlation plot between columns of 2 related tables Key Price1 Price2 Price3 1 .. .. .. 2 .. .. .. 3 .. .. .. 4 .. .. .. Key Demand1 Demand2 Demand3 1 .. .. .. 2 .. .. .. 3 .. .. .. 4 .. .. .. • Subgoal1: Combine 2 tables
  • 3. • Subgoal2: Create lag times • Subgoal3: Create correlation table • Subgaol4: Create a correlation plot Cost1 Demand1 Demand1 lag Demand1 2 lags .. .. .. .. .. .. .. .. Cost1 Demand1 Demand1 lag Cost1 1 .. .. Demand1 .. 1 .. Demand1 lag .. .. 1
  • 4. Install your packages, and input your tables • Call: Install.Packages("dplyr") Install.Packages(“corrplot ”) • Call: Library(dplyr) Library(corrplot) • CREATE 2 TABLES ONE BASED ON COST, AND ONE BASED ON DEMAND: COST<-READ.CSV(FIRST, HEADERS = TRUE, STRINGSASF ACTORS = FALSE) SET UP A DEMAND TABLE THE SAME WAY Cost • …… • …… Demand • …… • ……
  • 5. Sub Goal 1: Extract relevant columns and join 2 tables • From the dplyr package, use the select() call to create 2 new scaled down tables that only have relevant columns. • Use the merge() function to join your scaled down tables. In this case, a right-join is exemplified. • SELECT() WORKS LIKE THE SELECT CLAUSE IN SQL, BUT IN PLACE OF FROM, YOU JUST ENTER TABLE SOURCE IN FIRST ARGUMENT, AND THEN PLACE NUMBERS FOR EACH SEQUENTIAL COLUMN YOU WANT. • MERGE WORKS LIKE A SQL JOIN. THE TWO TABLES JOINED ARE LISTED AS THE FIRST TWO ARGUMENTS, THE COMMON FIELD OR “KEY” IS LISTED THIRD AND SPECIFIED WITH “by = KEY” AND YOU FINALLY SPECIFY LEFT, RIGHT, OR INNER/OUTER WITH THE “ALL.X”, “ALL.Y”,OR “ALL”. IN THIS CASE, USE: MERGE(COST, DEMAND, by = “KEY”, all.x = TRUE)
  • 6. Sub Goal 2: Create a new table tracking changes over time in both cause and effect variables Create vectors of change for input Get difference between each interval of the Cost column Create output vector single interval time lag Get the difference between each interval of the Demand column Create output vector using a double interval time lag Create difference between every other value of the Demand column
  • 7. Approach: Use the diff() function creating vectors and bind all three vectors together Use as.numeric() and diff() functions one<-diff( as.numeric( cost,1)) Use the same functions on the Demand row two<-diff( as.numeric( Demand,1)) Create a third similar vector using a difference of 2 three<-diff( as.numeric( Demand,2))
  • 8. Pad each new vector with the appropriate amount of “0”s and then combine them Use pipes, and the concatenation to return new equally-sized vectors • For the first 2 columns just add a single “0” like this: one<-as.vector(c(one, 0)) two<-as.vector(c(two, 0)) • For every proceeding vector add one more 0 than the last: three<-as.vector(c(three,0,0)) four<-as.vector(c(four,0,0,0)) …….. • Finally bind all of these columns together into a table: Time_lag<-cbind(one, two, three, four)
  • 9. Your table should have 0s at the end Your vectors represent changes in respective categorical information with 0s at the end Cost Chng Chng Chng … … 0 Cbind all of these (which should be of equal length due to the 0s) to get the following table; call it “lag” Cost Demand Demand2 Demand3 Chng Chng Chng2 Chng3 Chng Chng Chng2 Chng3 Chng Chng Chng2 Chng3 … … … 0 … … 0 0 0 0 0 0 Demand Chng Chng Chng … … 0 Demand2 Chng2 Chng2 Chng2 … 0 0 Demand3 Chng3 Chng3 Chng3 0 0 0 Chng = change between single spots Chng2 = change between 2 time spots Chng3 = change between 3 time spots
  • 10. Now, make sure these columns are numeric and then add the “key” column from before Apply to columns •Apply(lag, 2, as.numeric()) Specify 2 to iterate across cols Bind from Cost table •lag<-cbind(cost[,1],lag)
  • 11. Sub Goal 3: You might want to group data in the Key and use the aggregate() function Fact Key Cost Dem and1 Dem and2 1 1 Sum( chng) Sum( chng) Sum( chng)2 3 4 5 2 6 Sum( chng) Sum( chng) Sum( chng)7 8 9 10 … 11 .. .. .. • Group your key data by a segment (say by each 5 records). Ex. Fact<- ceiling(Lag$ke y/5) This sets up another column that can be factored and bind it to the lag table. • Then Use the aggregate() function which requires a list and specify a sum() as the function to aggregate by. Here is how that looks: Ex. aggregate(lag, list(lag$Fact),FU N =sum)
  • 12. Now create a correlation matrix and remove Key if you aggregated in previous step Select only the columns you need to compare as a matrix (kicking out key) • Now that you have the table (aggregated or not), select the cost, and demand lag columns as a new matrix: as.matrix(cbind(lag$cost, lag$demand1, lag$demand2, lag$demand3)) Use the Corr() function removing the key column if you did aggregate Cost Demand Demand2 Demand3 Chng_Agg Chng_Agg Chng2_Agg Chng3_Agg Chng_Agg Chng_Agg Chng2_Agg Chng3_Agg Chng_Agg Chng_Agg Chng2_Agg Chng3_Agg This will look much like the initial lag table when it was first created; call this Lag 2
  • 13. Sub Goal 4: Now, create your correlation matrix and a plot Create correlation matrix and plot with corr() and corrplot() functions clag<-corr(lag2) LagPlot<-corrplot(clag)
  • 14. Finally, you can adapt your correlation plot to see values and see shading better Use arguments like method = “shade”, tl.srt=30, and add.coef.col=“red” to modify as seen here.
  • 15. Explore other modifications and enjoy making correlation plots!