/* Contingency Std Residuals.jsl 09Mar2017 Copyright (c) 2017 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 computes the standardized residuals for a two-way contingency table. Author Mark Bailey (SAS Institute) Contact mark.bailey@sas.com Usage Simply open the data table and then 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 ); dlg# = Column Dialog( Title( "Contingency with STDRES" ), yCol# = Col List( "Y, Response", Min Col( 1 ), Max Col( 1 ) ), xCol# = Col List( "X, Factor", Min Col( 1 ), Max Col( 1 ) ), V List( rb# = Radio Buttons( "Not Adjusted", "Adjusted" ) ) ); If( dlg#["Button"] == -1, Throw( "User cancelled" ); ); Remove From( dlg# ); Eval List( dlg# ); yCol# = yCol#[1]; xCol# = xCol#[1]; cont# = Current Data Table() << Contingency( Y( yCol# ), X( xCol# ), Count( 1 ), Total %( 0 ), Col %( 0 ), Row %( 0 ), Expected( 0 ), Deviation( 0 ), Cell Chi Square( 0 ), Col Cum( 0 ), Col Cum %( 0 ), Row Cum( 0 ), Row Cum %( 0 ) ); contr# = cont# << Report; table# = contr#[CrossTabBox(1)] << Get As Matrix; Summarize( group y# = By( yCol# ) ); nCol# = N Items( group y# ); Summarize( group x# = By( xCol# ) ); nRow# = N Items( group x# ); n# = Sum( table# ); nidot# = table# * J( nCol#, 1, 1 ); ndotj# = table#` * J( nRow#, 1, 1 ); std res# = J( nRow#, nCol#, . ); For( r# = 1, r# <= nRow#, r#++, For( c# = 1, c# <= nCol#, c#++, nij# = table#[r#,c#]; eij# = (nidot#[r#] * ndotj#[c#]) / n#; pidot# = nidot#[r#] / n#; pdotj# = ndotj#[c#] / n#; Choose( rb#, std res#[r#,c#] = (nij# - eij#) / Sqrt( eij# * (1-pidot#) * (1-pdotj#) ), std res#[r#,c#] = (nij# - eij#) / Sqrt( eij# ); ) ) ); contr# << Append( Outline Box( "Standardized Residuals", tb# = Table Box( String Col Box( "Factor", group x# ) ), Text Box( Choose( rb#, "Not Adjusted", "Adjusted" ) ) ) ); For( c# = 1, c# <= nCol#, c#++, tb# << Append( Number Col Box( group y#[c#], std res#[0,c#] ) ); ); /* Revision History (date, change, person) 09Mar2017, created, 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.) */