Desktop User Guides > Professional > Table scripting > Getting started > Weighting tables
 
Weighting tables
Sample script file: WeightedTables.mrs
Weighting is another term for sample balancing. Use it when you want the figures in your tables to be an accurate reflection of the target population. For example, of the 602 respondents interviewed in the Museum survey, 56.31% were male and 43.69% were female. However, you might want to change the figures to reflect an even balance between the genders. You can do this using the genbalance variable, which is a special weighting variable that has been set up to inflate the responses from the female respondents and deflate the responses from the male responses so that when you use it to weight your tables, they reflect an even balance between the genders.
Table of age by gender, weighted using genbalance variable
To use the genbalance variable to weight a table of age by gender like the one shown in Creating a simple table of Age by Gender, you could use this script:
TableDoc.Tables.AddNew("Table1", "age * gender", "Weighted table of Age by Gender")
TableDoc.Table1.Weight = "GenBalance"
This example refers to the table in the Tables collection of the Table Document using its name. This is possible only in mrScriptBasic because of its dynamic property expansion feature and is equivalent to referring to the table by its index in the collection. For example, the following lines are equivalent:
TableDoc.Table1.Weight = "GenBalance"
TableDoc.Tables.Table1.Weight = "GenBalance"
TableDoc.Tables[0].Weight = "GenBalance"
TableDoc.Tables["Table1"].Weight = "GenBalance"
Here is the table produced by the script:
This graphic is described in the surrounding text.
In this example, the counts are shown with two decimal places by changing the number of decimal places for the counts default cell item:
TableDoc.Default.CellItems[0].Decimals = 2
Table of age by gender, weighted using genbalance variable, with weighted and unweighted counts
Compare the results with the unweighted table that was created in Creating a simple table of Age by Gender: an Unweighted Base row and column have been added to the table and the base row now shows equal numbers of male and female respondents. If a table is weighted, counts created using the Count keyword are always weighted. However, you can add unweighted counts to the table. The following script creates a similar table, but shows the unweighted counts as well as the weighted counts:
TableDoc.Tables.AddNew("Table2", "age * gender", "Weighted table showing unweighted counts")
TableDoc.Table2.Weight = "GenBalance"
TableDoc.Table2.CellItems.AddNew(8, 2) ' itUnweightedCount = 8
This graphic is described in the surrounding text.
By default, an unweighted base element is added to all weighted tables. This is because it is good practice to show the unweighted base values in addition to the weighted base values. However, the unweighted base elements are not necessary in this table, because it shows the unweighted counts. You can stop the unweighted base elements being added to the table by setting the AutoUnweightedBases table property (see Table properties) to False. The unweighted base element is added when you add the weighting to the table, so you need to set the property before you weight the table. For example:
With TableDoc.Tables
.AddNew("Table3", "age * gender", "Weighted table without automatic
       unweighted base elements")
.Table3.Properties["AutoUnweightedBases"] = False
.Table3.Weight = "GenBalance"
.Table3.CellItems.AddNew(8, 2) ' itUnweightedCount = 8
End With
If you do not show the unweighted counts in the table, you should include the unweighted base elements, to comply with good practice guidelines. For more information, see Base element.
By default, details of the weighting variable are shown in the right header position. For more information, see Annotation positions.
Weighting variables must be numeric variables. However, not all numeric variables are suitable for use as weights. Generally, weighting variables are created specially, typically using the Weight component. For more information, see Working with the Weight component.
Requirements
UNICOM Intelligence Reporter
Next
Working with numeric variables
See also
Getting started