Create New Variables - Automatically Combine Categories - By Value - Equally Spaced Categories

From Q
Jump to: navigation, search

Create a new variable with categories formed by dividing a numeric range into equally-spaced intervals The features in Automatically Combine Categories - By Value allow you to create categories from numeric data using one of server different rules. Each category contains a range of values. This is sometimes referred to as banding, binning, or aggregating.

There are four different methods available for combining numeric values in this tool:

  • Tidy categories divides the range of values into tidy categories, which are intervals of 2, 5, 10, 20, 50, and so on.
  • Percentiles divides the range up into percentiles. This is useful when you want to create categories which contain even proportions of the cases in your data.
  • Equally spaced categories divides the range of values into categories that all have the same range. This is similar to Tidy categories, but has additional options for customization.
  • Custom categories divides the range of values up into ranges of your choice. This is useful when you want to specify uneven ranges of values.

The Equally spaced categories method breaks the range of values into categories which all contain the same range of values from the original data. You can choose how these ranges are formed by specifying the Start point and End point for the data (by default these will be set to the lowest and highest values in the data) and then either choosing the Number of categories or the Increment. If you specify the Number of categories then the range defined by the start and end points will be evenly split among that many categories. If you specify the Increment, then categories of that width will be created, beginning from the start point.

Examples

Consider the following data, which has recorded the amount of money some respondents to a survey said they spent the last time they visited a particular fast-food chain:

The values range from $1 to $102. Applying the Equally spaced categories method to this data results in the following new categories:

There are a few key points to note:

  • The range of $1 to $102 produces uneven categories, so the Start point has been set to 0 and the End point for the range has been set to 120.
  • The Number of categories has been set to 6, resulting in new categories which are in increments of 20. This could have also been acheived by setting the Increment to 20.
  • The dollar sign has been added to the Number prefix field so that our new categories show us that the values refer to currencies.
  • The start points in each range show the smallest actual data value which falls into that range. This is to avoid confusion about which categories the start and end points have been assigned to when there are decimal values. For example, if the first category was $0 to $20 and the second category was $20 to $40 then it would be unclear which of these two categories the value of $20 had been assigned to.
  • The Category boundary setting has been set to End of range to ensure that the round values (20, 40, etc) appear in the upper end of each label.

The behavior of the Equally spaced categories method is slightly different when the variable you are working with contains categories which represent ranges of values. An example of this kind of data is the following, which comes from a survey question asking the respondent's their income bracket:

Here, each label represents a range of incomes with two numbers. That is, except for the first and last category, which contain single numbers: Under $1,000 and $200,000 or over. Applying the Equally spaced categories method to data like this results in new categories whose ranges are as similar as possible. For example, applying Equally spaced categories to the example above and choosing the Number of categories to be 5 gives the following set of combined categories:

Note that:

  • In the original data, the ranges of values represented by the highest and lowest categories are not known. That is, Under $1,000 and $200,000 or over each only contain a single number. If this is the case for your data, you must enter values in the Start of range and End of range fields in order for the algorithm to know how to group these categories. In this case I have set Start of range to 0 to indicate that the lowest income possible should be $0, and I have entered 250000 in the End of range field to indicate that I estimate most survey respondents to not have incomes greater than $250,000. You should choose these values appropriately for your data.
  • While we have asked for 5 categories, the table shows 7 because there are two categories in the original data which don't contain any numbers and hence don't make sense to combine with the rest of the data.
  • Each income bracket has only been combined with those which are directly above or below it, so that the new categories still represent meaningful ranges.
  • The start and end points for the new categories correspond to start and end points of the original data.
  • The ranges of values that are represented by the new categories as even as possible given the granularity of the original categories. The ranges are now: $59,999, $59,999, $39,999, $49,999, and $50,000. The more even the original categories, the more even the new categories will be.

Usage

  1. Select a table showing the question you want to use to create combined categories, or select one or more variables in the Variables and Questions tab.
  2. Select this option from Automate > Browse Online Library > Automatically Combine Categories.
  3. To change how the categories are combined:
    1. Select the new variable or question in the Variables and Questions tab.
    2. Right-click and select Edit R Variable.
    3. Choose the desired options in the Inputs section on the right.
    4. Click Update R Variable.
  1. Select one or more variables under Data Sets. If the variables are categorical or text then the data labels should contain some numeric values.
  2. Click the + symbol to the right of the selected variables.
  3. Select the desired option from Ready-Made New Variables > Automatically Combine Categories.
  4. Choose options for the new variable in the Inputs section on the right of the screen.

Options

The settings that are available for Automatically Combine Categories - By Value change depending on which method you choose, and whether the input data are categorical. Settings that are available for all methods are shown first, and method-specific settings are shown below.

Variable(s) Choose which variable(s) are being used to create new categories.

Use numbers found in category labels This option will only appear if there are any categorical variables selected in Variable(s). When this is ticked, the tool will try to identify numeric values from the data labels and use those values when forming categories. If this is not ticked, the tool will ignore the category labels and will instead use the underlying data values for each category. Labels contain This option will also only appear if there are any categorical variables selected in Variable(s). This option allows you to communicate the nature of the values in the labels:

Single values This causes the tool to assume that your labels always contain single values. Any labels which contain more than one numeric value will not be combined.
Ranges of values This causes the tool to assume your data labels describe ranges of values. The tool will expect to find pairs of labels, with the exeception of the lowest and highest values in the range.

Method The method to be used to identify ranges of values to use for creating new categories. The different methods are described above.

Category boundary Whether the value at the start of each range is included in the new category, or the value at the end of each range is included in the new category. For example, if dividing the interval up into ranges of 10, do we create a category which is 10 to 19 (Start of range) or 11 to 20 (End of range).

Label style Determine the style of the labels for the new categories.

Tidy labels describes the range of values that is contained within each range in English. For example, 10 to 20.
Inequality notation describes the range of values that is contained within each range using greater-than and less-than symbols. For example, 10 to <21.
Interval notation descibes the range of values using interval notation. For example, [10, 20) describes a range of values which include the value 10 and range up to 20 but does not include the value of 20. Similarly, (10, 20] describes a range which does not include 10 but includes all values greater than 10, up to and include the value of 20.

Use open-ended labels When ticked, this setting produces open-ended category labels at the start and end of the range. For example, and open ended label would be Less than 10, whereas a non-open ended label would be 0 to 9

Number prefix / Number suffix Allows you to add text before and after the numeric value in each new label. For example, you may wish to add a dollar-sign or other currency symbol in front of each number in the new labels. This is not available if using Ranges of values, where the text to place before and after is drawn from the original category labels.

Decimals in label Choose the number of decimals that are displayed in the new category labels.

Decimals symbol / Thousands symbol These options are only available when there are categorical variables selected in Variable(s). They allow you to communicate the number format of the data labels. For example, the number convention in the United States and other countries is to use periods to denote the position for decimal values, and commas to separate units of thousands, resulting in numbers of the form 10,000.50. In other countries, the convention is reversed, so that the same value may be represented by 10.000,00.

Tidy categories

Target Number of categories This option allows you to specify how many categories should be created when using the Tidy categories option. Note that the Tidy categories algorithm always tries to make ranges of values which are intervals of 2, 5, 10, 20, 50, etc, and so it will not always be able to acheive the desired number of categories. For more control, you can try using the Equally spaced categories method instead.

Percentiles

Percentages This option allows you to control how categories are created when using the Percentiles method. You can enter a single number here, to divide the range up into even percentiles. For example, entering 10 will create the 10, 20, 30, etc percentiles. Alternatively, if you don't want even percentiles, you can enter a comma-separated list of percentiles. For example, you can enter 50, 75, 85, 90, 95, 99, 100 to create these uneven percentiles.

Equally spaced categories

Number of categories Specify how many categories will be created. Altrnatively, rather than dividing the range into a set number of categories, you can set the Increment option to divide the range into categories that have a set width.

Start point / End point Use these options to determine where you want the range to start and end. This range will then be divided up according to the Number of categories setting, or the Increment setting.

Increment Use this option if you want to specify how wide each interval should be instead of specifying how many categories you wish to create.

Custom categories

Cut points Use this field to specify which values should define the new categories.

Always include highest and lowest values When this option is ticked, the entire range of values will always be included in the set of categories even if the highest and lowest values are not entered in Cut points. Turning this option off will result in missing values for any numeric values which fall outside the range of values specified in Cut points. This is useful if you want to exclude values which are too low or too high (e.g. outliers, respondents providing unrealistically high or low values in a survey).

Ranges of values

Number of categories Specify how many categories will be created. When the input data are categorical, and the category labels contain ranges, this tool will combine adjacent ranges into this many categories.

Start of range / End of range Many examples of range categories, like Age or Income questions from surveys, contain ranges that are open-ended at the start and end of the range. For example, an Age question may start with 18 years or younger and end with 65 years and older. If combining such range categories into Equally spaced categories, the algorithm does not how the start and end points for the range of values. These fields allow you to communicate values to use as the highest and lowest in the range. For example, if you conduct a survey, and the youngest respondents who were allowed to complete the survey were 13 years old, entering 13 in Start of range would have the effect of converting 18 years or younger to 13 to 18 from the perspective of finding ranges of values in this algorithm.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Functions for Automatically Combining Categories');

createAutomaticallyCombinedCategoryVariables('Value', {method: 'Equally spaced categories'});

See also