library(readr)
insurance <- read_csv("insurance.csv")
## Rows: 1338 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): sex, smoker, region
## dbl (4): age, bmi, children, charges
## 
## ℹ 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.
str(insurance)
## spc_tbl_ [1,338 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ age     : num [1:1338] 19 18 28 33 32 31 46 37 37 60 ...
##  $ sex     : chr [1:1338] "female" "male" "male" "male" ...
##  $ bmi     : num [1:1338] 27.9 33.8 33 22.7 28.9 ...
##  $ children: num [1:1338] 0 1 3 0 0 0 1 3 2 0 ...
##  $ smoker  : chr [1:1338] "yes" "no" "no" "no" ...
##  $ region  : chr [1:1338] "southwest" "southeast" "southeast" "northwest" ...
##  $ charges : num [1:1338] 16885 1726 4449 21984 3867 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   age = col_double(),
##   ..   sex = col_character(),
##   ..   bmi = col_double(),
##   ..   children = col_double(),
##   ..   smoker = col_character(),
##   ..   region = col_character(),
##   ..   charges = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
insurance$smoker <- as.factor(insurance$smoker)
insurance$region <- as.factor(insurance$region)

str(insurance)
## spc_tbl_ [1,338 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ age     : num [1:1338] 19 18 28 33 32 31 46 37 37 60 ...
##  $ sex     : chr [1:1338] "female" "male" "male" "male" ...
##  $ bmi     : num [1:1338] 27.9 33.8 33 22.7 28.9 ...
##  $ children: num [1:1338] 0 1 3 0 0 0 1 3 2 0 ...
##  $ smoker  : Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 1 1 1 ...
##  $ region  : Factor w/ 4 levels "northeast","northwest",..: 4 3 3 2 2 3 3 2 1 2 ...
##  $ charges : num [1:1338] 16885 1726 4449 21984 3867 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   age = col_double(),
##   ..   sex = col_character(),
##   ..   bmi = col_double(),
##   ..   children = col_double(),
##   ..   smoker = col_character(),
##   ..   region = col_character(),
##   ..   charges = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
#modelo 1

datos1 <- insurance

datos1$smoker <- relevel(datos1$smoker, ref = "yes")
datos1$region <- relevel(datos1$region, ref = "northeast")

modelo1 <- lm(charges ~ age + smoker + region, data = datos1)

summary(modelo1)
## 
## Call:
## lm(formula = charges ~ age + smoker + region, data = datos1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15695.5  -2069.1  -1292.6   -211.1  28563.2 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      21479.67     690.01  31.130   <2e-16 ***
## age                275.10      12.45  22.089   <2e-16 ***
## smokerno        -23797.23     434.61 -54.755   <2e-16 ***
## regionnorthwest   -294.97     502.27  -0.587    0.557    
## regionsoutheast    391.25     488.88   0.800    0.424    
## regionsouthwest   -436.71     502.27  -0.869    0.385    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6396 on 1332 degrees of freedom
## Multiple R-squared:  0.7221, Adjusted R-squared:  0.7211 
## F-statistic: 692.2 on 5 and 1332 DF,  p-value: < 2.2e-16
#Interpretación de coeficientes
# 21479.67 → Precio de la póliza para una persona fumadora en la región northeast con edad 0.
# 275.10 → Cambio en el precio de la póliza por cada año adicional de edad.
# −23797.23 →  Los no fumadores pagan 23,797.23 menos que los fumadores.
# −294.97 → Diferencia en el precio entre una persona fumadora en northwest respecto a northeast.
# 391.25 →  Diferencia en el precio entre una persona fumadora en southeast respecto a northeast.
# −436.71 → Diferencia en el precio entre una persona fumadora en southwest respecto a northeast.

#modelo 2
datos2 <- insurance

datos2$smoker <- relevel(datos2$smoker, ref = "no")
datos2$region <- relevel(datos2$region, ref = "northeast")

modelo2 <- lm(charges ~ age + smoker + region, data = datos2)

summary(modelo2)
## 
## Call:
## lm(formula = charges ~ age + smoker + region, data = datos2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15695.5  -2069.1  -1292.6   -211.1  28563.2 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -2317.56     612.90  -3.781 0.000163 ***
## age               275.10      12.45  22.089  < 2e-16 ***
## smokeryes       23797.23     434.61  54.755  < 2e-16 ***
## regionnorthwest  -294.97     502.27  -0.587 0.557117    
## regionsoutheast   391.25     488.88   0.800 0.423680    
## regionsouthwest  -436.71     502.27  -0.869 0.384744    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6396 on 1332 degrees of freedom
## Multiple R-squared:  0.7221, Adjusted R-squared:  0.7211 
## F-statistic: 692.2 on 5 and 1332 DF,  p-value: < 2.2e-16
#modelo 3
datos3 <- insurance

datos3$smoker <- relevel(datos3$smoker, ref = "yes")
datos3$region <- relevel(datos3$region, ref = "southeast")

modelo3 <- lm(charges ~ age + smoker + region, data = datos3)

summary(modelo3)
## 
## Call:
## lm(formula = charges ~ age + smoker + region, data = datos3)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15695.5  -2069.1  -1292.6   -211.1  28563.2 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      21870.92     667.95  32.743   <2e-16 ***
## age                275.10      12.45  22.089   <2e-16 ***
## smokerno        -23797.23     434.61 -54.755   <2e-16 ***
## regionnortheast   -391.25     488.88  -0.800   0.4237    
## regionnorthwest   -686.22     489.10  -1.403   0.1608    
## regionsouthwest   -827.96     489.13  -1.693   0.0907 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6396 on 1332 degrees of freedom
## Multiple R-squared:  0.7221, Adjusted R-squared:  0.7211 
## F-statistic: 692.2 on 5 and 1332 DF,  p-value: < 2.2e-16
# Interpretación de coeficientes
# 21870.92 → Precio de la póliza para una persona fumadora en la región southeast con edad 0.
# 275.10 → Cambio en el precio de la póliza por cada año adicional de edad.
# −23797.23 → Los no fumadores pagan 23,797.23 menos que los fumadores.
# −391.25 → Diferencia en el precio entre northeast y southeast.
# −686.22 → Diferencia en el precio entre northwest y southeast.
# −827.96 → Diferencia en el precio entre southwest y southeast.

#modelo 4

datos4 <- insurance

datos4$smoker <- relevel(datos4$smoker, ref = "yes")
datos4$region <- relevel(datos4$region, ref = "northeast")

modelo4 <- lm(charges ~ age + smoker * region, data = datos4)

summary(modelo4)
## 
## Call:
## lm(formula = charges ~ age + smoker * region, data = datos4)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -17738.0  -2057.8  -1330.3   -209.8  30886.8 
## 
## Coefficients:
##                           Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               19165.07     905.37  21.168  < 2e-16 ***
## age                         274.81      12.34  22.277  < 2e-16 ***
## smokerno                 -20864.75     867.83 -24.043  < 2e-16 ***
## regionnorthwest             219.26    1134.55   0.193  0.84679    
## regionsoutheast            4759.94    1018.43   4.674 3.26e-06 ***
## regionsouthwest            3182.35    1134.78   2.804  0.00511 ** 
## smokerno:regionnorthwest   -727.08    1262.10  -0.576  0.56465    
## smokerno:regionsoutheast  -5656.10    1157.56  -4.886 1.15e-06 ***
## smokerno:regionsouthwest  -4506.28    1262.43  -3.570  0.00037 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6325 on 1329 degrees of freedom
## Multiple R-squared:  0.7288, Adjusted R-squared:  0.7272 
## F-statistic: 446.4 on 8 and 1329 DF,  p-value: < 2.2e-16
# interpretacion 
# 19165.07 → Precio de la póliza para una persona fumadora en la región northeast con edad 0.
# 274.81 → Cambio en el precio de la póliza por cada año adicional de edad.
# −20864.75 → Los no fumadores pagan aproximadamente 20,864.75 menos que los fumadores en la región northeast.
# 4759.94 → Diferencia en el precio entre una persona fumadora en southeast respecto a northeast.
# 3182.35 → Diferencia en el precio entre una persona fumadora en southwest respecto a northeast.

#pronosticos 

precio_poliza <- data.frame(
  age = c(40,55,60,70,80),
  smoker = factor(c("yes","no","yes","no","yes"),
                  levels = levels(datos4$smoker)),
  region = factor(c("northeast","southeast","southwest","northwest","northeast"),
                  levels = levels(datos4$region))
)

predict(modelo4, newdata = precio_poliza)
##        1        2        3        4        5 
## 30157.53 12518.79 38836.11 17029.30 41149.99
# interpretacion1:precio estimado de la póliza para una persona de 40 años fumadora en la región northeast.30157.53 
#interpretacion 2:precio estimado de la póliza para una persona de 55 años no fumadora en la región southeast.12518.79 
#interpretacion 3:precio estimado de la póliza para una persona de 60 años fumadora en la región southwest.38836.11 
#interpretacion 4:precio estimado de la póliza para una persona de 70 años no fumadora en la región northwest.17029.30 
#interpretacion 5:precio estimado de la póliza para una persona de 80 años fumadora en la región northeast.41149.99