How do I predict from a linear regression function in matlab?

조회 수: 13 (최근 30일)
Hal
Hal 2020년 11월 4일
So im asked to predict how much energy consumed by light. I have variables in my file and had done a linear regression on Matlab and generated a function. So i tried calling that function, then inputed the output in another table, then did a predict(). but it is not working :'(
I need help lol
The function:
function [trainedModel, validationRMSE] = Model1Light(trainingData)
% [trainedModel, validationRMSE] = trainRegressionModel(trainingData)
% Returns a trained regression model and its RMSE. This code recreates the
% model trained in Regression Learner app. Use the generated code to
% automate training the same model with new data, or to learn how to
% programmatically train models.
%
% Input:
% trainingData: A table containing the same predictor and response
% columns as those imported into the app.
%
% Output:
% trainedModel: A struct containing the trained regression model. The
% struct contains various fields with information about the trained
% model.
%
% trainedModel.predictFcn: A function to make predictions on new data.
%
% validationRMSE: A double containing the RMSE. In the app, the
% History list displays the RMSE for each model.
%
% Use the code to train the model with new data. To retrain your model,
% call the function from the command line with your original data or new
% data as the input argument trainingData.
%
% For example, to retrain a regression model trained with the original data
% set T, enter:
% [trainedModel, validationRMSE] = trainRegressionModel(T)
%
% To make predictions with the returned 'trainedModel' on new data T2, use
% yfit = trainedModel.predictFcn(T2)
%
% T2 must be a table containing at least the same predictor columns as used
% during training. For details, enter:
% trainedModel.HowToPredict
% Auto-generated by MATLAB on 03-Nov-2020 22:03:24
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'Morning', 'Evening', 'T1', 'T4', 'RH_4', 'RH_7', 'T8', 'T9'};
predictors = inputTable(:, predictorNames);
response = inputTable.lights;
isCategoricalPredictor = [false, false, false, false, false, false, false, false];
% Train a regression model
% This code specifies all the model options and trains the model.
concatenatedPredictorsAndResponse = predictors;
concatenatedPredictorsAndResponse.lights = response;
linearModel = fitlm(...
concatenatedPredictorsAndResponse, ...
'linear', ...
'RobustOpts', 'off');
% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
linearModelPredictFcn = @(x) predict(linearModel, x);
trainedModel.predictFcn = @(x) linearModelPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedModel.RequiredVariables = {'Evening', 'Morning', 'RH_4', 'RH_7', 'T1', 'T4', 'T8', 'T9'};
trainedModel.LinearModel = linearModel;
trainedModel.About = 'This struct is a trained model exported from Regression Learner R2020a.';
trainedModel.HowToPredict = sprintf('To make predictions on a new table, T, use: \n yfit = c.predictFcn(T) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nThe table, T, must contain the variables returned by: \n c.RequiredVariables \nVariable formats (e.g. matrix/vector, datatype) must match the original training data. \nAdditional variables are ignored. \n \nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appregression_exportmodeltoworkspace'')">How to predict using an exported model</a>.');
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'Morning', 'Evening', 'T1', 'T4', 'RH_4', 'RH_7', 'T8', 'T9'};
predictors = inputTable(:, predictorNames);
response = inputTable.lights;
isCategoricalPredictor = [false, false, false, false, false, false, false, false];
validationPredictFcn = @(x) linearModelPredictFcn(x);
% Compute resubstitution predictions
validationPredictions = validationPredictFcn(predictors);
% Compute validation RMSE
isNotMissing = ~isnan(validationPredictions) & ~isnan(response);
validationRMSE = sqrt(nansum(( validationPredictions - response ).^2) / numel(response(isNotMissing) ));

답변 (0개)

카테고리

Help CenterFile Exchange에서 Multiple Linear Regression에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by