In our previous tutorial we constructed a Normal distribution with a variety of parameterisations. Now we will look at how to access the parameters of the distribution and how to update them. Parameters are handled by param6, full documentation can be found at the package website.

Accessing Parameters

First we will construct the Standard Normal distribution, the default parameterisation

N <- Normal$new()

To view all parameters in the distribution we use the parameters method

N$parameters()
#>        Id Support  Value            Tags
#>    <char>  <char> <list>          <list>
#> 1:   mean       ℝ      0        required
#> 2:   prec      ℝ+        linked,required
#> 3:     sd      ℝ+        linked,required
#> 4:    var      ℝ+      1 linked,required

Notice how not only the parameters in the given parameterisation are displayed, but all the ones possible. Individual values can be obtained with getParameterValue, which is a wrapper around get_values.

N$getParameterValue("prec")
#> [1] 1

Updating Parameters

Any parameter can be updated in distr6 using the setParameterValue method and all others are updated accordingly. For example,

N$setParameterValue(var = 2)
N$parameters()
#>        Id Support  Value            Tags
#>    <char>  <char> <list>          <list>
#> 1:   mean       ℝ      0        required
#> 2:   prec      ℝ+        linked,required
#> 3:     sd      ℝ+        linked,required
#> 4:    var      ℝ+      2 linked,required

Not only has the variance been updated, but so too have the precision and standard deviation parameters. But be careful not to set conflicting parameterisations.

The wrong way:

N$setParameterValue(prec = 2)
#> Error in .return_fail(msg = "Multiple linked parameters are set.", error_on_fail): Multiple linked parameters are set.

The right way:

N$setParameterValue(prec = 2, var = NULL)

Internal checks ensure that only valid parameter values are allowed, for example setting the precision to a negative number will throw an error.

N$setParameterValue(prec = -1)
#> Warning in sqrt(unlist(vars)): NaNs produced
#> Error in .return_fail(msg = sprintf("One or more of %s does not lie in %s.", : One or more of {-1} does not lie in ℝ+.

Finally, multiple parameters can be updated at the same time

N$setParameterValue(mean = 4, prec = 2)
N$parameters()
#>        Id Support  Value            Tags
#>    <char>  <char> <list>          <list>
#> 1:   mean       ℝ      4        required
#> 2:   prec      ℝ+      2 linked,required
#> 3:     sd      ℝ+        linked,required
#> 4:    var      ℝ+        linked,required

Summary

In this tutorial we looked at getting and setting parameters for the Normal distribution. In the next tutorial we look at accessing mathematical and statistical methods including the d/p/q/r functions.