/
rankr.R
74 lines (53 loc) · 2.17 KB
/
rankr.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
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
#' rankr
#' @title Undertakes paired comparisons for ranking a set of coefficients, considering whether the differences between the coefficients are significant or not.
#' @description The index numbers are based on the model coefficient table that comes straight out of the model, with no sorting.
#' @description The function will return a dataframe with the results for each pair of coeeficients tested.
#'
#' @import readr
#'
#' @param coeftable coefficients table from mlogit, with one row per coefficient
#' @param vcovmat variance covariance matrix from mlogit, with one row and one column per coefficient
#' @param mysongidlist a dataframe containing the list of song ids to be tested. It can contain other variables but only songid will be used.
#'
#' @return
#' @export
#'
#' @examples
#' songstobecompared <- summary %>% slice(seq(from=1, to=92, by=10))
#' mycomparisons <- rankr(coeftable = results_ml_Repeatr4, vcovmat = vcovmat_ml_Repeatr4, mysongidlist = songstobecompared)
#' mycomparisons
#'
rankr <- function(coeftable = NULL, vcovmat = NULL, mysongidlist = NULL) {
mysongidlist <- mysongidlist %>%
select(songid)
nsongs <- nrow(mysongidlist)
ntests <- nsongs - 1
for(test in 1:ntests) {
coefindex1 <- as.numeric(mysongidlist[test,1]-1)
coefindex2 <- as.numeric(mysongidlist[test+1,1]-1)
mytest <- diffr(coeftable = coeftable, vcovmat = vcovmat, coefindex1 = coefindex1, coefindex2 = coefindex2)
if(test == 1) {
myresultsdf <- mytest
} else {
myresultsdf <- rbind.data.frame(myresultsdf, mytest)
}
}
myresultsdf <- myresultsdf %>%
mutate(songid1 = parse_number(var1)) %>%
mutate(songid2 = parse_number(var2))
songidlookup1 <- songidlookup %>%
rename(songid1 = songid) %>%
rename(song1 = song)
songidlookup2 <- songidlookup %>%
rename(songid2 = songid) %>%
rename(song2 = song)
myresultsdf <- myresultsdf %>%
left_join(songidlookup1)
myresultsdf <- myresultsdf %>%
left_join(songidlookup2)
myresultsdf <- myresultsdf %>%
relocate(song1, song2)
myresultsdf <- myresultsdf %>%
select(song1, song2, mycoef1, mycoef2, mycoefdiff, myz, myp, lower95ci, upper95ci)
return(myresultsdf)
}