A scale represents the possible values that a variable can have. In this section, we will only deal with
discrete or categorical scales, where the number of possible values is finite. These possible values are often
referred to as the levels of the scale. Each level is assigned a numerical value called the level
index.
A scale can be ordered or unordered. On an ordered scale, it is meaningful to compare different values. For
example, the values 'good', 'average', 'bad' have a clear order, while 'red', 'orange', and 'purple' do not. An
unordered categorical scale is sometimes called a nominal scale. An ordered categorical scale is sometimes called an
ordinal scale.
Scales are implemented by the CategoricalScale class. The
levels of the scale can be any object.
Two derived classes, NumericalScale and DateTimeScale, implement scales whose levels represent an interval of
numerical values or DateTime values, respectively. These are covered in subsequent sections.
Constructing Scales
The CategoricalScale class has four constructors.
The first constructor takes one argument: an object that implements the ICollection interface. This
can be, for example, an array or an array list. The members of the collection are taken as the levels of the scale.
The scale is unordered.
| C# |
Copy
|
string[] colorArray = new string[]
{"red", "green", "blue"};
CategoricalScale scale1 = new CategoricalScale(dataArray);
|
| Visual Basic |
Copy
|
Dim colorArray As String() = New String() _
{"red", "green", "blue"}
Dim scale1 As CategoricalScale = New CategoricalScale(dataArray)
|
The second constructor takes one additional boolean parameter that specifies whether the scale is ordered. A value
of true indicates that the scale is ordered.
| C# |
Copy
|
string[] qualityArray = new string[]
{"poor", "average", "good"};
CategoricalScale scale2 = new CategoricalScale(dataArray, true);
|
| Visual Basic |
Copy
|
Dim qualityArray As String() = New String() _
{"poor", "average", "good"}
Dim scale2 As CategoricalScale = New CategoricalScale(dataArray, True)
|
The third constructor takes one System.Type parameter, which must be an enumeration type. The levels
of the scale are the values of the enum variable. By default, the scale is unordered.
| C# |
Copy
|
enum Shape
{
Round,
Square,
Rectangular
}
CategoricalScale scale3 = new CategoricalScale(typeof(Shape));
|
| Visual Basic |
Copy
|
Enum Shape
Round,
Square,
Rectangular
End Enum
Dim scale3 As CategoricalScale = New CategoricalScale(GetType(Shape))
|
The fourth constructor adds a boolean parameter that indicates if the scale is ordered.
| C# |
Copy
|
enum Quality
{
Poor,
Average,
Good
}
CategoricalScale scale3 = new CategoricalScale(typeof(Quality), true);
|
| Visual Basic |
Copy
|
Enum Quality
Poor,
Average,
Good
End Enum
Dim scale4 As CategoricalScale = New CategoricalScale(GetType(Quality), True)
|
In addition, scales can be constructed from categorical variables. This is covered in a later section.
Properties and Methods
The Count property returns the number of levels in
the scale. The IsOrdered property indicates whether
the scale is ordered or unordered. Certain descriptive statistics, such as the median, are only available for ordered
scales.
The CategoricalScale class has an Item[([( Int32])])
property, which serves as the indexer property in C#. It returns the object that represents the level corresponding
to the specified level index. Level indices range from 0 to Count-1 The GetLevelIndex(Object) method performs the inverse operation: it
returns the level index corresponding to the specified level. If the level isn't found, the value -1 is returned. The
GetLevels()()()() method returns an object array containing
all the levels.
| C# |
Copy
|
Console.WriteLine(scale2[1]);
Console.WriteLine(scale2.GetLevelIndex("poor"));
|
| Visual Basic |
Copy
|
Console.WriteLine(scale2.Item(1)) ' Prints 'average.'
Console.WriteLine(scale2.GetLevelIndex("poor")) ' Prints '0.'
|
The GetCaption method returns the text label of
the level at the specified level index. The GetCaptions()()()() method returns a string array containing the
captions for each level.
The GetEnumerator()()()() method returns an
IEnumerator object that can be used to iterate through the levels of the scale.
Scales as Mappings
It often happens that some numerical measurement must be converted into one of a number of categories. For
example, a blood pressure reading may be classified as 'low,' 'normal,' or 'high.' A scale can be used for this
purpose. Or we may be interested in the month of the year a certain date fell in. Two Map()()()() methods support this functionality.
The first Map overload takes an object as its argument and returns the level index of the level the
object maps to. The second Map overload takes an array of objects and returns an integer array of the
level indexes corresponding to those objects.
The CategoricalScale class only supports the trivial mapping from each level to itself. But several
derived classes support more meaningful mappings:
- The NumericalScale class maps
real numbers to intervals. This can be used to create histograms.
- The DateTimeScale class maps
DateTime values to time intervals.
These classes are discussed in greater detail in the next two sections.