Regression with standard errors (fitlm) Open ↗
Code
% Simulated house prices (not real data): price depends on size and age, plus noise
rng(12)
n = 120;
size_m2 = 50 + 150*rand(n, 1);
age = round(60*rand(n, 1));
price = 40 + 2.5*size_m2 - 0.8*age + 25*randn(n, 1); % thousands
T = struct('price', price, 'size_m2', size_m2, 'age', age);
mdl = fitlm(T, 'price ~ size_m2 + age') % coefficient table in the console
subplot(1, 2, 1)
scatter(size_m2, price, 18, age, 'filled'), colorbar
xlabel('size (m²)'), ylabel('price (k)'), title('Price by size, coloured by age')
subplot(1, 2, 2)
scatter(mdl.Fitted, mdl.Residuals, 14, 'filled'), yline(0)
xlabel('fitted'), ylabel('residual')
title(sprintf('R^2 = %.3f, +%.2f k per m² (SE %.2f)', mdl.Rsquared.Ordinary, mdl.Coefficients.Estimate(2), mdl.Coefficients.SE(2)))