install.packages("MASS")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("UsingR")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
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)


install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.1     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ dplyr::select()    masks MASS::select()
## ✖ dplyr::src()       masks Hmisc::src()
## ✖ dplyr::summarize() masks Hmisc::summarize()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
ggplot(data = mpg, aes(x = displ, y = hwy))+
  geom_point()

# PUNTO A-------------------------------------------------------------
data()
data(package="UsingR")
packageDescription("UsingR")
## Package: UsingR
## Version: 2.0-7
## Title: Data Sets, Etc. for the Text "Using R for Introductory
##         Statistics", Second Edition
## Author: John Verzani <verzani@math.csi.cuny.edu>
## Maintainer: John Verzani <verzani@math.csi.cuny.edu>
## Description: A collection of data sets to accompany the textbook "Using
##         R for Introductory Statistics," second edition.
## Depends: R (>= 2.15.0), MASS, HistData, Hmisc
## Suggests: zoo, ggplot2, vcd, lubridate, aplpack
## License: GPL (>= 2)
## LazyData: TRUE
## NeedsCompilation: no
## Packaged: 2022-01-10 19:16:26 UTC; jverzani
## Repository: RSPM
## Date/Publication: 2022-01-11 09:52:45 UTC
## Encoding: UTF-8
## Built: R 4.2.0; ; 2022-04-23 03:09:18 UTC; unix
## 
## -- File: /cloud/lib/x86_64-pc-linux-gnu-library/4.2/UsingR/Meta/package.rds
#Punto B---------------------------------------------------------------
#Representación gráfica de data sets 
#BD Bumpers
table(bumpers) #Agrupa en una tabla los  costos de los automóviles por marca 
## bumpers
##  618  795 1304 1308 1340 1456 1500 1600 1969 1999 2008 2129 2247 2284 2357 2381 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 2546 3002 3096 3113 3201 3266 3298 
##    1    1    1    1    1    1    1
#Grafica por agrupación por costos de reparación de automóviles  y la frecuencia de cuantas marcas se encuentran en ese rango
hist(bumpers)

boxplot(bumpers)

###############################################
# BD Firstchi
table(firstchi) #Agrupa en una tabla la edad de nacimiento de las madres cuando tuvieron su primer hijo o hija
## firstchi
## 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 32 33 35 36 38 39 42 
##  1  3  2  4  5  4  6 10  7  7  9  4  4  1  5  1  1  1  3  1  3  2  2  1
#Grafica por agrupación las edades de madres en el momento que tuvieron a su primer hijo o hija  y la frecuencia de cuantas madres
hist(firstchi) 

boxplot(firstchi)

###############################################
#BD Math
table(math)
## math
## 38 40 42 43 44 45 49 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 67 70 71 75 
##  1  1  2  1  1  1  2  1  1  1  1  3  1  1  1  1  1  1  1  1  1  1  1  1  1  1
hist(math) 

boxplot(math)

#Punto C----------------------------------------------------------------------
#Información de los bumpers
mean(bumpers) #Promedio de los bumpers
## [1] 2122.478
median(bumpers)#Media de los bumpers
## [1] 2129
sd(bumpers)
## [1] 798.4574
hist(bumpers)#Mostramos visualmente

#Información de los firstchi
mean(firstchi) #Promedio de los bumpers
## [1] 23.97701
median(firstchi)#Media de los bumpers
## [1] 23
sd(firstchi)
## [1] 6.254258
hist(firstchi)#Mostramos visualmente

mean(math) #Promedio de los math
## [1] 54.9
median(math)#Media de los bumpers
## [1] 54
sd(math)
## [1] 9.746264
hist(math)#mostramos visualmente 

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


#A Representa estos datos mediante un histograma y un gráfico de densidad superpuesto

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

#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)

#Si representa outliners, estos se encuentran representados como las circunferencias externas al gráfico o valores atípicos. El segundo menor outliner se encontraría en un valor superior a 2, pero menor a 3
data("brightness")
hist(brightness)

min(brightness[brightness > min(brightness)]) # Se encuentra el segundo valor mínimo de brillo, de los outliners atípicos que existen
## [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

boxplot(brightness)

# ejecutando la función quantile, se obtienen las posiciones de las denominadas bisagras. 
# definimos que la primer bisagra (Q1) es 7.702 y el tercer bisagra (Q3) es 9.130, 
quantile(brightness)
##      0%     25%     50%     75%    100% 
##  2.0700  7.7025  8.5000  9.1300 12.4300
brightness.sin <- brightness[brightness > 7.702 & brightness < 9.130]
#Almacenamos únicamente los valores que se encuentren en los quantiles mencionados anteriormente. 
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 ...
#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
#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))

#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 (pueseso) y CO2 (Emisiones de CO2) de cada pais
# Calcular la matriz de correlación
correlations <- cor(emissions[, c("GDP", "perCapita", "CO2")])

# Mostrar la matriz de correlación
print(correlations)
##                 GDP perCapita       CO2
## GDP       1.0000000 0.4325303 0.9501753
## perCapita 0.4325303 1.0000000 0.2757962
## CO2       0.9501753 0.2757962 1.0000000
# Crear un gráfico de dispersión para cada par de variables
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.
# Hacer una predicción de CO2 para un país con GDP = 10000 y perCapita = 2000

# Modelo de regresión para GDP
model_gdp <- lm(CO2 ~ GDP, data = emissions)
summary(model_gdp)
## 
## Call:
## lm(formula = CO2 ~ GDP, data = emissions)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1107.35   -81.47   -32.69   126.33  1438.79 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.043e+01  9.441e+01   0.216     0.83    
## GDP         7.815e-04  5.233e-05  14.933  1.2e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 427.4 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
# Modelo de regresión para perCapita
model_percapita <- lm(CO2 ~ perCapita, data = emissions)
summary(model_percapita)
## 
## Call:
## lm(formula = CO2 ~ perCapita, data = emissions)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1088.4  -681.4  -317.7    80.5  5479.7 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept) -223.99167  686.10910  -0.326    0.747
## perCapita      0.05040    0.03586   1.406    0.173
## 
## Residual standard error: 1318 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
# Modelo de regresión para ambos predictores
model_both <- lm(CO2 ~ GDP + perCapita, data = emissions)
summary(model_both)
## 
## Call:
## lm(formula = CO2 ~ GDP + 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 *  
## GDP          8.406e-04  5.198e-05  16.172 4.68e-14 ***
## 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
new_obs <- data.frame(GDP = 10000, perCapita = 2000)
predict(model_both, newdata = new_obs)
##        1 
## 457.6031
#c) Identifica los outliers y prueba de ajustar el modelo de nuevo sin ellos.
# Identificar y etiquetar outliers si existen
# Eliminar filas con valores faltantes
# Eliminar filas con valores faltantes
emissions <- na.omit(emissions)

# Identificar outliers
outliers <- which(emissions$CO2 > 6000 & emissions$GDP > 2e+12)

# Ajustar el modelo sin outliers si existen
if (length(outliers) > 0) {
  # Etiquetar outliers
  text(emissions$GDP[outliers], emissions$CO2[outliers], labels = row.names(emissions)[outliers])
  
  # Eliminar outliers y ajustar modelo sin ellos
  emissions_no_outliers <- emissions[-outliers, ]
  model_both_no_outliers <- lm(CO2 ~ GDP + perCapita, data = emissions_no_outliers)
  summary(model_both_no_outliers)
} else {
  # Ajustar modelo con todos los datos
  model_both <- lm(CO2 ~ GDP + perCapita, data = emissions)
  summary(model_both)
}
## 
## Call:
## lm(formula = CO2 ~ GDP + 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 *  
## GDP          8.406e-04  5.198e-05  16.172 4.68e-14 ***
## 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
#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. 

#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
"Perdieron peso "
## [1] "Perdieron peso "
#Total que perdieron peso:
perdieron <- length(anorexia[anorexia$diferencia < 0,"diferencia"])
perdieron
## [1] 29
#Gráfica
pie(c(ganaron, perdieron), labels = c("ganaron","perdieron"))

#2.7. La base de datos MASS posee la siguiente base de datos: “Melanoma” que contiene 205 pacientes con melanomas y 7 columnas

#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
table(Melanoma[,"ulcer"])
## 
##   0   1 
## 115  90
#c) ¿Que Relación 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.
result <-  aggregate(Melanoma[,"ulcer"], by=list(Melanoma[,"ulcer"]), FUN=length)
result
##   Group.1   x
## 1       0 115
## 2       1  90
presencia <- length(Melanoma[Melanoma$ulcer == 1,"ulcer"])
ausencia <- length(Melanoma[Melanoma$ulcer == 0,"ulcer"])

barplot(c(presencia, ausencia), col = c("green","yellow"), legend.text =  c("presencia","ausencia"))

pie(c(presencia, ausencia), col = c("green","yellow"), labels = c("presencia","ausencia"))

#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?
aggregate(babyboom[,"gender"], by = list(babyboom[,"gender"]), FUN = length)
##   Group.1  x
## 1    girl 18
## 2     boy 26
#) ¿Cual es la cantidad de niños nacidos en las primeras 12h?
babyboom[(babyboom$clock.time / 60) <= 12,]
##    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
length(babyboom[(babyboom$clock.time / 60) <= 12,"gender"])
## [1] 10
#c) ¿Cuantos niños nacieron por debajo de 3000gr?
babyboom[babyboom$wt < 3000,]
##    clock.time gender   wt running.time
## 6         405   girl 2208          245
## 7         407   girl 1745          247
## 8         422    boy 2846          262
## 13        814   girl 2576          494
## 18       1133    boy 2902          693
## 19       1209    boy 2635          729
## 29       1742   girl 2184         1062
## 31       1825   girl 2383         1105
## 40       2104    boy 2121         1264
length(babyboom[babyboom$wt < 3000,"gender"])
## [1] 9
#d) ¿Relación entre peso por debajo de 3000gr y sexo?
genders <- babyboom[babyboom$wt < 3000,]
table(genders$gender,genders$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
#e) ¿Grafique el promedio de pesos total, de niños y de niñas de forma adecuada?
aggregate(babyboom$wt, by = list(babyboom[,"gender"]), FUN = mean)
##   Group.1        x
## 1    girl 3132.444
## 2     boy 3375.308
#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
aggregate(Aids2$state, by=list(Aids2[,"state"]), FUN=length)
##   Group.1    x
## 1     NSW 1780
## 2   Other  249
## 3     QLD  226
## 4     VIC  588
#b) ¿Cuanto es el número de fallecidos?
totalMuertos <- Aids2[Aids2$status == "D",]

length(totalMuertos$state)
## [1] 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
#d) ¿Grafique de forma adecuada los tipos de transmisión?
types <- aggregate(Aids2$T.categ, by=list(Aids2[,"T.categ"]), FUN=length)

ggplot(types, aes(x=Group.1,y=x)) + geom_bar(stat = "identity")

#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
#Según lo anterior, la mayor tasa fue la del año 1983
# 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")