Desktop User Guides > Professional > Data management scripting > Working with the Weight component > Weight component examples > More on target weighting
 
More on target weighting
This mrScriptBasic example shows how to set up target weighting in a variable, Weight, that must already exist. If you run the example when this variable does not exist, an error occurs. (For an example that creates this variable, see Creating a new variable to hold the weighting information.) The weighting is based on two variables, age and gender, and is based on equal proportions of respondents in each cell of the weighting matrix:
Male
Female
11-16 years
5
5
17-20 years
5
5
21-24 years
5
5
25-34 years
5
5
35-44 years
5
5
45-54 years
5
5
55-64 years
5
5
65+ years
5
5
There are 16 cells in the weighting matrix. Here is the mrScriptBasic code to set up the weighting:
' This script weights the files created by Weight1a.mrs
' and Weight1b.mrs

Dim MDM2, WgtEng, Wgt, fso, ReptFile, Vars[2]

' Create the MDM object
Set MDM2 = CreateObject("MDM.Document")

' Open the .mdd file created by Weight1a.mrs MDM2.Open("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\
   Output\museum_weight.mdd")

' Create an instance of the Weight Engine object
Set WgtEng = CreateObject("mrWeight.WeightEngine")

WgtEng.Initialize(MDM2)

Vars[0] = "age"
Vars[1] = "gender"

Set Wgt = WgtEng.CreateWeight("Weight", Vars, 2)

Set fso = CreateObject("Scripting.FileSystemObject")
Set ReptFile = fso.CreateTextFile("C:\Program Files\IBM\SPSS\
   DataCollection\7\DDL\Output\Weight3.htm", True)
Wgt.CellRows.Targets = "16 * 5"
Wgt.TotalType = 3
Wgt.WeightedTotal = 5000

WgtEng.Prepare(Wgt)
ReptFile.Write(Wgt.Report)
WgtEng.Execute(Wgt)
ReptFile.Write(Wgt.Report)
ReptFile.Close()
' Close the database connection and flush pending data updates
Set WgtEng = Null MDM2.Close()
This example is in the UNICOM Intelligence Developer Documentation Library as a sample script called Weight3.mrs. For more information, see Sample mrScriptBasic files.
The variables that define the weighting matrix have been specified using an array variable and that the weighting targets have been specified as "16 * 5". This uses * (asterisk) as a convenient shorthand to indicate that all of the 16 cells in the weighting matrix have an identical target of 5.
Here is a table of age by gender run on the Museum example data weighted using the weight variable:
This graphic is described in the surrounding text.
Now, suppose you want to specify different proportions for each cell in the table:
Male
Female
11-16 years
1
2
17-20 years
2
4
21-24 years
3
6
25-34 years
4
8
35-44 years
5
10
45-54 years
6
12
55-64 years
7
14
65+ years
8
16
To specify this, replace the Wgt.CellRows.Targets = "16 * 5" line with the following:
Wgt.CellRows[0].Targets = "1; 2"
Wgt.CellRows[1].Targets = "2; 4"
Wgt.CellRows[2].Targets = "3; 6"
Wgt.CellRows[3].Targets = "4; 8"
Wgt.CellRows[4].Targets = "5; 10"
Wgt.CellRows[5].Targets = "6; 12"
Wgt.CellRows[6].Targets = "7; 14"
Wgt.CellRows[7].Targets = "8; 16"
Each CellRow object in the CellRows collection corresponds to one row in our weighting matrix. The order in which you specify the variables that are being used to define the weighting in the CreateWeight method, defines how the weighting matrix table is oriented. In this example, age was specified first and gender second, and so there are eight CellRow objects, each one corresponding to a category of the age variable. If you specified gender first and age second, there would be two CellRow objects: one for the Male category and one for the Female category of the gender variable.
Here is a table of age by gender run on the weighted data.
This graphic is described in the surrounding text.
You might want to set the same target for most, but not all, of the cells in the weighting matrix. You can do this by first specifying the same target for all of the cells in the weighting matrix and then specifying the targets for any individual cells that differ. For example:
Wgt.CellRows.Targets = "16 * 5"
Wgt.CellRows[3].Targets = "4; 8"
Wgt.CellRows[6].Targets = "5; 6"
When you are setting up weighting in a DMS file, you can specify the cells in the matrix using the names of the categories. For example, you could specify the same weighting using the category names as follows:
Wgt.CellRows.Targets = "16 * 5"
Wgt.CellRows[{e2534_years}].Cells[{Male}].Target = "4"
Wgt.CellRows[{e2534_years}].Cells[{Female}].Target = "8"
Wgt.CellRows[{e5564_years}].Cells[{Male}].Target = "5"
Wgt.CellRows[{e5564_years}].Cells[{Female}].Target = "6"
You can also use this method when you run a standalone .mrs script in mrScript Command Line Runner using the /m: option to specify the .mdd file. This means that the MDM Document is created as an intrinsic variable (called MDM) in the script, so you would need to remove the line that creates the MDM object: Set MDM = CreateObject("MDM.Document").
If the main example, in which there is only one variable in the weighting matrix, in Simple target weighting example was in a DMS file, you specify it like this:
Wgt.CellRows[0].Cells[{Male}].Target = "301"
Wgt.CellRows[0].Cells[{Female}].Target = "301"
You can use a filter to exclude cases from the weighting process. When you do this, the value in the variable that is being used to record the weights (Weight in these examples) will be unchanged for any cases that do not pass the filter. You create the filter using the Weight.FilterExpression property. You can use any expression that is supported by the UNICOM Intelligence Data Model.
For example, the following two lines are equivalent and would exclude from the weighting calculation any cases in the 11-16 years category of the age variable.
Wgt.FilterExpression = "Not age.ContainsAll({e1116_years})"
Wgt.FilterExpression = "Not (age >= {e1116_years})"
The first line uses the ContainsAll function, which is part of the UNICOM Intelligence Function Library that comes with the UNICOM Intelligence Data Model. The second line uses the equivalent Arithmetic operators. For more information, see UNICOM Intelligence Function Library and Expression evaluation.
Requirements
UNICOM Intelligence Professional
See
Weight component examples