Predict power consumption using linear regression

I want to predict power consumption per hour with this data using linear regression.
How can i do this?

 Accepted Answer

There are 89 days in the data, so the data ‘wrap’ to 24 hours.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1172328/data2022.csv', 'VariableNamingRule','preserve')
T1 = 2159×5 table
time power_consumption(MW) temperature(¡ÆC) humidity(%) insolation(MJ/m2) ____ _____________________ ________________ ___________ _________________ 1 808.08 3.3 66 0 2 776.95 3.2 59 0 3 753.25 3 62 0 4 750.78 2.5 61 0 5 753.73 2.2 67 0 6 768.75 2.2 68 0 7 790.86 1.5 71 0 8 801.18 0.9 74 0.01 9 760.35 2 71 0.21 10 631.42 6.3 52 0.91 11 562.01 7.7 42 1.5 12 524.11 8.5 41 1.93 13 503.51 8.2 40 2 14 527.51 8.7 42 1.83 15 554.49 8.2 40 1.41 16 636.05 7.5 42 0.56
VN = T1.Properties.VariableNames;
nrDays = nnz(T1.time == 24)
nrDays = 89
mdl = fitlm(T1.time, T1.('power_consumption(MW)'))
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ______ __________ (Intercept) 698.61 5.0312 138.86 0 x1 5.2509 0.35226 14.906 6.6857e-48 Number of observations: 2159, Error degrees of freedom: 2157 Root Mean Squared Error: 113 R-squared: 0.0934, Adjusted R-Squared: 0.093 F-statistic vs. constant model: 222, p-value = 6.69e-48
[y,yci] = predict(mdl, T1.time);
figure
plot(T1.time, T1.('power_consumption(MW)'), '.')
hold on
plot(T1.time, y, '-r')
plot(T1.time, yci, '--r')
hold off
grid
xlabel(VN{1})
ylabel(strrep(VN{2},'_','\_'))
.

4 Comments

Thank you for answer, but how can i use temperature, humidity and insolation to make linear regression model y~1+x1+x2+x3+x4?
As always, my pleasure!
Perhaps this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1172328/data2022.csv', 'VariableNamingRule','preserve')
T1 = 2159×5 table
time power_consumption(MW) temperature(¡ÆC) humidity(%) insolation(MJ/m2) ____ _____________________ ________________ ___________ _________________ 1 808.08 3.3 66 0 2 776.95 3.2 59 0 3 753.25 3 62 0 4 750.78 2.5 61 0 5 753.73 2.2 67 0 6 768.75 2.2 68 0 7 790.86 1.5 71 0 8 801.18 0.9 74 0.01 9 760.35 2 71 0.21 10 631.42 6.3 52 0.91 11 562.01 7.7 42 1.5 12 524.11 8.5 41 1.93 13 503.51 8.2 40 2 14 527.51 8.7 42 1.83 15 554.49 8.2 40 1.41 16 636.05 7.5 42 0.56
VN = T1.Properties.VariableNames;
nrDays = nnz(T1.time == 24)
nrDays = 89
mdl = fitlm(T1{:,[1 3:end]}, T1{:,2})
mdl =
Linear regression model: y ~ 1 + x1 + x2 + x3 + x4 Estimated Coefficients: Estimate SE tStat pValue ________ _______ _______ ___________ (Intercept) 845 8.1843 103.25 0 x1 6.6603 0.19058 34.948 2.3131e-212 x2 -12.007 0.36211 -33.16 3.6738e-195 x3 -0.50587 0.11485 -4.4045 1.1121e-05 x4 -92.248 2.3169 -39.815 2.7465e-260 Number of observations: 2159, Error degrees of freedom: 2154 Root Mean Squared Error: 60.3 R-squared: 0.743, Adjusted R-Squared: 0.743 F-statistic vs. constant model: 1.56e+03, p-value = 0
[y,yci] = predict(mdl, T1{1:24,[1 3:end]});
y = 24×1
778.6527 790.0547 797.5988 810.7686 817.9959 824.1503 837.6980 849.1226 825.6428 725.7094
yci = 24×2
773.2093 784.0961 784.5065 795.6030 792.4571 802.7405 805.6705 815.8667 813.1286 822.8632 819.4295 828.8710 832.6390 842.7571 843.5727 854.6725 820.7574 830.5282 721.6099 729.8089
figure
plot(T1.time, T1.('power_consumption(MW)'), '.')
hold on
plot(T1.time(1:24), y, '-r')
plot(T1.time(1:24), yci, '--r')
hold off
grid
xlabel(VN{1})
ylabel(strrep(VN{2},'_','\_'))
I am not certain what you want to do. This uses columns [1 3 4 5] to predict column 2, and only plots the first 24 hours because the plot looks extremely messy plotting all the data. (This is a linear regression, however it does not appear linear because of the independent predictor variables involved.)
Experiment to get the result you want.
.
As always, my pleasure!

Sign in to comment.

More Answers (1)

Hi,
You can use the function polyfit with x being the time and y being the power consumption, you will have to choose n to fit your data as you want. In your data, I guess when the time comes back to 1 it means it's another day ? Then you will need to change 1,2,3,..., 23 to 25,26,27,....47 for the second day and so on

Categories

Products

Release

R2022b

Asked:

Hkl
on 28 Oct 2022

Commented:

on 28 Oct 2022

Community Treasure Hunt

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

Start Hunting!