Thank u Jack for this nice solution!
Leon
Am 25.02.2013 um 01:20 schrieb "Riccardo (Jack) Lucchetti"
<r.lucchetti(a)univpm.it>:
On Sun, 24 Feb 2013, Pindar wrote:
> Hi,
>
> I'd like to point out, that a 'good' 'aggregate by' function
would do such a job as described below.
> Unfortunately I'm stuck with more then bivariate interaction but I'm sure
GRETL can do this,
> this question is how!
The script below should do what I think you need; the trick is creating a series which
contains, in coded form, all the possible combinations of values of the variables
contained in a list. This is what the function "pack" does; note that
"pack" also creates a matrix which is used in the "unpack" function
later.
See if this helps:
<hansl>
set echo off
set messages off
function series pack(list X, matrix *pwrof2)
/*
a check should be necessary that all series in X
are discrete; for the moment, let's just skip it
*/
series ret = 0
pwrtot = 1
k = nelem(X)
# first build the nvalues vector
pwrof2 = {}
n = 1
k = 1
loop foreach i X --quiet
ret += k*X.$i
n = 2^ceil(log2(1+max(X.$i)))
k *= n
pwrof2 |= n
end loop
return ret
end function
function matrix unpack(scalar x, matrix pwr)
k = rows(pwr)
matrix ret = zeros(k,1)
loop i=1..k --quiet
h = pwr[i]
a = x % h
ret[i] = a
x = (x-a)/h
end loop
return ret
end function
# --- END FUNCTIONS -----------------------------------------
open pizza4.gdt
series ages = age<=20 ? 0 : (age>20 && age <=40) ? 1 : 2
list A = female hs ages
matrix codes = {}
u = pack(A, &codes)
n_u = values(u)
loop i = 1..rows(n_u) --quiet
k = n_u[i]
smpl u==k --restrict --replace
c = unpack(k, codes)
l = 1
loop foreach j A --quiet
printf "%10s = %d", "$j", c[l]
l++
end loop
printf "\ncases = %d, mean(pizza) = %g, mean(income) = %g\n\n", \
$nobs, mean(pizza), mean(income)
end loop
smpl full
</hansl>
>
> It's even more complex since the R function is not that good either, cos e.g. for
MAVOVA one needs the variance-covariance-structure of
> the 'interaction series'. That's why in GRETL lists should be processed
which makes the stuff not easier.
>
> I worked around with the new '^' operator and have some promising results,
but a build-in function would be the best.
>
> Cheers
> Leon
>
> <hansl>
> open pizza4.gdt
> series ages = age<=20 ? 0 : (age>20 && age <=40) ? 1 : 2
> set echo off
> foreign language=R --send-data --quiet
> library(plyr)
> ddply(gretldata[,c(1,2,4,6,8)], ~ female + college + ages, numcolwise(mean))
> end foreign
> <hansl>
>
> While the bivariate case in rather easy as in
>
> <hansl>
> open pizza4.gdt
> set echo off
> series ages = age<=20 ? 0 : (age>20 && age <=40) ? 1 : 2
>
> list G1 = 2 4
> list G2 = 2 8
> list G3 = 4 8
> list G = G1 || G2
>
> scalar times = 2 # should also be possible to be
set to nelem(G)
>
> #set up of all combination list
> loop for i=2..times -q
> string listName = "inter$i"
> list @listName = null
> endloop
>
> list part1 = null
> list part2 = G
>
> # bivariate interaction
> loop foreach i G-q
> part1 = G.$i
> part2 -= G.$i
> inter2 += part2^part1
> part2 += G.$i
> endloop
> <hansl>
>
>
>
> Am 20.02.2013 17:45, schrieb Riccardo (Jack) Lucchetti:
>> On Wed, 20 Feb 2013, Allin Cottrell wrote:
>>> On Wed, 20 Feb 2013, Riccardo (Jack) Lucchetti wrote:
>>>> Very nice. The next step, logically, would be to support user-written
functions too. However, this would mean to complicate the code by a factor (I reckon) of 1
to 5 or so.
>>> I was afraid the factor might be at the top end of that range, but I
experimented and it's more like 0.25. So user-functions are now supported. The
function must take a single series argument and return a scalar. Example:
>>> <hansl>
>>> function scalar my_aggregator (series x)
>>> return sum(x^3)
>>> end function
>>> open credscore.gdt
>>> matrix m = aggregate(Income, Acc, my_aggregator)
>>> print m
>>> </hansl>
>> Now, I don't want to be guilty of personality cult, but at the risk of
sounding like an old Stalinist apparatchik I have to say I am left in awe by the skills of
our beloved leader.
>> Studying the cvs diff has been for several years my primary method for learning
C, but this case stands out as a true gem.
>> Thank you.
>> -------------------------------------------------------
>> Riccardo (Jack) Lucchetti
>> Dipartimento di Scienze Economiche e Sociali (DiSES)
>>
>> Università Politecnica delle Marche
>> (formerly known as Università di Ancona)
>>
>> r.lucchetti(a)univpm.it
>>
http://www2.econ.univpm.it/servizi/hpp/lucchetti
>> -------------------------------------------------------
>> _______________________________________________
>> Gretl-users mailing list
>> Gretl-users(a)lists.wfu.edu
>>
http://lists.wfu.edu/mailman/listinfo/gretl-users
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------