%% 问题二:基于灰色关联预测模型的急病就医需求预测
% 数据准备(基于问题一的结果和合理假设)
clear; clc; close all;
% 行政区名称
districts = {‘芙蓉区’, ‘天心区’, ‘岳麓区’, ‘开福区’, ‘雨花区’, ‘望城区’};
% 从问题一结果中提取的基础数据(早高峰时段)
demand_intensity = [17.50, 11.55, 10.61, 7.60, 9.58, 2.87]; % 需求强度
supply_capacity = [10.67, 10.27, 9.25, 7.30, 8.49, 6.40]; % 供给能力
supply_demand_ratio = [1.64, 1.12, 1.15, 1.04, 1.13, 0.45]; % 供需比
vulnerability_index = [0.07, 0.09, 0.11, 0.13, 0.12, 0.17]; % 医疗脆弱指数
% 假设的额外数据(基于公开数据和合理假设)
aging_rate = [12.5, 13.2, 14.1, 15.3, 13.8, 18.7]; % 老龄化率(%)
congestion_index = [8.7, 7.9, 7.2, 6.8, 7.5, 5.3]; % 交通拥堵指数(0-10)
population_density = [15200, 12300, 9800, 8600, 11200, 5400]; % 人口密度(人/平方公里)
% 假设的历史急病就医需求数据(基于医疗脆弱指数和人口密度生成)
% 这里使用模拟数据,实际应用中应使用真实数据
medical_demand_history = [
125, 118, 122, 130, 120, 135, 128; % 芙蓉区
98, 105, 110, 102, 108, 115, 100; % 天心区
85, 90, 88, 95, 92, 98, 87; % 岳麓区
75, 80, 78, 82, 85, 90, 77; % 开福区
92, 95, 98, 100, 96, 105, 94; % 雨花区
65, 70, 68, 72, 75, 80, 67 % 望城区
];
%% 灰色关联分析(确定主要影响因素)
fprintf(‘=== 灰色关联分析 ===\n’);
% 影响因素矩阵(每一行是一个行政区,每一列是一个因素)
factors = [demand_intensity’, supply_capacity’, supply_demand_ratio’, …
vulnerability_index’, aging_rate’, congestion_index’, population_density’];
% 参考序列(急病就医需求的历史平均值)
reference = mean(medical_demand_history, 2);
% 数据标准化
min_vals = min(factors);
max_vals = max(factors);
normalized_factors = (factors - min_vals) ./ (max_vals - min_vals);
min_ref = min(reference);
max_ref = max(reference);
normalized_ref = (reference - min_ref) ./ (max_ref - min_ref);
% 计算关联系数
rho = 0.5; % 分辨系数
correlation_coeff = zeros(size(factors));
for i = 1:size(factors, 1)
for j = 1:size(factors, 2)
delta = abs(normalized_ref(i) - normalized_factors(i, j));
min_delta = min(min(abs(normalized_ref - normalized_factors)));
max_delta = max(max(abs(normalized_ref - normalized_factors)));
correlation_coeff(i, j) = (min_delta + rho * max_delta) / (delta + rho * max_delta);
end
end
% 计算关联度
correlation_degree = mean(correlation_coeff);
% 显示关联度结果
factor_names = {‘需求强度’, ‘供给能力’, ‘供需比’, ‘脆弱指数’, ‘老龄化率’, ‘拥堵指数’, ‘人口密度’};
fprintf(‘各因素与急病就医需求的关联度:\n’);
for i = 1:length(factor_names)
fprintf(‘%s: %.4f\n’, factor_names{i}, correlation_degree(i));
end
% 选择关联度最高的3个因素作为预测模型的输入
[~, idx] = sort(correlation_degree, ‘descend’);
selected_factors = factor_names(idx(1:3));
fprintf(‘\n选择的主要影响因素: %s, %s, %s\n\n’, …
selected_factors{1}, selected_factors{2}, selected_factors{3});
%% 灰色预测模型 GM(1,N) - 多变量灰色模型
fprintf(‘=== 灰色预测模型 GM(1,N) ===\n’);
% 使用关联度最高的3个因素
X = factors(:, idx(1:3))‘;
Y = reference’;
% 1-AGO(累加生成)
n = length(Y);
X1 = cumsum(X, 2);
Y1 = cumsum(Y);
% 构造数据矩阵B和常数向量Yn
B = [-0.5*(Y1(1:n-1)+Y1(2:n)); X1(:, 2:n)‘]’;
Yn = Y(2:n)';
% 参数估计
params = pinv(B’*B)*B’*Yn;
a = params(1);
b = params(2:end);
fprintf(‘模型参数:\n’);
fprintf(‘a = %.4f\n’, a);
for i = 1:length(b)
fprintf(‘b%d = %.4f\n’, i, b(i));
end
% 模型求解
Y_pred = zeros(1, n);
Y_pred(1) = Y(1);
for k = 2:n
Y_pred(k) = (Y(1) - (bX1(:, k))/a) * exp(-a(k-1)) + (b*X1(:, k))/a;
end
% IAGO(累减生成)
Y_pred_final = [Y_pred(1), diff(Y_pred)];
% 预测下一个时间点的急病就医需求
next_X = [factors(:, idx(1:3))'; mean(factors(:, idx(1:3)), 2)]; % 添加下一期因素预测值
next_X1 = cumsum(next_X, 2);
next_k = n + 1;
next_Y_pred = (Y(1) - (bnext_X1(:, next_k))/a) * exp(-a(next_k-1)) + (b*next_X1(:, next_k))/a;
next_Y_pred_final = next_Y_pred - Y_pred(end);
fprintf(‘\n预测结果:\n’);
for i = 1:length(districts)
fprintf(‘%s: 历史平均需求 = %.2f, 模型拟合值 = %.2f\n’, …
districts{i}, reference(i), Y_pred_final(i));
end
fprintf(‘\n下一期急病就医需求预测:\n’);
for i = 1:length(districts)
fprintf(‘%s: %.2f\n’, districts{i}, next_Y_pred_final(i));
end
%% 模型评估
fprintf(‘\n=== 模型评估 ===\n’);
% 计算误差指标
errors = reference - Y_pred_final’;
mae = mean(abs(errors));
rmse = sqrt(mean(errors.^2));
mre = mean(abs(errors ./ reference)) * 100;
fprintf(‘平均绝对误差(MAE): %.4f\n’, mae);
fprintf(‘均方根误差(RMSE): %.4f\n’, rmse);
fprintf(‘平均相对误差(MRE): %.2f%%\n’, mre);
% 可视化结果
figure;
subplot(2,1,1);
bar([reference, Y_pred_final’]);
set(gca, ‘XTickLabel’, districts);
legend(‘历史平均需求’, ‘模型拟合值’);
title(‘各行政区急病就医需求对比’);
ylabel(‘需求数量’);
subplot(2,1,2);
bar(next_Y_pred_final);
set(gca, ‘XTickLabel’, districts);
title(‘下一期急病就医需求预测’);
ylabel(‘需求数量’);
% 关联度可视化
figure;
bar(correlation_degree);
set(gca, ‘XTick’, 1:length(factor_names), ‘XTickLabel’, factor_names);
xtickangle(45);
title(‘各因素与急病就医需求的关联度’);
ylabel(‘关联度’);
% 误差分析可视化
figure;
plot(1:length(districts), errors, ‘ro-’, ‘LineWidth’, 1.5);
set(gca, ‘XTick’, 1:length(districts), ‘XTickLabel’, districts);
xtickangle(45);
title(‘各行政区预测误差’);
ylabel(‘误差值’);
grid on;
%% 输出预测结果表格
fprintf(‘\n=== 预测结果汇总 ===\n’);
fprintf(‘行政区\t\t历史平均需求\t模型拟合值\t下一期预测\t误差\n’);
for i = 1:length(districts)
fprintf(‘%s\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n’, …
districts{i}, reference(i), Y_pred_final(i), next_Y_pred_final(i), errors(i));
end(错误使用 vertcat
要串联的数组的维度不一致。
出错 untitled7 (第 91 行)
B = [-0.5*(Y1(1:n-1)+Y1(2:n)); X1(:, 2:n)‘]’;)修改该代码,并给出修改后的完整代码