Error in fitlm function
6 views (last 30 days)
Show older comments
I am using
reg = fitlm(X_train, Y_train);
But it gives following errors;
Error in fitlm (line 122)
model = LinearModel.fit(X,varargin{:});
I request you to kindly have a look on attached matlab code and input file and suggest me how to fix it.
Devender
test()
0 Comments
Accepted Answer
Walter Roberson
on 7 Mar 2024
Moved: Walter Roberson
on 7 Mar 2024
% Load data
df = readtable('input_file.csv','VariableNamingRule','preserve');
dependent_vars = {'act_y_ha', 'act_per_ha'};
for jj = 1:length(dependent_vars)
var = dependent_vars{jj};
v = {'POS value', 'MAX sum', 'AVG sum', 'SOS', 'POS', 'EOS', ...
'Base', 'Duration', 'First half', 'Second half', 'Growth rate', ...
'Senescence rate', 'Peaks'};
v_to_use = v(ismember(v,df.Properties.VariableNames));
new_T = df(:,v_to_use);
X = new_T;
Y = df{:, var};
% Split data
cv = cvpartition(size(X, 1), 'HoldOut', 0.2);
idxTrain = training(cv);
idxTest = test(cv);
X_train = X{idxTrain, :};
X_test = X{idxTest, :};
Y_train = Y(idxTrain);
Y_test = Y(idxTest);
reg = fitlm(X_train, Y_train, 'linear');
% Prediction
Y_pred = predict(reg, X_test);
% Evaluation
r2_reg = 1 - sum((Y_test - Y_pred).^2)/sum((Y_test - mean(Y_test)).^2);
rmse = sqrt(mean((Y_test - Y_pred).^2));
disp([ char(var), ' ', num2str(r2_reg,2), ' ', num2str(rmse,2)]);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Analysis of Variance and Covariance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!