Telco_Cusomer_Churn = read.table("Telco_Cusomer_Churn.csv",
              header=TRUE, sep =",", row.names = 1,dec=".",
              stringsAsFactors = TRUE)
summary(Telco_Cusomer_Churn)
##     gender     SeniorCitizen    Partner    Dependents     tenure     
##  Female:3488   Min.   :0.0000   No :3641   No :4933   Min.   : 0.00  
##  Male  :3555   1st Qu.:0.0000   Yes:3402   Yes:2110   1st Qu.: 9.00  
##                Median :0.0000                         Median :29.00  
##                Mean   :0.1621                         Mean   :32.37  
##                3rd Qu.:0.0000                         3rd Qu.:55.00  
##                Max.   :1.0000                         Max.   :72.00  
##                                                                      
##  PhoneService          MultipleLines     InternetService
##  No : 682     No              :3390   DSL        :2421  
##  Yes:6361     No phone service: 682   Fiber optic:3096  
##               Yes             :2971   No         :1526  
##                                                         
##                                                         
##                                                         
##                                                         
##              OnlineSecurity              OnlineBackup 
##  No                 :3498   No                 :3088  
##  No internet service:1526   No internet service:1526  
##  Yes                :2019   Yes                :2429  
##                                                       
##                                                       
##                                                       
##                                                       
##             DeviceProtection              TechSupport  
##  No                 :3095    No                 :3473  
##  No internet service:1526    No internet service:1526  
##  Yes                :2422    Yes                :2044  
##                                                        
##                                                        
##                                                        
##                                                        
##               StreamingTV              StreamingMovies           Contract   
##  No                 :2810   No                 :2785   Month-to-month:3875  
##  No internet service:1526   No internet service:1526   One year      :1473  
##  Yes                :2707   Yes                :2732   Two year      :1695  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  PaperlessBilling                   PaymentMethod  MonthlyCharges  
##  No :2872         Bank transfer (automatic):1544   Min.   : 18.25  
##  Yes:4171         Credit card (automatic)  :1522   1st Qu.: 35.50  
##                   Electronic check         :2365   Median : 70.35  
##                   Mailed check             :1612   Mean   : 64.76  
##                                                    3rd Qu.: 89.85  
##                                                    Max.   :118.75  
##                                                                    
##   TotalCharges    Churn     
##  Min.   :  18.8   No :5174  
##  1st Qu.: 401.4   Yes:1869  
##  Median :1397.5             
##  Mean   :2283.3             
##  3rd Qu.:3794.7             
##  Max.   :8684.8             
##  NA's   :11
library(corrplot)
## corrplot 0.95 loaded
attach(Telco_Cusomer_Churn)
nuevosDatos = data.frame(tenure, MonthlyCharges, TotalCharges)

r = cor(na.omit(nuevosDatos))
corrplot(r, method="number")

library(ggplot2)
ggplot(Telco_Cusomer_Churn, aes(x = Churn, 
      y = MonthlyCharges, fill = Churn)) +
  geom_boxplot() +
  labs(title = "Cargos Mensuales por Nivel de Rotación",
       x = "Rotación (Fuga)",
       y = "Cargos Mensuales",
       fill = "Rotación") +
  theme_minimal()

# Interpretación Clave:
# * Observar la Mediana: Si la mediana de 'Si' es significativamente más alta que la de 'No',
#   sugiere que los clientes con planes o servicios más caros (cargos mensuales altos)
#   tienen mayor probabilidad de fuga.
ggplot(Telco_Cusomer_Churn, aes(y=TotalCharges, x=Churn, fill=Churn))+
  geom_boxplot()
## Warning: Removed 11 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

 {r}
##                   tenure MonthlyCharges TotalCharges
## tenure         1.0000000      0.2468618    0.8258805
## MonthlyCharges 0.2468618      1.0000000    0.6510648
## TotalCharges   0.8258805      0.6510648    1.0000000
ggplot(Telco_Cusomer_Churn, aes(x=Churn, fill=gender))+
  geom_bar()

ggplot(Telco_Cusomer_Churn,aes(x=tenure,y=MonthlyCharges))+
  geom_jitter()+
  geom_smooth(method = "lm", colour = "gold")
## `geom_smooth()` using formula = 'y ~ x'

  {r}
##                   tenure MonthlyCharges TotalCharges
## tenure         1.0000000      0.2468618    0.8258805
## MonthlyCharges 0.2468618      1.0000000    0.6510648
## TotalCharges   0.8258805      0.6510648    1.0000000
y=MonthlyCharges
x=tenure
cor(y=MonthlyCharges,x=tenure)
## [1] 0.2478999
modelo=lm(y ~ x)
summary(modelo)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -57.498 -27.251   6.245  24.943  54.376 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 54.92978    0.57476   95.57   <2e-16 ***
## x            0.30372    0.01415   21.47   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 29.15 on 7041 degrees of freedom
## Multiple R-squared:  0.06145,    Adjusted R-squared:  0.06132 
## F-statistic:   461 on 1 and 7041 DF,  p-value: < 2.2e-16
library(ggplot2)
library(gridExtra)

g1= ggplot(Telco_Cusomer_Churn,aes(x=Churn,y = MonthlyCharges))+
  geom_boxplot(fill="blue")

g2= ggplot(Telco_Cusomer_Churn, aes(x=Churn, y = TotalCharges))+
  geom_boxplot(fill="red")

g3= ggplot(Telco_Cusomer_Churn, aes(x=Churn, fill = gender))+
  geom_bar()

g4= ggplot(Telco_Cusomer_Churn,aes(x=tenure,y=MonthlyCharges))+
    geom_jitter()+
    geom_smooth(method = "lm",colour = "skyblue")
    
    grid.arrange(g1, g2, g3, g4)
## Warning: Removed 11 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## `geom_smooth()` using formula = 'y ~ x'