Error in fitlm function
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2024년 3월 7일
이동: Walter Roberson
2024년 3월 7일
% 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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Analysis of Variance and Covariance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!