File size: 9,230 Bytes
41592fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(ggExtra))
suppressPackageStartupMessages(library(forcats))



range01 <- function(x){(x-min(x))/(max(x)-min(x))}

shap_summary_plot<-function(shap_values){
  summary_plot <-
    shap_values %>% reshape2::melt() %>% group_by(class, variable) %>% 
    summarise(mean = mean(abs(value))) %>% 
    arrange(desc(mean)) %>%
    ggplot() +
   # ggdark::dark_theme_classic() +
    theme_classic()+
    geom_col(aes(
      y = variable,
      x = mean,
      group = class,
      fill = class
    ), position = "stack") +
    ylab("Feature")+
    xlab("Mean(|Shap Value|) Average impact on model output magnitude per activity.")+
    guides(fill=guide_legend(title="Activity"))
  summary_plot
  
}


shap_summary_plot_perclass<-function(shap_values, class="G",color="#F8766D"){
  shap_values <-shap_values %>% as.data.frame() %>% filter(class == {{class}} )
  summary_plot <-
    shap_values %>% reshape2::melt() %>% group_by(variable) %>% 
    summarise(mean = mean(abs(value))) %>% 
    ggplot() +
    theme_classic()+
    geom_col(aes(
      x = mean,
      y = fct_reorder(variable,mean)
    ),
    fill = color
    ) +
    ylab("Feature")+
    xlab(paste0("Mean(|Shap Value|) Average impact on model output magnitude for activity ", class))+
    guides(fill=guide_legend(title="Activity"))
  summary_plot
  
}


shap_beeswarm_plot<-function(shap_values,dataset){
  
  shap_values <- shap_values %>% reshape2::melt()
  dataset<-dataset %>% mutate(class=Activity) %>% select(-Activity) %>% 
    reshape2::melt() %>% group_by(variable) %>% 
    mutate(value_scale=range01(value))
  
  beeswarm_plot<-cbind(shap_values, feature_value=dataset$value_scale) %>% # filter(class=="GM") %>%
    ggplot()+
    facet_wrap(~class)+
    #ggdark::dark_theme_bw()+
    theme_classic()+
    geom_hline(yintercept=0, 
               color = "red", size=0.5)+
    ggforce::geom_sina(aes(x=variable,y=value,fill=feature_value),color="black", size=2.4,bins=4,alpha=0.9,shape=22)+
    scale_fill_gradient(low = "yellow", high = "red", na.value = NA)+
    scale_fill_gradient(low = "skyblue", high = "orange", na.value = NA)+
    xlab("Feature")+ylab("SHAP value")+
    theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))
  beeswarm_plot
  
  
}


#' Dependency plot for a particular feature. The plot considers 
#' activities and FP/TP
#'
#' @param feature a particular feature to calculate
#' @param dataset a dataset with goat information
#' @param shap  a shap value dataset for each feature.
#'
#' @return a dependency plot for each activity considering the selected feature
#' @export ggplot object
#'
#' @examples
#' 
#' dataset <-
#' readr::read_delim("data/split/seba-caprino_loocv.tsv", 
#'        delim = '\t')
#' selected_variables <-
#'  readr::read_delim(
#'    "data/topnfeatures/seba-caprino_selected_features.tsv",
#'    col_types = cols(),
#'    delim = '\t'
#'  )
#' dataset <-
#'  dataset %>% select(selected_variables$variable, 
#'  Anim, 
#'  Activity)
#' goat_model <- readRDS("models/boost/seba-caprino_model.rds")
#' shap_values <- calculate_shap(dataset, 
#'                model = goat_model,
#'                nsim = 30)
#' dependency_plot_full(feature = "Steps",
#'                      dataset = dataset,
#'                      shap = shap_values) 

dependency_plot <- function(feature, dataset, shap) {
  newdata <- dataset %>% mutate({{ feature }} := range01(!!sym(feature)))
  #activities <- c("G", "GM", "W", "R")
  activities<-dataset %>% pull(Activity) %>% unique()
  plots <- list()
  for (activity in activities) {
    s <- shap[which(shap$class == activity), 1:18]
    x <- newdata[which(newdata$Activity == activity), ]
    data <- cbind(
      shap = (s %>% as.data.frame %>% select(feature)),
      feature = (x %>% select(feature)),
      tp = x %>% mutate(tp = ifelse(Activity == predictions, "TP", "FP")) %>% 
        pull(tp)
    )
    names(data) <- c("shap", "feature", "tp")
    p <- ggplot(data, aes(x = feature)) +
      geom_point(aes(y = shap, color = tp), alpha = 0.3, size = 0.8) +
      geom_smooth(aes(y = shap),
                  se = FALSE,
                  size = 0.5,
                  linetype = "dashed") +
      geom_hline(
        yintercept = 0,
        color = 'red',
        size = 0.5,
        alpha = 0.5
      ) +
      xlab(feature) +
      labs(title = paste0("Activity ", activity)) +
      ylab("SHAP Value") +
      ylim(-0.1, 0.4) +
      xlim(0, 1) +
      theme_light() +
      theme(legend.position = 'none')
    
    p1 <-
      ggMarginal(
        p,
        type = "histogram",
        fill = 'gray',
        color = 'white',
        size = 10,
        xparams = list(bins = 25),
        yparams = list(bins = 15)
      ) #,margins='x')
    plots[[activity]] <- p1
  }
  #plots
  do.call(grid.arrange, c(plots, ncol = 4))
}


#' Dependency plot for a particular feature on a particular animal. 
#' The plot considers activities and FP/TP
#'
#' @param feature a particular feature to calculate
#' @param dataset a dataset with goat information
#' @param shap  a shap value dataset for each feature.
#' @param anim the id of the animal
#' @return a dependency plot for each activity considering the selected feature
#' @export ggplot object
#'
#' @examples
#' 
#' dataset <-
#' readr::read_delim("data/split/seba-caprino_loocv.tsv", 
#'        delim = '\t')
#' selected_variables <-
#'  readr::read_delim(
#'    "data/topnfeatures/seba-caprino_selected_features.tsv",
#'    col_types = cols(),
#'    delim = '\t'
#'  )
#' dataset <-
#'  dataset %>% select(selected_variables$variable, 
#'  Anim, 
#'  Activity)
#' goat_model <- readRDS("models/boost/seba-caprino_model.rds")
#' shap_values <- calculate_shap(dataset, 
#'                model = goat_model,
#'                nsim = 30)
#' dependency_plot_anim(feature = "Steps",
#'                      dataset = dataset,
#'                      shap = shap_values,
#'                      anim = 'a13') 
dependency_plot_anim<- function(feature,dataset,shap,anim){
  
  newdata <- dataset %>% mutate({{feature}} := range01(!!sym(feature)))
  plots<-list()
  activities<-newdata %>% filter(Anim == anim) %>% pull(Activity) %>% unique()
  for (activity in activities) {
    s <- shap[which(shap$class == activity &
                      shap$Anim == anim
    ), 1:18]
    x <- newdata[which(newdata$Activity == activity &
                         newdata$Anim == anim
    ),]
    data <- cbind(shap=(s %>% as.data.frame %>% select(feature)), 
                  feature = (x %>% select(feature)), 
                  tp = x %>% mutate(tp=ifelse(Activity == predictions,"TP","FP")) %>% pull(tp) )
    names(data)<-c("shap","feature","tp")
    
    p <- ggplot(data, aes(x = feature)) +
      geom_point(aes(y = shap, color = tp), alpha = 0.3, size = 1.8) +
      geom_smooth(aes(y = shap),
                  se = FALSE,
                  size = 0.5,
                  linetype = "dashed") +
      geom_hline(
        yintercept = 0,
        color = 'red',
        size = 0.5,
        alpha = 0.5
      ) +
      xlab(feature) +
      labs(title = paste0("Activity ", activity)) +
      ylab("SHAP Value") +
      ylim(-0.1, 0.4) +
      xlim(0, 1) +
      theme_light() +
      theme(legend.position = 'none')
    
    p1 <-
      ggMarginal(
        p,
        type = "histogram",
        fill = 'gray',
        color = 'white',
        size = 15,
        xparams = list(bins = 25),
        yparams = list(bins = 15)
      ) #,margins='x')
    plots[[activity]] <- p1
  }
  do.call(grid.arrange, c(plots, ncol = length(activities)))
}

#' contribution plot for SHAP  values
#'
#' @param shap shap values for a particular class, animal, etc. 
#' @param num_row the row number of the observation to show
#'
#' @return ggplot object
#' @export
#'
#' @examples
#' 
#' shap_values_G <- calculate_shap_class(
#' dataset = dataset, 
#' new_data = newdata, 
#' model= model, 
#' nsim = 100, 
#' function_class = p_function_G,
#' class_name ="G")
#' p1 <- contribution_plot(shap_values_G,num_row = 1) + 
#' labs(title="Anim a13: class G (FN)", subtitle = "SHAP analysis for class G") 
#' 
contribution_plot <-function(s, num_row = 1){
  s<-s[num_row,]
  s <- data.frame(
    Variable = names(s[,1:15]),
    Importance = apply(s[,1:15], MARGIN = 2, FUN = function(x) sum(x))
  )
  ggplot(s, aes(Variable, Importance, Importance,fill=Importance) )+
    geom_col() +
    coord_flip() +
    xlab("") +
    ylab("Shapley value")+
    theme_classic()+
    theme(legend.position = 'none')
}


contribution_plot_w_feature <-function(s, f, num_row = 1){
  d <- data.frame(
    variable = names(s[num_row,1:15]),
    importance = apply(s[num_row,1:15], MARGIN = 2, FUN = function(x) sum(x)),
    value = apply(f[num_row,1:15], MARGIN = 2, FUN = function(x) sum(x))
  )
  ggplot(d, aes(variable, importance, value,fill=value) )+
    geom_col() +
    geom_text(aes(label=round(value,digits = 2),hjust = 1.0),size=2)+
    coord_flip() +
    xlab("") +
    ylab("Shapley value")+
    scale_fill_gradient(low = 'lightgray', high = 'skyblue')+
    theme_classic()+
    theme(legend.position = 'none')
}