Skip to contents

Functionality for altering parameters:

A vector of 'true' parameters; possibly drawn from prior or posterior.

Add a true parameter vector to a model. Parameters can be created using arguments passed to make_parameters and make_priors.

Extracts parameters as a named vector

Usage

make_parameters(
  model,
  parameters = NULL,
  param_type = NULL,
  warning = TRUE,
  normalize = TRUE,
  ...
)

set_parameters(
  model,
  parameters = NULL,
  param_type = NULL,
  warning = FALSE,
  ...
)

get_parameters(model, param_type = NULL)

Arguments

model

A causal_model. A model object generated by make_model.

parameters

A vector of real numbers in [0,1]. Values of parameters to specify (optional). By default, parameters is drawn from model$parameters_df.

param_type

A character. String specifying type of parameters to make "flat", "prior_mean", "posterior_mean", "prior_draw", "posterior_draw", "define". With param_type set to define use arguments to be passed to make_priors; otherwise flat sets equal probabilities on each nodal type in each parameter set; prior_mean, prior_draw, posterior_mean, posterior_draw take parameters as the means or as draws from the prior or posterior.

warning

Logical. Whether to warn about parameter renormalization.

normalize

Logical. If parameter given for a subset of a family the residual elements are normalized so that parameters in param_set sum to 1 and provided params are unaltered.

...

Options passed onto make_priors.

Value

A vector of draws from the prior or distribution of parameters

An object of class causal_model. It essentially returns a list containing the elements comprising a model (e.g. 'statement', 'nodal_types' and 'DAG') with true vector of parameters attached to it.

A vector of draws from the prior or distribution of parameters

Examples


# make_parameters examples:

# Simple examples
model <- make_model('X -> Y')
data  <- make_data(model, n = 2)
model <- update_model(model, data)
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.000181 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 1.81 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 1.617 seconds (Warm-up)
#> Chain 1:                1.504 seconds (Sampling)
#> Chain 1:                3.121 seconds (Total)
#> Chain 1: 
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 2).
#> Chain 2: 
#> Chain 2: Gradient evaluation took 0.000125 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 1.25 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2: 
#> Chain 2: 
#> Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 2: 
#> Chain 2:  Elapsed Time: 1.367 seconds (Warm-up)
#> Chain 2:                1.404 seconds (Sampling)
#> Chain 2:                2.771 seconds (Total)
#> Chain 2: 
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 3).
#> Chain 3: 
#> Chain 3: Gradient evaluation took 0.000122 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 1.22 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3: 
#> Chain 3: 
#> Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 3: 
#> Chain 3:  Elapsed Time: 1.352 seconds (Warm-up)
#> Chain 3:                1.386 seconds (Sampling)
#> Chain 3:                2.738 seconds (Total)
#> Chain 3: 
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 4).
#> Chain 4: 
#> Chain 4: Gradient evaluation took 0.000131 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 1.31 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4: 
#> Chain 4: 
#> Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 4: 
#> Chain 4:  Elapsed Time: 1.234 seconds (Warm-up)
#> Chain 4:                1.424 seconds (Sampling)
#> Chain 4:                2.658 seconds (Total)
#> Chain 4: 
make_parameters(model, parameters = c(.25, .75, 1.25,.25, .25, .25))
#> Model parameters with associated probabilities: 
#> 
#> X.0 X.1 Y.00 Y.10 Y.01 Y.11
#> 0.25 0.75 0.625 0.125 0.125 0.125
make_parameters(model, param_type = 'flat')
#> No specific parameters to alter values for specified. Altering all parameters.
#>  X.0  X.1 Y.00 Y.10 Y.01 Y.11 
#> 0.50 0.50 0.25 0.25 0.25 0.25 
make_parameters(model, param_type = 'prior_draw')
#>        X.0        X.1       Y.00       Y.10       Y.01       Y.11 
#> 0.47669751 0.52330249 0.04510870 0.86453358 0.03805557 0.05230215 
make_parameters(model, param_type = 'prior_mean')
#>  X.0  X.1 Y.00 Y.10 Y.01 Y.11 
#> 0.50 0.50 0.25 0.25 0.25 0.25 
make_parameters(model, param_type = 'posterior_draw')
#>        X.0        X.1       Y.00       Y.10       Y.01       Y.11 
#> 0.96656043 0.03343957 0.51054887 0.09554801 0.34290003 0.05100309 
make_parameters(model, param_type = 'posterior_mean')
#>       X.0       X.1      Y.00      Y.10      Y.01      Y.11 
#> 0.7544312 0.2455688 0.3307413 0.1676434 0.3374368 0.1641785 


# \donttest{

#altering values using \code{alter_at}
make_model("X -> Y") %>% make_parameters(parameters = c(0.5,0.25),
alter_at = "node == 'Y' & nodal_type %in% c('00','01')")
#>   X.0   X.1  Y.00  Y.10  Y.01  Y.11 
#> 0.500 0.500 0.500 0.125 0.250 0.125 

#altering values using \code{param_names}
make_model("X -> Y") %>% make_parameters(parameters = c(0.5,0.25),
param_names = c("Y.10","Y.01"))
#>   X.0   X.1  Y.00  Y.10  Y.01  Y.11 
#> 0.500 0.500 0.125 0.500 0.250 0.125 

#altering values using \code{statement}
make_model("X -> Y") %>% make_parameters(parameters = c(0.5),
statement = "Y[X=1] > Y[X=0]")
#>       X.0       X.1      Y.00      Y.10      Y.01      Y.11 
#> 0.5000000 0.5000000 0.1666667 0.1666667 0.5000000 0.1666667 

#altering values using a combination of other arguments
make_model("X -> Y") %>% make_parameters(parameters = c(0.5,0.25),
node = "Y", nodal_type = c("00","01"))
#>   X.0   X.1  Y.00  Y.10  Y.01  Y.11 
#> 0.500 0.500 0.500 0.125 0.250 0.125 

# Normalize renormalizes values not set so that value set is not renomalized
make_parameters(make_model('X -> Y'),
               statement = 'Y[X=1]>Y[X=0]', parameters = .5)
#>       X.0       X.1      Y.00      Y.10      Y.01      Y.11 
#> 0.5000000 0.5000000 0.1666667 0.1666667 0.5000000 0.1666667 
make_parameters(make_model('X -> Y'),
               statement = 'Y[X=1]>Y[X=0]', parameters = .5,
               normalize = FALSE)
#>  X.0  X.1 Y.00 Y.10 Y.01 Y.11 
#>  0.5  0.5  0.2  0.2  0.4  0.2 

  # }

# set_parameters examples:

make_model('X->Y') %>% set_parameters(1:6) %>% grab("parameters")
#> Model parameters with associated probabilities: 
#> 
#> X.0 X.1 Y.00 Y.10 Y.01 Y.11
#> 0.3333333 0.6666667 0.1666667 0.2222222 0.2777778 0.3333333

# Simple examples
model <- make_model('X -> Y')
data  <- make_data(model, n = 2)
model <- update_model(model, data)
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.000143 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 1.43 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 1.44 seconds (Warm-up)
#> Chain 1:                1.451 seconds (Sampling)
#> Chain 1:                2.891 seconds (Total)
#> Chain 1: 
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 2).
#> Chain 2: 
#> Chain 2: Gradient evaluation took 0.000123 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 1.23 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2: 
#> Chain 2: 
#> Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 2: 
#> Chain 2:  Elapsed Time: 1.311 seconds (Warm-up)
#> Chain 2:                1.223 seconds (Sampling)
#> Chain 2:                2.534 seconds (Total)
#> Chain 2: 
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 3).
#> Chain 3: 
#> Chain 3: Gradient evaluation took 0.00012 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 1.2 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3: 
#> Chain 3: 
#> Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 3: 
#> Chain 3:  Elapsed Time: 1.257 seconds (Warm-up)
#> Chain 3:                1.433 seconds (Sampling)
#> Chain 3:                2.69 seconds (Total)
#> Chain 3: 
#> 
#> SAMPLING FOR MODEL 'simplexes' NOW (CHAIN 4).
#> Chain 4: 
#> Chain 4: Gradient evaluation took 0.000127 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 1.27 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4: 
#> Chain 4: 
#> Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 4: 
#> Chain 4:  Elapsed Time: 1.415 seconds (Warm-up)
#> Chain 4:                1.467 seconds (Sampling)
#> Chain 4:                2.882 seconds (Total)
#> Chain 4: 
set_parameters(model, parameters = c(.25, .75, 1.25,.25, .25, .25))
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 
#> 
#> Model has been updated and contains a posterior distribution with
#> 4 chains, each with iter=2000; warmup=1000; thin=1;  
#> Use grab(model, object = 'stan_summary') to inspect stan summary 
#> 
set_parameters(model, param_type = 'flat')
#> No specific parameters to alter values for specified. Altering all parameters.
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 
#> 
#> Model has been updated and contains a posterior distribution with
#> 4 chains, each with iter=2000; warmup=1000; thin=1;  
#> Use grab(model, object = 'stan_summary') to inspect stan summary 
#> 
set_parameters(model, param_type = 'prior_draw')
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 
#> 
#> Model has been updated and contains a posterior distribution with
#> 4 chains, each with iter=2000; warmup=1000; thin=1;  
#> Use grab(model, object = 'stan_summary') to inspect stan summary 
#> 
set_parameters(model, param_type = 'prior_mean')
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 
#> 
#> Model has been updated and contains a posterior distribution with
#> 4 chains, each with iter=2000; warmup=1000; thin=1;  
#> Use grab(model, object = 'stan_summary') to inspect stan summary 
#> 
set_parameters(model, param_type = 'posterior_draw')
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 
#> 
#> Model has been updated and contains a posterior distribution with
#> 4 chains, each with iter=2000; warmup=1000; thin=1;  
#> Use grab(model, object = 'stan_summary') to inspect stan summary 
#> 
set_parameters(model, param_type = 'posterior_mean')
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 
#> 
#> Model has been updated and contains a posterior distribution with
#> 4 chains, each with iter=2000; warmup=1000; thin=1;  
#> Use grab(model, object = 'stan_summary') to inspect stan summary 
#> 


# \donttest{

#altering values using \code{alter_at}
make_model("X -> Y") %>% set_parameters(parameters = c(0.5,0.25),
alter_at = "node == 'Y' & nodal_type %in% c('00','01')")
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 

#altering values using \code{param_names}
make_model("X -> Y") %>% set_parameters(parameters = c(0.5,0.25),
param_names = c("Y.10","Y.01"))
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 

#altering values using \code{statement}
make_model("X -> Y") %>% set_parameters(parameters = c(0.5),
statement = "Y[X=1] > Y[X=0]")
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 

#altering values using a combination of other arguments
make_model("X -> Y") %>% set_parameters(parameters = c(0.5,0.25),
node = "Y", nodal_type = c("00","01"))
#> 
#> Statement: 
#> X -> Y
#> 
#> Number of types by node:
#> X Y 
#> 2 4 
#> 
#> Number of unit types: 8
#> 


  # }