Matlab中相关分析的代码
以下是一份用于执行相关分析的 Matlab 代码示例:
% 创建两个向量 x = [1, 2, 3, 4, 5]; y = [3, 5, 7, 9, 11];
% 计算相关系数 r = corrcoef(x, y); disp(['相关系数:', num2str(r(1, 2))]);
% 绘制散点图 scatter(x, y); xlabel('x'); ylabel('y'); title('相关分析'); grid on;
% 绘制回归线 p = polyfit(x, y, 1); hold on; plot(x, polyval(p, x), 'r--'); legend('数据点', '回归线'); hold off;
% 计算线性回归方程 disp(['回归方程:y = ', num2str(p(1)), 'x + ', num2str(p(2))]);
% 计算残差平方和和拟合优度 yfit = polyval(p, x); SSresid = sum((y - yfit).^2); SStotal = (length(y) - 1) * var(y); rsq = 1 - SSresid/SStotal; disp(['残差平方和:', num2str(SSresid)]); disp(['拟合优度:', num2str(rsq)])
原文地址: https://www.cveoy.top/t/topic/fdgH 著作权归作者所有。请勿转载和采集!