/* ---------------------------------------------------------------------------
MrAICM Utility.
written by C R Young
last updated 9/24/07
Copyright (C) 2007 C R Young
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
compile as:
gcc AICM.c -o AICM -lm
---------------------------------------------------------------------------*/
#include
#include
#include
#include
#define MAXSTR 80
#define MAXLINE 2000
#define MAXMODELS 10
#define MAXFIELD 100
#define MAXFILES 20
struct Model_quantities {
char name[MAXSTR]; // name of the model (e.g., "unconstrained")
char filelist[MAXFILES][MAXSTR]; // list of filenames for this model (e.g., "sym_16S_run1.p")
int Numfiles; // number of files for this model
int MCMCdraws[MAXFILES]; // number of MCMC draws in each file (calculated on file read)
int burnin; // burnin specified for this model
int thin; // thinning interval for this model
double data[MAXFILES][MAXLINE]; // actual loglikelihood scores
int totaldraws; // sum of the MCMC draws across all files minus the burnin
int Alldraws; // sum of the MCMC draws across all files
double dhat; // effefctive number of model parameters
double mean_loglik; // mean of the loglikelihood scores
double var_loglik; // variance of the loglikelihood scores
double skew_loglik; // skewedness of the loglikelihood scores
double kurt_loglik; // kurtosis of the loglikelihood scores
double AICM; // MCMC estimate of AIC
double SE_AICM; // Standard error of AICM (assuming approximately independent MCMC draws!!!!)
double harmonicmean; // Harmonic mean of the raw likelihood scores
double AkaikeWeight;
double MaxLogL;
};
/* My error message */
void MyError(error_text)
char error_text[];
{
fprintf(stderr,"\nRY's run-time error...\n");
fprintf(stderr,"\n\t%s\n",error_text);
fprintf(stderr,"\n...now exiting to system...\n\n");
exit(1);
}
void Moment(int model, struct Model_quantities ModelData[])
{
//void MyError(char *error_text);
int j, n, file, line;
double ep=0.0, s, p, ave, var, adev, sdev, skew, kurt;
n = ModelData[model].totaldraws;
if(n <= 1) MyError("Error in Moment(): n can't be less than 2 to compute moments.\n");
// Find the mean
s = 0.0;
for(file=0; file 2 || argc < 2) {
printf("usage: MrAICM [infile]\n");
exit(1);
}
/* Read file list */
Files = fopen(argv[1], "r");
if (!Files)
{
printf ( "\nCould not find files.txt...\n\n");
MyError("No files specified.\n");
} else {
printf("\nReading file list...\n");
fscanf(Files,"%d", &Nummodels); // Read the number of hypotheses to be compared
printf("%d models to compare.\n", Nummodels);
if(Nummodels > MAXMODELS) MyError("Too many models to compare. Increase MAXMODELS.\n");
for(model=0; model MAXFILES) {
printf ( "\nToo many files for model %d\n\n", i);
MyError("Too many files. Increase MAXFILES.\n");
}
fscanf(Files,"%d %d", &ModelData[model].burnin, &ModelData[model].thin); // Read burnin and thinning interval for each model
printf("Burnin (applied to each file) is %d for model %d.\n", ModelData[model].burnin, model+1);
printf("Thinning interval (applied to each file) is %d for model %d.\n\n", ModelData[model].thin, model+1);
}
printf ("\n");
for(model=0; model-1; i--){
ungetc(check[i],currentfile);
}
if(end == 0) {
countline += 1; // count these lines that are not stored
}
}
}
} /* END IF ENDFILE */
} /* END WHILE ENDFILE */
ModelData[model].MCMCdraws[file] = countsample - 1;
printf("Number of draws in MCMC file: %d\n", countline - 1);
printf("Number of draws after thinning and burnin: %d\n", ModelData[model].MCMCdraws[file]);
/* Close all files */
if(fclose(currentfile) != 0) MyError("Could not close file: INFILE.\n");
} // END file loop
ModelData[model].totaldraws = 0;
for(file=0; file