首页 > 解决方案 > 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)

标签: matlablinear-regressionprediction

解决方案


我看到在过去的几天里,您一直在努力训练预测模型。以下是使用线性回归训练此类模型的示例。在此示例中,前几个步骤的值用于预测提前 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 

在此处输入图像描述

在此处输入图像描述


推荐阅读