Engee documentation
Notebook

Forecasting the growth of the permanent population of the Russian Federation

In this example we will consider extrapolation of data using polynomials, their evaluation and display.

The data set for extrapolation is the total growth of the permanent population of the Russian Federation for the period from 1990 to 2023. Source: showcase of statistical data of Rosstat.

Let's connect the library XLSX.jl to get and process the data set.

In [ ]:
Pkg.add(["XLSX"])
In [ ]:
using XLSX;

Data for interpolation and extrapolation are given in the file Прирост населения РФ.xlsx. Let's create vectors of data for analysis - years of population growth calculation and population growth values.

In [ ]:
file = "$(@__DIR__)/Прирост населения РФ.xlsx";
years = vec(XLSX.readdata(file, "Отчет!A4:A33")); # Вектор строчных значений формата "xxxx г."
Growth = vec(Int64.(XLSX.readdata(file, "Отчет!C4:C33"))); # Вектор целых чисел

In the data table, the values of the years of population growth calculation are presented in a string format like "XXXX г.". This format is not suitable for calculations and display, so let's create a vector of years of population growth calculation expressed in integer format.

In [ ]:
Years = convert(Vector{Int64}, zeros(length(years))); # Вектор для получения значений 
i = 1
for year in years
    Years[i] = parse(Int64, year[1:4]) # Получаем из строки только первые 4 значения и преобразуем в Int64
    i += 1
end

Let us plot the obtained data on the graph.

In [ ]:
using Plots
plotlyjs()
plot(Years, Growth,
           shape = :circle, ms = 3,
           legend = :topright, label = "Исходные данные",
           ylims = (minimum(Growth)-5e4, maximum(Growth)+5e4),
           xlabel = "Год", ylabel = "Прирост населения, чел.",
           title = "Прирост населения РФ в 1990 - 2023 гг.")
Out[0]:

For data forecasting let's load and connect the library Polynomials.jl.

In [ ]:
Pkg.add("Polynomials")
using Polynomials;
   Resolving package versions...
  No Changes to `/user/.project/Project.toml`
  No Changes to `/user/.project/Manifest.toml`

For further extrapolation of data we will calculate polynomials - 1, 2, 3 and 6 orders:

In [ ]:
Numbers = Years.-1990;
pol1 = fit(Numbers,Growth,1);
pol2 = fit(Numbers,Growth,2);
pol3 = fit(Numbers,Growth,3);
pol6 = fit(Numbers,Growth,6);

Let us plot the polynomials and the original data.

In [ ]:
scatter(Years, Growth,
              label = "Исходные данные", legend = :topright,
              ylims = (minimum(Growth)-5e4, maximum(Growth)+5e4),
              xlabel = "Год", ylabel = "Прирост населения, чел.",
              title = "Прирост населения РФ в 1990 - 2023 гг.")
plot!(Years, pol1.(Numbers), label="Полином 1 пор.")
plot!(Years, pol2.(Numbers), label="Полином 2 пор.")
plot!(Years, pol3.(Numbers), label="Полином 3 пор.")
plot!(Years, pol6.(Numbers), label="Полином 6 пор.")
Out[0]:

Calculate extrapolation of data for 2024 and 2025.

In [ ]:
Extra_Years = [2024, 2025];
Extra_Numbers = [34, 35];
Extra_G_1 = (pol1.(Extra_Numbers));
Extra_G_2 = (pol2.(Extra_Numbers));
Extra_G_3 = (pol3.(Extra_Numbers));
Extra_G_6 = (pol6.(Extra_Numbers));
#Numbers = vcat(Numbers, Extra_Numbers);

Plot polynomials, initial and forecast data.

In [ ]:
Plots.scatter(Years, Growth,
              label = "Исходные данные", legend = :top,
              ylims = (minimum(Extra_G_3)-5e4, maximum(Extra_G_6)+5e4),
              xlabel = "Год", ylabel = "Прирост населения, чел.")
Plots.plot!(Years, pol1.(Numbers), label = "Полином 1 пор.")
Plots.plot!(Years, pol2.(Numbers), label = "Полином 2 пор.")
Plots.plot!(Years, pol3.(Numbers), label = "Полином 3 пор.")
Plots.plot!(Years, pol6.(Numbers), label = "Полином 6 пор.", color = :orange)
Plots.scatter!(Extra_Years, Extra_G_1, color = :red, shape = :xcross, label = "Экстраполяция 1 пор.")
Plots.scatter!(Extra_Years, Extra_G_2, color = :green, shape = :xcross, label = "Экстраполяция 2 пор.")
Plots.scatter!(Extra_Years, Extra_G_3, color = :violet, shape = :xcross, label = "Экстраполяция 3 пор.")
Plots.scatter!(Extra_Years, Extra_G_6, color = :orange, shape = :xcross, label = "Экстраполяция 6 пор.")
Out[0]:

As can be seen from the construction, the extrapolated data for different orders have a significant scatter of values.

Conclusion

In this example, we have looked at extrapolating data and plotting it for different orders of polynomials.