加关注

# 零售创新，创新那些事儿，SPSS，VBA

## 日志 ### Mean Center By Excel,Spss Syntax

2010-08-24 18:30:27|  分类： spss学习 |  标签： |举报 |字号

下载LOFTER 我的照片书  |

下载 (574.54 KB)

2010-8-14 11:46

DATA LIST list/a1,a2.
begin data.
1    1
2    3
3    5
4    7
5    9
end data.
AGGREGATE
/BREAK=
*此处可以将变量名黏贴入excel进行操作.
/a1_mean=MEAN(a1)
/a2_mean=MEAN(a2).
*此处可以将变量名黏贴入excel进行操作.
COMPUTE a1_cen=a1-a1_mean.
COMPUTE a2_cen=a2-a2_mean.
EXECUTE.
match files /file=* /drop a1_mean,a2_mean.
---------------------------------补 充---------------------------------

matrix.
get x /var=id2.
compute centered=x -make(nrow(x),ncol(x),csum(x)/nrow(x)).
save centered /outfile=tmp2.sav /variables='ceny'.'ceny2'.
end matrix.
match files /file=* /file=tmp2.sav.
execute.

original url：http://www.glennlthompson.com/?p=73

SPSS macro for centering

by GLENN on April 24th, 2010

When running a regression analysis, it is sometimes desirable to center a predictor. Centering means subtracting a constant from all the scores in the variable, like so:

compute Centered_var = variable – constant.

execute.

In this case, the value zero for the variable ‘Centered_var’ is equal to the value of constant. Normally, we do not choose a random value for the contant. We choose something meaningful like the mean (average). There is a very good reason for this.

compute constant=meanofIV.

execute.

compute Centered_var = variable – constant.

execute.

The value zero has a special status in regression. The intercept of a regression equation, for example, is the estimated value of the dependent variable when all the predictors are zero. If we center the predictor about its mean, the intercept in the subsequent regression analysis will represent the estimated value of the dependent measure when the score on the predictor is ‘average’. In other words, the intercept in this situation becomes a (conditional) grand mean for the sample.

Mean centering is more commonly used when performing a moderated regression, usually involving the interaction between two continuous variables. It is necessary to avoid something called non-essential multicolinearity (colinearity that is caused by a mathematical artifact, but which can degrade the quality of an analysis if ignored).

If ever you are fortunate enough to obtain a significant moderation effect (i.e., an interaction), you can decompose the interaction quite easily via centering. Let’s say you want to test whether IV2 moderates the effect of IV1. To estimate the effect of IV1 when IV2 is high, pick an arbitrary value for IV2 and use that as the constant in the centering equation instead of the mean. The standard choice is based on the standard deviation:

compute constant=(meanofIV2)+ (1 standard deviation).

execute.

compute Centered_IV2= IV2- constant.

execute.

Once this variable has been generated, re-estimate your regression equation using it as a predictor and all of a sudden the beta coefficients for the main effects.

Now that I have explained what centering is all about, here is a macro that will do it for you automatically (see the bottom of the page).

The macro is called CenterIT. It accepts 3 arguments. The first argument is the name of the variable you want to center. The second argument is the grouping variable, if any (Sometimes it makes sense to center a variable within groups rather than for the entire sample). Finally, the third argument is the number of standard deviations away from the mean you want to center the variable.

For example, if I want to center a VariableX about the sample mean (i.e., no groups), I would first load the macro into by executing it. Select the text from Define to !enddefine, then right-click/run selection. Once this has been accomplished, Variable X can be centered about the sample mean using the following syntax (note that the second and third brackets are left blank):

CenterIT (VariableX)()().

This syntax will general a variable named: ‘Cent0_VariableX’, which is the mean-centered version of the original. The new version is the one you would use in your regression analysis.

If, for some reason I wanted to use group means for the centering (say, a desire to example the effect of VariableX while neutralizing its correlation with Gender),, I could use the following syntax:

CenterIT (VariableX)(Gender)().

Executing this syntax would generate a variable called ‘GenderCent0_VariableX’, which is the group mean-centered version of the original (the groups here based on gender, obviously).

Finally, if I wanted to center my variable around a value 1 standard deviation ABOVE the mean, then I would write the following syntax:

CenterIT (VariableX)()(1).

To center about 1 standard deviation BELOW the mean, I would write the following (Notice the negative sign).

CenterIT (VariableX)()(-1).

The first command generates a variable named: ‘Cent_1VariableX’. The second one a variable named ‘Cent_-1VariableX’.

Without further ado, here is the macro.

Enjoy.

***************************************macro definition begins.

Define CenterIT (!positional !enclose (‘(‘,’)')
/!positional !enclose (‘(‘,’)')
/!positional !enclose (‘(‘,’)')).

!LET !DV=!1
!LET !groups=!2
!LET !nSDs=!3

*if user does not supply a custom SD criterion, then assume a value of zero.
!IF (!nSDs !EQ !NULL) !THEN
!LET !nSDs=0
!IFEND

*obtaining mean and standard deviation.
!IF (!groups !NE !NULL) !THEN
sort cases by !groups.
!IFEND
execute.
AGGREGATE
/BREAK= !groups
/mean_cent=MEAN(!DV)
/SD_cent=SD(!DV).

*generating a new variable that is the centered version of the one the user supplied to the macro.
Compute !concat(!groups,’Cent_’,!DV) = (!DV)-(mean_cent+(!nSDs*SD_cent)).
execute.
!IF (!nSDs = 0) !THEN
variable labels
!concat(!groups,’Cent_’,!DV) !quote(!concat(‘The variable ‘, !DV, ‘ centered around the ‘ ,!groups, ‘ mean’)).
!else
variable labels
!concat(!groups,’Cent_’,!DV) !quote(!concat(‘The variable ‘, !DV, ‘ centered around a value ‘, !nSDs, ‘ standard deviations from the ‘,!groups,’ mean’)).
!IFEND

*cleaning up.
Delete variables mean_cent SD_cent .
!enddefine.

*************macro definition ends.

P.S.

If you use an earlier version of SPSS, you may need to provide a grouping variable for the macro to work; In this case, if you want to center around the sample mean, create a categorical variable with a single group, a constant, and use that as your grouping variable. Like so:

compute C=1.

execute.

评论这张

#### 评论

<#--最新日志，群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇，下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->