Guest User

Untitled

a guest
Jul 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. library(tidyverse)
  2.  
  3. theme_set(theme_bw())
  4.  
  5. generate_ice_table = function(csv_file){
  6. # Creates a matrix in which each row is a line curve, except last two columns.
  7. # Last two columns are the predictions and the values of the covariate.
  8. dat = read_csv(csv_file,
  9. col_names = F)
  10.  
  11. n_col = ncol(dat)
  12.  
  13.  
  14. # Finds row with first NA
  15. ix_start_ice = which(is.na(dat[,2])) + 1
  16.  
  17. x_values = as.numeric(dat[1,2:n_col])
  18. unique_x_values = unique(x_values)
  19.  
  20. ice_matrix = as.matrix(dat[ix_start_ice:nrow(dat),2:n_col]) %>%
  21. apply(., 2, as.numeric) %>%
  22. t()
  23.  
  24. actual_predicted = tibble(x = x_values, y_hat = 0.0)
  25. for(i in 1:(nrow(actual_predicted))){
  26. ix = which(abs(unique_x_values - x_values[i]) < 1e-8)
  27. actual_predicted$y_hat[i] = ice_matrix[i,ix]
  28. }
  29.  
  30. ice_table = cbind(ice_matrix,
  31. actual_predicted$y_hat,
  32. actual_predicted$x)
  33. return(ice_table)
  34. }
  35.  
  36.  
  37.  
  38.  
  39. plot_ice = function(
  40. csv_file,
  41. centered = F,
  42. point_size = 0.7,
  43. ice_line_size = 0.4,
  44. pdp_line_size = 1.3,
  45. ice_line_alpha = 0.5,
  46. point_alpha = 0.5){
  47.  
  48. ice_table = generate_ice_table(csv_file)
  49.  
  50. plot_out = plot_ice_aux(
  51. ice_table,
  52. centered = centered,
  53. point_size = point_size,
  54. ice_line_size = ice_line_size,
  55. pdp_line_size = pdp_line_size,
  56. ice_line_alpha = ice_line_alpha,
  57. point_alpha = point_alpha)
  58.  
  59. return(plot_out)
  60. }
  61.  
  62.  
  63.  
  64. plot_ice_aux = function(
  65. ice_table,
  66. centered = F,
  67. point_size = 0.7,
  68. ice_line_size = 0.4,
  69. pdp_line_size = 1.3,
  70. ice_line_alpha = 0.5,
  71. point_alpha = 0.5){
  72.  
  73. n_row = nrow(ice_table)
  74. n_col = ncol(ice_table)
  75. n_obs = ncol(ice_table) - 2
  76.  
  77. if(centered){
  78. y_ref = ice_table[,1]
  79. ice_table[,1:n_obs] = ice_table[,1:n_obs] - matrix(rep(y_ref, n_obs), ncol = n_obs, byrow = F)
  80. ice_table[,n_obs+1] = ice_table[,n_obs+1] - y_ref
  81. }
  82.  
  83.  
  84. vec = rep(0.0, n_obs*n_row)
  85. for(i in 1:n_row){
  86. ix_1 = (i-1)*n_obs + 1
  87. ix_2 = (i)*n_obs
  88. vec[ix_1:ix_2] = ice_table[i,1:n_obs]
  89. }
  90.  
  91. unique_x_values = unique(ice_table[,n_col])
  92.  
  93. df_ice = tibble(x = rep(unique_x_values, n_row),
  94. group = rep(1:n_row, each = n_obs),
  95. y_hat = vec)
  96.  
  97. pdp_df = df_ice %>%
  98. group_by(x) %>%
  99. summarize(y_hat = mean(y_hat))
  100.  
  101.  
  102. actual_predicted = tibble(
  103. x = ice_table[,n_obs+2],
  104. y_hat = ice_table[,n_obs+1]
  105. )
  106.  
  107.  
  108. plot_out = ggplot() +
  109. geom_line(data = df_ice,
  110. aes(x, y_hat, group = group),
  111. size = ice_line_size,
  112. alpha = ice_line_alpha,
  113. color = 'grey') +
  114. geom_point(data = actual_predicted,
  115. aes(x, y_hat),
  116. size = point_size,
  117. alpha = point_alpha) +
  118. geom_line(data = pdp_df,
  119. aes(x, y_hat),
  120. size = pdp_line_size)
  121.  
  122. return(plot_out)
  123. }
  124.  
  125.  
  126. csv_file = "~/Desktop/ICE_C5_Zone_06_STEP2_Sum_of_Bay_3_Total_Arcs.csv"
  127. plot_ice("~/Desktop/ICE_C5_Zone_06_STEP2_Sum_of_Bay_3_Total_Arcs.csv")
  128. plot_ice("~/Desktop/ICE_C5_Zone_06_STEP2_Sum_of_Bay_3_Total_Arcs.csv", centered = T)
  129.  
  130. csv_file = "~/Desktop/prueba_ice.csv"
  131. plot_ice("~/Desktop/prueba_ice.csv")
  132. plot_ice("~/Desktop/prueba_ice.csv", centered = T)
Add Comment
Please, Sign In to add comment