Diff of /R/bind_moves.R [000000] .. [dfe06d]

Switch to unified view

a b/R/bind_moves.R
1
2
3
## This function creates a named list of movement functions taking a single
4
## argument 'param'; all the rest (e.g. likelihood, prior, posterior functions,
5
## config, etc) is enclosed in the functions.
6
7
bind_moves <- function(moves = custom_moves(), config, data,
8
                       likelihoods, priors) {
9
10
    out <- custom_moves(moves)
11
12
13
    ## Binding:
14
15
    ## Each function needs to go through binding separately, as custom functions
16
    ## for likelihoods and priors often correspond to the same argument in
17
    ## different functions.
18
19
20
    ## remove move$mu if disabled
21
    if (!any(config$move_mu)) {
22
        out$mu <- NULL
23
    } else {
24
        out$mu <- bind_to_function(out$mu,
25
                                   data = data,
26
                                   config = config,
27
                                   custom_ll = likelihoods$genetic,
28
                                   custom_prior = priors$mu
29
                                   )
30
    }
31
32
33
    ## remove move$pi if disabled
34
    if (!any(config$move_pi)) {
35
        out$pi <- NULL
36
    } else {
37
        out$pi <- bind_to_function(out$pi,
38
                                   data = data,
39
                                   config = config,
40
                                   custom_ll = likelihoods$reporting,
41
                                   custom_prior = priors$pi
42
                                   )
43
    }
44
45
46
    ## remove move$eps if disabled
47
    if (!any(config$move_eps)) {
48
        out$eps <- NULL
49
    } else {
50
        out$eps <- bind_to_function(out$eps,
51
                                   data = data,
52
                                   config = config,
53
                                   custom_ll = likelihoods$contact,
54
                                   custom_prior = priors$eps
55
                                   )
56
    }
57
58
    ## remove move$lambda if disabled
59
    if (!any(config$move_lambda)) {
60
        out$lambda <- NULL
61
    } else {
62
        out$lambda <- bind_to_function(out$lambda,
63
                                   data = data,
64
                                   config = config,
65
                                   custom_ll = likelihoods$contact,
66
                                   custom_prior = priors$lambda
67
                                   )
68
    }
69
70
71
    ## remove move$alpha if no ancestry can be moved
72
    if (!any(config$move_alpha)) {
73
        out$alpha <- NULL
74
    } else {
75
        out$alpha <- bind_to_function(out$alpha,
76
                                      data = data,
77
                                      list_custom_ll = likelihoods
78
                                      )
79
    }
80
81
82
    ## remove move$t_inf if disabled
83
    if (!any(config$move_t_inf)) {
84
        out$t_inf <- NULL
85
    } else {
86
        out$t_inf <- bind_to_function(out$t_inf,
87
                                      data = data,
88
                                      list_custom_ll = likelihoods
89
                                      )
90
    }
91
92
93
    ## remove swap if disabled
94
    if (!any(config$move_swap_cases)) {
95
        out$swap_cases <- NULL
96
    } else {
97
        out$swap_cases <- bind_to_function(out$swap_cases,
98
                                      data = data,
99
                                      list_custom_ll = likelihoods
100
                                      )
101
    }
102
103
104
    ## remove move$kappa if disabled
105
    if (!any(config$move_kappa)) {
106
        out$kappa <- NULL
107
    } else {
108
        out$kappa <- bind_to_function(out$kappa,
109
                                      data = data,
110
                                      config = config,
111
                                      list_custom_ll = likelihoods
112
                                      )
113
    }
114
115
116
    ## perform binding for new unknown movements
117
    known_moves <- names(custom_moves())
118
    new_moves <- !names(out) %in% known_moves
119
    if (any(new_moves)) {
120
        for (i in seq_along(out)) {
121
            if (new_moves[i]) {
122
                out[[i]] <- bind_to_function(out[[i]],
123
                                             data = data,
124
                                             config = config,
125
                                             likelihoods = likelihoods,
126
                                             priors = priors
127
                                             )
128
            }
129
        }
130
    }
131
132
    ## the output is a list of movement functions with enclosed objects ##
133
    return(out)
134
135
}
136