-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.R
29 lines (25 loc) · 842 Bytes
/
utils.R
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
library(dplyr)
hdi.low <- function(vector, mass=0.95){
# Low end of `mass`% highest density interval
idx <- floor(length(vector) * (1 - mass) / 2)
return(sort(vector)[idx])
}
hdi.high <- function(vector, mass=0.95){
# High end of `mass`% highest density interval
idx <- ceiling(length(vector) - length(vector) * (1 - mass) / 2)
return(sort(vector)[idx])
}
postive.share <- function(vector){
# Fraction of entries greater than 0
return(length(vector[vector > 0])/length(vector))
}
append.player.info <- function(ratings.df, shots.df){
# Add player names and shot counts to ratings.df.
# This method assumes that player_id column exists in ratings.df.
return(
shots.df %>%
group_by(player_id) %>%
summarize(player_name=first(player_name), alt_id=first(alt_id), N=n()) %>%
merge(ratings.df)
)
}