******************************************************************************
* This program estimates the percentage of the total effect that is mediated *
* and the ratio of the indirect to the direct effect by using the Sobel test *
* Written by William Dudley PhD and Jose Benuzillo MA                        *
* University of Utah College of Nursing                                      *
* 12/13/2002                                                                 *
* Updated for version 12 02/02/2004                                          *
*****************************************************************************.

DEFINE !data_in()
'c:\temp\spss\example Data\'
!ENDDEFINE.


DEFINE !dat_out()
'c:\temp\spss\regression files\'
!ENDDEFINE.

GET
  FILE=!data_in +'Example.sav'.
exe.
                                          *define your own path, this is just 
                                          a suggested one.
       
 
compute indepvar	= Pain .    /*your IV*/
compute depenvar	= Depress.  /*your DV*/
compute mediator	= Function. /*you MEDIATOR*/
exe.

******************************************************************************
* Regression analysis with the IV predicting the MEDIATOR                    *
*****************************************************************************.

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT mediator
  /METHOD=ENTER indepvar
  /OUTFILE=COVB(!dat_out + 'reg1.sav') . 
                                          *define your own path, this is just 
                                          a suggested one.


*****************************************************************************
* Regression analysis with IV and MEDIATOR predicting DV                    *
****************************************************************************.

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT depenvar
  /METHOD=ENTER indepvar mediator
  /OUTFILE=COVB(!dat_out + 'reg2.sav').  
                                          *define your own path, this is just 
                                          a suggested one.


*****************************************************************************
* Regression analysis with IV predicting DV                                 *
****************************************************************************.

REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT depenvar
  /METHOD=ENTER  indepvar
  /OUTFILE=COVB(!dat_out + 'reg3.sav'). 
                                          *define your own path, this is just 
                                          a suggested one.


*****************************************************************************
* Here we compute a variable named reg to identify the three different      *
* regression analysis we have run                                           *
****************************************************************************.

get file=!dat_out + 'reg1.sav'.
compute reg=1.
exe.
SAVE OUTFILE=!dat_out + 'reg1.sav'
  /COMPRESSED.

get file = !dat_out + 'reg2.sav'.
compute reg=2.
exe.
SAVE OUTFILE=!dat_out + 'reg2.sav'
  /COMPRESSED.

get file=!dat_out + 'reg3.sav'.
compute reg=3.
exe.
SAVE OUTFILE=!dat_out + 'reg3.sav'
  /COMPRESSED.


****************************************************************************
* Here we concatenate our three files reg1 reg2 and reg3                   *
***************************************************************************.


add files file=!dat_out + 'reg1.sav'
         /file=!dat_out + 'reg2.sav'
         /file=!dat_out + 'reg3.sav'
/keep=  reg DEPVAR_ ROWTYPE_ VARNAME_  CONST_ indepvar mediator.
exe.





****************************************************************************
* Here we select only the statistics required to compute the Sobel         *
* equation (unstandardized coefficients and standard errors)               *
***************************************************************************.

sel if rowtype_ = 'EST' or rowtype_ = 'SE' or rowtype_ = 'DFE' 
                  or rowtype_ = 'SIG'.
exe. 


****************************************************************************
* In this step we reshape our wide data file into long form                *
***************************************************************************.

numeric a.
numeric sa.
numeric siga.
numeric b.
numeric sb.
numeric sigb.
numeric df.
numeric t.
numeric st.
numeric sig.
	if reg = 1 and rowtype_ = 'EST' a		= indepvar.
	if reg = 1 and rowtype_ = 'SE'  sa		= indepvar.
	if reg = 1 and rowtype_ = 'SIG' siga	= indepvar.
	if reg = 2 and rowtype_ = 'EST' b		= mediator.
	if reg = 2 and rowtype_ = 'SE'  sb		= mediator.
	if reg = 2 and rowtype_ = 'SIG' sigb	= mediator.
	if reg = 2 and rowtype_ = 'DFE' df		= mediator.
	if reg = 2 and rowtype_ = 'EST' tprime	= indepvar.
	if reg = 3 and rowtype_ = 'EST' t		= indepvar.
	if reg = 3 and rowtype_ = 'SE'  st		= indepvar.
	if reg = 3 and rowtype_ = 'SIG' sig		= indepvar.
exe.

compute regx=1.
exe.


aggregate outfile !dat_out + 'sobel.sav'
 /break regx
 /a = max(a)
 /sa=max(sa)
 /siga=max(siga)
 /b=max(b)
 /sb=max(sb)
 /sigb=max(sigb)
 /df=max(df)
 /sig=max(sig)
 /tprime = max(tprime)
 /t=max(t)
 /st=max(st).
get file !dat_out + 'sobel.sav'.      
                                          *define your own path, this is just 
                                          a suggested one.
format a b tprime t (F8.4).

compute ab = a*b.
compute ttprime = t-tprime.

format ab ttprime  (F8.4).

exe.

***************************************************************************
* Here we compute the sobel test and calculate the percentage of the total*
* effect that is mediated and the ratio of the indirect to the direct     *
* effect                                                                  *
* Further information about these tests, may be found in MacKinnon & Dwyer*
*(1993) Estimating mediated effects in prevention studies                 *
**************************************************************************.

compute sobel= ttprime/(sqrt (( (b*b)*(sa*sa) ) + ( (a*a)*(sb*sb) ))). 
compute absobel= abs(sobel).
compute p_val=2*(1-cdfnorm(absobel)).
compute t1=(t-(a*b)).
compute toteff=(a*b/((a*b)+t1)).
compute ratio=((a*b)/t1). 
compute toteff = 100* toteff.
compute goodman = ttprime/sqrt(((b*b)*(sa*sa))+((a*a)*(sb*sb))+((sa*sa)*(sb*sb))).
compute absgood = abs(goodman).
compute goodman2 = ttprime/sqrt(((b*b)*(sa*sa))+((a*a)*(sb*sb))-((sa*sa)*(sb*sb))).
compute absgood2 = abs(goodman2).
compute p_val2 = 2*(1-cdfnorm(absgood)).
compute p_val3 = 2*(1-cdfnorm(absgood2)).      
exe.


format  p_val p_val2 p_val3
	   sig siga sigb 
	   sobel goodman goodman2
        toteff ratio st sb sa(F8.6).
exe.

variable label
sig    'P value of c'
siga   'P value of a'
sigb   'P value of b'
a      'Reg coeff for the association between IV and MEDIATOR'
sa     'Standard error of a'
b      'Reg coeff for the association between the MEDIATOR and IV on DV'
sb     'Standard error of b'
df     'Degrees of freedom'
t      'Reg coeff for the association between IV and DV'
st     'Standard error of c'
sobel  'Sobel'
p_val  'P value'
goodman 'Goodman test'
p_val2  'P value'
goodman2 'GoodmanII test'
p_val3 'P Value'
toteff 'Percentage of the total effect that is mediated'
ratio  'Ratio of the indirect to the direct effect'.
exe.



Report
  /FORMAT= CHWRAP(ON) PREVIEW(OFF) CHALIGN(BOTTOM)  UNDERSCORE(ON)
  ONEBREAKCOLUMN(OFF) CHDSPACE(1)  SUMSPACE(0)  AUTOMATIC  NOLIST
   BRKSPACE(0)
  PAGE(1) MISSING'.' LENGTH(1, 59) ALIGN(LEFT) TSPACE(1) FTSPACE(1)
 MARGINS(1,110)
 /TITLE=
  LEFT 'Regression Analysis Results'
  RIGHT 'Page )PAGE'
   /VARIABLES
 t	(VALUES)  (RIGHT)  (OFFSET(0)) (9)
 st	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
 sig	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
 a	(VALUES)  (RIGHT)  (OFFSET(0)) (9)
 sa	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
 siga	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
 b	(VALUES)  (RIGHT)  (OFFSET(0)) (9)
 sb	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
 sigb	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
 /BREAK (TOTAL)  (SKIP(1)) /SUMMARY
 SUM( t) SKIP(1) SUM( st ) SUM( sig ) SUM( a ) SUM( sa ) SUM( siga ) SUM( b ) SUM( sb ) SUM( sigb )   '' .



Report
  /FORMAT= CHWRAP(ON) PREVIEW(OFF) CHALIGN(BOTTOM)  UNDERSCORE(ON)
  ONEBREAKCOLUMN(OFF) CHDSPACE(1)  SUMSPACE(0)  AUTOMATIC  NOLIST
   BRKSPACE(0)
  PAGE(2) MISSING'.' LENGTH(1, 59) ALIGN(LEFT) TSPACE(1) FTSPACE(1)
 MARGINS(1,100)
 /TITLE= 
  LEFT 'Mediation Analysis Results'
  RIGHT 'Page )PAGE'
 /VARIABLES
 sobel	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 p_val	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 toteff	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 ratio	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 goodman	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 p_val2	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 goodman2	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 p_val3	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 /BREAK (TOTAL) (SKIP(1)) /SUMMARY
 SUM( sobel) SKIP(1) SUM( p_val ) SUM( toteff ) SUM( ratio) SUM( goodman ) SUM( p_val2 ) SUM( goodman2 ) SUM( p_val3 ) ''.


Report
  /FORMAT= CHWRAP(ON) PREVIEW(OFF) CHALIGN(BOTTOM)  UNDERSCORE(ON)
  ONEBREAKCOLUMN(OFF) CHDSPACE(1)  SUMSPACE(0)  AUTOMATIC  NOLIST
   BRKSPACE(0)
  PAGE(3) MISSING'.' LENGTH(1, 59) ALIGN(LEFT) TSPACE(1) FTSPACE(1)
 MARGINS(1,45)
 /TITLE= 
  LEFT 'Percent Mediated'
  RIGHT 'Page )PAGE'
 /VARIABLES
 toteff	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 ratio	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
 /BREAK (TOTAL) (SKIP(1)) /SUMMARY
 SUM( toteff ) SKIP(1) SUM( ratio) ''.




