##Mostrar la tabla tabla de frecuencia 
#Cargar

tel <- read.csv('https://raw.githubusercontent.com/VictorGuevaraP/Estadistica-R/master/Caso_telef.csv', 
                    sep = ";", 
                    encoding = "latin1", 
                    stringsAsFactors = TRUE)
head(tel)
str(tel)
## 'data.frame':    120 obs. of  10 variables:
##  $ Cod_enc         : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Género          : int  1 0 0 1 1 1 1 0 1 1 ...
##  $ Edad            : int  27 28 24 33 42 28 40 35 39 40 ...
##  $ Llamadas        : int  7 4 9 5 4 7 5 3 4 8 ...
##  $ Minutos         : num  33 31.7 31.4 26.9 35.5 39.9 39.3 37.8 43.7 46.1 ...
##  $ Monto.facturado : num  90.7 95.7 99.3 83.3 97.3 ...
##  $ Tiempo.de.espera: num  16.9 1.5 2.8 4.6 3.5 4 14.5 2.7 11.5 0.5 ...
##  $ Opinión         : Factor w/ 5 levels "Bueno","Excelente",..: 5 1 4 4 2 4 3 4 1 4 ...
##  $ tipo_plan       : int  2 2 2 1 3 1 2 1 3 2 ...
##  $ Sucursal        : Factor w/ 4 levels "Suc. Centro",..: 2 2 2 2 2 2 2 2 2 2 ...
#Verificamos las dimensiones de los datos
dim(tel)
## [1] 120  10
#• Generamos una tabla de frecuencia de manera manual para Edad

table(tel$Edad)
## 
## 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 
##  1  1  3  1  5  3  1  4 11  4  3  7  6  8  9  7  7  5 14  7  9  1  3

#IX #Modelo missing #Comportamiento de Missing

library(VIM)
matrixplot(tel)

# Verificar columnas con missing

which(colSums(is.na(tel))!=0)
## named integer(0)
table(tel$Opinión)
## 
##     Bueno Excelente Muy Bueno    Pésimo   Regular 
##        23        19        19        44        15
library(summarytools)
## Warning: package 'summarytools' was built under R version 4.3.2
freq(tel$Llamadas, report.nas = TRUE)
## Frequencies  
## tel$Llamadas  
## Type: Integer  
## 
##               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
##           0      3      2.50           2.50      2.50           2.50
##           1      5      4.17           6.67      4.17           6.67
##           2     10      8.33          15.00      8.33          15.00
##           3     16     13.33          28.33     13.33          28.33
##           4     19     15.83          44.17     15.83          44.17
##           5     18     15.00          59.17     15.00          59.17
##           6     14     11.67          70.83     11.67          70.83
##           7     13     10.83          81.67     10.83          81.67
##           8     15     12.50          94.17     12.50          94.17
##           9      5      4.17          98.33      4.17          98.33
##          10      1      0.83          99.17      0.83          99.17
##          13      1      0.83         100.00      0.83         100.00
##        <NA>      0                               0.00         100.00
##       Total    120    100.00         100.00    100.00         100.00

##Modelo predictivo

# Regresión lineal simple
modelo1 <- lm(tel$Edad ~ tel$Género, data=tel)

summary(modelo1)
## 
## Call:
## lm(formula = tel$Edad ~ tel$Género, data = tel)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.8889  -4.3636   0.6364   4.1111   9.6364 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  33.8889     0.7333  46.213   <2e-16 ***
## tel$Género   -1.5253     0.9888  -1.543    0.126    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.389 on 118 degrees of freedom
## Multiple R-squared:  0.01977,    Adjusted R-squared:  0.01146 
## F-statistic: 2.379 on 1 and 118 DF,  p-value: 0.1256

#Estandiracion y normalizacion de variables

tel$Monto.facturado
##   [1]  90.7  95.7  99.3  83.3  97.3 114.5 106.0  99.0  90.2 102.5  92.2 110.1
##  [13]  96.5  88.3  85.6  89.7  88.8  85.4  89.0  77.1  97.8  84.0  84.2  91.7
##  [25]  74.1  88.8  91.6  80.0  88.4  78.5  88.3  85.6  88.6  78.8  81.6  86.3
##  [37]  83.8  90.7  95.7  99.3  83.3  97.3  88.1  81.9  83.8  82.2  91.5  82.8
##  [49]  94.3  96.5  78.8  83.3  95.6  78.3  87.8  95.8  84.7 100.2  82.5  82.6
##  [61]  94.5  86.3  88.3  83.7  83.8  84.1  83.2  84.8  98.9 119.1 103.4 109.6
##  [73]  97.5  94.8  93.1 108.0 101.0  89.6 102.2 106.7 104.0 100.6 104.1  78.9
##  [85] 101.6 114.6 102.9 114.5 106.0 100.6 103.0 110.5 112.4  90.7 112.5 108.3
##  [97] 114.5 106.0  99.0  90.2 102.5  92.2 110.1  96.5  92.0  84.2  91.7  74.1
## [109]  88.8  91.6  80.0  88.4  78.5  88.3  85.6  88.6  78.8  81.6  86.3  83.8
mediaMonto <- mean(tel$Monto.facturado)
#Media

hist(log(mediaMonto))

desv_est <- sd(tel$Monto.facturado)
#Desviacion ESTARNDAR

hist(log(desv_est))

MontoFactura <-(tel$Monto.facturado)/desv_est
#Valores Estandirisados

hist(log(MontoFactura))

#Transformacion de variable

#Comparacion de transformaciones 

llamada_sqrt <- sqrt(tel$Llamadas)
llamada_exp<- exp(tel$Llamadas)
llamada_ln <- log(tel$Llamadas)
llamada_log2 <- log(tel$Llamadas,base=2)
llamada_log5 <- log(tel$Llamadas,base = 5)

par(mfrow=c(3,3))
hist(tel$Llamadas)
hist(llamada_sqrt)
hist(llamada_exp)
hist(llamada_ln)
hist(llamada_log2)
hist(llamada_log5)
par(mfrow=c(1,1))

#Medida estadistica de tendencia

#Opción 1
Edades = sum(tel$Edad)/length(tel$Edad)
Edades
## [1] 33.05
#Opción 2
mean(Edades)
## [1] 33.05

#Medidas estadisticas de posicion

rm(list = ls())
 
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
getwd()
## [1] "D:/R-Study/ExamenFinal"