double anova(double *vec, int len, int *xtypes, int numeg) // anova 1 but f statistic { int i, k ; double y1, top, bot, ftail ; double *w0, *w1, *popsize, *wmean ; static int ncall2 = 0 ; if (numeg >= len) fatalx("bad anova\n") ; ZALLOC(w0, len, double) ; ZALLOC(w1, len, double) ; ZALLOC(wmean, numeg, double) ; ZALLOC(popsize, numeg, double) ; y1 = asum(vec, len)/ (double) len ; // mean vsp(w0, vec, -y1, len) ; for (i=0; i<len; i++) { k = xtypes[i] ; ++popsize[k] ; wmean[k] += w0[i] ; } /* debug */ if (numeg == 2) { ++ncall2 ; for (i=0; i<len; ++i) { if (ncall2<0) break ; k = xtypes[i] ; // printf("yy %4d %4d %12.6f %12.6f\n", i, k, vec[i], w0[i]) ; } } vsp(popsize, popsize, 1.0e-12, numeg) ; vvd(wmean, wmean, popsize, numeg) ; vvt(w1, wmean, wmean, numeg) ; top = vdot(w1, popsize, numeg) ; for (i=0; i<len ; i++) { k = xtypes[i] ; w1[i] = w0[i] - wmean[k] ; } bot = asum2(w1, len) / (double) (len-numeg) ; bot *= (double) (numeg-1) ; ftail = rtlf(numeg-1, len-numeg, top/bot) ; free(w0) ; free(w1) ; free(popsize) ; free(wmean) ; return ftail ; }
double chitest(double *a, double *p, int n) /* a is n boxes. Goodness of fit test to p */ { double *x, *b, *pp ; double y1=0.0, y2=0.0 ; int i ; ZALLOC(pp, n, double) ; if (p != NULL) copyarr(p,pp,n) ; else vclear(pp, 1.0, n) ; y1 = asum(pp,n) ; y2 = asum(a,n) ; if ( (y1==0.0) || (y2==0.0) ) { free(pp) ; return 0.0 ; } ZALLOC(x,n,double) ; ZALLOC(b,n,double) ; vst (x, pp, y2/y1, n) ; /* expected */ vsp (x, x, .0001, n) ; vvm (b, a, x, n) ; vvt (b, b, b, n) ; vvd (b, b, x, n) ; y1 = asum(b,n) ; free(x) ; free(b) ; return y1 ; }