Posted by
David Marso on
Feb 23, 2018; 6:31pm
URL: http://spssx-discussion.165.s1.nabble.com/testing-for-homoscedasticity-in-SPSS-tp5735562p5735568.html
DOH!
I thought that thread title looked familiar ;-)
DRAT...
I modified the macro further to utilize the new support for PIVOT TABLES in
ver 25.
Also changed the DO IF $CASENUM / PRINT business to use ECHO.
Other caveats are still operative regarding renaming of Residuals for
multiple runs.
see original thread
http://spssx-discussion.1045642.n5.nabble.com/BREUSCH-PAGAN-amp-KOENKER-TEST-MACRO-undefined-variables-td5727299.html#a5731408*------------.
* BREUSCH-PAGAN & KOENKER TEST MACRO *
* See 'Heteroscedasticity: Testing and correcting in SPSS'
* by Gwilym Pryce, for technical details.
* REVISION HISTORY *.
* Code by Marta Garcia-Granero 2002/10/28.
* Modified by David Marso 2014/09/18
* (changed AGGREGATE and MATCH to use MODE=ADDVARIABLES,
slight mods to MATRIX code, some formatting changes)
* Modified by David Marso 2018/02/23
* (Modified Output formatting to support new Pivot Table support in
MATRIX).
* The MACRO needs 3 arguments:
* the dependent, the number of predictors and the list of predictors
* (if they are consecutive, the keyword TO can be used) .
* (1) MACRO definition (select and run just ONCE).
DEFINE bpktest(
!POSITIONAL !TOKENS(1)
/!POSITIONAL !TOKENS(1)
/!POSITIONAL !CMDEND).
* Regression to GET the residuals and residual plots.
REGRESSION
/STATISTICS R ANOVA
/DEPENDENT !1
/METHOD=ENTER !3
/SCATTERPLOT=(*ZRESID,*ZPRED)
/RESIDUALS HIST(ZRESID) NORM(ZRESID)
/SAVE RESID(residual) .
ECHO "Examine the scatter plot of the residuals to detect model
misspecification and/or heteroscedasticity" .
ECHO "".
ECHO "Also, check the histogram and np plot of residuals to detect non
normality of residuals " .
ECHO "Skewness and kurtosis more than twice their SE indicate
non-normality".
* Checking normality of residuals.
DESCRIPTIVES VARIABLES=residual /STATISTICS=KURTOSIS SKEWNESS .
* New dependent variable (g) creation.
COMPUTE sq_res=residual**2.
AGGREGATE
/OUTFILE=* MODE ADDVARIABLES
/BREAK=
/rss = SUM(sq_res)
/N=N.
COMPUTE g=sq_res/(rss/n).
* BP&K tests.
* Regression of g on the predictors.
REGRESSION
/STATISTICS R ANOVA
/DEPENDENT g
/METHOD=ENTER !3
/SAVE RESID(resid) .
* Routine adapted from Gwilym Pryce.
MATRIX.
COMPUTE p=!2.
GET g
/ VARIABLES=g.
GET resid
/ VARIABLES=resid.
COMPUTE sq_res2 = resid&**2.
COMPUTE n = nrow(g).
COMPUTE rss = msum(sq_res2).
COMPUTE m0 = ident(n)-((1/n)*make(n,n,1)).
COMPUTE tss = transpos(g)*m0*g.
COMPUTE regss = tss-msum(sq_res2).
COMPUTE r_sq=1-(rss/tss).
COMPUTE bp_test=0.5*regss.
COMPUTE BP_sig=1-chicdf(bp_test,p).
COMPUTE k_test=n*r_sq.
COMPUTE K_sig=1-chicdf(k_test,p).
*Final report.
PRINT /TITLE " BP&K TESTS".
PRINT { regss , rss , tss, r_sq}
/TITLE "Sums of Squares Partitioning"
/FORMAT "F8.4"
/RLABELS " "
/CLABELS "Regression SS","Residual SS","Total SS","R-squared".
PRINT {n,p}
/TITLE "Problem Size"
/FORMAT "F4.0"
/RLABELS " "
/CLABELS "Sample size (N)","Number of predictors (P)".
PRINT {bp_test ,BP_sig ;k_test ,K_sig }
/TITLE " Breusch-Pagan and Koenker tests for Heteroscedasticity"
/FORMATS "F8.4"
/CLABELS "Test Statistic Chi Square (df=P)", "Significance level of
Chi-square df=(H0:homoscedasticity)"
/RLABELS "Breusch-Pagan","Koenker".
END MATRIX.
!ENDDEFINE.
* (2) Sample data (replace by your own)*.
INPUT PROGRAM.
- VECTOR x(20).
- LOOP #I = 1 TO 50.
- LOOP #J = 1 TO 20.
- COMPUTE x(#J) = NORMAL(1).
- END LOOP.
- END CASE.
- END LOOP.
- END FILE.
END INPUT PROGRAM.
EXECUTE.
* Sets the mode for displaying output in MATRIX.
SET MDISPLAY=TABLES.
* (3) MACRO CALL (select and run).
* x1 is the dependent and x2 TO x20 the predictors.
BPKTEST x1 19 x2 TO x20 .
PRogman wrote
> LISTSERV@.UGA
> (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from:
http://spssx-discussion.1045642.n5.nabble.com/=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"