Print R² Value
In this final step, we'll create a comprehensive program that reads data from a file, calculates the regression parameters, and prints the R² value with detailed interpretation.
First, create a sample data file:
cd ~/project
nano regression_data.txt
Add sample regression data:
1.0 2.0
2.0 4.0
3.0 5.0
4.0 4.0
5.0 5.0
Now, create the final R² calculation program:
nano r_squared_print.c
Enter the following code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Function to calculate linear regression parameters
void calculateRegressionParameters(double *x, double *y, int n,
double *slope, double *intercept) {
double sum_x = 0, sum_y = 0, sum_xy = 0, sum_x_squared = 0;
for (int i = 0; i < n; i++) {
sum_x += x[i];
sum_y += y[i];
sum_xy += x[i] * y[i];
sum_x_squared += x[i] * x[i];
}
// Calculate slope and intercept using least squares method
*slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x_squared - sum_x * sum_x);
*intercept = (sum_y - *slope * sum_x) / n;
}
// Function to compute R-squared
double computeRSquared(double *x, double *y, int n, double slope, double intercept) {
double total_variation = 0.0;
double explained_variation = 0.0;
double y_mean = 0.0;
// Calculate mean of y
for (int i = 0; i < n; i++) {
y_mean += y[i];
}
y_mean /= n;
// Compute variations
for (int i = 0; i < n; i++) {
total_variation += pow(y[i] - y_mean, 2);
double predicted_y = slope * x[i] + intercept;
explained_variation += pow(y[i] - predicted_y, 2);
}
// Calculate R-squared
return 1 - (explained_variation / total_variation);
}
// Function to interpret R-squared value
void interpretRSquared(double r_squared) {
printf("\nR² Interpretation:\n");
if (r_squared < 0.3) {
printf("Weak model fit: The model explains less than 30%% of the variance.\n");
} else if (r_squared < 0.5) {
printf("Moderate model fit: The model explains 30-50%% of the variance.\n");
} else if (r_squared < 0.7) {
printf("Good model fit: The model explains 50-70%% of the variance.\n");
} else {
printf("Excellent model fit: The model explains over 70%% of the variance.\n");
}
}
int main() {
FILE *file;
int n = 0, max_lines = 100;
double x[100], y[100];
double slope, intercept, r_squared;
// Open the data file
file = fopen("regression_data.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Read data from file
while (fscanf(file, "%lf %lf", &x[n], &y[n]) == 2) {
n++;
if (n >= max_lines) break;
}
fclose(file);
// Calculate regression parameters
calculateRegressionParameters(x, y, n, &slope, &intercept);
// Compute R-squared
r_squared = computeRSquared(x, y, n, slope, intercept);
// Print results
printf("Regression Analysis Results:\n");
printf("Number of Data Points: %d\n", n);
printf("Slope: %.4f\n", slope);
printf("Intercept: %.4f\n", intercept);
printf("R-squared (R²): %.4f\n", r_squared);
// Interpret R-squared
interpretRSquared(r_squared);
return 0;
}
Compile the program:
gcc -o r_squared_print r_squared_print.c -lm
Run the program:
./r_squared_print
Example output:
Regression Analysis Results:
Number of Data Points: 5
Slope: 0.6000
Intercept: 1.5000
R-squared (R²): 0.5600
R² Interpretation:
Good model fit: The model explains 50-70% of the variance.
Key points:
- Reads data from an external file
- Calculates regression parameters using least squares method
- Computes R² value
- Provides an interpretation of the R² value
- Helps understand the model's predictive power