File: methods-BFodds.R

package info (click to toggle)
r-cran-bayesfactor 0.9.12-4.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: cpp: 1,555; sh: 16; makefile: 7
file content (379 lines) | stat: -rw-r--r-- 11,930 bytes parent folder | download
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# constructor
BFodds <- function(BFinit, logodds = NULL, bayesFactor = NULL){
  if(is.null(logodds)) logodds = data.frame(odds = BFinit@bayesFactor$bf * 0)
  rownames(logodds) = rownames(BFinit@bayesFactor)
  new("BFodds", numerator = BFinit@numerator,
      denominator = BFinit@denominator,
      logodds = logodds,
      bayesFactor = bayesFactor,
      version = BFInfo(FALSE))
}

setValidity("BFodds", function(object){
  if( length(object@numerator) != nrow(object@logodds)) return("Number of numerator models does not equal number of Bayes factors.")
  if( !is.null(object@bayesFactor)){
    if( length(object@numerator) != length(object@bayesFactor)) return("Number of numerator models does not equal number of Bayes factors.")
    for(i in 1:length(object@numerator)){
      if( !(object@numerator[[i]] %same% object@bayesFactor@numerator[[i]])){
        return("Models in numerator are not the same as the numerators in Bayes factor.")
      }
    }
    if( !(object@denominator %same% object@denominator))
      return("Model in denominator is not the same as the denominator in Bayes factor.")
  }
  numeratorsAreBFs = sapply(object@numerator,function(el) inherits(el,"BFmodel"))
  if( any(!numeratorsAreBFs)) return("Some numerators are not BFmodel objects.")
  # check numerators all have same data types as denominator
  dataTypeDenom = object@denominator@dataTypes
  dataTypesEqual = unlist(lapply(object@numerator,
                                 function(model, compType)
                                   model@dataTypes %com% compType,
                                 compType=dataTypeDenom))
  if( any(!dataTypesEqual)) return("Data types are not equal across models.")

  typeDenom = object@denominator@type
  typesEqual = unlist(lapply(object@numerator,
                             function(model, compType)
                               identical(model@type, compType),
                             compType=typeDenom))

  if( any(!typesEqual)) return("Model types are not equal across models.")

  classDenom = class(object@denominator)
  typesEqual = unlist(lapply(object@numerator,
                             function(model, compType)
                               identical(class(model), compType),
                             compType=classDenom))

  if( any(!typesEqual)) return("Model classes are not equal across models.")

  return(TRUE)
})


#' @rdname extractOdds-methods
#' @aliases extractOdds,BFodds-method
setMethod("extractOdds", "BFodds", function(x, logodds = FALSE, onlyodds = FALSE){
  z = x@logodds
  if(is.null(x@bayesFactor)){
    z$error = 0
  }else{
    bfs = extractBF(x@bayesFactor, logbf=TRUE)
    z$odds = z$odds + bfs$bf
    z$error = x@bayesFactor@bayesFactor$error
  }
  if(!logodds) z$odds = exp(z$odds)
  if(onlyodds) z = z$odds
  return(z)
})

setMethod('show', "BFodds", function(object){
  is.prior = is.null(object@bayesFactor)
  if(is.prior){
    cat("Prior odds\n--------------\n")
  }else{
    cat("Posterior odds\n--------------\n")
  }
  odds = extractOdds(object, logodds = TRUE)
  odds$odds = sapply(odds$odds, expString)

  indices = paste("[",1:nrow(odds),"]",sep="")

  # pad model names
  nms = paste(indices,rownames(odds),sep=" ")
  maxwidth = max(nchar(nms))
  nms = str_pad(nms,maxwidth,side="right",pad=" ")

  # pad Bayes factors
  maxwidth = max(nchar(odds$odds))
  oddsString = str_pad(odds$odds,maxwidth,side="right",pad=" ")



  for(i in 1:nrow(odds)){
    if(is.prior){
      cat(nms[i]," : ",oddsString[i],"\n",sep="")
    }else{
      cat(nms[i]," : ",oddsString[i]," \u00B1",round(odds$error[i]*100,2),"%\n",sep="")
    }
  }

  cat("\nAgainst denominator:\n")
  cat(" ",object@denominator@longName,"\n")
  cat("---\nModel type: ",class(object@denominator)[1],", ",object@denominator@type,"\n\n",sep="")

})

setMethod('summary', "BFodds", function(object){
  show(object)
})

#' @rdname BFodds-class
#' @name /,numeric,BFodds-method
#' @param e1 Numerator of the ratio
#' @param e2 Denominator of the ratio
setMethod('/', signature("numeric", "BFodds"), function(e1, e2){
  if( (e1 == 1) & (length(e2)==1) ){
    numer = e2@numerator[[1]]
    denom = list(e2@denominator)
    odds_df = e2@logodds
    if(is.null(e2@bayesFactor)){
      bf = NULL
    }else{
      bf = 1/e2@bayesFactor
    }
    rownames(odds_df) = denom[[1]]@shortName
    odds_df$odds = -odds_df$odds
    oddsobj = new("BFodds",numerator=denom, denominator=numer,
                  bayesFactor=bf, logodds = odds_df,
                  version = BFInfo(FALSE))
    return(oddsobj)
  }else if( e1 != 1 ){
    stop("Dividend must be 1 (to take reciprocal).")
  }else if( length(e2)>1 ){
    allNum = as(e2,"list")
    #BFlist = BFBayesFactorList(lapply(allNum, function(num) 1 / num))
    stop("Length of odds object must be ==1 to take reciprocal.")
  }
}
)

#' @rdname BFodds-class
#' @name /,BFodds,BFodds-method
setMethod('/', signature("BFodds", "BFodds"), function(e1, e2){
  if( length(e2) > 1) stop("Length of divisor must be ==1 to divide.")
  if( !(e1@denominator %same% e2@denominator) )
    stop("Odds have different denominator models; they cannot be compared.")
  if(!is.null(e1@bayesFactor) & !is.null(e1@bayesFactor)){
    bf = e1@bayesFactor / e2@bayesFactor
  }else if(is.null(e1@bayesFactor) & is.null(e1@bayesFactor)){
    bf = NULL
  }else{
    stop("Both odds objects must be prior, or both must be posterior.")
  }
  if( (length(e2)==1) ){
    logodds = data.frame(odds=e1@logodds$odds - e2@logodds$odds)
    rownames(logodds) = rownames(e1@logodds)

    oddsobj = new("BFodds",numerator=e1@numerator, denominator=e2@numerator[[1]],
                  bayesFactor=bf, logodds = logodds,
                  version = BFInfo(FALSE))

    return(oddsobj)
  }else{
    stop("Length of divisor must be ==1 to divide.")
  }
}
)

#' @rdname BFodds-class
#' @name *,BFodds,BFBayesFactor-method
setMethod('*', signature("BFodds", "BFBayesFactor"), function(e1, e2){
  if(!is.null(e1@bayesFactor))
    stop("Cannot multiply posterior odds object with Bayes factor.")
  new("BFodds", numerator = e1@numerator,
      denominator = e1@denominator,
      logodds = e1@logodds,
      bayesFactor = e2,
      version = BFInfo(FALSE))
}
)



#' @rdname BFodds-class
#' @name [,BFodds,index,missing,missing-method
#' @param x BFodds object
#' @param i indices indicating elements to extract
#' @param j unused for BFodds objects
#' @param drop unused
#' @param ... further arguments passed to related methods
setMethod("[", signature(x = "BFodds", i = "index", j = "missing",
                         drop = "missing"),
          function (x, i, j, ..., drop) {
            if((na <- nargs()) == 2){
              newodds = x
              x@numerator = x@numerator[i, drop=FALSE]
              x@logodds = x@logodds[i, ,drop=FALSE]
              if(is.null(x@bayesFactor)){
                x@bayesFactor = NULL
              }else{
                x@bayesFactor = x@bayesFactor[i]
              }
            }else stop("invalid nargs()= ",na)
            return(x)
          })

#' @rdname recompute-methods
#' @aliases recompute,BFodds-method
setMethod("recompute", "BFodds", function(x, progress = getOption('BFprogress', interactive()), multicore = FALSE, callback = function(...) as.integer(0), ...){
  bf = as.BFBayesFactor(x)
  bf = recompute(bf, progress = progress,
            multicore = multicore,
            callback = callback,
            ...)
  x@bayesFactor = bf
  return(x)
  })

#' @rdname priorOdds-method
#' @name priorOdds<-,BFodds,numeric-method
#' @docType methods
#' @exportMethod
setReplaceMethod("priorOdds", signature(object = "BFodds", value = "numeric"), definition = function (object, value) {
  priorLogodds(object) <- log(value)
  object
})

#' @rdname priorLogodds-method
#' @name priorLogodds<-,BFodds,numeric-method
#' @docType methods
#' @exportMethod
setReplaceMethod("priorLogodds", signature(object = "BFodds", value = "numeric"), definition = function (object, value) {
  object@logodds$odds <- value
  object
})


setAs("BFodds", "BFBayesFactor",
      function( from, to ){
        as.BFBayesFactor.BFodds(from)
      })

setAs("BFodds", "BFprobability",
      function( from, to ){
        as.BFprobability.BFodds(from)
      })


######
# S3
######


as.BFBayesFactor.BFodds <- function(object){
  if(!is.null(object@bayesFactor)){
    return(object@bayesFactor)
  }else{
    stop("Cannot convert prior odds to Bayes factor; no data has been given.")
  }
}

as.BFprobability.BFodds <- function(object, normalize = NULL, lognormalize = NULL){
  if(is.null(lognormalize) & is.null(normalize)){
    lognormalize = 0
  }else if(is.null(lognormalize) & !is.null(normalize)){
    lognormalize = log(normalize)
  }else if(!is.null(normalize)){
    stop("Cannot specify foth normalize and lognormalize.")
  }
  return(BFprobability(object, lognormalize))
}


length.BFodds <- function(x)
  nrow(x@logodds)

c.BFodds <-
  function(..., recursive = FALSE)
  {
    z = list(...)
    if(length(z)==1) return(z[[1]])
    correctClass = unlist(lapply(z, function(object) inherits(object,"BFodds")))
    if(any(!correctClass)) stop("Cannot concatenate odds with non-odds object.")

    denoms = lapply(z, function(object){ object@denominator })
    samedenom = unlist(lapply(denoms[-1],
                             function(el, cmp){
                               el %same% cmp
                             },
                             cmp=denoms[[1]]))
    if(any(!samedenom)) stop("Cannot concatenate odds objects with different denominator models.")

    logodds = lapply(z, function(object){object@logodds})
    df_rownames = unlist(lapply(z, function(object){rownames(object@logodds)}))
    df_rownames = make.unique(df_rownames, sep=" #")
    logodds = do.call("rbind",logodds)
    rownames(logodds) = df_rownames

    ### Grab the Bayes factors
    is.prior = 1:length(z) * NA
    for(i in 1:length(is.prior)){
      is.prior[i] = is.null(z[[i]]@bayesFactor)
    }
    if(all(!is.prior)){
      bfs = lapply(z, function(object){ object@bayesFactor })
      bfs = do.call("c", bfs)
      bf = BFodds(bfs,
                  logodds = logodds,
                  bayesFactor = bfs)
    }else if(all(is.prior)){
      numerators = unlist(lapply(z, function(object){object@numerator}),recursive=FALSE, use.names=FALSE)

      bf = new("BFodds", numerator = numerators, denominator = z[[1]]@denominator,
               logodds = logodds, bayesFactor = NULL, version = BFInfo(FALSE))
    }else{
      stop("Cannot concatenate prior odds with posterior odds.")
    }

    return(bf)
  }


names.BFodds <- function(x) {
  rownames(extractOdds(x))
}

# See https://www-stat.stanford.edu/~jmc4/classInheritance.pdf
sort.BFodds <- function(x, decreasing = FALSE, ...){
  ord = order(extractOdds(x, logodds=TRUE)$odds, decreasing = decreasing)
  return(x[ord])
}

max.BFodds <- function(..., na.rm=FALSE){
  joinedodds = do.call('c',list(...))
  el <- head(joinedodds, n=1)
  return(el)
}

min.BFodds <- function(..., na.rm=FALSE){
  joinedodds = do.call('c',list(...))
  el <- tail(joinedodds, n=1)
  return(el)
}

which.max.BFodds <- function(x){
  index = which.max(extractOdds(x, logodds=TRUE)$odds)
  names(index) = names(x)[index]
  return(index)
}

which.min.BFodds <- function(x){
  index = which.min(extractOdds(x, logodds=TRUE)$odds)
  names(index) = names(x)[index]
  return(index)
}

head.BFodds <- function(x, n=6L, ...){
  n = ifelse(n>length(x),length(x),n)
  x = sort(x, decreasing=TRUE)
  return(x[1:n])
}

tail.BFodds <- function(x, n=6L, ...){
  n = ifelse(n>length(x),length(x),n)
  x = sort(x)
  return(x[n:1])}

as.data.frame.BFodds <- function(x, row.names = NULL, optional=FALSE,...){
  df = extractOdds(x)
  return(df)
}

as.vector.BFodds <- function(x, mode = "any"){
  if( !(mode %in% c("any", "numeric"))) stop("Cannot coerce to mode ", mode)
  v = extractOdds(x)$odds
  names(v) = names(x)
  return(v)
}