Error in fitlm function

3 views (last 30 days)
Devendra
Devendra on 7 Mar 2024
Moved: Walter Roberson on 7 Mar 2024
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()
df = 100×20 table
Var1 POS value MAX sum AVG sum SOS POS EOS Base Duration First half Second half Growth rate Senescence rate Peaks total_cane act_y_ha act_per_ha field_yield total_FS variety ____ _________ _______ _______ ____ ___ ___ ______ ________ __________ ___________ ___________ _______________ _____ __________ ________ __________ ___________ ________ _____________ 0 3.1771 149.35 92.117 79 334 729 2.9793 650 255 395 2.6824 -1.5491 2 8719 1026 118.7 11.57 1008.8 {'NCO334' } 1 3.9655 180.34 122.08 19 229 729 3.8176 710 210 500 3.6809 -2.633 3 23962 1653 188.9 11.43 2739 {'CO740' } 2 3.3081 156.67 103.54 99 334 729 3.0564 630 235 395 2.8323 -1.408 1 28828 1386 159 11.47 3307.5 {'NCO334' } 3 3.4482 139.52 85.993 39 229 759 3.2743 720 190 530 2.5075 -2.7583 1 6108 364 31.7 8.72 532.6 {'CO740' } 4 3.8306 176.85 119.2 19 294 729 3.6499 710 275 435 3.5651 -2.2468 2 44705 1739 208.8 12 5365.5 {'NCO334' } 5 3.4701 145.87 111.34 99 319 699 3.2575 600 220 380 3.0761 -1.0609 1 4604 2093 233.1 11.14 512.9 {'Mex54-245'} 6 3.733 196.27 145.09 39 294 789 3.5574 750 255 495 3.2482 -3.4094 3 38435 2159 267.6 12.39 4762.6 {'NCO334' } 7 3.0975 167.82 123.67 -236 114 454 2.8192 690 350 340 2.5781 -2.4782 2 7567 1182 117.6 9.95 752.9 {'Mex54-245'} 8 3.2312 150.91 119.68 129 334 729 2.9267 600 205 395 1.8209 -1.4896 1 5698 1266 143 11.29 643.3 {'B41227' } 9 3.6544 188.29 123.56 79 294 759 3.4433 680 215 465 3.3051 -2.348 3 21706 1632 203.4 12.47 2705.7 {'NCO334' } 10 3.8049 167.1 119.96 129 319 729 3.5039 600 190 410 2.1563 -2.0176 3 5500 1618 189.9 11.74 645.7 {'Mex54-245'} 11 3.7917 162.24 113.38 99 324 699 3.6036 600 225 375 3.4075 -1.1251 2 28625 2157 248.2 11.51 3293.8 {'Mex54-245'} 12 2.6391 109.44 92.363 129 334 669 2.3069 540 205 335 0.48525 -0.70277 1 6729 1373 144.7 10.54 709.1 {'C86/56' } 13 3.525 162.31 123.12 129 229 759 3.2314 630 100 530 1.9618 -1.8762 2 12643 1642 191.3 11.65 1472.7 {'NCO334' } 14 3.3622 168.53 130.99 129 334 759 3.0431 630 205 425 1.839 -1.7134 3 12237 1654 199.5 12.07 1476.5 {'B52298' } 15 4.3618 184.75 134.22 19 294 759 4.1654 740 275 465 3.3812 -3.3908 2 28287 1911 222.7 11.65 3295.8 {'B52298' }
X = 100×13 table
POS value MAX sum AVG sum SOS POS EOS Base Duration First half Second half Growth rate Senescence rate Peaks _________ _______ _______ ____ ___ ___ ______ ________ __________ ___________ ___________ _______________ _____ 3.1771 149.35 92.117 79 334 729 2.9793 650 255 395 2.6824 -1.5491 2 3.9655 180.34 122.08 19 229 729 3.8176 710 210 500 3.6809 -2.633 3 3.3081 156.67 103.54 99 334 729 3.0564 630 235 395 2.8323 -1.408 1 3.4482 139.52 85.993 39 229 759 3.2743 720 190 530 2.5075 -2.7583 1 3.8306 176.85 119.2 19 294 729 3.6499 710 275 435 3.5651 -2.2468 2 3.4701 145.87 111.34 99 319 699 3.2575 600 220 380 3.0761 -1.0609 1 3.733 196.27 145.09 39 294 789 3.5574 750 255 495 3.2482 -3.4094 3 3.0975 167.82 123.67 -236 114 454 2.8192 690 350 340 2.5781 -2.4782 2 3.2312 150.91 119.68 129 334 729 2.9267 600 205 395 1.8209 -1.4896 1 3.6544 188.29 123.56 79 294 759 3.4433 680 215 465 3.3051 -2.348 3 3.8049 167.1 119.96 129 319 729 3.5039 600 190 410 2.1563 -2.0176 3 3.7917 162.24 113.38 99 324 699 3.6036 600 225 375 3.4075 -1.1251 2 2.6391 109.44 92.363 129 334 669 2.3069 540 205 335 0.48525 -0.70277 1 3.525 162.31 123.12 129 229 759 3.2314 630 100 530 1.9618 -1.8762 2 3.3622 168.53 130.99 129 334 759 3.0431 630 205 425 1.839 -1.7134 3 4.3618 184.75 134.22 19 294 759 4.1654 740 275 465 3.3812 -3.3908 2
Y = 100×1
1026 1653 1386 364 1739 2093 2159 1182 1266 1632
Error using classreg.regr.TermsRegression.createFormula
The terms matrix must have one column for each variable in the dataset or table. You may not specify the response as a separate vector with a dataset or table.

Error in LinearModel.createFormula (line 1076)
formula = classreg.regr.TermsRegression.createFormula(supplied,modelDef, ...

Error in LinearModel.fit (line 1058)
model.Formula = LinearModel.createFormula(supplied,modelDef,X, ...

Error in fitlm (line 122)
model = LinearModel.fit(X,varargin{:});

Error in test (line 27)
reg = fitlm(X_train, Y_train);

Accepted Answer

Walter Roberson
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
Warning: Regression design matrix is rank deficient to within machine precision.
act_y_ha 0.66 2.3e+02
Warning: Regression design matrix is rank deficient to within machine precision.
act_per_ha 0.6 32

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!