matlab - Matlab中的多元线性回归预测
问题描述
我正在尝试基于两个预测变量 (X) 来预测能量输出 (y)。我总共有 7034 个样本(Xtot 和 ytot),对应近 73 天的记录。
我在数据中选择了一周。
然后,我使用 fitlm 创建 MLR 模型。
继续进行预测。
这是正确的吗?这是应该用来获得提前 48 步预测的方式吗?
谢谢!
Xtot = dadosPVPREV(2:3,:);%predictors
ytot = dadosPVPREV(1,:);%variable to be predicted
Xtot = Xtot';
ytot = ytot';
X = Xtot(1:720,:);%period into consideration - predictors
y = ytot(1:720,:);%period into considaration - variable to be predicted
lmModel = fitlm(X, y, 'linear', 'RobustOpts', 'on'); %MLR fit
Xnew = Xtot(720:769,:); %new predictors of the y
ypred = predict(lmModel, Xnew); %predicted values of y
yreal = ytot(720:769); %real values of the variable to be predicted
RMSE = sqrt(mean((yreal-ypred).^2)); %calculation of the error between the predicted and real values
figure; plot(ypred);hold; plot(yreal)
解决方案
我看到在过去的几天里,您一直在努力训练预测模型。以下是使用线性回归训练此类模型的示例。在此示例中,前几个步骤的值用于预测提前 5 步。Mackey-Glass 函数用作训练模型的数据集。
close all; clc; clear variables;
load mgdata.dat; % importing Mackey-Glass dataset
T = mgdata(:, 1); % time steps
X1 = mgdata(:, 2); % 1st predictor
X2 = flipud(mgdata(:, 2)); % 2nd predictor
Y = ((sin(X1).^2).*(cos(X2).^2)).^.5; % response
to_x = [-21 -13 -8 -5 -3 -2 -1 0]; % time offsets in the past, used for predictors
to_y = +3; % time offset in the future, used for reponse
T_trn = ((max(-to_x)+1):700)'; % time slice used to train model
i_x_trn = bsxfun(@plus, T_trn, to_x); % indices of steps used to construct train data
X_trn = [X1(i_x_trn) X2(i_x_trn)]; % train data set
Y_trn = Y(T_trn+to_y); % train responses
T_tst = (701:(max(T)-to_y))'; % time slice used to test model
i_x_tst = bsxfun(@plus, T_tst, to_x); % indices of steps used to construct train data
X_tst = [X1(i_x_tst) X2(i_x_tst)]; % test data set
Y_tst = Y(T_tst+to_y); % test responses
mdl = fitlm(X_trn, Y_trn) % training model
Y2_trn = feval(mdl, X_trn); % evaluating train responses
Y2_tst = feval(mdl, X_tst); % evaluating test responses
e_trn = mse(Y_trn, Y2_trn) % train error
e_tst = mse(Y_tst, Y2_tst) % test error
此外,在某些模型中使用数据转换技术生成新特征可以减少预测误差:
featGen = @(x) [x x.^2 sin(x) exp(x) log(x)]; % feature generator
mdl = fitlm(featGen(X_trn), Y_trn)
Y2_trn = feval(mdl, featGen(X_trn)); % evaluating train responses
Y2_tst = feval(mdl, featGen(X_tst)); % evaluating test responses
推荐阅读
- java - java BufferedReader & Writer 不能按预期工作
- python - 为什么python在算法中只打印一个数据集?
- r - 在 lapply 中使用特定列计算多个回归分析
- python - 我正在尝试使用 python 从 postgressql 中的列中提取一个值。但我总是得到这个错误:
- typescript - 角度反应控制值未正确更新
- c++ - std::vector 中的 C++ 外部类(定义格式错误)
- react-native - 关于 react-native 应用程序中 pptx 文件的问题
- python - 将数据从 Django 发送到另一台服务器
- xml - XSD 验证错误。“发现问题始于:属性”
- python - PyQt5 过滤数据框