R/biips_model.r
Create a biips model object described in BUGS language.
biips_model(file, data = parent.frame(), sample_data = TRUE, quiet = FALSE, seed = get_seed())
file | string. path of the BUGS file which defines the stochastic
model. Alternatively, |
---|---|
data | either a list or environment containing constants and
observed values or a character vector containing names of variables
existing in the workspace. Any numeric objects in |
sample_data | logical. Toggle the evaluation of the 'data' block of
the BUGS model that generates or transforms data. (default = |
quiet | logical. Deactivate verbosity. (default = |
seed | int. RNG seed used for data generation if |
An object of class biips
which can be used to generate samples
from the posterior distribution of the parameters.
An object of class biips
is a list of functions that share a common
environment. The functions can be used to query information on the model.
Get an external pointer to the compiled model object.
Get a string. filename of the BUGS model.
Get a named list of the original data of the model.
Get a character vector. BUGS language definition of the model.
Recompile the model using the original data set.
biips_variable_names
, biips_nodes
,
biips_print_dot
, biips_add_function
,
biips_add_distribution
modelfile <- system.file('extdata', 'hmm.bug', package = 'rbiips') stopifnot(nchar(modelfile) > 0) cat(readLines(modelfile), sep = '\n')#> var c_true[tmax], x_true[tmax], c[tmax], x[tmax], y[tmax] #> #> data #> { #> x_true[1] ~ dnorm(0, 1/5) #> y[1] ~ dnorm(x_true[1], exp(logtau_true)) #> for (t in 2:tmax) #> { #> c_true[t] ~ dcat(p) #> x_true[t] ~ dnorm(0.5*x_true[t-1]+25*x_true[t-1]/(1+x_true[t-1]^2)+8*cos(1.2*(t-1)), ifelse(c_true[t]==1, 1/10, 1/100)) #> y[t] ~ dnorm(x_true[t]/4, exp(logtau_true)) #> } #> } #> #> model #> { #> logtau ~ dunif(-3, 3) #> x[1] ~ dnorm(0, 1/5) #> y[1] ~ dnorm(x[1], exp(logtau)) #> for (t in 2:tmax) #> { #> c[t] ~ dcat(p) #> x[t] ~ dnorm(0.5*x[t-1]+25*x[t-1]/(1+x[t-1]^2)+8*cos(1.2*(t-1)), ifelse(c[t]==1, 1/10, 1/100)) #> y[t] ~ dnorm(x[t]/4, exp(logtau)) #> } #> }data <- list(tmax = 10, p = c(.5, .5), logtau_true = log(1), logtau = log(1)) model <- biips_model(modelfile, data, sample_data = TRUE)#> * Parsing model in: /home/adrien/Dropbox/workspace/rbiips/inst/extdata/hmm.bug #> * Compiling data graph #> Declaring variables #> Resolving undeclared variables #> Allocating nodes #> Graph size: 169 #> Sampling data #> Reading data back into data table #> * Compiling model graph #> Declaring variables #> Resolving undeclared variables #> Allocating nodes #> Graph size: 180# NOT RUN { tmax <- 10 p <- c(.5, .5) logtau_true <- log(1) logtau <- logtau_true datanames <- c('tmax', 'p', 'logtau_true', 'logtau') model <- biips_model(modelfile, datanames, sample_data = TRUE) # }is.biips(model)#> [1] TRUEprint(model)#> Biips model: #> #> var c_true[tmax], x_true[tmax], c[tmax], x[tmax], y[tmax] #> #> data #> { #> x_true[1] ~ dnorm(0, 1/5) #> y[1] ~ dnorm(x_true[1], exp(logtau_true)) #> for (t in 2:tmax) #> { #> c_true[t] ~ dcat(p) #> x_true[t] ~ dnorm(0.5*x_true[t-1]+25*x_true[t-1]/(1+x_true[t-1]^2)+8*cos(1.2*(t-1)), ifelse(c_true[t]==1, 1/10, 1/100)) #> y[t] ~ dnorm(x_true[t]/4, exp(logtau_true)) #> } #> } #> #> model #> { #> logtau ~ dunif(-3, 3) #> x[1] ~ dnorm(0, 1/5) #> y[1] ~ dnorm(x[1], exp(logtau)) #> for (t in 2:tmax) #> { #> c[t] ~ dcat(p) #> x[t] ~ dnorm(0.5*x[t-1]+25*x[t-1]/(1+x[t-1]^2)+8*cos(1.2*(t-1)), ifelse(c[t]==1, 1/10, 1/100)) #> y[t] ~ dnorm(x[t]/4, exp(logtau)) #> } #> } #> #> #> Fully observed variables: #> logtau logtau_true p tmax x_true y #> #> Partially observed variables: #> c_truemodel$data()#> $c_true #> [1] NA 1 1 2 2 2 2 2 2 2 #> #> $logtau #> [1] 0 #> #> $logtau_true #> [1] 0 #> #> $p #> [1] 0.5 0.5 #> #> $tmax #> [1] 10 #> #> $x_true #> [1] 0.47533257 16.33434599 6.31949315 11.52958885 -13.37977857 #> [6] -13.15885327 -5.90507787 0.09503471 -12.36838766 3.72976906 #> #> $y #> [1] 1.90029184 5.68284522 1.92715909 5.08112951 -1.85787382 -3.12871458 #> [7] -0.25034406 0.05559467 -2.15270389 1.05645770 #>variable.names(model)#> [1] "c" "c_true" "logtau" "logtau_true" "p" #> [6] "tmax" "x" "x_true" "y"biips_variable_names(model)#> [1] "c" "c_true" "logtau" "logtau_true" "p" #> [6] "tmax" "x" "x_true" "y"biips_nodes(model)#> id name type #> 1 0 c_true[2] const #> 2 1 c_true[3] const #> 3 2 c_true[4] const #> 4 3 c_true[5] const #> 5 4 c_true[6] const #> 6 5 c_true[7] const #> 7 6 c_true[8] const #> 8 7 c_true[9] const #> 9 8 c_true[10] const #> 10 9 logtau_true const #> 11 10 p const #> 12 11 tmax const #> 13 12 x_true const #> 14 13 3 const #> 15 14 (-3) logic #> 16 15 logtau stoch #> 17 21 (exp(logtau)) logic #> 18 16 0 const #> 19 17 1 const #> 20 55 (3-1) logic #> 21 18 5 const #> 22 19 (1/5) logic #> 23 87 (5-1) logic #> 24 20 x[1] stoch #> 25 22 y[1] stoch #> 26 23 c[2] stoch #> 27 39 (c[2]==1) logic #> 28 24 0.5 const #> 29 25 (0.5*x[1]) logic #> 30 26 25 const #> 31 27 (25*x[1]) logic #> 32 28 2 const #> 33 29 (x[1]^2) logic #> 34 30 (1+(x[1]^2)) logic #> 35 31 ((25*x[1])/(1+(x[1]^2))) logic #> 36 34 (2-1) logic #> 37 32 8 const #> 38 137 (8-1) logic #> 39 33 1.2 const #> 40 35 (1.2*(2-1)) logic #> 41 36 (cos((1.2*(2-1)))) logic #> 42 37 (8*(cos((1.2*(2-1))))) logic #> 43 38 ((0.5*x[1])+((25*x[1])/(1+(x[1]^2)))+(8*(cos((1.2*(2-1)))))) logic #> 44 56 (1.2*(3-1)) logic #> 45 57 (cos((1.2*(3-1)))) logic #> 46 58 (8*(cos((1.2*(3-1))))) logic #> 47 88 (1.2*(5-1)) logic #> 48 89 (cos((1.2*(5-1)))) logic #> 49 90 (8*(cos((1.2*(5-1))))) logic #> 50 138 (1.2*(8-1)) logic #> 51 139 (cos((1.2*(8-1)))) logic #> 52 140 (8*(cos((1.2*(8-1))))) logic #> 53 40 10 const #> 54 41 (1/10) logic #> 55 170 (10-1) logic #> 56 171 (1.2*(10-1)) logic #> 57 172 (cos((1.2*(10-1)))) logic #> 58 173 (8*(cos((1.2*(10-1))))) logic #> 59 42 100 const #> 60 43 (1/100) logic #> 61 44 (ifelse((c[2]==1),(1/10),(1/100))) logic #> 62 45 x[2] stoch #> 63 50 (0.5*x[2]) logic #> 64 51 (25*x[2]) logic #> 65 52 (x[2]^2) logic #> 66 53 (1+(x[2]^2)) logic #> 67 54 ((25*x[2])/(1+(x[2]^2))) logic #> 68 59 ((0.5*x[2])+((25*x[2])/(1+(x[2]^2)))+(8*(cos((1.2*(3-1)))))) logic #> 69 46 4 const #> 70 47 (x[2]/4) logic #> 71 48 y[2] stoch #> 72 71 (4-1) logic #> 73 72 (1.2*(4-1)) logic #> 74 73 (cos((1.2*(4-1)))) logic #> 75 74 (8*(cos((1.2*(4-1))))) logic #> 76 49 c[3] stoch #> 77 60 (c[3]==1) logic #> 78 61 (ifelse((c[3]==1),(1/10),(1/100))) logic #> 79 62 x[3] stoch #> 80 63 (x[3]/4) logic #> 81 64 y[3] stoch #> 82 66 (0.5*x[3]) logic #> 83 67 (25*x[3]) logic #> 84 68 (x[3]^2) logic #> 85 69 (1+(x[3]^2)) logic #> 86 70 ((25*x[3])/(1+(x[3]^2))) logic #> 87 75 ((0.5*x[3])+((25*x[3])/(1+(x[3]^2)))+(8*(cos((1.2*(4-1)))))) logic #> 88 65 c[4] stoch #> 89 76 (c[4]==1) logic #> 90 77 (ifelse((c[4]==1),(1/10),(1/100))) logic #> 91 78 x[4] stoch #> 92 79 (x[4]/4) logic #> 93 80 y[4] stoch #> 94 82 (0.5*x[4]) logic #> 95 83 (25*x[4]) logic #> 96 84 (x[4]^2) logic #> 97 85 (1+(x[4]^2)) logic #> 98 86 ((25*x[4])/(1+(x[4]^2))) logic #> 99 91 ((0.5*x[4])+((25*x[4])/(1+(x[4]^2)))+(8*(cos((1.2*(5-1)))))) logic #> 100 81 c[5] stoch #> 101 92 (c[5]==1) logic #> 102 93 (ifelse((c[5]==1),(1/10),(1/100))) logic #> 103 94 x[5] stoch #> 104 95 (x[5]/4) logic #> 105 96 y[5] stoch #> 106 98 (0.5*x[5]) logic #> 107 99 (25*x[5]) logic #> 108 100 (x[5]^2) logic #> 109 101 (1+(x[5]^2)) logic #> 110 102 ((25*x[5])/(1+(x[5]^2))) logic #> 111 97 c[6] stoch #> 112 109 (c[6]==1) logic #> 113 110 (ifelse((c[6]==1),(1/10),(1/100))) logic #> 114 103 6 const #> 115 104 (6-1) logic #> 116 105 (1.2*(6-1)) logic #> 117 106 (cos((1.2*(6-1)))) logic #> 118 107 (8*(cos((1.2*(6-1))))) logic #> 119 108 ((0.5*x[5])+((25*x[5])/(1+(x[5]^2)))+(8*(cos((1.2*(6-1)))))) logic #> 120 111 x[6] stoch #> 121 112 (x[6]/4) logic #> 122 113 y[6] stoch #> 123 115 (0.5*x[6]) logic #> 124 116 (25*x[6]) logic #> 125 117 (x[6]^2) logic #> 126 118 (1+(x[6]^2)) logic #> 127 119 ((25*x[6])/(1+(x[6]^2))) logic #> 128 114 c[7] stoch #> 129 126 (c[7]==1) logic #> 130 127 (ifelse((c[7]==1),(1/10),(1/100))) logic #> 131 120 7 const #> 132 121 (7-1) logic #> 133 122 (1.2*(7-1)) logic #> 134 123 (cos((1.2*(7-1)))) logic #> 135 124 (8*(cos((1.2*(7-1))))) logic #> 136 125 ((0.5*x[6])+((25*x[6])/(1+(x[6]^2)))+(8*(cos((1.2*(7-1)))))) logic #> 137 128 x[7] stoch #> 138 129 (x[7]/4) logic #> 139 130 y[7] stoch #> 140 132 (0.5*x[7]) logic #> 141 133 (25*x[7]) logic #> 142 134 (x[7]^2) logic #> 143 135 (1+(x[7]^2)) logic #> 144 136 ((25*x[7])/(1+(x[7]^2))) logic #> 145 141 ((0.5*x[7])+((25*x[7])/(1+(x[7]^2)))+(8*(cos((1.2*(8-1)))))) logic #> 146 131 c[8] stoch #> 147 142 (c[8]==1) logic #> 148 143 (ifelse((c[8]==1),(1/10),(1/100))) logic #> 149 144 x[8] stoch #> 150 145 (x[8]/4) logic #> 151 146 y[8] stoch #> 152 148 (0.5*x[8]) logic #> 153 149 (25*x[8]) logic #> 154 150 (x[8]^2) logic #> 155 151 (1+(x[8]^2)) logic #> 156 152 ((25*x[8])/(1+(x[8]^2))) logic #> 157 147 c[9] stoch #> 158 159 (c[9]==1) logic #> 159 160 (ifelse((c[9]==1),(1/10),(1/100))) logic #> 160 153 9 const #> 161 154 (9-1) logic #> 162 155 (1.2*(9-1)) logic #> 163 156 (cos((1.2*(9-1)))) logic #> 164 157 (8*(cos((1.2*(9-1))))) logic #> 165 158 ((0.5*x[8])+((25*x[8])/(1+(x[8]^2)))+(8*(cos((1.2*(9-1)))))) logic #> 166 161 x[9] stoch #> 167 162 (x[9]/4) logic #> 168 163 y[9] stoch #> 169 165 (0.5*x[9]) logic #> 170 166 (25*x[9]) logic #> 171 167 (x[9]^2) logic #> 172 168 (1+(x[9]^2)) logic #> 173 169 ((25*x[9])/(1+(x[9]^2))) logic #> 174 174 ((0.5*x[9])+((25*x[9])/(1+(x[9]^2)))+(8*(cos((1.2*(10-1)))))) logic #> 175 164 c[10] stoch #> 176 175 (c[10]==1) logic #> 177 176 (ifelse((c[10]==1),(1/10),(1/100))) logic #> 178 177 x[10] stoch #> 179 178 (x[10]/4) logic #> 180 179 y[10] stoch #> observed discrete #> 1 TRUE TRUE #> 2 TRUE TRUE #> 3 TRUE TRUE #> 4 TRUE TRUE #> 5 TRUE TRUE #> 6 TRUE TRUE #> 7 TRUE TRUE #> 8 TRUE TRUE #> 9 TRUE TRUE #> 10 TRUE TRUE #> 11 TRUE FALSE #> 12 TRUE TRUE #> 13 TRUE FALSE #> 14 TRUE TRUE #> 15 TRUE TRUE #> 16 TRUE FALSE #> 17 TRUE FALSE #> 18 TRUE TRUE #> 19 TRUE TRUE #> 20 TRUE TRUE #> 21 TRUE TRUE #> 22 TRUE FALSE #> 23 TRUE TRUE #> 24 FALSE FALSE #> 25 TRUE FALSE #> 26 FALSE TRUE #> 27 FALSE TRUE #> 28 TRUE FALSE #> 29 FALSE FALSE #> 30 TRUE TRUE #> 31 FALSE FALSE #> 32 TRUE TRUE #> 33 FALSE FALSE #> 34 FALSE FALSE #> 35 FALSE FALSE #> 36 TRUE TRUE #> 37 TRUE TRUE #> 38 TRUE TRUE #> 39 TRUE FALSE #> 40 TRUE FALSE #> 41 TRUE FALSE #> 42 TRUE FALSE #> 43 FALSE FALSE #> 44 TRUE FALSE #> 45 TRUE FALSE #> 46 TRUE FALSE #> 47 TRUE FALSE #> 48 TRUE FALSE #> 49 TRUE FALSE #> 50 TRUE FALSE #> 51 TRUE FALSE #> 52 TRUE FALSE #> 53 TRUE TRUE #> 54 TRUE FALSE #> 55 TRUE TRUE #> 56 TRUE FALSE #> 57 TRUE FALSE #> 58 TRUE FALSE #> 59 TRUE TRUE #> 60 TRUE FALSE #> 61 FALSE FALSE #> 62 FALSE FALSE #> 63 FALSE FALSE #> 64 FALSE FALSE #> 65 FALSE FALSE #> 66 FALSE FALSE #> 67 FALSE FALSE #> 68 FALSE FALSE #> 69 TRUE TRUE #> 70 FALSE FALSE #> 71 TRUE FALSE #> 72 TRUE TRUE #> 73 TRUE FALSE #> 74 TRUE FALSE #> 75 TRUE FALSE #> 76 FALSE TRUE #> 77 FALSE TRUE #> 78 FALSE FALSE #> 79 FALSE FALSE #> 80 FALSE FALSE #> 81 TRUE FALSE #> 82 FALSE FALSE #> 83 FALSE FALSE #> 84 FALSE FALSE #> 85 FALSE FALSE #> 86 FALSE FALSE #> 87 FALSE FALSE #> 88 FALSE TRUE #> 89 FALSE TRUE #> 90 FALSE FALSE #> 91 FALSE FALSE #> 92 FALSE FALSE #> 93 TRUE FALSE #> 94 FALSE FALSE #> 95 FALSE FALSE #> 96 FALSE FALSE #> 97 FALSE FALSE #> 98 FALSE FALSE #> 99 FALSE FALSE #> 100 FALSE TRUE #> 101 FALSE TRUE #> 102 FALSE FALSE #> 103 FALSE FALSE #> 104 FALSE FALSE #> 105 TRUE FALSE #> 106 FALSE FALSE #> 107 FALSE FALSE #> 108 FALSE FALSE #> 109 FALSE FALSE #> 110 FALSE FALSE #> 111 FALSE TRUE #> 112 FALSE TRUE #> 113 FALSE FALSE #> 114 TRUE TRUE #> 115 TRUE TRUE #> 116 TRUE FALSE #> 117 TRUE FALSE #> 118 TRUE FALSE #> 119 FALSE FALSE #> 120 FALSE FALSE #> 121 FALSE FALSE #> 122 TRUE FALSE #> 123 FALSE FALSE #> 124 FALSE FALSE #> 125 FALSE FALSE #> 126 FALSE FALSE #> 127 FALSE FALSE #> 128 FALSE TRUE #> 129 FALSE TRUE #> 130 FALSE FALSE #> 131 TRUE TRUE #> 132 TRUE TRUE #> 133 TRUE FALSE #> 134 TRUE FALSE #> 135 TRUE FALSE #> 136 FALSE FALSE #> 137 FALSE FALSE #> 138 FALSE FALSE #> 139 TRUE FALSE #> 140 FALSE FALSE #> 141 FALSE FALSE #> 142 FALSE FALSE #> 143 FALSE FALSE #> 144 FALSE FALSE #> 145 FALSE FALSE #> 146 FALSE TRUE #> 147 FALSE TRUE #> 148 FALSE FALSE #> 149 FALSE FALSE #> 150 FALSE FALSE #> 151 TRUE FALSE #> 152 FALSE FALSE #> 153 FALSE FALSE #> 154 FALSE FALSE #> 155 FALSE FALSE #> 156 FALSE FALSE #> 157 FALSE TRUE #> 158 FALSE TRUE #> 159 FALSE FALSE #> 160 TRUE TRUE #> 161 TRUE TRUE #> 162 TRUE FALSE #> 163 TRUE FALSE #> 164 TRUE FALSE #> 165 FALSE FALSE #> 166 FALSE FALSE #> 167 FALSE FALSE #> 168 TRUE FALSE #> 169 FALSE FALSE #> 170 FALSE FALSE #> 171 FALSE FALSE #> 172 FALSE FALSE #> 173 FALSE FALSE #> 174 FALSE FALSE #> 175 FALSE TRUE #> 176 FALSE TRUE #> 177 FALSE FALSE #> 178 FALSE FALSE #> 179 FALSE FALSE #> 180 TRUE FALSE# NOT RUN { dotfile <- 'hmm.dot' biips_print_dot(model, dotfile) cat(readLines(dotfile), sep = '\n') # }