2.1 Carga(O Intala primero y luego carga) el paquete UsingR

library(UsingR)
## Warning: package 'UsingR' was built under R version 4.3.3
## Loading required package: MASS
## Loading required package: HistData
## Warning: package 'HistData' was built under R version 4.3.3
## Loading required package: Hmisc
## Warning: package 'Hmisc' was built under R version 4.3.3
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(MASS)

A.¿Cuantos Conjuntos de Datos de trabajo contiene el paquete?

data(package = "UsingR")
length(ls("package:UsingR"))
## [1] 150

B. Represente graficamente los datos contenidos en los conjuntos de datos(“datasets”) bumpers,firstchi, math con histograma y/o un boxplot

par(mfrow=c(1,2))

hist(bumpers, main = "Histograma de Bumpers", xlab = "Valores", ylab = "Frecuencia")
boxplot(bumpers, main = "Boxplot de Bumpers", ylab = "Valores")

par(mfrow=c(1,2))

hist(firstchi, main = "Histograma de Firstchi", xlab = "Valores", ylab = "Frecuencia")
boxplot(firstchi, main = "Boxplot de Firstchi", ylab = "Valores")

par(mfrow=c(1,2))

hist(math, main = "Histograma de Math", xlab = "Valores", ylab = "Frecuencia")
boxplot(math, main = "Boxplot de Math", ylab = "Valores")

C. Estima visualmente las medias, medianas y desviaciones estandar de cada conjunto de datos y a continuación calcula los valores anteriores con las funciones adecuadas.¿Qué grafico resulta de mayor ayuda para la aproximación?.

#Bumpers
mean(bumpers)
## [1] 2122.478
median(bumpers)
## [1] 2129
sd(bumpers)
## [1] 798.4574
hist(bumpers, main = "Histograma de Bumpers", xlab = "Valores", ylab = "Frecuencia")

#Firstchi
hist(firstchi, main = "Histograma de Firstchi", xlab = "Valores", ylab = "Frecuencia")

mean(firstchi)
## [1] 23.97701
median(firstchi)
## [1] 23
sd(firstchi)
## [1] 6.254258
#Math
hist(math, main = "Histograma de Math", xlab = "Valores", ylab = "Frecuencia")

mean(math)
## [1] 54.9
median(math)
## [1] 54
sd(math)
## [1] 9.746264

2.2 El conjunto de datos brightness contiene información sobre el brillo de 963 estrellas.

A. Represente estos datos mediante un histograma y un grafico de densidad superpuesto.

data(brightness)
hist(brightness, prob = TRUE)
lines(density(brightness),lwd=2, col="blue")

B. Representa graficamente estos datos mediante un histograma de caja (boxplot).¿Dirias que los datos presentan “outliers”? Cual es el segundo menor outlier.

outliers = boxplot(brightness)$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

Sí presentan outliers. El segundo menor outliers es:

#Extraer el segundo menor outliner
min(brightness[brightness > min(brightness)])
## [1] 2.28

C. 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.

quantile(brightness)
##      0%     25%     50%     75%    100% 
##  2.0700  7.7025  8.5000  9.1300 12.4300
brightness.sin= brightness[brightness >  7.7025 & brightness < 9.130]
boxplot(brightness.sin) 

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 ...
head(UScereal)

B. Utiliza los datos de cereales para investigar algunas asociaciones entre sus variables:

1. 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

2. La relacion entre fat y vitamins.

 table(UScereal$fat,UScereal$vitamins)
##            
##             100% enriched none
##   0            1       18    3
##   0.6666667    0        1    0
##   1            3        7    0
##   1.1363636    0        1    0
##   1.3333333    1        8    0
##   1.4925373    0        4    0
##   1.6          0        1    0
##   2            0        2    0
##   2.6666667    0        3    0
##   2.9850746    0        4    0
##   3.030303     0        2    0
##   4            0        4    0
##   6            0        1    0
##   9.0909091    0        1    0

3. La relacion entre fat y shelf

 table(UScereal$fat,UScereal$shelf)
##            
##              1  2  3
##   0         10  3  9
##   0.6666667  0  1  0
##   1          2  5  3
##   1.1363636  0  0  1
##   1.3333333  2  4  3
##   1.4925373  2  1  1
##   1.6        1  0  0
##   2          0  1  1
##   2.6666667  1  1  1
##   2.9850746  0  1  3
##   3.030303   0  0  2
##   4          0  1  3
##   6          0  0  1
##   9.0909091  0  0  1

4. 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

5. La relación entre fibre y manufacturer.

table(UScereal$fibre,UScereal$mfr)
##            
##             G K N P Q R
##   0         9 2 0 3 2 2
##   1         0 7 0 0 1 0
##   1.333333  1 2 0 0 0 1
##   1.6       1 0 0 0 0 0
##   2         3 0 0 0 0 0
##   2.666667  2 1 0 0 0 0
##   2.985075  0 0 0 0 1 0
##   3         3 0 0 0 0 0
##   3.409091  0 0 0 1 0 0
##   3.75      0 1 0 0 0 0
##   4         2 1 0 0 1 0
##   4.477612  0 2 1 0 0 1
##   5         1 0 0 0 0 0
##   5.970149  0 0 1 0 0 1
##   6.666667  0 1 0 0 0 0
##   7.462687  0 1 0 2 0 0
##   8         0 1 0 0 0 0
##   8.955224  0 0 0 1 0 0
##   9.090909  0 0 0 1 0 0
##   12        0 0 0 1 0 0
##   27.272727 0 1 0 0 0 0
##   28        0 1 0 0 0 0
##   30.30303  0 0 1 0 0 0

6. 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

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?

data(mammals)
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))

2.5. Enlaza la base de datos emissions del paquete UsingR.

data(emissions)
str(emissions)
## 'data.frame':    26 obs. of  3 variables:
##  $ GDP      : num  8083000 3080000 1740000 1320000 1242000 ...
##  $ perCapita: num  29647 24409 21197 22381 21010 ...
##  $ CO2      : num  6750 1320 1740 550 675 540 2000 700 370 480 ...
head(emissions)

A. Estudia la relación entre las variables GDP (Gross Domestic Product), perCapita (pues eso) y CO2 (Emisiones de CO2) de cada pais.

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
pairs(emissions[, c("GDP", "perCapita", "CO2")])

B. Construye un modelo de regresión para predecir las emisiones de CO2 a partir de cada una de las variables.

#PerCapita
capita_r=lm(perCapita ~ CO2,data = emissions)
summary(capita_r)
## 
## Call:
## lm(formula = perCapita ~ CO2, data = emissions)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -15006  -3491   2955   4648  10350 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 16714.300   1586.333  10.536 1.75e-10 ***
## CO2             1.509      1.074   1.406    0.173    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7211 on 24 degrees of freedom
## Multiple R-squared:  0.07606,    Adjusted R-squared:  0.03757 
## F-statistic: 1.976 on 1 and 24 DF,  p-value: 0.1726
#GDP
gdp_r= lm(GDP ~ CO2, data = emissions)
summary(gdp_r)
## 
## Call:
## lm(formula = GDP ~ CO2, data = emissions)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1675695  -125418   -11255    47815  1497913 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  57082.67  114313.83   0.499    0.622    
## CO2           1155.31      77.37  14.933  1.2e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 519600 on 24 degrees of freedom
## Multiple R-squared:  0.9028, Adjusted R-squared:  0.8988 
## F-statistic:   223 on 1 and 24 DF,  p-value: 1.197e-13

C. Identifica los outliers y prueba de ajustar el modelo de nuevo sin ellos.

boxplot(emissions)

emissions_sin = which(emissions$CO2 %in% c(boxplot.stats(emissions$CO2)$out))
 
CO2_sin = emissions$CO2[-c(emissions_sin)]
 
GDP_sin = emissions$GDP[-c(emissions_sin)]

perCapita_sin = emissions$perCapita[-c(emissions_sin)]

regresin_sin = lm(CO2_sin ~ GDP_sin + perCapita_sin,  data = emissions)
summary(regresin_sin)
## 
## Call:
## lm(formula = CO2_sin ~ GDP_sin + perCapita_sin, 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        4.912e-04  6.989e-05   7.028 1.09e-06 ***
## perCapita_sin -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 + perCapita_sin,CO2_sin)

boxplot(emissions$CO2)

boxplot(CO2_sin)

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)
str(anorexia)
## 'data.frame':    72 obs. of  3 variables:
##  $ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Prewt : num  80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
##  $ Postwt: num  80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
head(anorexia)

A. ¿Que Tipo de tratamiento más efectivo?

tratamiento = which(anorexia$Postwt>anorexia$Prewt)
caso_exito = anorexia[c(tratamiento),]
mejor_tra = which.max(table(caso_exito$Treat))
mejor_tra  = c(paste(names(mejor_tra),": 
",max(table(caso_exito$Treat))," casos exitosos"))
table(caso_exito$Treat)
## 
##  CBT Cont   FT 
##   18   11   13
mejor_tra
## [1] "CBT : \n 18  casos exitosos"

B. ¿Cuantos pacientes ganaron y cuantos perdieron peso?

#Casos donde ganan peso
perdida_peso = which(anorexia$Postwt<anorexia$Prewt)
casos_fracaso = anorexia[c(perdida_peso),]
ganar_peso = length(tratamiento)
ganar_peso
## [1] 42
#Casos donde pierden peso
pierden_peso = nrow(casos_fracaso)
pierden_peso
## [1] 29

C. Grafique el punto b de forma adecuada

barplot(c(ganar_peso,pierden_peso), main = "Pacientes ganaron y perdieron peso", ylab = "Numero de pacientes", col = c("blue","red"))
legend("topright", legend = c(paste("Ganan peso: 
",ganar_peso),paste("Perdida peso: ",pierden_peso)),
 fill = c("blue","red"))

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)
str(Melanoma)
## 'data.frame':    205 obs. of  7 variables:
##  $ time     : int  10 30 35 99 185 204 210 232 232 279 ...
##  $ status   : int  3 3 2 3 1 1 1 3 1 1 ...
##  $ sex      : int  1 1 1 0 1 1 1 0 1 0 ...
##  $ age      : int  76 56 41 71 52 28 77 60 49 68 ...
##  $ year     : int  1972 1968 1977 1968 1965 1971 1972 1974 1968 1971 ...
##  $ thickness: num  6.76 0.65 1.34 2.9 12.08 ...
##  $ ulcer    : int  1 0 0 0 1 1 1 1 1 1 ...
head(Melanoma)

A. Obtenga el número de fallecidos por Melanoma y otras causas.

fallecidos = nrow(Melanoma[Melanoma$status==1,]) + nrow(Melanoma[Melanoma$status==3,])
fallecidos
## [1] 71

B. Determine la presencia y ausencia de Melanoma.

# No  tienen ulcera
sum(Melanoma$ulcer=="0") 
## [1] 115
# Si tienen ulcera
sum(Melanoma$ulcer=="1") 
## [1] 90

C. ¿Que relacion entre tamaño de tumor y muerte?.

table(Melanoma$thickness, Melanoma$status)
##        
##          1  2  3
##   0.1    0  1  0
##   0.16   0  6  1
##   0.24   0  1  0
##   0.32   1  5  0
##   0.48   0  4  0
##   0.58   0  1  0
##   0.64   0  4  0
##   0.65   0  8  2
##   0.81   3  8  0
##   0.97   2  9  0
##   1.03   0  1  0
##   1.13   0  4  0
##   1.29   0 14  2
##   1.34   1  1  0
##   1.37   0  1  0
##   1.45   0  2  1
##   1.53   0  1  0
##   1.62   3  8  1
##   1.76   1  0  0
##   1.78   0  2  0
##   1.94   2  8  0
##   2.1    1  2  0
##   2.24   1  0  0
##   2.26   3  2  0
##   2.34   1  0  0
##   2.42   1  0  0
##   2.58   3  6  0
##   2.74   0  1  0
##   2.9    0  2  1
##   3.06   1  1  0
##   3.22   2  7  1
##   3.54   5  3  0
##   3.56   1  0  0
##   3.87   3  3  0
##   4.04   1  0  0
##   4.09   0  1  0
##   4.19   2  0  0
##   4.51   1  0  0
##   4.82   0  1  0
##   4.83   1  1  0
##   4.84   3  1  1
##   5.16   2  1  0
##   5.48   1  1  0
##   5.64   0  1  0
##   5.8    2  0  0
##   6.12   0  1  1
##   6.44   1  0  0
##   6.76   0  0  1
##   7.06   1  1  0
##   7.09   0  2  0
##   7.41   1  0  0
##   7.73   1  1  0
##   7.89   0  1  0
##   8.06   0  1  0
##   8.38   0  1  0
##   8.54   0  0  1
##   9.66   0  1  0
##   12.08  1  0  0
##   12.24  0  1  0
##   12.56  0  0  1
##   12.88  1  1  0
##   13.85  1  0  0
##   14.66  1  0  0
##   17.42  1  0  0

D. Grafique el punto b de forma adecuada

barplot(table(Melanoma$ulcer), main="Presencia y ausencia de Melanoma", xlab= "ulcer", ylab = "Cantidad", beside = TRUE, legend.text = c("Ausencia","Presencia"), col=c("orange","blue"))

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)
str(babyboom)
## 'data.frame':    44 obs. of  4 variables:
##  $ clock.time  : num  5 104 118 155 257 405 407 422 431 708 ...
##  $ gender      : Factor w/ 2 levels "girl","boy": 1 1 2 2 2 1 1 2 2 2 ...
##  $ wt          : num  3837 3334 3554 3838 3625 ...
##  $ running.time: num  5 64 78 115 177 245 247 262 271 428 ...

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?

cantidad_nacidos= (babyboom[babyboom$clock.time<=708,])
cantidad_nacidos
primeras_horas= nrow(babyboom[babyboom$clock.time< 708,])
cat("Niños que nacieron en las primeras 12 horas: ", primeras_horas)
## Niños que nacieron en las primeras 12 horas:  9

C. ¿Cuantos niños nacieron por debajo de 3000gr?

nacidos_m= nrow(babyboom[babyboom$gender == "boy" & babyboom$wt<3000,])
cat("Niños con un peso menor a los 3000gr: ", nacidos_m)
## Niños con un peso menor a los 3000gr:  4

D. ¿Relación entre peso por debajo de 3000gr y sexo?

relacion = babyboom[babyboom$wt < 3000,]
table(relacion$gender,relacion$wt)
##       
##        1745 2121 2184 2208 2383 2576 2635 2846 2902
##   girl    1    0    1    1    1    1    0    0    0
##   boy     0    1    0    0    0    0    1    1    1
barplot(table(relacion$gender,relacion$wt<3000), beside = T, main = "Relación entre peso y sexo", col = c("blue", "red"), xlab = "Genero", 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
barplot(c(p_ninos,p_ninas),ylab = "Peso (gr)",main = "Promedio de pesos total (niños y niñas)",names.arg=c("Niña", "Niño"), col = c("blue", "violet"), ylim = c(0, 3500))

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)
str(Aids2)
## 'data.frame':    2843 obs. of  7 variables:
##  $ state  : Factor w/ 4 levels "NSW","Other",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ sex    : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
##  $ diag   : int  10905 11029 9551 9577 10015 9971 10746 10042 10464 10439 ...
##  $ death  : int  11081 11096 9983 9654 10290 10344 11135 11069 10956 10873 ...
##  $ status : Factor w/ 2 levels "A","D": 2 2 2 2 2 2 2 2 2 2 ...
##  $ T.categ: Factor w/ 8 levels "hs","hsid","id",..: 1 1 1 5 1 1 8 1 1 2 ...
##  $ age    : int  35 53 42 44 39 36 36 31 26 27 ...

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
fallecidos = sum(Aids2$status == "D")
cat("Número de fallecidos: ",fallecidos)
## Número de fallecidos:  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), xlab= "Tipo de Transmisión",ylab="Cantidad", beside=TRUE, col=c("red", "green"))

legend("topright",levels(Aids2$sex),fill =c("red","green"))

mujer_trans = Aids2[Aids2$sex=='F',]
barplot(table(mujer_trans$sex=='F',mujer_trans $T.categ), main = "Relación Mujeres y tipo de transmisión",ylab = "Cantidad", col="red")

hombre_trans = Aids2[Aids2$sex=='M',]
barplot(table(hombre_trans$sex=='M',hombre_trans$T.categ), main = "Relación Hombres y tipo de transmisión",ylab = "Cantidad", col="blue")

D. Grafique de forma adecuada los tipos de transmisión

colores = c("Red","blue","yellow","red","green","pink","orange","brown")

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)

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)
str(crime)
## 'data.frame':    51 obs. of  2 variables:
##  $ y1983: num  416 614 494 298 773 ...
##  $ y1993: num  872 660 671 576 1120 ...

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
total_83 = round(sum(crime$y1983))
total_83
## [1] 22313
total_93 = round(sum(crime$y1993))
total_93
## [1] 30948

B. Qué estado presenta la mayor tasa de crímenes en cada año.

max_estado_1993 = crime[crime$y1993 == max(crime$y1993),]
max_estado_1993
max_estado_1983 = crime[crime$y1983 == max(crime$y1983),]
max_estado_1983

C. Que estado presenta la mayor tasa de crimen acumulado en ambos años.

crime["acumulado"] = crime$y1983 + crime$y1993

crime[crime$acumulado == max(crime$acumulado),]

D. Gráfica el punto b de forma adecuada.

orden_estados = rownames(crime)[order(crime$y1983)]
crime_sorted = crime[orden_estados, ]
numeros = 1:nrow(crime_sorted)

barplot(t(as.matrix(crime_sorted[, c("y1983", "y1993")])), beside = TRUE, main = "Tasa de Crimenes por Estado en 1983 y 1993", xlab = " Estados", ylab = "Tasa de Crimen", col = c("red", "blue"), legend.text = c("1983", "1993"), args.legend = list(x = "topleft", bty = "n"))