Engee documentation
Notebook

Forecasting the growth of the permanent population of the Russian Federation

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

The data set for extrapolation is the total increase in the permanent population of the Russian Federation for the period from 1990 to 2023. Source: [Rosstat statistical data showcase](https://showdata.gks.ru/finder /).

Connecting the library XLSX.jl to receive and process a set of data.

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

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

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 are presented in a lowercase format like "XXXX г.". This format is not suitable for calculations and display, so we will create a vector of years for calculating population growth, 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's plot the data obtained on a 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]:

To predict the data, download 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 the data, we calculate the 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's plot the graphs of the polynomials and the initial 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]:

Let's calculate the 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);

Let's plot the graphs of the polynomials, the 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 range of values.

Conclusion

In this example, we examined the extrapolation of data and their construction for various orders of polynomials.