Adding a Category to the Value Attributes using the API

Adding a Category to the Value Attributes using the API

Sometimes a variable in a data set will be missing a category (e.g., maybe nobody said “not aware” for Coke). If using a rich data file format, such as an SPSS data file, this is not a problem, as the categories are included in the metadata. However, if using text, CSV, Excel, or SQL query, the value attributes are automatically constructed from the data. If the data does not contain ‘will exclude the non-used category’, it can be useful to add the category to the value attributes. This post describes how to do this using the API.

This works by coping and modifying the existing variable (you can’t change the raw variable itself).

Step 1: Do everything in Getting Started with the Displayr API

Once you have done this, open up the document and it should look like this:

Step 2: Obtain the Document secret

To modify a document using the API we need to know its Document secret. This is found by following these steps:

  1. Go to the document’s settings page (if in the document, click on the cog at the top right of the screen and press Document Settings)
  2. Expand out the Properties section.
  3. The document secret is located in the bottom-right corner.

Step 3: Download the addcategory.zip file

  1. Click here to download the zip file
  2. Double-click on it to open it
  3. Save its contents somewhere on your computer or network

The zip file contains:

  • A file called addcategory.QScript which contains a QScript for adding a category to the Gender variable set (note that variable sets are referred to as questions in QScript) and creates a page with a table on it showing a summary of the new variable
  • A file called addcategory.py which contains a Python script for running the QScript

Step 4: Edit and run the addcategory.py

  1. Open the file in a text editor
  2. On line 20, replace insert-document-secret with the document secret (as described above)
  3. Save the file
  4. Run the addcategory.py script using the process in Step 6 of Getting Started with the Displayr API
  5. Check out the new page for Gender that has been added to your document

About Tim Bock

Tim Bock is the founder of Displayr. Tim is a data scientist, who has consulted, published academic papers, and won awards, for problems/techniques as diverse as neural networks, mixture models, data fusion, market segmentation, IPO pricing, small sample research, and data visualization. He has conducted data science projects for numerous companies, including Pfizer, Coca Cola, ACNielsen, KFC, Weight Watchers, Unilever, and Nestle. He is also the founder of Q www.qresearchsoftware.com, a data science product designed for survey research, which is used by all the world’s seven largest market research consultancies. He studied econometrics, maths, and marketing, and has a University Medal and PhD from the University of New South Wales (Australia’s leading research university), where he was an adjunct member of staff for 15 years.