Nombre: Valeria Yesquén Mayanga - 20202239
library(rio)
library(magrittr)
library(knitr)
library(MASS)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(survival)
library(survminer)
## Loading required package: ggplot2
## Loading required package: ggpubr
##
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
##
## myeloma
library(stringr)
admi = import("admision.xlsx")
str(admi)
## 'data.frame': 400 obs. of 4 variables:
## $ admitido : num 0 1 1 1 0 1 1 0 1 0 ...
## $ letras : num 380 660 800 640 520 760 560 400 540 700 ...
## $ ciencias : num 361 367 400 319 293 300 298 308 339 392 ...
## $ prestigio: num 2 2 4 1 1 3 4 3 2 3 ...
admi$admitido = as.factor(admi$admitido)
str(admi)
## 'data.frame': 400 obs. of 4 variables:
## $ admitido : Factor w/ 2 levels "0","1": 1 2 2 2 1 2 2 1 2 1 ...
## $ letras : num 380 660 800 640 520 760 560 400 540 700 ...
## $ ciencias : num 361 367 400 319 293 300 298 308 339 392 ...
## $ prestigio: num 2 2 4 1 1 3 4 3 2 3 ...
h1=formula(admitido~letras + ciencias + prestigio)
rlog1=glm(h1, data=admi,family = binomial)
summary(rlog1)
##
## Call:
## glm(formula = h1, family = binomial, data = admi)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -6.249705 1.155732 -5.408 6.39e-08 ***
## letras 0.002294 0.001092 2.101 0.0356 *
## ciencias 0.007770 0.003275 2.373 0.0177 *
## prestigio 0.560031 0.127137 4.405 1.06e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 499.98 on 399 degrees of freedom
## Residual deviance: 459.44 on 396 degrees of freedom
## AIC: 467.44
##
## Number of Fisher Scoring iterations: 4
rlog1$coefficients
## (Intercept) letras ciencias prestigio
## -6.249705332 0.002293960 0.007770136 0.560031387
exp(rlog1$coefficients)
## (Intercept) letras ciencias prestigio
## 0.001931023 1.002296593 1.007800402 1.750727449
100*round(abs(1-exp(coef(rlog1)["letras"])),4)
## letras
## 0.23
100*round(abs(1-exp(coef(rlog1)["ciencias"])),4)
## ciencias
## 0.78
100*round(abs(1-exp(coef(rlog1)["prestigio"])),4)
## prestigio
## 75.07
mortalidad = import("mortalidad.xlsx")
str(mortalidad)
## 'data.frame': 263 obs. of 6 variables:
## $ pais : chr "MAR" "ABW" "AFG" "AGO" ...
## $ TasaFertil1mMuje : num 32.3 24.3 73.1 157.4 20.7 ...
## $ EmployPerPop : num 22.4 NA 16.2 69.7 39.5 ...
## $ Tuberculosis100m : num 101 14 189 370 16 5.9 NA 0.79 25 47 ...
## $ MaterMort100m : num 121 NA 396 477 29 NA 156 6 52 25 ...
## $ UndernourishPerPop: num 3.5 NA 23 14 4.9 ...
mortalidad=mortalidad[complete.cases(mortalidad),]
modeloA=formula(MaterMort100m~Tuberculosis100m +EmployPerPop)
rg1=lm(modeloA,data=mortalidad)
summary(rg1)
##
## Call:
## lm(formula = modeloA, data = mortalidad)
##
## Residuals:
## Min 1Q Median 3Q Max
## -500.53 -89.79 -49.00 33.07 1044.18
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -29.99461 45.62215 -0.657 0.5117
## Tuberculosis100m 0.73456 0.09399 7.815 4.51e-13 ***
## EmployPerPop 2.18668 0.92684 2.359 0.0194 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 185.9 on 179 degrees of freedom
## Multiple R-squared: 0.2935, Adjusted R-squared: 0.2856
## F-statistic: 37.18 on 2 and 179 DF, p-value: 3.141e-14
modeloB=formula(MaterMort100m~TasaFertil1mMuje+EmployPerPop)
rg2=lm(modeloB,data=mortalidad)
summary(rg2)
##
## Call:
## lm(formula = modeloB, data = mortalidad)
##
## Residuals:
## Min 1Q Median 3Q Max
## -276.87 -71.77 -2.35 37.66 897.65
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -70.8583 33.3665 -2.124 0.0351 *
## TasaFertil1mMuje 4.3144 0.2606 16.556 <2e-16 ***
## EmployPerPop 0.4119 0.6891 0.598 0.5508
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 135.3 on 179 degrees of freedom
## Multiple R-squared: 0.6256, Adjusted R-squared: 0.6215
## F-statistic: 149.6 on 2 and 179 DF, p-value: < 2.2e-16
carcel = import("dataCarcel.xlsx")
str(carcel)
## 'data.frame': 432 obs. of 10 variables:
## $ semanasLibre : num 20 17 25 52 52 52 23 52 52 52 ...
## $ fueArrestado : num 1 1 1 0 0 0 1 0 0 0 ...
## $ tuvoApoyoDinero : num 0 0 0 1 0 0 0 1 0 0 ...
## $ edad : num 27 18 19 23 19 24 25 21 22 20 ...
## $ esNegro : num 1 1 0 1 0 1 1 1 1 1 ...
## $ expLaboralPrevia : num 0 0 1 1 1 1 1 1 0 1 ...
## $ casado : num 0 0 0 1 0 0 1 0 0 0 ...
## $ libertadCondicional: num 1 1 1 1 1 0 1 1 0 0 ...
## $ vecesEnCarcel : num 3 8 13 1 3 2 0 4 6 0 ...
## $ nivelEduca : num 2 3 2 4 2 3 3 2 2 4 ...
carcel$survival=with(carcel,Surv(time = semanasLibre,event = as.numeric(fueArrestado)))
carcel%>%
rmarkdown::paged_table()
carcel$casado = as.factor(carcel$casado)
str(carcel)
## 'data.frame': 432 obs. of 11 variables:
## $ semanasLibre : num 20 17 25 52 52 52 23 52 52 52 ...
## $ fueArrestado : num 1 1 1 0 0 0 1 0 0 0 ...
## $ tuvoApoyoDinero : num 0 0 0 1 0 0 0 1 0 0 ...
## $ edad : num 27 18 19 23 19 24 25 21 22 20 ...
## $ esNegro : num 1 1 0 1 0 1 1 1 1 1 ...
## $ expLaboralPrevia : num 0 0 1 1 1 1 1 1 0 1 ...
## $ casado : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 2 1 1 1 ...
## $ libertadCondicional: num 1 1 1 1 1 0 1 1 0 0 ...
## $ vecesEnCarcel : num 3 8 13 1 3 2 0 4 6 0 ...
## $ nivelEduca : num 2 3 2 4 2 3 3 2 2 4 ...
## $ survival : 'Surv' num [1:432, 1:2] 20 17 25 52+ 52+ 52+ 23 52+ 52+ 52+ ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr [1:2] "time" "status"
## ..- attr(*, "type")= chr "right"
COX1= formula(survival~edad+casado)
rcox1 <- coxph(COX1,data=carcel)
summary(rcox1)
## Call:
## coxph(formula = COX1, data = carcel)
##
## n= 432, number of events= 114
##
## coef exp(coef) se(coef) z Pr(>|z|)
## edad -0.06672 0.93545 0.02082 -3.205 0.00135 **
## casado1 -0.49314 0.61070 0.37269 -1.323 0.18576
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## edad 0.9355 1.069 0.8981 0.9744
## casado1 0.6107 1.637 0.2942 1.2678
##
## Concordance= 0.616 (se = 0.027 )
## Likelihood ratio test= 17.27 on 2 df, p=2e-04
## Wald test = 13.99 on 2 df, p=9e-04
## Score (logrank) test = 14.51 on 2 df, p=7e-04