Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In other articles I provide the mathematical definitions of D-error and worked examples of how to calculate D-error, but in the real world, most people will use existing tools to compute D-error. In this article I describe how to use R to compute the D-error for a choice experiment design.

## Preparing the design

The design needs to be in the form of an R numeric matrix where the first column contains the version number, the second column contains the task number, the third column contains the question number and the fourth column contains the alternative number.

The subsequent columns contain levels for each attribute, represented by numbers starting from 1. I’ve shown a small design matrix — 2 versions, 3 questions per version, 2 alternatives per question and 3 attributes (2,2,3 levels) — in the R printout below: In addition to the design, you also need an R vector containing the number of levels in each attribute. In this example, it would be an R vector consisting of 2,2 and 3, which I save to a variable called attribute.levels using attribute.levels <- c(2,2,3).

The next step is to load an R package containing the function that we use to compute D-error. The package is called flipChoice and it is in the Displayr organization on GitHub. You may first need to install an R package called devtools from CRAN, by entering the following in the R command line:

```install.packages(“devtools”)

```

Once you have done this, the R commands to install and load flipChoice are

```devtools::install_github(“Displayr/flipChoice”)

library(flipChoice)

```

## Calling the D-Error function

To compute the D-error of the design, which I shall assume is saved in the matrix called design, use the following R command:

```DError(design, attribute.levels, effects = FALSE)

```

By setting effects = FALSE, I have chosen to use dummy coding instead of effects coding. Also, since I have not passed in priors for the parameters, D0-error will be computed in this case.

## Specifying priors

Priors are specified as an extra parameter in the call to DError. DP-error is computed when prior is a vector of parameters

```prior <- c(0.5, 1.0, -1.0, -2.0)

DError(design, attribute.levels, effects = FALSE, prior = prior)

```

On the other hand, DB-error is computed when prior is a matrix with two columns. The first and second columns of this matrix correspond to the means and standard deviations of the normal distributions of the prior parameters.

There’s plenty to do in R. Find out more by checking out the “R How to” section of the blog!