% ========================================================================= % MATLAB Script: GDP vs. Happiness Score Analysis (Base MATLAB Only) % Dataset: Countries_Income_GDP_Happy_Data-1.csv (N = 111 countries) % Note: Uses strictly Base MATLAB functions — NO add-on toolboxes required. % ========================================================================= clear; clc; close all; %% 1. Load Dataset (Base MATLAB Core Function) filename = 'Countries_Income_GDP_Happy_Data-1.csv'; opts = detectImportOptions(filename); data = readtable(filename, opts); % Extract selected features x = data.GDP; % Independent variable: GDP Index y = data.happyScore; % Dependent variable: Happiness Score N = length(x); % Sample size (N = 111) %% 2. Calculate Summary Statistics (Base MATLAB) mean_x = mean(x); std_x = std(x); min_x = min(x); max_x = max(x); mean_y = mean(y); std_y = std(y); min_y = min(y); max_y = max(y); %% 3. Pearson Correlation & Linear Regression (Base MATLAB) [R, P] = corrcoef(x, y); r = R(1, 2); % Pearson correlation coefficient p_val_corr = P(1, 2); % Correlation p-value from corrcoef r2 = r^2; % Coefficient of determination (R-squared) % Linear regression coefficients (y = slope * x + intercept) p = polyfit(x, y, 1); slope = p(1); intercept = p(2); %% 4. Hypothesis Testing (Base MATLAB — without Statistics Toolbox 'tinv') % H0: rho = 0 (No linear correlation) vs H1: rho != 0 df = N - 2; % Degrees of freedom (109) t_stat = (r * sqrt(df)) / sqrt(1 - r2); % Calculated t-statistic alpha = 0.05; % Significance level % Base MATLAB calculation of critical value without Statistics Toolbox: % Uses inverse error function (erfinv) and Cornish-Fisher expansion z_crit = sqrt(2) * erfinv(1 - alpha); % Base normal quantile (~1.960) t_crit = z_crit + (z_crit^3 + z_crit)/(4*df); % Adjusted t critical value (~1.982) % Base MATLAB p-value approximation using complementary error function (erfc) p_val = erfc(abs(t_stat) / sqrt(2)); %% 5. Display Analytical Results in Command Window fprintf('=========================================================\n'); fprintf(' BASE MATLAB ANALYSIS: GDP vs. HAPPINESS SCORE (No Toolkits)\n'); fprintf('=========================================================\n'); fprintf('Sample Size (N) : %d countries\n\n', N); fprintf('--- Descriptive Statistics ---\n'); fprintf('GDP Index (X) : Mean = %.4f, Std = %.4f, Min = %.4f, Max = %.4f\n', ... mean_x, std_x, min_x, max_x); fprintf('Happiness Score (Y) : Mean = %.4f, Std = %.4f, Min = %.4f, Max = %.4f\n\n', ... mean_y, std_y, min_y, max_y); fprintf('--- Correlation & Regression ---\n'); fprintf('Pearson Correlation (r) : %.4f\n', r); fprintf('R-squared (R^2) : %.4f (%.2f%% of variance explained)\n', r2, r2*100); fprintf('Regression Line Equation : y = %.4f * x + %.4f\n\n', slope, intercept); fprintf('--- Hypothesis Testing (H0: rho = 0 at alpha = 0.05) ---\n'); fprintf('Degrees of Freedom (df) : %d\n', df); fprintf('Calculated t-statistic : %.4f\n', t_stat); fprintf('Critical t-value (Base MATLAB): ±%.4f\n', t_crit); fprintf('P-value (erfc approx) : %.4e\n', p_val); if abs(t_stat) > t_crit fprintf('Decision : REJECT H0 (Statistically Significant)\n'); fprintf('Conclusion : A strong, positive correlation exists between GDP and Happiness.\n'); else fprintf('Decision : FAIL TO REJECT H0\n'); end fprintf('=========================================================\n\n'); %% 6. Scatter Plot & Visualization (Base MATLAB Graphics) figure('Name', 'GDP vs Happiness Analysis', 'Color', 'w'); % Scatter plot of data points scatter(x, y, 55, [0 0.4470 0.7410], 'filled', 'MarkerFaceAlpha', 0.8); hold on; % Linear trendline x_fit = linspace(min_x, max_x, 200); y_fit = polyval(p, x_fit); plot(x_fit, y_fit, 'r-', 'LineWidth', 2.2); % Formatting chart elements (Title, X-label, Y-label set to black) title('Country GDP per Capita vs. Happiness Score', 'FontSize', 13, 'FontWeight', 'bold', 'Color', 'k'); xlabel('GDP Index (X)', 'FontSize', 11, 'FontWeight', 'bold', 'Color', 'k'); ylabel('Happiness Score (0-10) (Y)', 'FontSize', 11, 'FontWeight', 'bold', 'Color', 'k'); % Set axis ticks and frame lines to black ax = gca; ax.XColor = 'k'; ax.YColor = 'k'; grid on; box on; % Legend (Default MATLAB color — NOT forced to black) legend('Countries (N = 111)', sprintf('Fit Line: y = %.4fx + %.4f', slope, intercept), ... 'Location', 'northwest', 'FontSize', 10); % Statistical annotation box (text set to black) stats_str = {sprintf('r = %.4f', r), ... sprintf('R^2 = %.4f', r2), ... sprintf('t(%d) = %.2f', df, t_stat), ... 'p < 0.0001'}; text(0.70, 0.20, stats_str, 'Units', 'normalized', 'FontSize', 10, 'Color', 'k', ... 'BackgroundColor', [0.95 0.95 0.95], 'EdgeColor', [0.7 0.7 0.7], 'Margin', 8); hold off;