Hank,
I'll write up some comments for you.
Greg
----------------------------------------------------------------------------
--------
Greg Reese, Ph.D.
Senior Research Computing Specialist
Research Computing Support Group
Gaskill Hall, Room 352
Miami University
Oxford, OH 45056
(513)529-1411
[log in to unmask]
> -----Original Message-----
> From: Research Computing Support
> [mailto:[log in to unmask]] On Behalf Of Martin
> Henry H. Stevens
> Sent: Wednesday, April 12, 2006 5:40 AM
> To: [log in to unmask]
> Subject: ANOVA-like randomization in C
>
> Hi Greg et al.
> I was hoping someone could comment the following C code so I
> could understand what is done. I am not familiar with C, but
> am familiar with simple programming techniques (e.g. loops),
> and the algebra of ANOVA.
> This code is used for multivariate randomization test in an R package.
> Hank Stevens
>
> Begin forwarded message:
>
> > From: Jari Oksanen <[log in to unmask]>
> > Date: April 12, 2006 2:10:15 AM EDT
> > To: "Martin Henry H. Stevens" <[log in to unmask]>
> > Subject: Re: goodness of fit in factorfit
> >
> > Martin,
> >
> > It's in C code which reads like this:
> >
> > void goffactor(double *ord, int *f, double *w, int *nrow,
> int *ndim,
> > int *nlev,
> > double *sw, double *swx, double *swxx, double *var) {
> > int i, j, k;
> >
> > for (i = 0; i < (*nlev); i++) {
> > sw[i] = 0.0;
> > }
> > for (i = 0; i < (*nrow); i++) {
> > sw[f[i]] += w[i];
> > }
> >
> > var[0] = 0.0;
> > for (k = 0; k < (*ndim); k++) {
> > for (i = 0; i < (*nlev) ; i++) {
> > swx[i] = swxx[i] = 0.0;
> > }
> > for (i = k * (*nrow), j = 0; j < (*nrow); j++, i++) {
> > swx[f[j]] += w[j]*ord[i];
> > swxx[f[j]] += w[j]*ord[i]*ord[i];
> > }
> > for (j = 0; j < (*nlev); j++) {
> > if (sw[j] > 0)
> > var[0] += swxx[j] - swx[j]*swx[j]/sw[j];
> > }
> > }
> > return;
> > }
> >
> > (This is in C because calculations are repeated in each
> permutation,
> > and this really was slow in R.)
> >
> > So the key line is
> >
> > var[0] += swxx[j] - swx[j]*swx[j]/sw[j];
> >
> > where 'j' is the factor level (and summation is over all
> levels), sw
> > is the sum of weights (or n in level if weights = 1 or
> equal), swx is
> > the weighted sum, ans swxx the weighted sum of squares. So
> this is the
> > usual shortcut equation for getting centred SS or
> sum((x-mean(x))^2).
> > All items are indexed by the factor level 'j' and the var
> will get the
> > sum over factor levels. The same function also is used for
> the total
> > variance, but then there is only one class so that the
> function gives
> > the total SS in the data. This is the same way as done in oneway
> > ANOVA, and parallel (and comparable) to the way used in vectorfit.
> >
> > I hope this helps.
> >
> > Cheers, jazza
> > --
> > Jari Oksanen -- Dept Biology, Univ Oulu, 90014 Oulu,
> Finland Ph. +358
> > 85531526, cell +358 405136529, fax +358 85531061, skype
> jhoksane email
> > [log in to unmask], homepage http://cc.oulu.fi/~jarioksa/
> >
>
> Dr. Martin Henry H. Stevens, Assistant Professor
> 338 Pearson Hall
> Botany Department
> Miami University
> Oxford, OH 45056
>
> Office: (513) 529-4206
> Lab: (513) 529-4262
> FAX: (513) 529-4243
> http://www.cas.muohio.edu/~stevenmh/
> http://www.muohio.edu/ecology/
> http://www.muohio.edu/botany/
> "E Pluribus Unum"
|