Solely trying on the graphs we are able to see that the tendency is clearly optimistic, however how a lot?, do we are going to reside without end some day?, there’s a huge distinction between women and men?. For answering these and all different questions that I’ve in my thoughts we’re going to use a linear regression mannequin.
I’ve calculated it with sklearn like this:
from sklearn.linear_model import LinearRegression#probamos el caso especifico de España para la linear regression
pais="Spain"
spain_df = df[df['COUNTRY'] == pais]
spain_df = spain_df[spain_df['SEX'] == "_T"]
X = spain_df["YEAR"].values.reshape(-1, 1) # Reshape X
y = spain_df["LIFE"].values
reg = LinearRegression().match(X, y)
print("X and y rating is", r2_score(y, reg.predict(X)))
a = reg.coef_[0] # a
b = reg.intercept_ # b
print("The linear equation is: y =", a, "x +", b)
With that we get for Spain the outcomes that X and y have a r2_score of 0.9611, so the mannequin is actually good for this particular case. The road that we get is:
y_spain = 0.261628x — 443.9572
With this values we all know that Spain annually that passes have 0.2616 life expectacy, and we additionally know that within the yr 0, Spain would have -443.95 life expectacy, so we’re actually positive that previously the road have to be with much less slope, since is not possible for somebody to reside -443.95 years, and the life expectacy ought to at all times be better than 25(or so). In any case, we’re going to do the calculations for all of the international locations and enter them in a brand new dataframe.
paises= df["COUNTRY"].valuesfor i in (df["COUNTRY"].distinctive()):
df2=df[df["COUNTRY"]==i]
X = df2["YEAR"].values.reshape(-1, 1) # Remodelamos X a una matriz de una columna
y = df2["LIFE"].values
reg = LinearRegression().match(X, y)
df_reg.loc[i] = [i, reg.coef_[0], reg.intercept_]
df_reg
On this new dataframe with columns title, m(slope) and n.
We make the imply of all international locations and we get the worth of 0.3168, so from 1950 to 2023 annually that goes by, individuals on the planet reside 0.3168 years longer(0.3069 within the case of males and 0.32715 within the case of ladies(so distinction between women and men it’s not actually huge).
Now we are able to take a better look within the nation with the slowest and the best slope, and we discover that the nation with much less slope is Lesotho with 0.012374(Nauru for males with 0.000693 and Lesotho for girls with 0.000452).