Introducción

#En este ejercicio ajustaremos modelos de regresión logistica que nos permitan determinar el sexo de un individuo a través de los huesos del húmero, considerando 2 variables diferentes.

setwd("~/sandy no borrar por favor")
library(pacman)
p_load(haven,dplyr,ggplot2,MASS,tinytex)
Hombro <- read_sav("Datos hombro.sav")
table (Hombro$sexoN)
## 
##  1  2 
## 50 30
Hombro$sexoN <- factor(Hombro$sexoN,
                       levels = c(2, 1),
                       labels = c("Mujer", "Hombre"))
table (Hombro$sexoN)
## 
##  Mujer Hombre 
##     30     50
Hombro_sinNA <- na.omit(Hombro[, c("sexoN", "LMHD","ABHD")])
modelo1 <- glm(sexoN ~ LMHD + ABHD,
                  data = Hombro_sinNA,
                  family = binomial(link = "logit"))
summary(modelo1)
## 
## Call:
## glm(formula = sexoN ~ LMHD + ABHD, family = binomial(link = "logit"), 
##     data = Hombro_sinNA)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -50.0490    12.6492  -3.957  7.6e-05 ***
## LMHD         -0.4774     0.3048  -1.566   0.1173    
## ABHD          0.6555     0.3294   1.990   0.0466 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 89.495  on 66  degrees of freedom
## Residual deviance: 39.992  on 64  degrees of freedom
## AIC: 45.992
## 
## Number of Fisher Scoring iterations: 6
Hombro_sinNA$prob_Hombre <- predict(modelo1, type = "response")
Hombro_sinNA$predicho <- ifelse(Hombro_sinNA$prob_Hombre >= 0.5, "Hombre", "Mujer")
table(Real = Hombro_sinNA$sexoN, Predicho = Hombro_sinNA$predicho)
##         Predicho
## Real     Hombre Mujer
##   Mujer       5    21
##   Hombre     38     3
mean(Hombro_sinNA$sexoN == Hombro_sinNA$predicho) * 100
## [1] 88.0597
ggplot(Hombro_sinNA, aes(x = prob_Hombre, fill = sexoN)) +
  geom_density(alpha = 0.4) +
  geom_vline(xintercept = 0.5, linetype = "dashed") +
  theme_minimal() +
  labs(title = "Probabilidades predichas de ser Hombre",
       x = "P(Hombre)", y = "Densidad")

coef(modelo1)
## (Intercept)        LMHD        ABHD 
## -50.0490482  -0.4773712   0.6555261
cat("Ecuación logística:\nlogit(p) = ",
    round(coef(modelo1)[1], 4), " + ",
    round(coef(modelo1)[2], 4), "*LMHD + ",
    round(coef(modelo1)[3], 4), "*ABHD\n")
## Ecuación logística:
## logit(p) =  -50.049  +  -0.4774 *LMHD +  0.6555 *ABHD

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.