How to implement a Winsorized mean function using Spotfire Expression Functions.

How to implement a Winsorized mean function using Spotfire Expression Functions.

book

Article ID: KB0081602

calendar_today

Updated On:

Products Versions
Spotfire Analyst All Versions

Description

A Winsorized mean is a Winsorized statistical measure of central tendency, much like the mean and median, and even more similar to the truncated mean (available directly in the Spotfire expression as the TrimmedMean() function). It involves the calculation of the mean after replacing certain parts of a probability distribution or sample at the high and low ends with the most extreme remaining values, typically doing so for an equal amount at both extremes.

To define a new custom function, select Edit > Data Function Properties from the Spotfire desktop client's main menu, then select the "Expression Functions" tab in the "Data Function Properties" dialog.  Here you’ll be able to add a new function or edit existing ones. Custom functions consist of a script using TERR/R syntax that includes the function declaration, along with the output assignment.

Issue/Introduction

How to implement a Winsorized mean function using Spotfire Expression Functions.

Resolution

1). In Edit > Data Function Properties > Expression Functions, create a new function by clicking the 'New' button.
 

2). Enter the following details:

Name: WinsorizedMean

Description: Calculates the mean after replacing certain parts of a probability distribution at the high and low ends with the most extreme remaining values, typically doing so for an equal amount at both extremes.

Example: WinsorizedMean(arg1)

Category: Statistical functions

Return type: Real

Script:

# Define the TrimmedMeanWinsor function:

WinsorizedMean <- function( input )
{
    p95 <- quantile( input, .95 )
    p05 <- quantile( input, .05 )
    input <- replace( input, input < p05, p05 )
    input <- replace( input, input > p95, p95 )
    out <- mean( input )
    out
}
# Run the function to produce the output
output <- rep( WinsorizedMean( input1 ), length( input1 ) )

3). Click "OK" to save the Expression Function.

 

When using custom expressions in Spotfire, you can now use the custom WinsorizedMean() function by passing it a single column. For example:
WinsorizedMean([myColumn1])

 

See the attached (Filename: WinsorizedMean.png).

 

Note:

This uses a hard coded p95 and p05 percentiles for the upper and lower limits used in the Winsorizing. The .95 and .05 values can be manually adjusted as required. Additionally, you could modify the Expression Function to take two additional arguments, so you pass the upper and lower limits explicitly.

 

Disclaimer:

The script code in this article is only a sample to be used as a reference. It is not intended to be used "As Is" in a Production environment. Always test in a Development environment. Make modifications to the script in accordance with your implementation specifications that best suit your business requirements. Refer to the API reference(s) cited in this article for usage of the classes and methods used in the script.

Additional Information

Doc: Details on Expression Functions
External: Winsorized Mean
 

Attachments

How to implement a Winsorized mean function using Spotfire Expression Functions. get_app