valToVec: Translate a scalar value to a probability vector

Description Usage Arguments Details Value Author(s) Examples

View source: R/valToVec.R

Description

This routine translates a scalar value into a vector of length N containing N-1 zeros and one 1.0. The index position of the 1.0 within the returning vector depends on the position of val with respect to the bounds x_lower and x_upper.

Usage

1
valToVec(val, N = 10, x_lower = 0, x_upper = 1)

Arguments

val

Value to be translated

N

Dimension of resulting vector

x_lower

Lower bound

x_upper

Upper bound

Details

Always provide N>1. If you convert the resulting vector back with <<vecToVal>> (see Package "myBayes"). The resulting value deviates from the original by a maximum difference of +- 1/N.

Value

A probability vector containing N-1 zeros and one 1.0.

Author(s)

Philipp van Wickevoort Crommelin

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (val, N = 10, x_lower = 0, x_upper = 1)
{
    if (val > x_upper | val < x_lower)
        print("warning: val is not within bounds of x_lower and x_upper")
    if (N > 1) {
        dx = (x_upper - x_lower)/(N - 1)
        ref = seq(from = x_lower, to = x_upper, by = dx)
        diff = abs(ref - rep(val, N))
        minInd = which(diff == min(diff))
        result = rep(0, N)
        result[minInd] = 1
        return(result)
    }
    else {
        print(paste0("N needs to be greater than 1. N is currently set to ",
            N))
        return(NA)
    }
  }

PhilippVWC/myBayes documentation built on Oct. 2, 2020, 8:25 a.m.