On Thu, 30 Aug 2012, Andreas Tudyka wrote:
Hi I am new to gretl and I am trying to find out how the
calculations
behind different commands are done.
For example, if I run the pca command I would like to see how exactly the
eigenvectors are multplied with my data and how the data is standardized
etc.. This also applies to the princomp function.
Is it possible to look "into" these commands and functions or would I have
to resort to some source code in C?
For the command and function that you mention, yes, your only choice is to
look at the C code; it may seem intimidating at first, but it's very
instructive and IMHO it pays off in the long run to get acquainted with
a low-level language like C. The file you want to look at is
http://gretl.cvs.sourceforge.net/viewvc/gretl/gretl/plugin/pca.c?view=log
That said:
(i) it would be very nice to have, for each gretl command/function, a
detailed explanation of its inner working like the "Methods and Formulas"
section of the Stata manual(s). We simply don't have the manpower to do
this (but, unlike Stata, you do have the C source to look at).
(ii) the pca command can be replicated pretty easily through a hansl
function, so an answer to your specific question is implied by the
following script:
<hansl>
function list PC(list X, scalar n)
scalar k = nelem(X)
matrix mX = {X}
scalar N = rows(mX)
# note the dof adjustment
matrix Z = (mX .- meanc(mX)) ./ sdc(mX) * (sqrt(1 - 1/N))
matrix C = Z'Z ./ N
matrix Evec = {}
matrix Eval = eigensym(C, &Evec)
list ret = null
loop i=1..n --quiet
series PC_via_func_$i = Z * Evec[,k-i+1]
ret += PC_via_func_$i
end loop
return ret
end function
open maschool.gdt
list X = 2 3 4 6
pca X --save=2
matrix z = princomp({X}, 2)
series PC_alt_1 = z[,1]
series PC_alt_2 = z[,2]
list L = PC(X, 2)
# check that the principal components are in fact the same
ols PC_alt_1 0 PC_via_func_1
ols PC_alt_2 0 PC_via_func_2
</hansl>
--------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Economia
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
--------------------------------------------------