How to Compute D-Error for a Choice Experiment Using Displayr
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 Displayr to compute the D-error for a choice experiment design.
Preparing the design
The design needs to be in the form of an R output table. If the design is not already in this form, the easiest way to input external data is by clicking on Home/Insert > Enter Data in the ribbon menu at the top. This should create a new R output called table. Click on the red “Paste or type data” button on the right-hand side and a spreadsheet editor dialog box should appear.
Enter the design into the cells; alternatively you can paste them in from Excel. 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 provided a small design matrix — 2 versions, 3 questions per version, 2 alternatives per question and 3 attributes (2,2,3 levels) — below:
Once you have entered this design, click on the OK button. Then click on the calculate button in the top right. The design should appear as a table in the output area.
Computing D-error
The D-error will be computed using R code though an R Output, which is created by clicking on Insert > R Output in the ribbon menu. In the R CODE box on the right, enter the following code:
library(flipChoice) attribute.levels <- c(2,2,3) DError(`table`, attribute.levels, effects = FALSE)You will need to replace the assignment to the variable attribute.levels with the appropriate vector. In this example, I’ve assigned it c(2,2,3) because there are 2, 2 and 3 levels in the three attributes in the design.
I have assumed here that the R output containing the design is called table; if it isn’t, replace table in the code above with the R output name. Remember to keep the backticks (`) around the name to ensure that names containing spaces or special characters will still work.
Once you have modified the R code, click on the Calculate button in the top right and the D-error should appear in the output. The default number of decimal places shown is 1; if this is insufficient, the number of decimal places shown can be increased via the Number section of the Appearance tab.
Note that by setting effects = FALSE, I have chosen to use dummy coding instead of effects coding.
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. For example:
prior <- c(0.5, 1.0, -1.0, -2.0) DError(`table`, 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.
Want to find out how to more in Displayr? Check out “Using Displayr“!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.