suppressPackageStartupMessages(library(AER))
suppressPackageStartupMessages(library(GLMsData))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(MASS, exclude = "select"))
suppressPackageStartupMessages(library(plotly))

3. Use la base de datos de los Affairs, disponible en la libreria AER contiene información del número de infidelidades en grupo de 601 sujetos.

(3a) Estime el “mejor” modelo de regresión para explicar el número infidelidades en la muestra. Muestre las medidas de bondad de ajuste.

Descripcion del Ejercicio

affairs (Negocios) numérico. ¿Con qué frecuencia tuvo relaciones sexuales extramatrimoniales durante el último año? 0 = ninguno, 1 = una vez, 2 = dos veces, 3 = 3 veces, 7 = 4 a 10 veces, 12 = mensual, 12 = semanal, 12 = diario.

gender (género) factor que indica género.

age (edad) variable numérica codificación edad en años: 17,5 = menos de 20 años, 22 = 20–24, 27 = 25–29, 32 = 30–34, 37 = 35–39, 42 = 40–44, 47 = 45–49, 52 = 50–54, 57 = 55 o más.

yearsmarried (años casado) variable numérica que codifica el número de años de matrimonio: 0,125 = 3 meses o menos, 0,417 = 4 a 6 meses, 0,75 = 6 meses a 1 año, 1,5 = 1 a 2 años, 4 = 3 a 5 años, 7 = 6 a 8 años , 10 = 9 a 11 años, 15 = 12 o más años.

children (niños) factor. ¿Hay hijos en el matrimonio?

religiousness (religiosidad) Variable numérica que codifica la religiosidad: 1 = anti, 2 = nada, 3 = ligeramente, 4 = algo, 5 = mucho.

education (educación)

nivel de educación de codificación de variable numérica: 9 = escuela primaria, 12 = graduado de escuela secundaria, 14 = algo de universidad, 16 = graduado de universidad, 17 = algún trabajo de posgrado, 18 = maestría, 20 = doctorado, doctorado en medicina u otro grado avanzado.

occupation (ocupación) Ocupación de codificación de variable numérica según clasificación de Hollingshead (numeración inversa).

rating (clasificación) Variable numérica que codifica la autoevaluación del matrimonio: 1 = muy infeliz, 2 = algo infeliz, 3 = promedio, 4 = más feliz que el promedio, 5 = muy feliz.

library(GLMsData)
data(Affairs)
glimpse(Affairs)
Rows: 601
Columns: 9
$ affairs       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ gender        <fct> male, female, female, male, male, female, female, male, …
$ age           <dbl> 37, 27, 32, 57, 22, 32, 22, 57, 32, 22, 37, 27, 47, 22, …
$ yearsmarried  <dbl> 10.00, 4.00, 15.00, 15.00, 0.75, 1.50, 0.75, 15.00, 15.0…
$ children      <fct> no, no, yes, yes, no, no, no, yes, yes, no, yes, yes, ye…
$ religiousness <int> 3, 4, 1, 5, 2, 2, 2, 2, 4, 4, 2, 4, 5, 2, 4, 1, 2, 3, 2,…
$ education     <dbl> 18, 14, 12, 18, 17, 17, 12, 14, 16, 14, 20, 18, 17, 17, …
$ occupation    <int> 7, 6, 1, 6, 6, 5, 1, 4, 1, 4, 7, 6, 6, 5, 5, 5, 4, 5, 5,…
$ rating        <int> 4, 4, 4, 5, 3, 5, 3, 4, 2, 5, 2, 4, 4, 4, 4, 5, 3, 4, 5,…
hist(Affairs$affairs)

No hay una buena asimetria.

# Crear variable dummy para gender
Affairs$gender <- ifelse(Affairs$gender == "male", 1, 0)

# Agrupar edad y crear variables dummy
Affairs$age_group <- cut(Affairs$age, breaks = c(0, 29, 39, Inf), labels = c("Under 30", "30-39", "40 and over"))
age_dummies <- model.matrix(~ age_group - 1, data = Affairs)
Affairs <- cbind(Affairs, age_dummies)

# Agrupar años casados y crear variables dummy
Affairs$yearsmarried_group <- cut(Affairs$yearsmarried, breaks = c(-Inf, 1, 5, 10, Inf), labels = c("0-1", "1-5", "5-10", "10+"))
years_dummies <- model.matrix(~ yearsmarried_group - 1, data = Affairs)
Affairs <- cbind(Affairs, years_dummies)

# Crear variable dummy para children
Affairs$children <- ifelse(Affairs$children == "yes", 1, 0)

# Convertir religiousness a factor y crear variables dummy
Affairs$religiousness <- as.factor(Affairs$religiousness)
religiousness_dummies <- model.matrix(~ religiousness - 1, data = Affairs)
Affairs <- cbind(Affairs, religiousness_dummies)

# Convertir education a factor y crear variables dummy
Affairs$education <- as.factor(Affairs$education)
education_dummies <- model.matrix(~ education - 1, data = Affairs)
Affairs <- cbind(Affairs, education_dummies)

# Convertir occupation a factor y crear variables dummy
Affairs$occupation <- as.factor(Affairs$occupation)
occupation_dummies <- model.matrix(~ occupation - 1, data = Affairs)
Affairs <- cbind(Affairs, occupation_dummies)

# Convertir rating a factor y crear variables dummy
Affairs$rating <- as.factor(Affairs$rating)
rating_dummies <- model.matrix(~ rating - 1, data = Affairs)
Affairs <- cbind(Affairs, rating_dummies)

# Opcional: eliminar las columnas originales si ya no son necesarias
Affairs$age_group <- NULL
Affairs$yearsmarried_group <- NULL


head(Affairs)
   affairs gender age yearsmarried children religiousness education occupation
4        0      1  37        10.00        0             3        18          7
5        0      0  27         4.00        0             4        14          6
11       0      0  32        15.00        1             1        12          1
16       0      1  57        15.00        1             5        18          6
23       0      1  22         0.75        0             2        17          6
29       0      0  32         1.50        0             2        17          5
   rating age_groupUnder 30 age_group30-39 age_group40 and over
4       4                 0              1                    0
5       4                 1              0                    0
11      4                 0              1                    0
16      5                 0              0                    1
23      3                 1              0                    0
29      5                 0              1                    0
   yearsmarried_group0-1 yearsmarried_group1-5 yearsmarried_group5-10
4                      0                     0                      1
5                      0                     1                      0
11                     0                     0                      0
16                     0                     0                      0
23                     1                     0                      0
29                     0                     1                      0
   yearsmarried_group10+ religiousness1 religiousness2 religiousness3
4                      0              0              0              1
5                      0              0              0              0
11                     1              1              0              0
16                     1              0              0              0
23                     0              0              1              0
29                     0              0              1              0
   religiousness4 religiousness5 education9 education12 education14 education16
4               0              0          0           0           0           0
5               1              0          0           0           1           0
11              0              0          0           1           0           0
16              0              1          0           0           0           0
23              0              0          0           0           0           0
29              0              0          0           0           0           0
   education17 education18 education20 occupation1 occupation2 occupation3
4            0           1           0           0           0           0
5            0           0           0           0           0           0
11           0           0           0           1           0           0
16           0           1           0           0           0           0
23           1           0           0           0           0           0
29           1           0           0           0           0           0
   occupation4 occupation5 occupation6 occupation7 rating1 rating2 rating3
4            0           0           0           1       0       0       0
5            0           0           1           0       0       0       0
11           0           0           0           0       0       0       0
16           0           0           1           0       0       0       0
23           0           0           1           0       0       0       1
29           0           1           0           0       0       0       0
   rating4 rating5
4        1       0
5        1       0
11       1       0
16       0       1
23       0       0
29       0       1
modelo_poi_Affairs=glm(affairs~.,data=Affairs,family=poisson(link="log"))

Seleccion del Mejor Subconjunto de Variables

step(modelo_poi_Affairs)
Start:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6 + occupation7 + rating1 + 
    rating2 + rating3 + rating4 + rating5


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6 + occupation7 + rating1 + 
    rating2 + rating3 + rating4


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6 + occupation7 + rating1 + 
    rating2 + rating3


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6 + occupation7 + rating1 + 
    rating2


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6 + occupation7 + rating1


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6 + occupation7


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5 + occupation6


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4 + occupation5


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3 + 
    occupation4


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2 + occupation3


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1 + occupation2


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20 + occupation1


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18 + education20


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17 + 
    education18


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16 + education17


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14 + education16


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12 + education14


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9 + education12


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5 + 
    education9


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4 + religiousness5


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3 + religiousness4


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2 + religiousness3


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1 + 
    religiousness2


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+` + religiousness1


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10` + `yearsmarried_group10+`


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `age_group40 and over` + `yearsmarried_group0-1` + `yearsmarried_group1-5` + 
    `yearsmarried_group5-10`


Step:  AIC=2717
affairs ~ gender + age + yearsmarried + children + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`

                           Df Deviance  AIC
- children                  1     2163 2715
- gender                    1     2165 2717
<none>                            2163 2717
- `yearsmarried_group0-1`   1     2169 2721
- `yearsmarried_group1-5`   1     2170 2722
- `age_groupUnder 30`       1     2177 2729
- `yearsmarried_group5-10`  1     2178 2730
- `age_group30-39`          1     2178 2730
- occupation                6     2193 2735
- yearsmarried              1     2191 2743
- age                       1     2201 2753
- education                 6     2243 2785
- religiousness             4     2317 2863
- rating                    4     2372 2918

Step:  AIC=2715
affairs ~ gender + age + yearsmarried + religiousness + education + 
    occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`

                           Df Deviance  AIC
- gender                    1     2165 2715
<none>                            2163 2715
- `yearsmarried_group0-1`   1     2169 2719
- `yearsmarried_group1-5`   1     2171 2721
- `age_groupUnder 30`       1     2177 2727
- `yearsmarried_group5-10`  1     2178 2728
- `age_group30-39`          1     2178 2728
- occupation                6     2195 2735
- yearsmarried              1     2191 2741
- age                       1     2201 2751
- education                 6     2243 2783
- religiousness             4     2318 2862
- rating                    4     2372 2916

Step:  AIC=2715
affairs ~ age + yearsmarried + religiousness + education + occupation + 
    rating + `age_groupUnder 30` + `age_group30-39` + `yearsmarried_group0-1` + 
    `yearsmarried_group1-5` + `yearsmarried_group5-10`

                           Df Deviance  AIC
<none>                            2165 2715
- `yearsmarried_group0-1`   1     2170 2718
- `yearsmarried_group1-5`   1     2172 2720
- `age_groupUnder 30`       1     2178 2726
- `yearsmarried_group5-10`  1     2179 2727
- `age_group30-39`          1     2179 2727
- occupation                6     2199 2737
- yearsmarried              1     2192 2740
- age                       1     2201 2749
- education                 6     2244 2782
- religiousness             4     2320 2861
- rating                    4     2373 2915

Call:  glm(formula = affairs ~ age + yearsmarried + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`, 
    family = poisson(link = "log"), data = Affairs)

Coefficients:
             (Intercept)                       age              yearsmarried  
                  2.3162                   -0.0625                    0.2096  
          religiousness2            religiousness3            religiousness4  
                 -0.6941                   -0.6394                   -1.3677  
          religiousness5               education12               education14  
                 -1.5002                   -0.6506                   -1.2884  
             education16               education17               education18  
                 -1.4361                   -0.5808                   -1.0036  
             education20               occupation2               occupation3  
                 -0.8839                   -1.1154                    0.5161  
             occupation4               occupation5               occupation6  
                  0.5829                    0.4460                    0.4248  
             occupation7                   rating2                   rating3  
                  0.1580                    0.4854                   -0.5214  
                 rating4                   rating5       `age_groupUnder 30`  
                 -0.6021                   -0.9590                   -0.8923  
        `age_group30-39`   `yearsmarried_group0-1`   `yearsmarried_group1-5`  
                 -0.5754                    1.3622                    1.2433  
`yearsmarried_group5-10`  
                  0.9901  

Degrees of Freedom: 600 Total (i.e. Null);  573 Residual
Null Deviance:      2930 
Residual Deviance: 2160     AIC: 2710
mejor_modelo_poi_Affairs <- glm(formula = affairs ~ age + yearsmarried + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`, 
    family = poisson(link = "log"), data = Affairs)
summary(mejor_modelo_poi_Affairs)

Call:
glm(formula = affairs ~ age + yearsmarried + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`, 
    family = poisson(link = "log"), data = Affairs)

Coefficients:
                         Estimate Std. Error z value Pr(>|z|)    
(Intercept)                2.3162     0.8014    2.89  0.00385 ** 
age                       -0.0625     0.0107   -5.82  5.9e-09 ***
yearsmarried               0.2096     0.0407    5.15  2.7e-07 ***
religiousness2            -0.6941     0.1138   -6.10  1.1e-09 ***
religiousness3            -0.6394     0.1146   -5.58  2.4e-08 ***
religiousness4            -1.3677     0.1233  -11.09  < 2e-16 ***
religiousness5            -1.5002     0.1646   -9.11  < 2e-16 ***
education12               -0.6506     0.2640   -2.46  0.01374 *  
education14               -1.2884     0.2597   -4.96  7.0e-07 ***
education16               -1.4361     0.2753   -5.22  1.8e-07 ***
education17               -0.5808     0.2579   -2.25  0.02434 *  
education18               -1.0036     0.2671   -3.76  0.00017 ***
education20               -0.8839     0.2827   -3.13  0.00177 ** 
occupation2               -1.1154     0.5969   -1.87  0.06167 .  
occupation3                0.5161     0.1447    3.57  0.00036 ***
occupation4                0.5829     0.1395    4.18  2.9e-05 ***
occupation5                0.4460     0.1209    3.69  0.00022 ***
occupation6                0.4248     0.1354    3.14  0.00170 ** 
occupation7                0.1580     0.2553    0.62  0.53616    
rating2                    0.4854     0.1638    2.96  0.00305 ** 
rating3                   -0.5214     0.1744   -2.99  0.00280 ** 
rating4                   -0.6021     0.1640   -3.67  0.00024 ***
rating5                   -0.9590     0.1715   -5.59  2.2e-08 ***
`age_groupUnder 30`       -0.8923     0.2440   -3.66  0.00026 ***
`age_group30-39`          -0.5754     0.1509   -3.81  0.00014 ***
`yearsmarried_group0-1`    1.3622     0.5860    2.32  0.02010 *  
`yearsmarried_group1-5`    1.2433     0.4590    2.71  0.00676 ** 
`yearsmarried_group5-10`   0.9901     0.2611    3.79  0.00015 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 2925.5  on 600  degrees of freedom
Residual deviance: 2164.7  on 573  degrees of freedom
AIC: 2715

Number of Fisher Scoring iterations: 7

Medidas de Bondad de Ajuste

Pseudo R^2

library(DescTools)

Adjuntando el paquete: 'DescTools'
The following object is masked from 'package:car':

    Recode
PseudoR2(modelo_poi_Affairs, 'Nagelkerke')
Nagelkerke 
    0.7211 

Nota: x < 0.2 El estadistico Nagelkerke recomienda una mala explicacion del modelo de la variable de interes.

0.21 > x < 0.4 El estadistico Nagelkerke recomienda que es una buena explicacion del modelo de la variable de interes.

x > 0.41 El estadistico Nagelkerke recomienda una excelente explicacion del modelo de la variable de interes.

El modelo de regresion binomial negativa explica de manera excelente capturar las complejidades de las infidelidades en la muestra analizada, proporcionando una base sólida para comprender los factores que influyen en este comportamiento.

Prueba de la Deviance

summary(mejor_modelo_poi_Affairs)

Call:
glm(formula = affairs ~ age + yearsmarried + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`, 
    family = poisson(link = "log"), data = Affairs)

Coefficients:
                         Estimate Std. Error z value Pr(>|z|)    
(Intercept)                2.3162     0.8014    2.89  0.00385 ** 
age                       -0.0625     0.0107   -5.82  5.9e-09 ***
yearsmarried               0.2096     0.0407    5.15  2.7e-07 ***
religiousness2            -0.6941     0.1138   -6.10  1.1e-09 ***
religiousness3            -0.6394     0.1146   -5.58  2.4e-08 ***
religiousness4            -1.3677     0.1233  -11.09  < 2e-16 ***
religiousness5            -1.5002     0.1646   -9.11  < 2e-16 ***
education12               -0.6506     0.2640   -2.46  0.01374 *  
education14               -1.2884     0.2597   -4.96  7.0e-07 ***
education16               -1.4361     0.2753   -5.22  1.8e-07 ***
education17               -0.5808     0.2579   -2.25  0.02434 *  
education18               -1.0036     0.2671   -3.76  0.00017 ***
education20               -0.8839     0.2827   -3.13  0.00177 ** 
occupation2               -1.1154     0.5969   -1.87  0.06167 .  
occupation3                0.5161     0.1447    3.57  0.00036 ***
occupation4                0.5829     0.1395    4.18  2.9e-05 ***
occupation5                0.4460     0.1209    3.69  0.00022 ***
occupation6                0.4248     0.1354    3.14  0.00170 ** 
occupation7                0.1580     0.2553    0.62  0.53616    
rating2                    0.4854     0.1638    2.96  0.00305 ** 
rating3                   -0.5214     0.1744   -2.99  0.00280 ** 
rating4                   -0.6021     0.1640   -3.67  0.00024 ***
rating5                   -0.9590     0.1715   -5.59  2.2e-08 ***
`age_groupUnder 30`       -0.8923     0.2440   -3.66  0.00026 ***
`age_group30-39`          -0.5754     0.1509   -3.81  0.00014 ***
`yearsmarried_group0-1`    1.3622     0.5860    2.32  0.02010 *  
`yearsmarried_group1-5`    1.2433     0.4590    2.71  0.00676 ** 
`yearsmarried_group5-10`   0.9901     0.2611    3.79  0.00015 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 2925.5  on 600  degrees of freedom
Residual deviance: 2164.7  on 573  degrees of freedom
AIC: 2715

Number of Fisher Scoring iterations: 7

Null deviance: 2925.5 on 600 degrees of freedom Residual deviance: 2164.7 on 573 degrees of freedom

deviance_inv_Affairs = 1-pchisq(760.8, 27)

Esto implica que hay suficiente evidencia para rechazar la hipótesis nula de que el modelo se ajusta adecuadamente a los datos.

Prueba de Coeficientes Estimados

summary(modelo_poi_Affairs)

Call:
glm(formula = affairs ~ ., family = poisson(link = "log"), data = Affairs)

Coefficients: (26 not defined because of singularities)
                         Estimate Std. Error z value Pr(>|z|)    
(Intercept)                2.3877     0.8070    2.96  0.00309 ** 
gender                     0.1042     0.0886    1.18  0.23954    
age                       -0.0641     0.0108   -5.92  3.1e-09 ***
yearsmarried               0.2129     0.0408    5.21  1.8e-07 ***
children                  -0.0485     0.1081   -0.45  0.65357    
religiousness2            -0.6863     0.1141   -6.02  1.8e-09 ***
religiousness3            -0.6334     0.1148   -5.52  3.5e-08 ***
religiousness4            -1.3656     0.1234  -11.07  < 2e-16 ***
religiousness5            -1.4864     0.1647   -9.02  < 2e-16 ***
education12               -0.6465     0.2641   -2.45  0.01437 *  
education14               -1.2922     0.2599   -4.97  6.6e-07 ***
education16               -1.4500     0.2755   -5.26  1.4e-07 ***
education17               -0.5881     0.2581   -2.28  0.02269 *  
education18               -1.0258     0.2679   -3.83  0.00013 ***
education20               -0.9254     0.2843   -3.25  0.00113 ** 
occupation2               -1.1828     0.6001   -1.97  0.04873 *  
occupation3                0.4782     0.1486    3.22  0.00129 ** 
occupation4                0.5205     0.1490    3.49  0.00048 ***
occupation5                0.4156     0.1238    3.36  0.00079 ***
occupation6                0.3569     0.1469    2.43  0.01513 *  
occupation7                0.0897     0.2616    0.34  0.73182    
rating2                    0.4914     0.1657    2.97  0.00302 ** 
rating3                   -0.5217     0.1757   -2.97  0.00298 ** 
rating4                   -0.5986     0.1659   -3.61  0.00031 ***
rating5                   -0.9595     0.1723   -5.57  2.5e-08 ***
`age_groupUnder 30`       -0.9141     0.2447   -3.74  0.00019 ***
`age_group30-39`          -0.5945     0.1515   -3.92  8.7e-05 ***
`age_group40 and over`         NA         NA      NA       NA    
`yearsmarried_group0-1`    1.3601     0.5880    2.31  0.02071 *  
`yearsmarried_group1-5`    1.2422     0.4597    2.70  0.00689 ** 
`yearsmarried_group5-10`   0.9993     0.2612    3.83  0.00013 ***
`yearsmarried_group10+`        NA         NA      NA       NA    
religiousness1                 NA         NA      NA       NA    
religiousness2                 NA         NA      NA       NA    
religiousness3                 NA         NA      NA       NA    
religiousness4                 NA         NA      NA       NA    
religiousness5                 NA         NA      NA       NA    
education9                     NA         NA      NA       NA    
education12                    NA         NA      NA       NA    
education14                    NA         NA      NA       NA    
education16                    NA         NA      NA       NA    
education17                    NA         NA      NA       NA    
education18                    NA         NA      NA       NA    
education20                    NA         NA      NA       NA    
occupation1                    NA         NA      NA       NA    
occupation2                    NA         NA      NA       NA    
occupation3                    NA         NA      NA       NA    
occupation4                    NA         NA      NA       NA    
occupation5                    NA         NA      NA       NA    
occupation6                    NA         NA      NA       NA    
occupation7                    NA         NA      NA       NA    
rating1                        NA         NA      NA       NA    
rating2                        NA         NA      NA       NA    
rating3                        NA         NA      NA       NA    
rating4                        NA         NA      NA       NA    
rating5                        NA         NA      NA       NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 2925.5  on 600  degrees of freedom
Residual deviance: 2163.2  on 571  degrees of freedom
AIC: 2717

Number of Fisher Scoring iterations: 7

El modelo de regresión de Poisson se ajustó a los datos de infidelidades utilizando múltiples variables predictivas. La llamada al modelo indica que hay un número significativo de coeficientes no definidos debido a singularidades, lo que sugiere colinealidad entre algunas variables.

Muchas variables son estadísticamente significativas (p < 0.05). Entre ellas:

Edad: Un coeficiente negativo (-0.0641) sugiere que, a medida que aumenta la edad, el número esperado de infidelidades disminuye. Años casado: Un coeficiente positivo (0.2129) indica que más años de matrimonio se asocian con un mayor número de infidelidades. Religiosidad: Las categorías de religiosidad muestran coeficientes negativos, lo que sugiere que a mayor nivel de religiosidad, menor número de infidelidades. Educación: La mayoría de las categorías educativas también tienen efectos negativos sobre el número de infidelidades, indicando que niveles más altos de educación pueden estar asociados con menos infidelidades.

Bondad de Ajuste: Desviación Nula: 2925.5 con 600 grados de libertad. Desviación Residual: 2163.2 con 571 grados de libertad. AIC: 2717, que es un indicador de la calidad del ajuste del modelo. Un AIC más bajo generalmente sugiere un mejor ajuste, aunque se debe comparar con otros modelos. Colinealidad: La presencia de coeficientes no definidos sugiere que algunas variables podrían estar altamente correlacionadas entre sí. Esto debe ser investigado para mejorar la interpretabilidad y el ajuste del modelo.

Interpretación de Coeficientes: Por ejemplo, un hombre con 37 años, 8 años de matrimonio, sin hijos, poco religioso (religiosidad 1), con un grado de escolaridad en el nivel de maestría (educación 17), y muy feliz con su matrimonio (rating 5) puede esperar un número específico de infidelidades que se puede calcular a partir del modelo.

Interpretacion del coeficiente del modelo

coef(modelo_poi_Affairs)
             (Intercept)                   gender                      age 
                 2.38775                  0.10421                 -0.06411 
            yearsmarried                 children           religiousness2 
                 0.21291                 -0.04851                 -0.68633 
          religiousness3           religiousness4           religiousness5 
                -0.63341                 -1.36560                 -1.48642 
             education12              education14              education16 
                -0.64649                 -1.29222                 -1.45004 
             education17              education18              education20 
                -0.58809                 -1.02585                 -0.92544 
             occupation2              occupation3              occupation4 
                -1.18283                  0.47815                  0.52051 
             occupation5              occupation6              occupation7 
                 0.41557                  0.35688                  0.08965 
                 rating2                  rating3                  rating4 
                 0.49141                 -0.52174                 -0.59858 
                 rating5      `age_groupUnder 30`         `age_group30-39` 
                -0.95947                 -0.91411                 -0.59453 
  `age_group40 and over`  `yearsmarried_group0-1`  `yearsmarried_group1-5` 
                      NA                  1.36015                  1.24216 
`yearsmarried_group5-10`  `yearsmarried_group10+`           religiousness1 
                 0.99934                       NA                       NA 
          religiousness2           religiousness3           religiousness4 
                      NA                       NA                       NA 
          religiousness5               education9              education12 
                      NA                       NA                       NA 
             education14              education16              education17 
                      NA                       NA                       NA 
             education18              education20              occupation1 
                      NA                       NA                       NA 
             occupation2              occupation3              occupation4 
                      NA                       NA                       NA 
             occupation5              occupation6              occupation7 
                      NA                       NA                       NA 
                 rating1                  rating2                  rating3 
                      NA                       NA                       NA 
                 rating4                  rating5 
                      NA                       NA 
exp(c(2.38775, 0.10421, 0.21291, 0.47815, 0.52051, 0.41557, 0.35688, 0.08965, 0.49141, 1.36015, 1.24216, 1.24216, 0.99934))
 [1] 10.889  1.110  1.237  1.613  1.683  1.515  1.429  1.094  1.635  3.897
[11]  3.463  3.463  2.716

Los valores (1.110, 1.237, etc.) representan el cambio en la tasa esperada de infidelidades por cada unidad de cambio en la variable correspondiente, manteniendo las otras variables constantes. Por ejemplo, un valor de 1.110 para gender indica que ser masculino (en comparación con femenino, que sería la referencia) está asociado con un aumento del 11.0% en la tasa de infidelidades. Cualquier variable cuyo coeficiente expone un valor mayor a 1 sugiere un efecto positivo sobre la tasa de infidelidades. Por ejemplo, occupation3 (1.614) y occupation4 (1.684) indican que tener estos tipos de ocupación está relacionado con tasas significativamente más altas de infidelidades. Al observar los resultados, se puede hacer comparaciones entre diferentes grupos. Por ejemplo, si occupation4 tiene un valor de 1.684 y occupation5 tiene un valor de 1.515, esto sugiere que occupation4 está asociado con una mayor tasa de infidelidades en comparación con occupation5.

El coeficiente para years married (1.237) indica que a medida que aumentan los años de matrimonio, también hay un incremento en la tasa de infidelidades, lo que puede ser sorprendente y sugiere una compleja dinámica en las relaciones a largo plazo.

La relación entre las variables demográficas y el comportamiento de infidelidad es evidente en este modelo, y los resultados sugieren que hay factores significativos que influyen en la tasa de infidelidades, como la ocupación, el estado civil y la satisfacción en el matrimonio.

1 / exp(c(-0.06411, -0.04851, -0.68633, -0.63341, -1.36560,  -1.48642, -0.64649, -1.29222, -1.45004, -0.58809,  -1.02585, -0.92544, -1.18283, -0.52174, -0.59858,  -0.95947, -0.91411, -0.59453))
 [1] 1.066 1.050 1.986 1.884 3.918 4.421 1.909 3.641 4.263 1.801 2.789 2.523
[13] 3.264 1.685 1.820 2.610 2.495 1.812

CONCLUSIONES

Los valores obtenidos (1.066, 1.050, 1.986, etc.) representan la relación entre las variables independientes y la variable dependiente (número de infidelidades) en el contexto del modelo. Específicamente, cada valor es el factor de cambio esperado en el número de infidelidades asociado a un incremento unitario en la variable correspondiente.

Algunos coeficientes son significativamente mayores que otros (3.918 y 4.421), lo que sugiere que ciertas variables tienen un efecto más fuerte en la probabilidad de infidelidades en comparación con otras. Esto puede indicar que, la ocupación o el nivel de satisfacción en el matrimonio (según las variables que representan estos factores) tienen un impacto notable.

Los valores cercanos a 1 (como 1.066 y 1.050) indican un efecto relativamente pequeño sobre el número de infidelidades, sugiriendo que cambios en estas variables están asociados a cambios marginales en el resultado.

Estos resultados pueden ser útiles para identificar qué características demográficas o de situación personal están más asociadas con el comportamiento de infidelidades, permitiendo a los investigadores o profesionales del área de la psicología o la sociología diseñar intervenciones más efectivas.

summary(mejor_modelo_poi_Affairs)

Call:
glm(formula = affairs ~ age + yearsmarried + religiousness + 
    education + occupation + rating + `age_groupUnder 30` + `age_group30-39` + 
    `yearsmarried_group0-1` + `yearsmarried_group1-5` + `yearsmarried_group5-10`, 
    family = poisson(link = "log"), data = Affairs)

Coefficients:
                         Estimate Std. Error z value Pr(>|z|)    
(Intercept)                2.3162     0.8014    2.89  0.00385 ** 
age                       -0.0625     0.0107   -5.82  5.9e-09 ***
yearsmarried               0.2096     0.0407    5.15  2.7e-07 ***
religiousness2            -0.6941     0.1138   -6.10  1.1e-09 ***
religiousness3            -0.6394     0.1146   -5.58  2.4e-08 ***
religiousness4            -1.3677     0.1233  -11.09  < 2e-16 ***
religiousness5            -1.5002     0.1646   -9.11  < 2e-16 ***
education12               -0.6506     0.2640   -2.46  0.01374 *  
education14               -1.2884     0.2597   -4.96  7.0e-07 ***
education16               -1.4361     0.2753   -5.22  1.8e-07 ***
education17               -0.5808     0.2579   -2.25  0.02434 *  
education18               -1.0036     0.2671   -3.76  0.00017 ***
education20               -0.8839     0.2827   -3.13  0.00177 ** 
occupation2               -1.1154     0.5969   -1.87  0.06167 .  
occupation3                0.5161     0.1447    3.57  0.00036 ***
occupation4                0.5829     0.1395    4.18  2.9e-05 ***
occupation5                0.4460     0.1209    3.69  0.00022 ***
occupation6                0.4248     0.1354    3.14  0.00170 ** 
occupation7                0.1580     0.2553    0.62  0.53616    
rating2                    0.4854     0.1638    2.96  0.00305 ** 
rating3                   -0.5214     0.1744   -2.99  0.00280 ** 
rating4                   -0.6021     0.1640   -3.67  0.00024 ***
rating5                   -0.9590     0.1715   -5.59  2.2e-08 ***
`age_groupUnder 30`       -0.8923     0.2440   -3.66  0.00026 ***
`age_group30-39`          -0.5754     0.1509   -3.81  0.00014 ***
`yearsmarried_group0-1`    1.3622     0.5860    2.32  0.02010 *  
`yearsmarried_group1-5`    1.2433     0.4590    2.71  0.00676 ** 
`yearsmarried_group5-10`   0.9901     0.2611    3.79  0.00015 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 2925.5  on 600  degrees of freedom
Residual deviance: 2164.7  on 573  degrees of freedom
AIC: 2715

Number of Fisher Scoring iterations: 7
coef(mejor_modelo_poi_Affairs)
             (Intercept)                      age             yearsmarried 
                 2.31621                 -0.06247                  0.20964 
          religiousness2           religiousness3           religiousness4 
                -0.69413                 -0.63943                 -1.36773 
          religiousness5              education12              education14 
                -1.50022                 -0.65056                 -1.28839 
             education16              education17              education18 
                -1.43608                 -0.58079                 -1.00358 
             education20              occupation2              occupation3 
                -0.88389                 -1.11537                  0.51609 
             occupation4              occupation5              occupation6 
                 0.58288                  0.44603                  0.42478 
             occupation7                  rating2                  rating3 
                 0.15796                  0.48539                 -0.52136 
                 rating4                  rating5      `age_groupUnder 30` 
                -0.60214                 -0.95903                 -0.89230 
        `age_group30-39`  `yearsmarried_group0-1`  `yearsmarried_group1-5` 
                -0.57538                  1.36217                  1.24326 
`yearsmarried_group5-10` 
                 0.99010 
exp(c(1.1640, 1.2613, 0.6691))
[1] 3.203 3.530 1.952
positivos <- c(2.31621, 0.20964, 0.51609, 0.58288, 0.44603, 0.42478, 0.15796, 0.48539, 1.36217, 1.24326, 0.99010)

resultados_positivos <- exp(positivos); resultados_positivos
 [1] 10.137  1.233  1.675  1.791  1.562  1.529  1.171  1.625  3.905  3.467
[11]  2.692
negativos <- c(-0.06247, -0.69413, -0.63943, -1.36773, -1.50022, -0.65056, -1.28839, -1.43608, -0.58079, -1.00358, -0.88389, -1.11537, -0.52136,  -0.60214, -0.95903, -0.89230, -0.57538)

resultados_negativos <- 1 / exp(-negativos); resultados_negativos
 [1] 0.9394 0.4995 0.5276 0.2547 0.2231 0.5218 0.2757 0.2379 0.5595 0.3666
[11] 0.4132 0.3278 0.5937 0.5476 0.3833 0.4097 0.5625

1.233, 1.675, 1.791, 1.562, 1.529: Indican que las variables correspondientes tienen un efecto positivo sobre la cantidad de infidelidades, aumentando la expectativa del número de infidelidades.

1.171, 1.625: Reflejan un aumento moderado en el número esperado de infidelidades asociado a las variables correspondientes.

3.905, 3.467, 2.692: Representan incrementos más significativos en la expectativa de infidelidades, sugiriendo un impacto notable de esas variables en el comportamiento.

0.9394: Un ligero efecto que indica que la variable correspondiente puede reducir la expectativa de infidelidades.

0.4995, 0.5276: Sugerencias de un efecto más pronunciado, donde la presencia de estas variables podría reducir significativamente la expectativa de infidelidades.

0.2547, 0.2231: Valores bajos que indican que estas variables son muy influyentes en la disminución de infidelidades.

0.5218, 0.2757, 0.2379: Reflejan que, a medida que estas variables aumentan, el número esperado de infidelidades tiende a disminuir.

0.5595, 0.3666, 0.4132, 0.3278: Refuerzan la idea de que ciertas características o condiciones reducen la probabilidad de infidelidades.

0.5937, 0.5476, 0.3833, 0.4097, 0.5625: Estos resultados continúan mostrando que varios factores tienen un efecto negativo considerable sobre la probabilidad de infidelidades.

(3b) ¿Existe algún dato atípico o influyente?. Muestre los estadísticos que sustenten dicha afirmación.

Verificar datos atípicos

par(mfrow=c(1,2))
plot(abs(residuals(mejor_modelo_poi_Affairs)))
abline(h=2,col = 'red')
plot(abs(residuals(modelo_poi_Affairs,type = 'pearson')))
abline(h=2,col = 'red')

# Crear un dataframe con los residuos
residuos_poi_Affairs = data.frame(
  deviance = abs(residuals(mejor_modelo_poi_Affairs)),
  pearson = abs(residuals(mejor_modelo_poi_Affairs, type='pearson'))
)

# Filtrar los residuos que cumplen la condición
resultados_filt_poi_Affairs = residuos_poi_Affairs[residuos_poi_Affairs$deviance > 2 & residuos_poi_Affairs$pearson > 2, ]

# Mostrar los resultados filtrados
resultados_filt_poi_Affairs
     deviance pearson
11      3.201   2.263
50      3.210   2.270
93      2.965   2.096
856     3.253   2.301
925     2.929   2.071
949     3.057   2.162
1260    2.834   2.004
1424    3.186   2.253
1705    4.050   2.864
1920    3.035   2.146
1930    3.057   2.162
9012    3.356   2.373
6       2.045   2.773
12      2.037   2.757
43      2.202   2.655
122     4.464   6.318
126     4.578   8.022
159     2.134   2.554
181     3.075   3.771
186     3.166   4.321
189     3.771   5.662
232     3.873   5.921
252     6.280  11.538
253     5.291   8.336
275     3.188   4.364
328     2.115   2.913
367     2.647   3.365
369     2.513   3.143
392     5.797   9.842
423     3.163   4.313
513     4.665   6.766
520     6.059  10.729
526     4.530   6.462
528     4.197   6.809
553     4.698   8.447
611     2.265   3.232
625     4.485   6.363
635     2.771   3.580
657     5.590  12.510
758     3.408   4.824
770     3.246   4.481
834     5.544  12.250
975     4.002   5.375
981     3.248   4.486
1007    4.852   7.204
1011    2.652   3.374
1035    3.035   4.064
1056    4.197   5.760
1080    4.351   6.078
1138    6.164  11.104
1150    3.934   5.245
1198    2.101   2.504
1294    5.112  10.111
1353    5.962  10.390
1427    3.535   4.525
1445    3.124   3.848
1460    2.027   2.397
1550    4.622   8.175
1564    3.805   5.005
1573    7.273  16.117
1622    8.886  28.981
1674    6.022  10.599
1682    3.547   5.130
1685    3.505   5.036
1732    2.787   3.333
1763    2.409   2.976
1782    4.790   7.058
1784    4.161   5.688
1840    4.092   6.506
1876    4.648   6.726
1941    2.213   2.672

La mayoría de los valores de deviance se encuentran en un rango relativamente bajo, lo que sugiere que el modelo está capturando bien la variabilidad en los datos. Por ejemplo, muchos valores son inferiores a 5, lo que indica un ajuste razonable.

Sin embargo, hay valores más altos (como 8.886 y 7.273) que sugieren que algunas observaciones podrían estar influyendo en el modelo de manera desproporcionada. Estos puntos pueden ser candidatos a valores atípicos o influentes.

Se debe prestar atención a los valores con desviaciones significativamente más altas, como 8.886 y 7.273. Estos podrían ser indicadores de observaciones que no siguen el patrón general del modelo. Las observaciones con desviaciones superiores a 5 pueden ser revisadas para entender su impacto en el modelo y considerar su posible exclusión o un tratamiento especial.

Datos Influyentes

par(mfrow = c(1,1))
library(car)
influencePlot(mejor_modelo_poi_Affairs)

     StudRes      Hat   CookD
174    2.453 0.416846 0.17228
1138   6.751 0.057979 0.28771
1259   1.128 0.485297 0.04464
1573   7.509 0.013294 0.12667
1622   9.315 0.009209 0.28139
1674   6.655 0.066602 0.30673

Las observaciones 1138, 1573, 1622 y 1674 no cumplen la regla de StudRes (son valores atípicos). Los valores de Hat no son excesivamente preocupantes, pero hay que vigilar los altos. CookD está en un rango aceptable, lo que indica que, aunque hay observaciones influyentes, no son problemáticas en términos de ajuste del modelo.

(3c) Estime el promedio esperado de infidelidades de un hombre con 37 años, con 8 años de casado, sin hijos, poco religioso, con un grado de escolaridad en el nivel de maestría, muy feliz con su matrimonio y ocupación en el rango de 8.

# Coeficientes del modelo
intercepto <- 2.3162
coef_edad <- -0.0625
coef_yearsmarried <- 0.2096
religiousness <- c(-0.6941, -0.6394, -1.3677, -1.5002)  # Representa los niveles de religiosidad
education <- c(-0.6506, -1.2884, -1.4361, -0.5808, -1.0036, -0.8839)  # Niveles de educación
occupation <- c(-1.1154, 0.5161, 0.5829, 0.4460, 0.4248, 0.1580)  # Rangos de ocupación
rating <- c(0.4854, -0.5214, -0.6021, -0.9590)  # Niveles de felicidad

# Valores de entrada
edad <- 37
yearsmarried <- 8
children <- 0  # Sin hijos
religiousness_level <- 2  # Poco religioso (puedes ajustar según tu codificación)
education_level <- 6  # Maestría (ajusta según tu codificación)
rating_level <- 1  # Muy feliz (ajusta según tu codificación)
occupation_level <- 5  # Ocupación en rango 8 (ajusta según tu codificación)

# Cálculo del promedio esperado
promedio_esperado <- intercepto +
                     coef_edad * (edad - 30) +
                     coef_yearsmarried * yearsmarried +
                     religiousness[religiousness_level] +
                     education[education_level] +
                     rating[rating_level] +
                     occupation[occupation_level]

# Mostrar el resultado
exp(promedio_esperado)  # Para obtener el valor esperado de infidelidades
[1] 18.96

La conclusión de que el promedio esperado de infidelidades para un hombre de 37 años, con 8 años de casado, sin hijos, poco religioso, con un grado de escolaridad en el nivel de maestría, muy feliz en su matrimonio y con ocupación en el rango de 8 es de aproximadamente 18.96 sugiere que, según el modelo, este hombre podría esperar cerca de 19 infidelidades en el periodo de tiempo considerado.