#Punto 2.1a
library(UsingR)
## Loading required package: MASS
## Loading required package: HistData
## Loading required package: Hmisc
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(MASS)
library(ggplot2)
data(package= "UsingR")
#tiene 157 conjuntos de datos
#Punto 2.1b
#Representa gráficamente los datos contenidos en los conjuntos de datos (“datasets”) bumpers, firstchi, math con un histograma y/o un boxplot.
data("bumpers")
hist(bumpers)

data("firstchi")
boxplot(firstchi)

data("math")
boxplot(math)

#Punto 2.1c
"Datos de bumpers"
## [1] "Datos de bumpers"
mean(bumpers)
## [1] 2122.478
median(bumpers)
## [1] 2129
sd(bumpers)
## [1] 798.4574
"Datos de firstchi"
## [1] "Datos de firstchi"
mean(firstchi)
## [1] 23.97701
median(firstchi)
## [1] 23
sd(firstchi)
## [1] 6.254258
"Datos de math"
## [1] "Datos de math"
mean(math)
## [1] 54.9
median(math)
## [1] 54
sd(math)
## [1] 9.746264
boxplot(bumpers)

boxplot(firstchi)

boxplot(math)

#Punto 2.2 a) Representa estos datos mediante un histograma y un gráfico de densidad superpuesto. 

hist(brightness, probability = TRUE)
lines(density(brightness), col="green",lwd=3)

#punto2.2 b) Representa gráficamente estos datos mediante un diagrama de caja (boxplot). ¿Dirias que los datos presentan “outliers”? Cuál es el segundo menor outlier? 

boxplot(brightness)

outliers <- boxplot(brightness, plot = FALSE)$out
outliers
##  [1] 12.31 11.71  5.53 11.28  4.78  5.13  4.37  5.04 12.43 12.04  4.55 11.55
## [13] 12.14 11.63  4.99 11.67  4.61 11.99 12.04  5.55 12.17 11.55 11.79 12.19
## [25]  2.07 11.65 11.73  2.28  5.42  3.88  5.54  5.29  5.01 11.55  4.89 11.80
## [37]  5.41  5.24
sort(outliers)[2]
## [1] 2.28
#Punto 2.2 Deseamos conservar los datos que de ninguna forma puedan ser considerados atípicos.Crea una nueva variable denominada brightness.sin que contenga tan solo los valores que se encuentren por encima de la primera bisagra y por debajo de la cuarta. 

data("brightness")
q1 <- quantile(brightness, 0.25)
q3 <- quantile(brightness, 0.75)
brightness.sin <- brightness[brightness >= q1 & brightness <= q3]
#Punto 2.3 El paquete MASS contiene la base de datos UScereal con información relativa a desayunos con cereales.
#a) ¿Cuál es el tipo de datos de cada variable? 
data("UScereal")
str(UScereal)
## 'data.frame':    65 obs. of  11 variables:
##  $ mfr      : Factor w/ 6 levels "G","K","N","P",..: 3 2 2 1 2 1 6 4 5 1 ...
##  $ calories : num  212 212 100 147 110 ...
##  $ protein  : num  12.12 12.12 8 2.67 2 ...
##  $ fat      : num  3.03 3.03 0 2.67 0 ...
##  $ sodium   : num  394 788 280 240 125 ...
##  $ fibre    : num  30.3 27.3 28 2 1 ...
##  $ carbo    : num  15.2 21.2 16 14 11 ...
##  $ sugars   : num  18.2 15.2 0 13.3 14 ...
##  $ shelf    : int  3 3 3 1 2 3 1 3 2 1 ...
##  $ potassium: num  848.5 969.7 660 93.3 30 ...
##  $ vitamins : Factor w/ 3 levels "100%","enriched",..: 2 2 2 2 2 2 2 2 2 2 ...
#b) Utiliza los datos de cereales para investigar algunas asociaciones entre sus variables:

#i. La relación entre manufacturer y shelf.
table(UScereal$mfr,UScereal$shelf)
##    
##      1  2  3
##   G  6  7  9
##   K  4  7 10
##   N  2  0  1
##   P  2  1  6
##   Q  0  3  2
##   R  4  0  1
#ii. La relación entre fat y vitamins.
table(UScereal$vitamins,UScereal$fat)
##           
##             0 0.6666667  1 1.1363636 1.3333333 1.4925373 1.6  2 2.6666667
##   100%      1         0  3         0         1         0   0  0         0
##   enriched 18         1  7         1         8         4   1  2         3
##   none      3         0  0         0         0         0   0  0         0
##           
##            2.9850746 3.030303  4  6 9.0909091
##   100%             0        0  0  0         0
##   enriched         4        2  4  1         1
##   none             0        0  0  0         0
#iii. La relación entre fat y shelf.
table(UScereal$shelf,UScereal$fat)
##    
##      0 0.6666667  1 1.1363636 1.3333333 1.4925373 1.6  2 2.6666667 2.9850746
##   1 10         0  2         0         2         2   1  0         1         0
##   2  3         1  5         0         4         1   0  1         1         1
##   3  9         0  3         1         3         1   0  1         1         3
##    
##     3.030303  4  6 9.0909091
##   1        0  0  0         0
##   2        0  1  0         0
##   3        2  3  1         1
#iv. La relación entre carbohydrates y sugars.
table(UScereal$carbo,UScereal$sugars)
##           
##            0 0.8 1.769912 2 3 4 4.477612 5.681818 6 6.666667 7.462687 8.270677
##   10.52632 0   0        0 0 0 0        0        0 0        0        0        1
##   11       0   0        0 0 0 0        0        0 0        0        0        0
##   12       0   0        0 0 0 0        0        0 0        0        0        0
##   12.5     0   0        0 0 0 0        0        0 0        0        0        0
##   13       1   0        0 0 0 0        0        0 0        0        0        0
##   13.6     0   1        0 0 0 0        0        0 0        0        0        0
##   14       0   0        0 1 0 0        0        0 0        0        0        0
##   14.66667 0   0        0 0 0 0        0        0 0        0        0        0
##   15       0   0        0 0 0 0        0        0 1        0        0        0
##   15.15152 0   0        0 0 0 0        0        0 0        0        0        0
##   15.33333 0   0        0 0 0 0        0        0 0        0        0        0
##   16       1   0        0 0 2 0        0        0 0        0        0        0
##   16.41791 0   0        0 0 0 0        0        0 0        0        0        0
##   17       0   0        0 0 1 0        0        0 0        0        0        0
##   17.04545 0   0        0 0 0 0        0        1 0        0        0        0
##   17.33333 0   0        0 0 0 0        0        0 0        0        0        0
##   17.5     0   0        0 0 0 0        0        0 0        0        0        0
##   17.91045 0   0        0 0 0 0        0        0 0        0        0        0
##   18.66667 0   0        0 0 0 0        0        0 0        0        0        0
##   19.40299 0   0        0 0 0 0        0        0 0        0        1        0
##   20       0   0        0 0 1 0        0        0 0        0        0        0
##   20.35398 0   0        1 0 0 0        0        0 0        0        0        0
##   20.89552 0   0        0 0 0 0        0        0 0        0        0        0
##   21       0   0        0 1 2 0        0        0 0        0        0        0
##   21.21212 0   0        0 0 0 0        0        0 0        0        0        0
##   21.33333 0   0        0 0 0 0        0        0 0        0        0        0
##   22       0   0        0 0 2 0        0        0 0        0        0        0
##   22.38806 0   0        0 0 0 0        0        0 0        0        0        0
##   24       0   0        0 0 0 0        0        0 0        1        0        0
##   25.37313 0   0        0 0 0 0        1        0 0        0        0        0
##   26       0   0        0 0 0 0        0        0 0        0        0        0
##   26.66667 0   0        0 0 0 0        0        0 0        0        0        0
##   27       0   0        0 0 0 0        0        0 0        0        0        0
##   28       0   0        0 0 0 1        0        0 0        0        0        0
##   28.35821 1   0        0 0 0 0        0        0 0        0        0        0
##   29.85075 1   0        0 0 0 0        0        0 0        0        0        0
##   30       0   0        0 0 0 0        0        0 0        0        0        0
##   31.34328 0   0        0 0 0 0        0        0 0        0        0        0
##   39.39394 0   0        0 0 0 0        0        0 0        0        0        0
##   68       0   0        0 0 0 0        0        0 0        0        0        0
##           
##            8.75 8.955224 10.447761 10.666667 11 12 12.121212 13 13.333333
##   10.52632    0        0         0         0  0  0         0  0         0
##   11          0        0         0         0  0  0         0  1         0
##   12          0        0         0         0  1  1         0  2         0
##   12.5        0        0         0         0  0  0         0  0         0
##   13          0        0         0         0  0  2         0  0         0
##   13.6        0        0         0         0  0  0         0  0         0
##   14          0        0         0         0  0  0         0  0         1
##   14.66667    0        0         0         0  0  0         0  0         1
##   15          0        0         0         0  0  0         0  0         0
##   15.15152    0        0         0         0  0  0         0  0         0
##   15.33333    0        0         0         0  0  0         0  0         1
##   16          0        0         0         0  0  0         0  0         0
##   16.41791    0        0         0         0  0  0         0  0         0
##   17          0        0         0         0  0  0         0  0         0
##   17.04545    0        0         0         0  0  0         0  0         0
##   17.33333    0        0         0         0  0  1         0  0         0
##   17.5        1        0         0         0  0  0         0  0         0
##   17.91045    0        1         0         0  0  0         0  0         0
##   18.66667    0        0         0         0  0  0         0  0         0
##   19.40299    0        0         0         0  0  0         0  0         0
##   20          0        0         0         0  0  1         0  0         0
##   20.35398    0        0         0         0  0  0         0  0         0
##   20.89552    0        0         0         0  0  0         0  0         0
##   21          0        0         0         0  0  0         0  0         0
##   21.21212    0        0         0         0  0  0         0  0         0
##   21.33333    0        0         0         1  0  0         0  0         0
##   22          0        0         0         0  0  0         0  0         0
##   22.38806    0        1         0         0  0  0         0  0         0
##   24          0        0         0         1  0  0         0  0         0
##   25.37313    0        0         0         0  0  0         0  0         0
##   26          0        0         0         0  0  0         0  0         0
##   26.66667    0        0         0         0  0  1         0  0         0
##   27          0        0         0         0  0  0         0  0         0
##   28          0        0         0         0  0  1         0  0         0
##   28.35821    0        0         0         0  0  0         0  0         0
##   29.85075    0        0         0         0  0  0         0  0         0
##   30          0        0         0         0  0  1         0  0         0
##   31.34328    0        0         1         0  0  0         0  0         0
##   39.39394    0        0         0         0  0  0         1  0         0
##   68          0        0         0         0  0  1         0  0         0
##           
##            13.432836 14 14.666667 14.925373 15.151515 16 17.045455 17.910448
##   10.52632         0  0         0         0         0  0         0         0
##   11               0  1         0         0         0  0         0         0
##   12               0  0         0         0         0  0         0         0
##   12.5             0  0         0         0         0  0         1         0
##   13               0  0         0         0         0  0         0         0
##   13.6             0  0         0         0         0  0         0         0
##   14               0  0         0         0         0  0         0         0
##   14.66667         0  0         0         0         0  0         0         0
##   15               0  1         0         0         0  0         0         0
##   15.15152         0  0         0         0         0  0         0         0
##   15.33333         0  0         0         0         0  0         0         0
##   16               0  0         0         0         0  1         0         0
##   16.41791         0  0         0         0         0  0         0         0
##   17               0  0         0         0         0  0         0         0
##   17.04545         0  0         0         0         0  0         0         0
##   17.33333         0  0         0         0         0  1         0         0
##   17.5             0  0         0         0         0  0         0         0
##   17.91045         0  0         0         1         0  0         0         0
##   18.66667         0  0         1         0         0  1         0         0
##   19.40299         0  0         0         0         0  0         0         0
##   20               0  1         0         0         0  0         0         0
##   20.35398         0  0         0         0         0  0         0         0
##   20.89552         0  0         0         0         0  0         0         1
##   21               0  0         0         0         0  1         0         0
##   21.21212         0  0         0         0         1  0         0         0
##   21.33333         0  0         0         0         0  0         0         0
##   22               0  0         0         0         0  0         0         0
##   22.38806         1  0         0         0         0  0         0         0
##   24               0  0         0         0         0  0         0         0
##   25.37313         0  0         0         0         0  0         0         0
##   26               0  1         0         0         0  0         0         0
##   26.66667         0  0         0         0         0  0         0         0
##   27               0  0         0         0         0  0         0         0
##   28               0  0         0         0         0  0         0         0
##   28.35821         0  0         0         0         0  0         0         0
##   29.85075         0  0         0         0         0  0         0         0
##   30               0  0         0         0         0  0         0         0
##   31.34328         0  0         0         0         0  0         0         0
##   39.39394         0  0         0         0         0  0         0         0
##   68               0  0         0         0         0  0         0         0
##           
##            18.181818 19.402985 20 20.895522
##   10.52632         0         0  0         0
##   11               0         0  0         0
##   12               0         0  1         0
##   12.5             0         0  0         0
##   13               0         0  0         0
##   13.6             0         0  0         0
##   14               0         0  0         0
##   14.66667         0         0  0         0
##   15               0         0  0         0
##   15.15152         1         0  0         0
##   15.33333         0         0  0         0
##   16               0         0  0         0
##   16.41791         0         0  0         1
##   17               0         0  0         0
##   17.04545         0         0  0         0
##   17.33333         0         0  0         0
##   17.5             0         0  0         0
##   17.91045         0         0  0         0
##   18.66667         0         0  0         0
##   19.40299         0         0  0         0
##   20               0         0  0         0
##   20.35398         0         0  0         0
##   20.89552         0         0  0         0
##   21               0         0  0         0
##   21.21212         0         0  0         0
##   21.33333         0         0  0         0
##   22               0         0  0         0
##   22.38806         0         0  0         0
##   24               0         0  0         0
##   25.37313         0         1  0         0
##   26               0         0  0         0
##   26.66667         0         0  0         0
##   27               0         0  1         0
##   28               0         0  0         0
##   28.35821         0         0  0         0
##   29.85075         0         0  0         0
##   30               0         0  0         0
##   31.34328         0         0  0         0
##   39.39394         0         0  0         0
##   68               0         0  0         0
#v. La relación entre fibre y manufacturer.
table(UScereal$mfr, UScereal$fibre)
##    
##     0 1 1.333333 1.6 2 2.666667 2.985075 3 3.409091 3.75 4 4.477612 5 5.970149
##   G 9 0        1   1 3        2        0 3        0    0 2        0 1        0
##   K 2 7        2   0 0        1        0 0        0    1 1        2 0        0
##   N 0 0        0   0 0        0        0 0        0    0 0        1 0        1
##   P 3 0        0   0 0        0        0 0        1    0 0        0 0        0
##   Q 2 1        0   0 0        0        1 0        0    0 1        0 0        0
##   R 2 0        1   0 0        0        0 0        0    0 0        1 0        1
##    
##     6.666667 7.462687 8 8.955224 9.090909 12 27.272727 28 30.30303
##   G        0        0 0        0        0  0         0  0        0
##   K        1        1 1        0        0  0         1  1        0
##   N        0        0 0        0        0  0         0  0        1
##   P        0        2 0        1        1  1         0  0        0
##   Q        0        0 0        0        0  0         0  0        0
##   R        0        0 0        0        0  0         0  0        0
#vi. La relación entre sodium y sugars
table(UScereal$sodium, UScereal$sugars)
##            
##             0 0.8 1.769912 2 3 4 4.477612 5.681818 6 6.666667 7.462687 8.270677
##   0         3   0        0 0 0 0        0        0 0        0        0        0
##   51.13636  0   0        0 0 0 0        0        0 0        0        0        0
##   90        0   0        0 0 0 0        0        0 0        0        0        0
##   93.33333  0   0        0 0 0 0        0        0 0        0        0        0
##   125       0   0        0 0 0 0        0        0 0        0        0        0
##   135.33835 0   0        0 0 0 0        0        0 0        0        0        1
##   140       0   0        0 0 0 0        0        0 0        0        0        0
##   159.09091 0   0        0 0 0 0        0        1 0        0        0        0
##   173.33333 0   0        0 1 0 0        0        0 0        0        0        0
##   180       0   0        0 0 0 0        0        0 0        0        0        0
##   186.66667 0   0        0 0 0 0        0        0 0        0        0        0
##   190       0   0        0 0 0 0        0        0 0        0        0        0
##   200       0   0        0 0 3 0        0        0 0        0        0        0
##   212.38938 0   0        1 0 0 0        0        0 0        0        0        0
##   220       0   0        0 0 1 0        0        0 1        0        0        0
##   223.8806  0   0        0 0 0 0        0        0 0        0        0        0
##   226.66667 0   0        0 0 0 0        0        0 0        0        0        0
##   227.27273 0   0        0 0 0 0        0        0 0        0        0        0
##   230       0   0        0 0 1 0        0        0 0        0        0        0
##   232       0   1        0 0 0 0        0        0 0        0        0        0
##   238.80597 0   0        0 0 0 0        0        0 0        0        0        0
##   240       0   0        0 0 0 0        0        0 0        0        0        0
##   253.33333 0   0        0 0 0 0        0        0 0        1        0        0
##   266.66667 0   0        0 0 0 0        0        0 0        0        0        0
##   270       0   0        0 0 0 0        0        0 0        0        0        0
##   280       1   0        0 0 1 0        0        0 0        0        0        0
##   283.58209 0   0        0 0 0 0        0        0 0        0        0        0
##   290       0   0        0 1 1 0        0        0 0        0        0        0
##   293.33333 0   0        0 0 0 0        0        0 0        0        0        0
##   298.50746 0   0        0 0 0 0        0        0 0        0        0        0
##   313.43284 0   0        0 0 0 0        0        0 0        0        1        0
##   320       0   0        0 0 1 0        0        0 0        0        0        0
##   328.35821 0   0        0 0 0 0        0        0 0        0        0        0
##   333.33333 0   0        0 0 0 1        0        0 0        0        0        0
##   340       0   0        0 0 0 0        0        0 0        0        0        0
##   343.28358 0   0        0 0 0 0        1        0 0        0        0        0
##   358.20896 0   0        0 0 0 0        0        0 0        0        0        0
##   373.33333 0   0        0 0 0 0        0        0 0        0        0        0
##   393.93939 0   0        0 0 0 0        0        0 0        0        0        0
##   680       0   0        0 0 0 0        0        0 0        0        0        0
##   787.87879 0   0        0 0 0 0        0        0 0        0        0        0
##            
##             8.75 8.955224 10.447761 10.666667 11 12 12.121212 13 13.333333
##   0            1        0         0         0  0  1         0  0         0
##   51.13636     0        0         0         0  0  0         0  0         0
##   90           0        0         0         0  0  1         0  0         0
##   93.33333     0        0         0         0  0  0         0  0         0
##   125          0        0         0         0  0  0         0  1         0
##   135.33835    0        0         0         0  0  0         0  0         0
##   140          0        0         0         0  0  1         0  0         0
##   159.09091    0        0         0         0  0  0         0  0         0
##   173.33333    0        0         0         0  0  0         0  0         0
##   180          0        0         0         0  0  1         0  2         0
##   186.66667    0        0         0         0  0  0         0  0         1
##   190          0        0         0         0  0  0         0  0         0
##   200          0        0         0         0  0  0         0  0         0
##   212.38938    0        0         0         0  0  0         0  0         0
##   220          0        0         0         0  1  0         0  0         0
##   223.8806     0        1         0         0  0  0         0  0         0
##   226.66667    0        0         0         0  0  1         0  0         0
##   227.27273    0        0         0         0  0  0         1  0         0
##   230          0        0         0         0  0  0         0  0         0
##   232          0        0         0         0  0  0         0  0         0
##   238.80597    0        0         0         0  0  0         0  0         0
##   240          0        0         0         0  0  0         0  0         1
##   253.33333    0        0         0         0  0  0         0  0         0
##   266.66667    0        0         0         1  0  0         0  0         0
##   270          0        0         0         0  0  1         0  0         0
##   280          0        0         0         1  0  1         0  0         0
##   283.58209    0        0         0         0  0  0         0  0         0
##   290          0        0         0         0  0  0         0  0         0
##   293.33333    0        0         0         0  0  0         0  0         0
##   298.50746    0        1         0         0  0  0         0  0         0
##   313.43284    0        0         0         0  0  0         0  0         0
##   320          0        0         0         0  0  0         0  0         0
##   328.35821    0        0         1         0  0  0         0  0         0
##   333.33333    0        0         0         0  0  0         0  0         1
##   340          0        0         0         0  0  0         0  0         0
##   343.28358    0        0         0         0  0  0         0  0         0
##   358.20896    0        0         0         0  0  0         0  0         0
##   373.33333    0        0         0         0  0  1         0  0         0
##   393.93939    0        0         0         0  0  0         0  0         0
##   680          0        0         0         0  0  1         0  0         0
##   787.87879    0        0         0         0  0  0         0  0         0
##            
##             13.432836 14 14.666667 14.925373 15.151515 16 17.045455 17.910448
##   0                 0  0         0         0         0  0         0         0
##   51.13636          0  0         0         0         0  0         1         0
##   90                0  0         0         0         0  0         0         0
##   93.33333          0  0         0         0         0  0         0         0
##   125               0  1         0         0         0  0         0         0
##   135.33835         0  0         0         0         0  0         0         0
##   140               0  0         0         0         0  0         0         0
##   159.09091         0  0         0         0         0  0         0         0
##   173.33333         0  0         0         0         0  0         0         0
##   180               0  0         0         0         0  1         0         0
##   186.66667         0  0         0         0         0  0         0         0
##   190               0  1         0         0         0  0         0         0
##   200               0  0         0         0         0  0         0         0
##   212.38938         0  0         0         0         0  0         0         0
##   220               0  0         0         0         0  0         0         0
##   223.8806          0  0         0         0         0  0         0         0
##   226.66667         0  0         0         0         0  0         0         0
##   227.27273         0  0         0         0         0  0         0         0
##   230               0  0         0         0         0  0         0         0
##   232               0  0         0         0         0  0         0         0
##   238.80597         0  0         0         1         0  0         0         0
##   240               0  0         0         0         0  0         0         0
##   253.33333         0  0         0         0         0  0         0         0
##   266.66667         0  0         1         0         0  0         0         0
##   270               0  0         0         0         0  0         0         0
##   280               0  2         0         0         0  2         0         0
##   283.58209         1  0         0         0         0  0         0         0
##   290               0  0         0         0         0  0         0         0
##   293.33333         0  0         0         0         0  1         0         0
##   298.50746         0  0         0         0         0  0         0         0
##   313.43284         0  0         0         0         0  0         0         0
##   320               0  0         0         0         0  0         0         0
##   328.35821         0  0         0         0         0  0         0         0
##   333.33333         0  0         0         0         0  0         0         0
##   340               0  0         0         0         0  0         0         0
##   343.28358         0  0         0         0         0  0         0         0
##   358.20896         0  0         0         0         0  0         0         1
##   373.33333         0  0         0         0         0  0         0         0
##   393.93939         0  0         0         0         0  0         0         0
##   680               0  0         0         0         0  0         0         0
##   787.87879         0  0         0         0         1  0         0         0
##            
##             18.181818 19.402985 20 20.895522
##   0                 0         0  0         0
##   51.13636          0         0  0         0
##   90                0         0  0         0
##   93.33333          0         0  1         0
##   125               0         0  0         0
##   135.33835         0         0  0         0
##   140               0         0  0         0
##   159.09091         0         0  0         0
##   173.33333         0         0  0         0
##   180               0         0  0         0
##   186.66667         0         0  0         0
##   190               0         0  0         0
##   200               0         0  0         0
##   212.38938         0         0  0         0
##   220               0         0  0         0
##   223.8806          0         1  0         0
##   226.66667         0         0  0         0
##   227.27273         0         0  0         0
##   230               0         0  0         0
##   232               0         0  0         0
##   238.80597         0         0  0         0
##   240               0         0  0         0
##   253.33333         0         0  0         0
##   266.66667         0         0  0         0
##   270               0         0  0         0
##   280               0         0  0         0
##   283.58209         0         0  0         0
##   290               0         0  0         0
##   293.33333         0         0  0         0
##   298.50746         0         0  0         1
##   313.43284         0         0  0         0
##   320               0         0  0         0
##   328.35821         0         0  0         0
##   333.33333         0         0  0         0
##   340               0         0  1         0
##   343.28358         0         0  0         0
##   358.20896         0         0  0         0
##   373.33333         0         0  0         0
##   393.93939         1         0  0         0
##   680               0         0  0         0
##   787.87879         0         0  0         0
#punto 2.4. El conjunto de datos mammals contiene datos sobre la relación entre peso corporal y peso del cerebro
#a) ¿Cuál es la correlación lineal entre estas variables? 
cor(mammals$body, mammals$brain)
## [1] 0.9341638
#b) Representa los datos mediante la instrucción plot
plot(mammals)

#c) Transforma los datos mediante la función log y repite el estudio. ¿Cómo cambian los resultados?
plot(log(mammals))

#punto 2.5 Enlaza la base de datos emissions del paquete UsingR. 
data("emissions")
head(emissions)
##                   GDP perCapita  CO2
## UnitedStates  8083000     29647 6750
## Japan         3080000     24409 1320
## Germany       1740000     21197 1740
## France        1320000     22381  550
## UnitedKingdom 1242000     21010  675
## Italy         1240000     21856  540
#a Estudia la relación entre las variables GDP (Gross Domestic Product), perCapita (pues eso) y CO2 (Emisiones de CO2) de cada pais. 
pairs(emissions)

cor(emissions)
##                 GDP perCapita       CO2
## GDP       1.0000000 0.4325303 0.9501753
## perCapita 0.4325303 1.0000000 0.2757962
## CO2       0.9501753 0.2757962 1.0000000
#b Construye un modelo de regresión para predecir las emisiones de CO2 a partir de cada una de las variables. 
r_lineal= lm(emissions$CO2 ~ emissions$GDP + emissions$perCapita, data = emissions)
summary(r_lineal)
## 
## Call:
## lm(formula = emissions$CO2 ~ emissions$GDP + emissions$perCapita, 
##     data = emissions)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1037.3  -167.4    10.8   153.2  1052.0 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          5.100e+02  2.044e+02   2.495   0.0202 *  
## emissions$GDP        8.406e-04  5.198e-05  16.172 4.68e-14 ***
## emissions$perCapita -3.039e-02  1.155e-02  -2.631   0.0149 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 382.8 on 23 degrees of freedom
## Multiple R-squared:  0.9253, Adjusted R-squared:  0.9188 
## F-statistic: 142.5 on 2 and 23 DF,  p-value: 1.102e-13
plot(emissions$GDP+emissions$perCapita,emissions$CO2)
abline(r_lineal,col = "darkblue")
## Warning in abline(r_lineal, col = "darkblue"): only using the first two of 3
## regression coefficients

emissions$CO2
##  [1] 6750 1320 1740  550  675  540 2000  700  370  480  240  400  145   75   80
## [16]   54   75  125  420   75   56  160  150   76   85   63
CO2_predict <- predict(r_lineal,emissions)
plot(emissions$GDP+emissions$perCapita,CO2_predict)

CO2_predict
##  UnitedStates         Japan       Germany        France UnitedKingdom 
##   6403.720110   2357.274571   1328.457202    939.412260    915.510264 
##         Italy        Russia        Canada         Spain     Australia 
##    888.117914    948.030553    418.174003    551.546727    203.695487 
##   Netherlands        Poland       Belgium        Sweden       Austria 
##    137.905405    524.997147      3.295727     57.168681      6.260516 
##   Switzerland      Portugal        Greece       Ukraine       Denmark 
##    -65.251059    177.533136    235.468677    538.782254    -82.034073 
##        Norway       Romania CzechRepublic       Finland       Hungary 
##   -213.820805    449.888660    273.235200     -5.729292    353.120804 
##       Ireland 
##     59.239930
cor(emissions$CO2,CO2_predict)
## [1] 0.9619321
#c Identifica los outliers y prueba de ajustar el modelo de nuevo sin ellos.
boxplot(emissions)

boxplot.stats(emissions$CO2)$out 
## [1] 6750 1320 1740 2000
out_emisiones <- which(emissions$CO2 %in% c(boxplot.stats(emissions$CO2)$out)) #obtener datos atípicos de emisiones de CO2
out_emisiones
## [1] 1 2 3 7
CO2.sin.out = emissions$CO2[-c(out_emisiones)]
CO2.sin.out #vector datos atípicos
##  [1] 550 675 540 700 370 480 240 400 145  75  80  54  75 125 420  75  56 160 150
## [20]  76  85  63
GDP.sin.out = emissions$GDP[-c(out_emisiones)]
perCapita.sin.out = emissions$perCapita[-c(out_emisiones)]

#Ajuste del modelo
r_lineal2<- lm(CO2.sin.out ~ GDP.sin.out + perCapita.sin.out, data = emissions)
summary(r_lineal2)
## 
## Call:
## lm(formula = CO2.sin.out ~ GDP.sin.out + perCapita.sin.out, data = emissions)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -130.88  -63.84  -32.27   16.79  334.38 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        2.233e+02  7.202e+01   3.101  0.00588 ** 
## GDP.sin.out        4.912e-04  6.989e-05   7.028 1.09e-06 ***
## perCapita.sin.out -8.525e-03  4.114e-03  -2.072  0.05209 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 121.4 on 19 degrees of freedom
## Multiple R-squared:  0.7225, Adjusted R-squared:  0.6933 
## F-statistic: 24.73 on 2 and 19 DF,  p-value: 5.142e-06
plot(GDP.sin.out + perCapita.sin.out,CO2.sin.out)
abline(r_lineal2,col = "red")
## Warning in abline(r_lineal2, col = "red"): only using the first two of 3
## regression coefficients

CO2.sin.out
##  [1] 550 675 540 700 370 480 240 400 145  75  80  54  75 125 420  75  56 160 150
## [20]  76  85  63
CO2_predict_sin_out <- predict(r_lineal2,emissions)
## Warning: 'newdata' had 26 rows but variables found have 22 rows
plot(GDP.sin.out + perCapita.sin.out,CO2_predict_sin_out)

CO2_predict_sin_out
##         1         2         3         4         5         6         7         8 
## 680.88253 654.25862 646.06437 365.61631 399.04321 238.03584 206.78744 299.22605 
##         9        10        11        12        13        14        15        16 
## 141.55107 141.31414 126.49820 106.00521 168.25755 181.41829 263.30493  88.55427 
##        17        18        19        20        21        22 
##  51.07759 235.63791 185.49960 104.74791 198.02424 112.19471
cor(CO2.sin.out,CO2_predict_sin_out)
## [1] 0.8499957
#Boxplots de CO2 con outliers y sin outliers respectivamente:
boxplot(emissions$CO2)

boxplot(CO2.sin.out)

#Punto 2.6 La base de datos MASS posee la siguiente base de datos: “anorexia” que contiene el cambio de peso en pacientes femeninas la cual posee 72 filas y 3 columnas. 
data("anorexia")
head(anorexia)
##   Treat Prewt Postwt
## 1  Cont  80.7   80.2
## 2  Cont  89.4   80.1
## 3  Cont  91.8   86.4
## 4  Cont  74.0   86.3
## 5  Cont  78.1   76.1
## 6  Cont  88.3   78.1
#a) ¿Que Tipo de tratamiento más efectivo?
hist(anorexia)

mean_anorexia <- aggregate(anorexia[,2:length(anorexia)], by=list(anorexia$Treat), FUN = mean)
mean_anorexia["diferencia"] <- mean_anorexia$Postwt - mean_anorexia$Prewt
mean_anorexia[mean_anorexia$diferencia == max(mean_anorexia$diferencia),]
##   Group.1    Prewt   Postwt diferencia
## 3      FT 83.22941 90.49412   7.264706
#b) ¿Cuantos pacientes ganaron y cuantos perdieron peso?
anorexia["diferencia"] <- anorexia$Postwt - anorexia$Prewt
#Total que Ganaron peso:
ganaron <- length(anorexia[anorexia$diferencia > 0,"diferencia"])
ganaron
## [1] 42
#Total que perdieron peso:
perdieron <- length(anorexia[anorexia$diferencia < 0,"diferencia"])
perdieron
## [1] 29
#c) Grafique el punto b de forma adecuada
barplot(c(ganaron,perdieron), main = "Pacientes que ganaron y que perdieron peso", ylab = "Cantidad de pacientes", col = c("red","skyblue"))
legend("topright", legend = c(paste("Ganaron peso: ",ganaron),paste("Perdieron peso: ",perdieron)),fill = c("red","skyblue"))

#punto 2.7 La base de datos MASS posee la siguiente base de datos: “Melanoma” que contiene 205 pacientes con melanomas y 7 columnas.
data("Melanoma")
head(Melanoma)
##   time status sex age year thickness ulcer
## 1   10      3   1  76 1972      6.76     1
## 2   30      3   1  56 1968      0.65     0
## 3   35      2   1  41 1977      1.34     0
## 4   99      3   0  71 1968      2.90     0
## 5  185      1   1  52 1965     12.08     1
## 6  204      1   1  28 1971      4.84     1
#a) Obtenga el número de fallecidos por Melanoma y otras causas.
length(Melanoma[Melanoma$status == 1 | Melanoma$status == 3,"status"])
## [1] 71
#b) Determine la presencia y ausencia de Melanoma.
colores = c("dimgrey","coral","pink")
pie(table(Melanoma$status),labels = c(nrow(Melanoma[Melanoma$status==1,]),nrow(Melanoma[Melanoma$status==2,]), nrow(Melanoma[Melanoma$status==3,])),main = "Presencia y ausencia de Melanoma en una base de datos de 205 personas", col = colores)
legend("topright",c(paste(1,":  MuertexMelanoma"),paste(2,": Vivo"),paste(3,": MuertexOtras causas")),fill = colores)

#c) ¿Que Relación entre tamaño de tumor y muerte?
Melanoma1 = Melanoma
s1 <- which((Melanoma1$status==1)) #Posiciones de las personas muertas
s2 <- which((Melanoma1$status==2)) #Posiciones de las personas vivas
s3 <- which((Melanoma1$status==3)) #Posiciones de las personas muertas por otras causas

#Se reagrupa las personas que murieron por Melanoma u otras causas con valor=1  y los que no murieron con valor=0. Esto con el fin de sacar la correlación entre muerte y tamaño del tumor:
Melanoma1$status = replace(Melanoma1$status,s3,1) #Remplazar en status el valor de las personas que murieron por otras causas
Melanoma1$status = replace(Melanoma1$status,s2,0) #Remplazar en status el valor de las personas que no muerieron 
c(paste("Correlación entre el tamaño del tumor y estado del paciente: ",cor(Melanoma1$thickness,Melanoma1$status),3))
## [1] "Correlación entre el tamaño del tumor y estado del paciente:  0.314179811783222 3"
#d) Grafique el punto b de forma adecuada.
remplazo = replace(Melanoma1$status,Melanoma1$status==0,"Vivos")
remplazo = replace(remplazo,Melanoma1$status==1,"Fallecidos")
boxplot(Melanoma1$thickness~remplazo, main = "Relación entre tamaño de tumor y muerte",ylab = "Tamaño del tumor (mm)",xlab = "Estado del paciente")

#Punto 2.8. La base de datos UsingR posee la siguiente base de datos: “babyboom” que contiene la estadística de nacimiento de 44 bebes en un periodo de 24 horas con peso y sexo, con 4 columnas
data("babyboom")
head(babyboom)
##   clock.time gender   wt running.time
## 1          5   girl 3837            5
## 2        104   girl 3334           64
## 3        118    boy 3554           78
## 4        155    boy 3838          115
## 5        257    boy 3625          177
## 6        405   girl 2208          245
#a) ¿Cual es el número de niños y niñas?
table(babyboom$gender)
## 
## girl  boy 
##   18   26
#b)Cual es la cantidad de niños nacidos en las primeras 12h
print("Niños nacidos en las primeras 12 horas ")
## [1] "Niños nacidos en las primeras 12 horas "
babyboom[babyboom$clock.time<= 708,] 
##    clock.time gender   wt running.time
## 1           5   girl 3837            5
## 2         104   girl 3334           64
## 3         118    boy 3554           78
## 4         155    boy 3838          115
## 5         257    boy 3625          177
## 6         405   girl 2208          245
## 7         407   girl 1745          247
## 8         422    boy 2846          262
## 9         431    boy 3166          271
## 10        708    boy 3520          428
primeros_nacidos = nrow(babyboom[babyboom$clock.time<708,])
primeros_nacidos
## [1] 9
#c)Cuantos niños nacieron por debajo de 3000gr
print("Niños con pesos menores a 3000gr: ")
## [1] "Niños con pesos menores a 3000gr: "
ninos_bajo_peso= nrow(babyboom[babyboom$gender=='boy' & babyboom$wt<3000,])
ninos_bajo_peso
## [1] 4
#d)Relación entre peso por debajo de 3000gr y sexo
barplot(table(babyboom$gender,babyboom$wt<3000),beside = T,col = c("pink","blue"),xlab="Género",ylab="cantidad")

#e)Grafique el promedio de pesos total, de niños y de niñas de forma adecuada
p_ninos = median(babyboom$wt[babyboom$gender=='boy'])
p_ninos
## [1] 3404
p_ninas = median(babyboom$wt[babyboom$gender=='girl'])
p_ninas
## [1] 3381
#Graficar:
boxplot(babyboom$wt,ylab = "Peso (gr)",main = "Promedio de pesos total (niños y niñas)")
points(p_ninos, col = "skyblue", pch = 15)
points(p_ninas, col = "red", pch = 15)
legend(x = "topleft", legend = c(paste("Niños: ",p_ninos),paste("Niñas: ",p_ninas)), fill = c("skyblue", "red"),title = "Promedio de pesos total: ")

#2.9 La base de datos UsingR posee la siguiente base de datos: “Aids2” que contiene la estadística de 2843 pacientes con sida con 4 columnas.
data("Aids2")
head(Aids2)
##   state sex  diag death status T.categ age
## 1   NSW   M 10905 11081      D      hs  35
## 2   NSW   M 11029 11096      D      hs  53
## 3   NSW   M  9551  9983      D      hs  42
## 4   NSW   M  9577  9654      D    haem  44
## 5   NSW   M 10015 10290      D      hs  39
## 6   NSW   M  9971 10344      D      hs  36
#a) Determine en número de contagios por estado
table(Aids2$state,Aids2$T.categ)
##        
##           hs hsid   id  het haem blood mother other
##   NSW   1539   50   28   18   30    70      3    42
##   Other  204    4   12    8    6     5      2     8
##   QLD    186    7    4    5    4    15      1     4
##   VIC    536   11    4   10    6     4      1    16
#b) ¿Cuanto es el número de fallecidos?
table(Aids2$status)
## 
##    A    D 
## 1082 1761
N_fallecidos = Aids2[Aids2$status=='D',]
print(paste("Cantidad de personas fallecidas: ",length(N_fallecidos$status)))
## [1] "Cantidad de personas fallecidas:  1761"
#c) ¿Cual es la relación entre sexo y tipo de transmisión?
table(Aids2$sex,Aids2$T.categ)
##    
##       hs hsid   id  het haem blood mother other
##   F    1    0   20   20    0    37      4     7
##   M 2464   72   28   21   46    57      3    63
barplot(table(Aids2$sex,Aids2$T.categ),beside = T,col = c("pink","darkgreen"),xlab="Tipo de transmición por sexos",ylab="cantidad") 
legend("topright",levels(Aids2$sex),fill = c("pink","darkgreen"))

#Relación entre F (mujeres) y tipos de transmisión:
relacion_F_trans = Aids2[Aids2$sex=='F',]
barplot(table(relacion_F_trans$sex=='F',relacion_F_trans$T.categ),main = "Relación entre mujeres y tipo de transmisión",ylab = "Cantidad de pacientes")

# Relación entre M (hombres) y tipos de transmisión:
relacion_M_trans = Aids2[Aids2$sex=='M',]
barplot(table(relacion_M_trans$sex=='M',relacion_M_trans$T.categ),main = "Relación entre hombres y tipo de transmisión",ylab = "Cantidad de pacientes")

#d) ¿Grafique de forma adecuada los tipos de transmisión?
colores = c("Red","blue","violet","red","green","thistle","orange","yellow")
pie(table(Aids2$T.categ),col = colores,labels = table(Aids2$T.categ), main = "Cantidad y tipos de transmisión")
legend("topright",legend = levels(Aids2$T.categ),fill = colores)

table(Aids2$T.categ)
## 
##     hs   hsid     id    het   haem  blood mother  other 
##   2465     72     48     41     46     94      7     70
#Punto 2.10. La base de datos UsingR posee la siguiente base de datos: “crime” que contiene la tasa de crímenes de 50 estados de los E.E.U.U en los años 1983 y 1993, posee 3 columnas: Estado (no marcado), y1983, y1993. Se requiere un informe con los siguientes puntos.

data(crime)
head(crime)
##            y1983  y1993
## Alabama    416.0  871.7
## Alaska     613.8  660.5
## Arizona    494.2  670.8
## Arkansas   297.7  576.5
## California 772.6 1119.7
## Colorado   476.4  578.8
#a) La tasa total en 1993 fue mayor o menor que en 1983.
mean(crime$y1993)
## [1] 606.8294
mean(crime$y1983)
## [1] 437.5196
#b) Qué estado presenta la mayor tasa de crímenes en cada año.
crime[crime$y1993 == max(crime$y1993),]
##     y1983  y1993
## DC 1985.4 2832.8
crime[crime$y1983 == max(crime$y1983),]
##     y1983  y1993
## DC 1985.4 2832.8
#c) Que estado presenta la mayor tasa de crimen acumulado en ambos anos.
crime["acumulado"] <- crime$y1983 + crime$y1993
crime[crime$acumulado == max(crime$acumulado),]
##     y1983  y1993 acumulado
## DC 1985.4 2832.8    4818.2
#d) Gráfica el punto b de forma adecuada.
ggplot(crime, aes(y=acumulado,x=row.names(crime)))+geom_bar(position="stack", stat="identity")