Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAX 100
- int main(){
- int sum = 0;
- int x[MAX], y[MAX];
- double mx=0.0, my=0.0;
- int i = 0;
- char filename[80];
- printf("Regression Analysis Calculator\n");
- printf("==============================\n");
- printf("Enter the name of the data file: "); scanf("%s",filename);
- FILE * f;
- f = fopen(filename, "r");
- if (f == NULL){
- printf("Cannot open file\n");
- return;
- }
- while (!feof(f)){
- fscanf(f,"%d",&x[sum]);
- fscanf(f,"%d",&y[sum]);
- mx += x[sum]; my +=y[sum];
- sum++;
- }
- mx /= n;
- my /= n;
- fclose(f);
- // find slope(t)
- float t = 0.0;
- float ts =0.0, ms = 0.0;
- for(i=0; i<sum; i++){
- ts += (x[i] - mx)*(y[i] - my);
- ms += (x[i] - mx)*(x[i] -mx);
- }
- t = ts/ms;
- // y-intercept (b)
- float b = my - t*mx;
- // find sx and sy
- float sx = 0.0, sy=0.0;
- for(i=0; i<sum; i++){
- sx += x[i]*x[i];
- sy += y[i]*y[i];
- }
- // d : standard deviation
- float dx = ( sx / n ) - mx*mx;
- float dy = ( sy / n ) - my*my;
- // correlation coefficient : r = { [ (x1y1) + (x2y2) + ... + (xnyn) ] / n - mx my} / [ dx dy ]
- float r = 0.0;
- float p1 = 0.0;
- for(i=0; i<sum; i++){
- p1 += x[i]*y[i];
- }
- r = (p1/sum - mx*my) / (dx*dy);
- printf("The number of data values read from this file was %d\n", sum);
- printf("The slope of the least squares fit is : %f",t);
- printf("The y-intercept of the least squares fit is : %f",b);
- printf("The correlation coefficient is: %f", r);
- }
Advertisement
Add Comment
Please, Sign In to add comment