library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
who<-read_csv("C:/Users/lauvi/Desktop/Datos R/WHO.csv")
## Rows: 1647 Columns: 24
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Country, Cod, Status
## dbl (21): Year, Life.expectancy, Adult.mortality, Infant.deaths, Alcohol, Pe...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
names(who)
## [1] "Country" "Cod" "Year"
## [4] "Status" "Life.expectancy" "Adult.mortality"
## [7] "Infant.deaths" "Alcohol" "Percentage.expenditure"
## [10] "Hepatitis.B" "Measles" "BMI"
## [13] "Polio" "Total.expenditure" "Diphteria"
## [16] "HIV.AIDS" "GDP" "Population"
## [19] "Thinness.10.19.years" "Thinness.5.9.years" "HDI"
## [22] "Schooling" "Homicides" "Fertility"
ggplot(who, aes(x=Infant.deaths, y=Life.expectancy))+
geom_point()
## Warning: Removed 191 rows containing missing values or values outside the scale range
## (`geom_point()`).
cor(who$Infant.deaths, who$Life.expectancy , use = "complete.obs")
## [1] -0.9208866
modelo1 <- lm(Infant.deaths ~ Life.expectancy, who )
summary(modelo1)
##
## Call:
## lm(formula = Infant.deaths ~ Life.expectancy, data = who)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5543 -0.8536 -0.1076 0.8821 13.6460
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.856529 0.306635 100.63 <2e-16 ***
## Life.expectancy -0.386913 0.004295 -90.08 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.35 on 1454 degrees of freedom
## (191 observations deleted due to missingness)
## Multiple R-squared: 0.848, Adjusted R-squared: 0.8479
## F-statistic: 8114 on 1 and 1454 DF, p-value: < 2.2e-16
modelo2 <- lm(Infant.deaths ~ Life.expectancy + Measles + Polio , who )
summary(modelo2)
##
## Call:
## lm(formula = Infant.deaths ~ Life.expectancy + Measles + Polio,
## data = who)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5596 -0.7449 -0.0240 0.7222 12.8597
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 32.387752 0.297935 108.707 <2e-16 ***
## Life.expectancy -0.337426 0.005103 -66.127 <2e-16 ***
## Measles -0.007250 0.007695 -0.942 0.346
## Polio -0.056659 0.003244 -17.463 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.233 on 1332 degrees of freedom
## (311 observations deleted due to missingness)
## Multiple R-squared: 0.8791, Adjusted R-squared: 0.8789
## F-statistic: 3230 on 3 and 1332 DF, p-value: < 2.2e-16
## El modelo No 2 muestra que hay correlacion entre el Polio, la expectativa de vida, el intercepto es un valor de ajuste
Se realiza Prueba de Normalidad Shapiro
# Normalidad
shapiro.test(modelo1$residuals)
##
## Shapiro-Wilk normality test
##
## data: modelo1$residuals
## W = 0.95332, p-value < 2.2e-16
Con la prueba Shapiro al ser el valor p es menor que el 0.05, entonces se descarta “Ho:la distribución es normal”. por tal motivo los datos no estan normalmente distribuidos con estas variables. Hay rechazo de Ho. lo que implica que nos se debe usar un test parametrico.
library(nortest)
lillie.test(modelo1$residuals)
##
## Lilliefors (Kolmogorov-Smirnov) normality test
##
## data: modelo1$residuals
## D = 0.050029, p-value = 4.323e-09
# Varianza constante
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
bptest(modelo1)
##
## studentized Breusch-Pagan test
##
## data: modelo1
## BP = 86.949, df = 1, p-value < 2.2e-16
## El valor es menor que el 0.05 por tal motivo no tiene varianzas constantes
newdata<-data.frame(Infant.deaths = c(3,4,2,5,1),
Life.expectancy = c(80, 30,
90,50,
85))
predict(modelo1, newdata)
## 1 2 3 4 5
## -0.09650926 19.24913968 -3.96563904 11.51088010 -2.03107415