Regresión Lineal Simple

Para este ejercicio utilizaremos la base de datos de indicadores sociodemográficos de todo el mundo, organizada por quinquenios. La fuente original de los datos es el proyecto Gapminder

https://www.gapminder.org/data/

Consideraremos las siguientes variables

Contexto

Evolución de la tasa de fertilidad femenina (1800-2020)

Evolución de la expectativa de vida femenina (1800-2020)

Carga de datos


load("dataWorld_q.rda") 

names(dataWorld_q)
##  [1] "country"    "quinq"      "tfr"        "yearSchF"   "contracep" 
##  [6] "age1mar"    "sanitat"    "water"      "birthSkill" "childMort" 
## [11] "deathRate"  "extPov"     "famWorkFem" "femWork"    "incomePp"  
## [16] "income10p"  "gini"       "lifExpFem"  "lifExpTot"  "maleWork"  
## [21] "materMort"  "vaccMeas"   "schGenEq"   "doctor"     "teenFert"

Desarrollo

Primer paso: Selecciono mis casos


table(dataWorld_q$quinq)
## 
## 1950-1954 1955-1959 1960-1964 1965-1969 1970-1974 1975-1979 1980-1984 1985-1989 
##       194       194       194       194       194       194       194       194 
## 1990-1994 1995-1999 2000-2004 2005-2009 2010-2014 2015-2019     2020+ 
##       194       194       194       194       194       194         0


data2 <- subset(dataWorld_q, quinq == "1995-1999" )

Segundo paso: análisis gráfico y cálculo de correlaciones


plot(data2$tfr, data2$lifExpFem, pch=19, col= "black")


Tercer paso: calculo de correlación y análisis de regresión


cor.test(data2$lifExpFem, data2$tfr)
## 
##  Pearson's product-moment correlation
## 
## data:  data2$lifExpFem and data2$tfr
## t = -24.457, df = 182, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.9055724 -0.8369750
## sample estimates:
##        cor 
## -0.8756182

modelo1 <- lm(lifExpFem ~ tfr, data = data2)
summary(modelo1)
## 
## Call:
## lm(formula = lifExpFem ~ tfr, data = data2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.4436  -2.8010   0.5704   3.3742  15.7574 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  86.3094     0.8347  103.40   <2e-16 ***
## tfr          -5.1315     0.2098  -24.46   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.154 on 182 degrees of freedom
##   (10 observations deleted due to missingness)
## Multiple R-squared:  0.7667, Adjusted R-squared:  0.7654 
## F-statistic: 598.1 on 1 and 182 DF,  p-value: < 2.2e-16


Ejercicio propuesto

Calcular modelo 2 con variable independiente años promedio de educación “yearSchF”


cor.test(data2$lifExpFem, data2$yearSchF)
## 
##  Pearson's product-moment correlation
## 
## data:  data2$lifExpFem and data2$yearSchF
## t = 17.957, df = 171, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7495750 0.8545086
## sample estimates:
##       cor 
## 0.8083698


modelo2 <- lm(lifExpFem ~yearSchF, data = data2)


summary(modelo2)
## 
## Call:
## lm(formula = lifExpFem ~ yearSchF, data = data2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -18.0673  -4.1456   0.6799   4.5076  12.5367 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  50.0708     1.1085   45.17   <2e-16 ***
## yearSchF      2.4080     0.1341   17.96   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.336 on 171 degrees of freedom
##   (21 observations deleted due to missingness)
## Multiple R-squared:  0.6535, Adjusted R-squared:  0.6514 
## F-statistic: 322.5 on 1 and 171 DF,  p-value: < 2.2e-16

Estimación puntual


new.data <- data.frame(tfr = 4)
predict(modelo1 , new.data, type = "response")
##        1 
## 65.78326


new.data <- data.frame(yearSchF = 10)
predict(modelo2 , new.data, type = "response")
##       1 
## 74.1505

Otros gráficos preliminares


data <- read.csv("datatfr.csv")

names(data)[1]<-"Año"

data2020<-subset(data, data$Año <=2020)

Data: Evolución de la tasa de fertildiad femenina (1800-2020)


data <- read.csv("datatfr.csv")

names(data)[1]<-"Año"

data2020<-subset(data, data$Año <=2020)

Data: Evolución de la expectativa de vida femenina (1800-2020)


data1 <- read.csv("evf.csv")

names(data1)[1]<-"Año"

data1.2020<-subset(data1, Año <=2020 & Año >= 1950)