Metrisim Logo
Title
Go to content
 

How to use the R conjoint package for conjoint analysis

 
by Craig Kolb, 23 April 2025

This article explains how you can use R conjoint, and also indicates where you can optionally use Metrisim as a supplement if you wish. In a nutshell, Metrisim's market simulator is compatible with the R conjoint package, and can automatically create a simulator from csv files created by R conjoint with minimal changes.

Conjoint analysis with the R conjoint analysis package involves a 1. design stage (where you use R to create an experimental design that can in turn be used to create product profiles for your survey); 2. an analysis / parameter estimation phase where you estimate the partworths for two purposes: a.) determine what attributes are important and which levels have the biggest impact. b.) provide the partworths needed to build a market simulator. 3. simulation stage.

This tutorial assumes you’ve installed the open source R statistical software from the CRAN website and that you’ve also installed the conjoint package. Either open the console or use an IDE like ‘R commander’ and set the working directory to the folder you want to save your files to.
You can adapt the example code below to generate a fractional factorial design; after the point where it says expand.grid. For instance, Brand is the attribute name and Kinder Green is one of the levels. Change the attribute names and levels to your own, and add or delete attributes as needed. Note: anything after a ‘#’ symbol in R, is a comment that is not part of the code.

Example R code to generate an experimental design suitable for metric conjoint analysis
library(conjoint)
experiment<-expand.grid(
        Brand<-c("Kinder Green","Clover","Dewfresh","First Choice","Parmalat"),
        Price<-c("9.99","13.99","17.99","21.99"),
        Type<-c("Cow Milk","Cow-Free Milk (from cells)","Cow-Free Milk (from       flora)"),
        Cap<-c("Plastic","Bioplastic"),
        Pack<-c("Recyclable","Not recyclable"))
design<-caFactorialDesign(data=experiment, type="fractional")
print(design)
str(design)   #check number of runs included
print(cor(caEncodedDesign(design)))   #examine attribute correlations (as low as possible)
edesign<-caEncodedDesign(design)

 
# Write CSV in R
write.csv(design, file = "design_text.csv")
write.csv(edesign, file = "edesign.csv")

 
The above code saves design_text.csv file and edesign.csv into your working folder. It is worth remembering, the design_text.csv can optionally be uploaded to Metrisim, if you want Metrisim to use this design instead of its own design_text when using Metrisim's 'estimator' module later on. Delete any index columns if they appear before uploading - it should look like the screenshot below, with the attribute names in the first row for Metrisim to accept it. Keep copies of both as you will need them after your survey.

Design_text.csv screenshot


Usually, you would want to make a copy of the design_text.csv and use that to generate more presentable product profiles that you can save as individual images to use in your survey tool. Note that in the language of experimental design, the row/run of the experimental design is the same thing as a product profile in conjoint analysis.
Example of a survey ready product profile image created from a single run in the design.csv file

Estimating partworths using R conjoint

Once your survey is complete you need to estimate partworth utilities from the ratings. You need to prepare your ratings file as in the screenshot below for R conjoint.
Example of how the ratings file should be formatted


 
R conjoint package also requires the edesign.csv you created prior to your survey as well as a ‘levels.csv’ file as shown in the screenshot below. If you use Metrisim's estimator module to estimate partworths, these files are not required.
Example of how the levels should be formatted for R conjoint
  
 


You can use the code example below as a starting point.
R conjoint analysis code
library(conjoint)
# 1) IMPORT EDESIGN with headings added manually in file (DELETE the card number column before import)
edesign2<-read.table("edesign.csv", header=TRUE, sep=",")
# 2) IMPORT LEVELS Create vector of level labels (write "Levels" on first row as header)
levels<-read.table("levels.csv", header=TRUE, sep=",")
# 3) IMPORT RATINGS  
ratings<- read.table("ratings.csv", header=TRUE, sep=",", row.names="id")
# 4) GENERATE PARTWORTHS
caPartUtilities (ratings,edesign2,levels)   #individual level partworths
indiv<-caPartUtilities (ratings,edesign2,levels)
write.csv(indiv, file = "pws.csv")

Output and simulator

The above script will generate various statistics as output, such as the coefficients and will also output partworth charts. However, unlike Metrisim's simulator, this does not allow for the possibility of incorporating respondent weights (for representation) or volume weights. The screenshot below, shows partworths per UHT milk brand output by the R conjoint package. As can be seen Parmalat leads, while profiles branded as Kinder Green have the lowest purchase probability.


 


The above script will also save the partworths estimated per respondent to a csv file called pws.csv; see the example screenshot below. The partworths are compatible with Metrisim. Note: if you are going to use the pws.csv to build a conjoint analysis market simulator in Metrisim and you want to keep an id column in your pws.csv, check that column header (first row in the first column) is ‘id’ not blank like in the screenshot.

Example of how the partworths saved by the script should look

Take R conjoint to the next level with Metrisim

While R conjoint is a very useful open-source tool, it is limited in terms of the simulator, and is missing various features such as a way to incorporate external effects, calibration or respondent / volume weighting and interactivity. It is also not very user-friendly for non-technical users and is fairly cumbersome to use. The output is also not too attractive, and must be manually positioned in PowerPoint. The cluster analysis is also not presented in a way that is useful in practice.

Metrisim provides a solution to all of this, while being compatible with the R conjoint package’s output files. Metrisim uses the partworths to automatically construct a market simulator that provides better accuracy and enhances analyst productivity.  Metrisim also offers a complete suite - including design and estimation - which offers an alternative path to using open source tools such as R conjoint, for those seeking more convenience.

You can try Metrisim for free by registering and uploading the provided demo files, read more here.
Learn more about the advantages of metric (ratings-based) conjoint analysis here.


Back to content