File: methods-BFmodelSample.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 (226 lines) | stat: -rw-r--r-- 9,160 bytes parent folder | download | duplicates (3)
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
setMethod('show', signature = c("BFmcmc"),
  function(object){
    show(S3Part(object))
    show(object@model)
  }
)

setAs("BFmcmc" , "mcmc",
      function ( from , to ){
        as.mcmc(from)
      })

setAs("BFmcmc" , "matrix",
      function ( from , to ){
        as.matrix(from)
      })

setAs("BFmcmc" , "data.frame",
      function ( from , to ){
        as.data.frame(from)
      })


#' @rdname recompute-methods
#' @aliases recompute,BFmcmc-method
setMethod('recompute', signature(x = "BFmcmc", progress="ANY"),
  function(x, progress, ...){
    posterior(model=x@model, data = x@data, progress = progress, ...)
  }
)

setMethod('compare', signature(numerator = "BFmcmc", denominator = "BFmcmc"),
  function(numerator, denominator, ...){
    compare(numerator = numerator@model, data = numerator@data, ...) /
      compare(numerator = denominator@model, data = denominator@data, ...)
  }
)

setMethod('compare', signature(numerator = "BFmcmc", denominator = "missing"),
          function(numerator, denominator, ...){
            compare(numerator = numerator@model, data = numerator@data, ...)
          }
)

#' @rdname posterior-methods
#' @aliases posterior,BFmodel,missing,data.frame,missing-method
setMethod("posterior", signature(model="BFmodel", index="missing", data="data.frame", iterations="missing"),
  function(model, index, data, iterations, ...)
    stop("Iterations must be specified for posterior sampling.")
  )

#' @rdname posterior-methods
#' @aliases posterior,BFBayesFactor,missing,missing,missing-method
setMethod("posterior", signature(model="BFBayesFactor", index="missing", data="missing", iterations="missing"),
          function(model, index, data, iterations, ...)
            stop("Iterations must be specified for posterior sampling.")
)

#' @rdname posterior-methods
#' @aliases posterior,BFBayesFactor,numeric,missing,numeric-method
setMethod('posterior', signature(model = "BFBayesFactor", index = "numeric", data = "missing", iterations = "numeric"),
  function(model, index, data, iterations, ...){
    if(length(model[index])>1) stop("Index must specify single element.")
    posterior(model = model[index], iterations = iterations, ...)
  }
)

#' @rdname posterior-methods
#' @aliases posterior,BFBayesFactor,missing,missing,numeric-method
setMethod('posterior', signature(model = "BFBayesFactor", index = "missing", data = "missing", iterations = "numeric"),
  function(model, index=NULL, data, iterations, ...){
    if(length(model)>1) stop("Index argument required for posterior with multiple numerators.")
     posterior(model = model@numerator[[1]], data = model@data, iterations = iterations, ...)
  }
)

#' @rdname posterior-methods
#' @aliases posterior,BFlinearModel,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFlinearModel", index = "missing", data = "data.frame", iterations = "numeric"),
  function(model, index = NULL, data, iterations, ...){

    rscaleFixed = rpriorValues("allNways","fixed",model@prior$rscale[['fixed']])
    rscaleRandom = rpriorValues("allNways","random",model@prior$rscale[['random']])
    rscaleCont = rpriorValues("regression",,model@prior$rscale[['continuous']])
    rscaleEffects = model@prior$rscale[['effects']]

    formula = formula(model@identifier$formula)
    checkFormula(formula, data, analysis = "lm")

    factors = fmlaFactors(formula, data)[-1]
    nFactors = length(factors)
    dataTypes = model@dataTypes
    relevantDataTypes = dataTypes[names(dataTypes) %in% factors]

    dv = stringFromFormula(formula[[2]])
    dv = composeTerm(dv)
    if(model@type != "JZS") stop("Unknown model type.")

    if( nFactors == 0 ){
      stop("Sampling from intercept-only model not implemented.")
    }else if(all(relevantDataTypes == "continuous")){
      ## Regression
      X = fullDesignMatrix(formula, data, dataTypes)
      chains = linearReg.Gibbs(y = data[[dv]],covariates = X,iterations = iterations, rscale = rscaleCont, ...)
    }else if(all(relevantDataTypes != "continuous")){
      # ANOVA or t test
      chains = nWayFormula(formula=formula, data = data,
                       dataTypes = dataTypes,
                       rscaleFixed = rscaleFixed,
                       rscaleRandom = rscaleRandom,
                       rscaleEffects = rscaleEffects,
                       iterations = iterations,
                       posterior = TRUE, ...)
    }else{
      # GLM
      chains = nWayFormula(formula=formula, data = data,
                       dataTypes = dataTypes,
                       rscaleFixed = rscaleFixed,
                       rscaleRandom = rscaleRandom,
                       rscaleCont = rscaleCont,
                       rscaleEffects = rscaleEffects,
                       iterations = iterations,
                       posterior = TRUE, ...)
    }

    return(new("BFmcmc",chains, model = model, data = data))

  }
)

#' @rdname posterior-methods
#' @aliases posterior,BFindepSample,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFindepSample", index = "missing", data = "data.frame", iterations = "numeric"),
  function(model, index = NULL, data, iterations, ...){
    formula = formula(model@identifier$formula)
    rscale = model@prior$rscale
    interval = model@prior$nullInterval
    nullModel = ( formula[[3]] == 1 )
    chains = ttestIndepSample.Gibbs(formula, data, nullModel, iterations,rscale, interval,...)
    new("BFmcmc",chains,model = model, data = data)
})

#' @rdname posterior-methods
#' @aliases posterior,BFcontingencyTable,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFcontingencyTable", index = "missing", data = "data.frame", iterations = "numeric"),
          function(model, index = NULL, data, iterations, ...){
            mod = formula(model@identifier)
            type = model@type
            prior = model@prior$a
            marg = model@prior$fixedMargin
            chains = sampleContingency(mod, type, marg, prior, data = data, iterations = iterations, ...)
            new("BFmcmc",chains,model = model, data = data)
          })

#' @rdname posterior-methods
#' @aliases posterior,BFoneSample,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFoneSample", index = "missing", data = "data.frame", iterations = "numeric"),
  function(model, index = NULL, data, iterations, ...){
     mu = model@prior$mu
     rscale = model@prior$rscale
     interval = model@prior$nullInterval
     nullModel = ( model@identifier$formula == "y ~ 0" )
     chains = ttestOneSample.Gibbs(y = data$y, nullModel, iterations = iterations, rscale = rscale,
                          nullInterval = interval, ...)
     new("BFmcmc",chains,model = model, data = data)
})

#' @rdname posterior-methods
#' @aliases posterior,BFmetat,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFmetat", index = "missing", data = "data.frame", iterations = "numeric"),
          function(model, index = NULL, data, iterations, ...){
            rscale = model@prior$rscale
            interval = model@prior$nullInterval
            nullModel = ( model@identifier$formula == "d = 0" )
            chains = meta.t.Metrop(t = data$t, n1 = data$n1, n2 = data$n2, nullModel, iterations = iterations,
                                   rscale = rscale, nullInterval = interval,  ...)
            new("BFmcmc",chains, model = model, data = data)
          })


#' @rdname posterior-methods
#' @aliases posterior,BFproportion,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFproportion", index = "missing", data = "data.frame", iterations = "numeric"),
          function(model, index = NULL, data, iterations, ...){
            rscale = model@prior$rscale
            p = model@prior$p0
            interval = model@prior$nullInterval
            nullModel = ( model@identifier$formula == "p = p0" )

            chains = proportion.Metrop(y = data$y, N = data$N, nullModel, iterations = iterations,
                                      nullInterval = interval, p = p, rscale = rscale, ...)
            new("BFmcmc",chains, model = model, data = data)
          })

#' @rdname posterior-methods
#' @aliases posterior,BFcorrelation,missing,data.frame,numeric-method
setMethod('posterior', signature(model = "BFcorrelation", index = "missing", data = "data.frame", iterations = "numeric"),
          function(model, index = NULL, data, iterations, ...){
            rscale = model@prior$rscale
            interval = model@prior$nullInterval
            nullModel = ( model@identifier$formula == "rho = 0" )

            chains = correlation.Metrop(y = data$y, x = data$x, nullModel, iterations = iterations,
                                       nullInterval = interval, rscale = rscale, ...)
            new("BFmcmc",chains, model = model, data = data)
          })



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

as.mcmc.BFmcmc <- function(x, ...){
  return(S3Part(x))
}

as.matrix.BFmcmc <- function(x,...){
  return(as.matrix(S3Part(x)))
}

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