Package descriptive, working with frequency lists



Hi Mario,

thank you very much.
Your function is just what I want. One correction: Make res local,
otherwise you might change a globally defined variable of that name.

I have added functions for variance and standard deviation, though I?m
afraid they won?t work for multivariate samples, I?m not familiar with that.

Could someone improve that to work with multivariate samples?

Best regards,

Andreas

/* remove last element of a list. This function
   will help us to extract the data from the table */
butlast(lst) := reverse(rest(reverse(lst))) $


/* returns true if y is a list of lists of equal size,
    and false otherwise. You can call this function
    whenever you need to check the input format */
listsofequalsize(y) :=
    listp(y) and
    every(listp, y) and
    every(lambda([z], length(z) = length(y[1])), y) $


/* some inner comments follow */
table_mean(tbl) :=
    block([fr, sfr, dat, sol, res],

        /* check the input format */
        if not listsofequalsize(tbl)
            then error("Table has not the correct format"),

        /* build the list of frequencies and calculate the total */
        fr: map(last, tbl),
        sfr: lsum(i, i, fr),

        /* extract sample records */
        dat: map(butlast, tbl),

        /* calculate the mean */
        res: dat. fr / sfr,

        /* in the univariate case, return only the number */
        if length(res) = 1 then res: first(res),
        res  )$
table_var(tbl):=
   block([tmean, res,sfr, sample_size],
    if not listsofequalsize(tbl)
        then error("Table has not the correct format"),
    tmean: table_mean(tbl),
    sfr: lsum( (i[1]-tmean)^2 * i[2],i,tbl ),
    sample_size: lsum(i[2],i,tbl),
    res: 1/sample_size * sfr )$
table_var1(tbl):=
   block([tmean, res,sfr, sample_size],
    if not listsofequalsize(tbl)
        then error("Table has not the correct format"),
    tmean: table_mean(tbl),
    sfr: lsum( (i[1]-tmean)^2 * i[2],i,tbl ),
    sample_size: lsum(i[2],i,tbl),
    res: 1/(sample_size-1)* sfr )$
table_std(tbl):=sqrt(table_var(tbl))$
table_std1(tbl):=sqrt(table_var1(tbl))$