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

Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
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 signalsInvezz1
 
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
 
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 interactionfulawalesam
 
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
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
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
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
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
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
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
 

Recently uploaded (20)

Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
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
 
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...
 
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
 
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
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
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...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
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
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
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
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
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
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
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
 

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!