Matlab中的多元线性回归预测

Matlab中的多元线性回归预测,matlab,linear-regression,prediction,Matlab,Linear Regression,Prediction,我试图根据两个预测因子(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:

我试图根据两个预测因子(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 


您并没有预测要向前走48步。在预测模型中,我们尝试使用
x(t)
估计
y(t+n)
。因此,您需要将培训模型的
y
更改为
ytot(49:769)
。此外,时间序列预测模型中一种非常常见的技术是使用过去的多个步骤来预测未来的一个步骤。它就像:
[x(t)x(t-1)x(t-2)…]->y(t+49)]