**R – Displayr**, and kindly contributed to R-bloggers)

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).

## Loading the flipChoice package

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, D_{0}-error will be computed in this case.

## Specifying priors

Priors are specified as an extra parameter in the call to DError. D_{P}-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, D_{B}-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!**

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Displayr**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...