% 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