Am 26.10.2014 um 14:22 schrieb Pindar:
Hi there,
I'm interested in calculating a geometric mean inside the aggregate
function in a panel dataset.
While my UDF works in plain script the results when using as aggregation
function are wrong.
Could you please help me with this issue, here is the hansl script:
function scalar fn_geomean(series X)
catch scalar Y = prodc({X})^(1/nobs(X))
if $error<>0
Y=NAN
endif
return Y
end function
open abdata.gdt
series test = abs(n)
matrix testmean = aggregate(test, const, mean)
eval mean(test)~ testmean[, 3]
matrix testgeomean = aggregate(test, const, fn_geomean)
eval fn_geomean(test) ~ testgeomean[, 3]
Most importantly, I get a gretl crash when I insert the line
'smpl ok(test) --restrict'
after the line where 'test' is created, so that looks like there might
be a deeper issue here. (However, this is with an older snapshot from
August on Windows, haven't tested yet whether it also occurs in newer
versions.)
So I don't know the reason for the discrepancy, but my suspicion would
be the missing values in the panel that somehow get treated differently
perhaps, depending on how the function is invoked. This may or may not
be a bug indpendent of the crash issue -- do you get the discrepancy
also when you work with a balanced panel?
OTOH, multiplying hundreds of numbers that could be close to zero is
probably not a good idea numerically anyway -- but this of course
doesn't explain the different results that you get.
cheers,
sven