Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.17 KB | None | 0 0
  1. ## Uncomment the next 3 lines to install the developer version of googleVis
  2. # library(httr)
  3. # )
  4. # install.packages(c("devtools","RJSONIO", "knitr", "shiny", "httpuv"))
  5. # library(devtools)
  6. # install_github("mages/googleVis")
  7. require(googleVis)
  8. # install.packages('RPostgreSQL')
  9. library(RPostgreSQL)
  10.  
  11. drv <- PostgreSQL()
  12. con <- dbConnect(drv, host = "host_host", user= "user_user", password="password_password", dbname="dbname_dbname")
  13.  
  14. sql_query <- paste0("
  15. with groups as (
  16.  select 'Mono-User' as segment,'Active' as group
  17.                    union all
  18.                    select 'Newbie' as segment,'Active' as group
  19.                    union all
  20.                    select 'Optimizer' as segment,'Active' as group
  21.                    union all
  22.                    select 'User' as segment,'Active' as group
  23.                    union all
  24.                    select 'Lost card' as segment,'At risk' as group
  25.                    union all
  26.                    select 'Closure initiated' as segment,'At risk' as group
  27.                    union all
  28.                    select 'Silent' as segment,'At risk' as group
  29.                    union all
  30.                    select 'Closed contract' as segment,'Closed contract' as group
  31.                    union all
  32.                    select 'AML' as segment,'AML' as group
  33. )
  34. ,original_set as (
  35. SELECT
  36. g1.group || to_char(ms1.mdate, ' YYMM') AS source,
  37. g2.group || to_char(ms2.mdate, ' YYMM') AS target,
  38. count(*) as count
  39. FROM ms1
  40. LEFT JOIN ms2 ON ms1.person_id = ms2.person_id AND ms2.mdate = ms1.mdate + INTERVAL '1 month'
  41. left join groups g1 on ms1.client_category=g1.segment
  42. left join groups g2 on ms2.client_category=g2.segment
  43. WHERE 1 = 1
  44. GROUP BY 1, 2
  45.  
  46. )
  47. select source as source,
  48. target as target,
  49. count
  50. from original_set
  51. where source is not null and target is not null
  52. order by 2,1;
  53.  
  54.                    ")
  55.  
  56. rs <- dbSendQuery(con, sql_query)
  57. stats <- dbFetch(rs)
  58.  
  59. dbDisconnect(con)
  60.  
  61. require(googleVis)
  62. plot(
  63.   gvisSankey(stats, from="source",to="target", weight="count",
  64.              options=list(
  65.                width=1280, height=720,
  66.                sankey="{link:{color:{fill:'lightblue'}},
  67.                        iterations: 32,
  68.                        node: { width: 10,
  69.                                color: { fill: '#a61d4c' },
  70.                                label: { fontSize: 14,
  71.                                         color: '#871b47',
  72.                                         bold: false,
  73.                                         italic: false }
  74.                              }
  75.                      }"
  76.              ))
  77. )
  78.  
  79.  
  80. ## Focus: Movement from M1 to M6 directly
  81. require(googleVis)
  82. # install.packages('RPostgreSQL')
  83. library(RPostgreSQL)
  84.  
  85. drv <- PostgreSQL()
  86. con <- dbConnect(drv, host = "host_host", user= "user_user", password="password_password", dbname="dbname_dbname")
  87.  
  88. sql_query <- paste0("
  89. with jan as (
  90.    SELECT
  91.                    person_id,
  92.                    client_category || to_char(mdate, ' (Mon)') as segment
  93.                    FROM ms
  94.                    WHERE mdate = date(20160101)
  95. ), jun as (
  96.                    SELECT
  97.                    person_id,
  98.                    client_category || to_char(mdate, ' (Mon)') as segment
  99.                    FROM ms
  100.                    WHERE mdate = date(20160601)
  101. )
  102. select jan.segment as source,
  103. jun.segment as target,
  104. count(*) as count
  105. from jun inner join jan on jan.person_id=jun.person_id
  106. group by 1,2
  107. union ALL
  108. select
  109. jun.segment,
  110. 'total',
  111. count(*) as count
  112. from jun
  113. group by 1,2
  114.  
  115.                    ")
  116.  
  117. rs <- dbSendQuery(con, sql_query)
  118. stats <- dbFetch(rs)
  119.  
  120. dbDisconnect(con)
  121.  
  122. require(googleVis)
  123. plot(
  124.   gvisSankey(stats, from="source",to="target", weight="count",
  125.              options=list(
  126.                width=1280, height=720,
  127.                sankey="{link:{color:{fill:'lightblue'}},
  128.               iterations: 32,
  129.               node: { width: 10,
  130.               color: { fill: '#a61d4c' },
  131.               label: { fontSize: 14,
  132.               color: '#871b47',
  133.               bold: false,
  134.               italic: false }
  135.               }
  136.               }"
  137.              ))
  138.              )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement