In statistics, the Box-Cox transformation of the response variable Y is used to make the linear model more appropriate to the data. It can be used to attempt to impose linearity, reduce skewness or stabilize the residual variance. The Box-Cox transformation is defined as:
However, the Box-Cox transformation is not yet supported in SPSS. I write a macro to compute log-likelihood for different lambda. If you have any better way, please let me know.
*
* Box-Cox Transformation
*
SET LENGTH=NONE.
SET MXLOOP = 100000000.
MATRIX.
GET X/VARIABLES=x/MISSING=OMIT.
GET Y/VARIABLE=y /MISSING=OMIT.
COMPUTE NR =NROW(X).
COMPUTE NC =NCOL(X).
COMPUTE PI=4*ARTAN(1).
COMPUTE XX=MAKE(NR,NC+1,1).
COMPUTE YLAM=MAKE(NR,1,1).
COMPUTE BOXCOX=MAKE(61,2,0).
COMPUTE YTEMP=0.
LOOP I=1 TO NR.
COMPUTE XX(I,1)=1.
COMPUTE XX(I,2)=X(I).
COMPUTE YTEMP=YTEMP+LN(Y(I)).
END LOOP.
LOOP TEMP=1 TO 61.
COMPUTE LAMBDA=-3.1 + TEMP * 0.1.
DO IF LAMBDA=0.
COMPUTE YLAM(:)=LN(Y(:)).
ELSE.
COMPUTE YLAM(:)=(Y(:)&**LAMBDA -1)/LAMBDA.
END IF.
COMPUTE BETA=INV(T(XX)*XX)*T(XX)*YLAM.
COMPUTE MSE = T(YLAM-XX*BETA)*(YLAM-XX*BETA)/NR.
COMPUTE LOGLIKE=-1*NR/2*LN(2*PI)-NR/2*LN(MSE)-1/2+(LAMBDA-1)*YTEMP.
COMPUTE BOXCOX(TEMP,1)= LAMBDA.
COMPUTE BOXCOX(TEMP,2)= LOGLIKE.
END LOOP.
SAVE BOXCOX /OUTFILE=*.
END MATRIX.
RENAME VARIABLES COL1= LAMBDA COL2= LOGLIKE.
GRAPH
/SCATTERPLOT(BIVAR)=LAMBDA WITH LOGLIKE /MISSING=LISTWISE
/TITLE= 'Log-Likelihood versus lambda'.
spss 教學網站 spss教學講義下載 spss教學pdf spss教學網 spss教學講義下載 spss教學網站 spss教學下載 spss教學講義 spss教學pdf roc curve spss教學 manova spss教學 spss教學手冊 spss 16教學
讀者回應 ( 2 意見 )
訂閱發佈留言 (Atom)
Hey, could you please let me know how to use your Box-Cox transformation SPSS syntax? What are the y, x1 and x2? Thanks a lot.
Please reply to maernest@polyu.edu.hk at your earliest possible.
tep 1. Open your sav file
Step 2. File-→New-→Syntax
Step 3. Copy the code into syntax window
Step 4. From the SPSS Syntax editor menu → Run→All
Dependent variable: Y
Independent variable: X1, X2, X3, ...., Xp
If you have any problem, let me know!
Good luck!
發佈留言
Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.
如果這篇文章對你有幫助,那請留個訊息給我~