/* CI for CV.jsl 25Nov2013 Copyright (c) 2013 by SAS Institute Inc., Cary, NC 27513, USA. All rights reserved. Note: please read the disclaimer at the end of this script. Purpose This script adds the CI for the CV to the Disrtibution platform report layer. Reference Vangel, Mark G. (1996) "Confidence Intervals for a Normal Coefficient of Variation,"" The American Statistician 15(1) 21-26. Author Mark Bailey (SAS Institute) Contact mark.bailey@sas.com Usage Simply open the data table and run this script by any one of these methods: Edit > Run Script Control-R Click "Run Script" button in tool bar Future Improvement Ideas None at this time. */ Names Default To Here( 1 ); // user choices. dlg = Column Dialog( Title( "CI for CV" ), yCol = Col List( "Y, Response", Data Type( Numeric ), Min Col( 1 ) ), byCol = Col List( "By, Group", Max Col( 1 ), ), "", Line Up( 2, "Significance", alpha = Edit Number( 0.05 ) ), "Select data for CI" ); // check if user decides to quit. If( dlg["Button"] == -1, Throw( "User cancelled" ) ); // process information returned from dialog. Remove From( dlg ); Eval List( dlg ); // check if user input bad value for alpha. If( Not( 0 < alpha < 1), New Window( "Alert", << Modal, Text Box( "Error condition: Significance outside range (0,1)" ), Text Box( " " ), Button Box( "OK" ) ); Throw( "Error: alpha outside (0,1) range" ); ); yExpr = Expr( Y() ); For( c = 1, c <= N Items( yCol ), c++, Insert Into( yExpr, yCol[c] ); ); dist = Expr( Distribution() ); Insert Into( dist, Name Expr( yExpr ) ); If( N Items( byCol ), byExpr = Insert( Expr( By() ), byCol[1] ); Insert Into( dist, Name Expr( byExpr ) ); Summarize( group = By( byCol[1] ) ); ); dist = Eval( dist ); If( Not( N Items( byCol ) ), dist = Eval List( List( dist ) ); ); dist << Customize Summary Statistics( Mean(1), Std Dev(1), N(1) ); distr = dist << Report(); estMap = Function( { dbRef }, Associative Array( dbRef["Summary Statistics"][StringColBox(1)] << Get, dbRef["Summary Statistics"][NumberColBox(1)] << Get As Matrix ); ); ci = Function( { m }, { cv, nu, u }, cv = m["Std Dev"] / m["Mean"]; nu = m["N"] - 1; u = Matrix( List( Chi Square Quantile( 1 - alpha/2, nu ), Chi Square Quantile( alpha/2, nu ) ) ); cv/Sqrt(((((u+2)/(nu+1))-1)*cv^2)+(u/nu)); ); g = not done = 1; While( not done, For( c = 1, c <= N Items( yCol ), c++, distCol = Eval( Substitute( Expr( distr[g][yyy] ), Expr( yyy ), yCol[c] << Get Name ) ); map = estMap( distCol ); ciCV = ci( map ); distCol << Append( Outline Box( "CV", Table Box( String Col Box( "Parameter", { "CV%" } ), Number Col Box( "Estimate", { 100*map["Std Dev"]/map["Mean"] } ), Number Col Box( "Lower", { 100*ciCV[1] } ), Number Col Box( "Upper", { 100*ciCV[2] } ) ), Text Box( "Significance is " || Char( alpha ) ) ) ) ); If( N Items( byCol ), g++; not done = g <= N Items( group ), not done = 0; ); ); /* Revision History (date, change, person) 25Nov2013, created, Mark Bailey 10Jul2014, added By group capability, Mark Bailey */ /* Disclaimer by SAS Institute Inc. License Agreement for Corrective Code or Additional Functionality SAS INSTITUTE INC. IS PROVIDING YOU WITH THE COMPUTER SOFTWARE CODE INCLUDED WITH THIS AGREEMENT ("CODE") ON AN "AS IS" BASIS, AND AUTHORIZES YOU TO USE THE CODE SUBJECT TO THE TERMS HEREOF. BY USING THE CODE, YOU AGREE TO THESE TERMS. YOUR USE OF THE CODE IS AT YOUR OWN RISK. SAS INSTITUTE INC. MAKES NO REPRESENTATION OR WARRANTY, EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE, WITH RESPECT TO THE CODE. The Code is intended to be used solely as part of a product ("Software") you currently have licensed from SAS or one of its subsidiaries or authorized agents ("SAS"). The Code is designed to either correct an error in the Software or to add functionality to the Software, but has not necessarily been tested. Accordingly, SAS makes no representation or warranty that the Code will operate error-free. SAS is under no obligation to maintain or support the Code. Neither SAS nor its licensors shall be liable to you or any third party for any general, special, direct, indirect, consequential, incidental or other damages whatsoever arising out of or related to your use or inability to use the Code, even if SAS has been advised of the possibility of such damages. Except as otherwise provided above, the Code is governed by the same agreement that governs the Software. If you do not have an existing agreement with SAS governing the Software, you may not use the Code. (SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand and product names are registered trademarks or trademarks of their respective companies.) */