Skip to contents

Adds residual error to a generated response, based on a supplied variance values.

Usage

addResidualError(
  response,
  covariance,
  errStruc = c("Additive", "Proportional", "Log-Normal", "Combined"),
  seed = .deriveFromMasterSeed()
)

Arguments

response

(Required) Numeric vector of response data

covariance

(Required) Residual error (co)variance. This would be the lower-triangle of the matrix, or the matrix itself. The function parseCovMatrix is used to ensure that the matrix is in the right format.

errStruc

(Optional) Function describing how to apply residual error: "Additive", "Log-Normal" or "Proportional". "Additive" is the default

seed

(Optional) Random seed to use. Derived from the current random seed by default

Value

A numeric vector

Details

The first step in the algorithm will be to set the random number seed to the "seed" argument. Then, a number of samples will be drawn from a multivariate normal distribution with mean 0 and variance set by the (parsed) covariance input. The number of samples to take will be set by the number of elements in the "response" vector.

Based on the "errStruc" input, the algorithm should continue as follows. * If it is Additive, the residual errors should be added to the response vector. * If it is Log Normal, the response vector is multiplied by the exponentiated residual error. * If it is Proportional, the response vector is multiplied by "1 + the residual error".

Note

In earlier versions of MSToolkit, the "Proportional" Error Structure behaved differently (it assumed parameters passed were on the log scale).

MSToolkit currently has a limit of two residual error variances / covariances to create additive, proportional or combined additive + proportional error structures. At this time MSToolkit cannot simulate from time-series or complex residual variance structures i.e. multi-variate Normal distributions with number of error terms = number of observations.

See also

This function is typically not directly called by the user, but rather called by the function createResponse that is the high-level function for the response component.

The function createResponseVariable is also in the response component. It create the the response onto which addResidualError adds error.

Author

Romain Francois, Rich Pugh

Examples


  myVec <- 1:10
  addResidualError(response = myVec, covariance = "1")
#>             [,1]
#>  [1,] -0.5559861
#>  [2,]  1.6682897
#>  [3,]  3.1384323
#>  [4,]  4.4766347
#>  [5,]  4.7580239
#>  [6,]  4.8747408
#>  [7,]  7.5780672
#>  [8,]  7.6151936
#>  [9,]  9.0413677
#> [10,] 10.9098678