2008年2月24日 星期日

SPSS macro for Ridge regression


In Regression model, collinearity means that within the set of predictor variables, some of the predictor variables are (nearly) totally predicted by the other independent variables . When the predictor variables are highly correlated amongst themselves, the coefficients of the resulting least squares fit may be very imprecise. By allowing a small amount of bias in the estimates, more reasonable coefficients may often be obtained. Ridge regression is one method to address these issues. Often, small amounts of bias lead to dramatic reductions in the variance of the estimated model coefficients. In SPSS, you can use the following syntax to perform Ridge regression.

INCLUDE ’C:\Program Files\SPSS\Ridge regression.sps’.
RIDGEREG DEP=Y /ENTER = X1 to Xp


Compute principal component score in SPSS


In SPSS, the syntax can help you to compute principal component score. If you want to use correlation matrix, please transform your observations to zscore in SPSS first.

MATRIX.
GET X/VARIABLES=X1 to Xp/MISSING=OMIT.
COMPUTE NR =NROW(X).
COMPUTE NC =NCOL(X).
COMPUTE XX1=SSCP(X).
COMPUTE XX2=CSUM(X).
COMPUTE XX3=XX2/NR.
COMPUTE SIGMA=(XX1-T(XX3)*XX3*NR)/(NR-1).
COMPUTE SDIAG=MAKE(NC,NC,0).
LOOP J=1 TO NC.
COMPUTE SDIAG(J,J)=1/SQRT(SIGMA(J,J)).
END LOOP.
COMPUTE CORR=SDIAG*SIGMA*SDIAG.
PRINT SIGMA.
PRINT SDIAG.
PRINT CORR.
CALL EIGEN(SIGMA,V,LAMBDA).
PRINT LAMBDA.
PRINT V.
COMPUTE VV=T(V)*V.
PRINT VV.
COMPUTE PSCORE=X*V.
SAVE PSCORE /OUTFILE=*.
END MATRIX.


2008年2月21日 星期四

Compute Cook's distance in M$ Excel





In statistics, the Cook's distance is a commonly used estimate of the influence of a data point when doing least squares regression. Cook's distance measures the effect of deleting a given observation. Data points with large residuals (outliers) and/or high leverage may distort the outcome and accuracy of a regression. Points with a Cook's distance of 1 or more are considered to merit closer examination in the analysis.

Sub cook()
Dim xArray As Variant, yArray As Variant, zArray As Variant
Dim Fn As Object
Set Fn = Application.WorksheetFunction
Range("A1:D26").Select
nrow = Selection.Rows.Count
ncol = Selection.Columns.Count
n = nrow - 1
p = ncol - 2
xArray = Range(Cells(2, 2), Cells(nrow, ncol)).Value
yArray = Range(Cells(2, 1), Cells(nrow, 1)).Value
xx = Fn.MMult(Fn.Transpose(xArray), xArray)
xy = Fn.MMult(Fn.Transpose(xArray), yArray)
xxinverse = Fn.MInverse(xx)
beta = Fn.MMult(xxinverse, xy)
tbeta = Fn.Transpose(beta)
yhat = Fn.MMult(xArray, beta)
hArray = Fn.MMult(Fn.MMult(xArray, xxinverse), Fn.Transpose(xArray))
SSE = Fn.Index(Fn.MMult(Fn.Transpose(yArray), yArray), 1, 1) -Fn.Index(Fn.MMult(Fn.MMult(Fn.Transpose(yArray), hArray), yArray), 1, 1)
MSE = SSE / (n - p - 1)
Cells(1, ncol + 1) = "yhat"
Cells(1, ncol + 2) = "residual"
Cells(1, ncol + 3) = "hii"
Cells(1, ncol + 4) = "sred"
Cells(1, ncol + 5) = "cook"
For i = 2 To nrow
residual = Fn.Index(yArray, i - 1, 1) - Fn.Index(yhat, i - 1, 1)
hii = Fn.Index(hArray, i - 1, i - 1)
sred = residual / (MSE * (1 - Fn.Index(hArray, i - 1, i - 1))) ^ 0.5
cook = sred ^ 2 / 3 * hii / (1 - hii)
Cells(i, ncol + 1) = Fn.Index(yhat, i - 1, 1)
Cells(i, ncol + 2) = residual
Cells(i, ncol + 3) = hii
Cells(i, ncol + 4) = sred
Cells(i, ncol + 5) = cook
Next
Range(Cells(1, ncol + 1), Cells(nrow, ncol + 5)).Select
Selection.NumberFormatLocal = "0.0000_ "
End Sub


2008年2月20日 星期三

SPSS macro for Canonical Correlation Analysis




In statistics, canonical correlation analysis, introduced by Harold Hotelling, is a way of making sense of cross-covariance matrices. In SPSS, on does Canonical Correlation Analysis by using the canonical correlation macro. In the syntax window, type

Include file ’C:\Program files\SPSS\canonical correlation.sps’.
Cancorr
set1=x1,x2/
set2=y1,y2/.

Then, click "Run"→"All" via SPSS Syntax editor menu.


2008年2月17日 星期日

Export your data to Excel in Mathematica





When you use the built-in function "Tableform" in Mathematica, you can export you data to Excel by using following command



Export["ur file.
csv", %]



Then, open this file via M$ Excel.




2008年2月1日 星期五

My beamer template



\documentclass[t, cjk, hyperref={CJKbookmarks=true}]{beamer}
\usetheme{Madrid} % Madrid
\setbeamertemplate{itemize item}{$\blacksquare$} %\square
\setbeamertemplate{itemize subitem}{$\rightarrow$} %\vartriangleright \blacktriangleright
\setbeamertemplate{blocks}[rounded][shadow=true]
\usepackage{CJKutf8} 
\usepackage{inputenc}
\title[統計分析講義]{統計分析講義} 
\subtitle[資料的統計量數]{資料的統計量數}
\author[戴忠淵]{戴忠淵}
\institute[樹德科技大學企管系]{樹德科技大學企業管理系 \\[1ex]
\texttt{chungyuandye@gmail.com}
}
\date{\today}
\graphicspath{{stat/}}%設定圖形路徑
\begin{document}
\begin{CJK}{UTF8}{bsmi} 
\frame{\titlepage}
\section{大綱}
\frame{\frametitle{大綱}\tableofcontents}
\section{xxxxx}
\frame {\frametitle{xxxx}
\begin{enumerate}
\item<1-> aaa
\item<2-> bbb
\item<2-> ccc
\end{enumerate}
}
\clearpage
\end{CJK}
\end{document}