#Punto 2.1
options(repos = c(CRAN = "https://cran.r-project.org"))

install.packages("UsingR")
## Installing package into 'C:/Users/migue/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'UsingR' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\migue\AppData\Local\Temp\RtmpgXBLX8\downloaded_packages
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)

#Punto b: Representación gráfica los datos de 'bumpers', 'firstchi' y 'math' con histogramas y/o boxplots
data()
hist(bumpers, main = "Histograma de Bumpers Data", xlab = "Valores")

hist(firstchi, main = "Histograma de Firstchi Data", xlab = "Valores")

hist(math, main = "Histograma de Math Data", xlab = "Valores")

boxplot(bumpers, main = "Boxplot de Bumpers Data")

boxplot(firstchi, main = "Boxplot de Firstchi Data")

boxplot(math, main = "Boxplot de Math Data")

# Punto c: medias, medianas y desviaciones estándar
mean_bumpers <- mean(bumpers)
median_bumpers <- median(bumpers)
sd_bumpers <- sd(bumpers)
mean_firstchi <- mean(firstchi)
median_firstchi <- median(firstchi)
sd_firstchi <- sd(firstchi)
mean_math <- mean(math)
median_math <- median(math)
sd_math <- sd(math)

cat("Estimaciones visuales para 'bumpers':\n")
## Estimaciones visuales para 'bumpers':
cat("Media:", mean_bumpers, "\n")
## Media: 2122.478
cat("Mediana:", median_bumpers, "\n")
## Mediana: 2129
cat("Desviación Estándar:", sd_bumpers, "\n\n")
## Desviación Estándar: 798.4574
cat("Estimaciones visuales para 'firstchi':\n")
## Estimaciones visuales para 'firstchi':
cat("Media:", mean_firstchi, "\n")
## Media: 23.97701
cat("Mediana:", median_firstchi, "\n")
## Mediana: 23
cat("Desviación Estándar:", sd_firstchi, "\n\n")
## Desviación Estándar: 6.254258
cat("Estimaciones visuales para 'math':\n")
## Estimaciones visuales para 'math':
cat("Media:", mean_math, "\n")
## Media: 54.9
cat("Mediana:", median_math, "\n")
## Mediana: 54
cat("Desviación Estándar:", sd_math, "\n")
## Desviación Estándar: 9.746264
#Punto 2.2:
library(UsingR)
data(brightness)

#Punto a: Representa los datos mediante un histograma 
hist(brightness, main = "Histograma de Brightness Data", xlab = "Valores", col = "lightblue", prob = TRUE)
lines(density(brightness), col = "green", lwd = 2)

# Punto b: Representa gráficamente los datos mediante un diagrama de caja (boxplot)
outlier = boxplot(brightness)$out

min_outlier = sort(outlier, decreasing = FALSE)
min_outlier
##  [1]  2.07  2.28  3.88  4.37  4.55  4.61  4.78  4.89  4.99  5.01  5.04  5.13
## [13]  5.24  5.29  5.41  5.42  5.53  5.54  5.55 11.28 11.55 11.55 11.55 11.63
## [25] 11.65 11.67 11.71 11.73 11.79 11.80 11.99 12.04 12.04 12.14 12.17 12.19
## [37] 12.31 12.43
min_2 = min_outlier[2]
min_2
## [1] 2.28
# Punto c: datos no atípicos
q1 <- quantile(brightness, 0.25)
q3 <- quantile(brightness, 0.75)
iqr <- q3 - q1

brightness.sin <- brightness[brightness >= (q1 - 1.5 * iqr) & brightness <= (q3 + 1.5 * iqr)]

cat("La nueva variable brightness.sin contiene", length(brightness.sin), "valores.")
## La nueva variable brightness.sin contiene 928 valores.
# Punto 3
library("UsingR")

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 ...
# Relacion 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
# Relacion 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
# Relacion 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
# Relacion carbo 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
# Relacion 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
# Relacion sodium y sugar
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
data(mammals)

# Punto 2.4: Calcular la correlación lineal entre peso corporal y peso del cerebro
correlation_original <- cor(mammals$brain, mammals$body)
cat("Correlación lineal original:", correlation_original, "\n")
## Correlación lineal original: 0.9341638
# Punto b: Representar los datos mediante la instrucción plot
plot(mammals$body, mammals$brain, main = "Relación entre Peso Corporal y Peso del Cerebro", xlab = "Peso Corporal", ylab = "Peso del Cerebro")

# Punto c: Transformar los datos mediante la función log y repetir el estudio
mammals$log_body <- log(mammals$body)
mammals$log_brain <- log(mammals$brain)

# correlación lineal después de la transformación
correlation_log <- cor(mammals$log_brain, mammals$log_body)
cat("Correlación lineal después de la transformación:", correlation_log, "\n")
## Correlación lineal después de la transformación: 0.9595748
# Representar los datos transformados
plot(mammals$log_body, mammals$log_brain, main = "Relación entre Log(Peso Corporal) y Log(Peso del Cerebro)", xlab = "Log(Peso Corporal)", ylab = "Log(Peso del Cerebro)")

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
# Punto 2.5
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
# Punto a: Relacion entre variables
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
# Punto b: modelo de regresion lineal
lineal_regre = lm(emissions$CO2 ~ emissions$GDP + emissions$perCapita, data = emissions)
summary(lineal_regre)
## 
## 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(lineal_regre,col = "black")
## Warning in abline(lineal_regre, col = "black"): 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
prediccion_CO2 = predict(lineal_regre,emissions)
plot(emissions$GDP+emissions$perCapita,prediccion_CO2)

prediccion_CO2
##  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,prediccion_CO2)
## [1] 0.9619321
# Punto c Identificacion outliers
boxplot(emissions)

boxplot(emissions$CO2)

quantile(emissions$CO2)
##     0%    25%    50%    75%   100% 
##   54.0   77.0  200.0  547.5 6750.0
CO2_sin = emissions$CO2[emissions$CO2 > 77.0 & emissions$CO2 < 547.5]
boxplot(CO2_sin, col = "blue")

# Paso 2.6a: Cargar el paquete MASS si aún no lo has cargado
MASS::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
## 7   Cont  87.3   75.1
## 8   Cont  75.1   86.7
## 9   Cont  80.6   73.5
## 10  Cont  78.4   84.6
## 11  Cont  77.6   77.4
## 12  Cont  88.7   79.5
## 13  Cont  81.3   89.6
## 14  Cont  78.1   81.4
## 15  Cont  70.5   81.8
## 16  Cont  77.3   77.3
## 17  Cont  85.2   84.2
## 18  Cont  86.0   75.4
## 19  Cont  84.1   79.5
## 20  Cont  79.7   73.0
## 21  Cont  85.5   88.3
## 22  Cont  84.4   84.7
## 23  Cont  79.6   81.4
## 24  Cont  77.5   81.2
## 25  Cont  72.3   88.2
## 26  Cont  89.0   78.8
## 27   CBT  80.5   82.2
## 28   CBT  84.9   85.6
## 29   CBT  81.5   81.4
## 30   CBT  82.6   81.9
## 31   CBT  79.9   76.4
## 32   CBT  88.7  103.6
## 33   CBT  94.9   98.4
## 34   CBT  76.3   93.4
## 35   CBT  81.0   73.4
## 36   CBT  80.5   82.1
## 37   CBT  85.0   96.7
## 38   CBT  89.2   95.3
## 39   CBT  81.3   82.4
## 40   CBT  76.5   72.5
## 41   CBT  70.0   90.9
## 42   CBT  80.4   71.3
## 43   CBT  83.3   85.4
## 44   CBT  83.0   81.6
## 45   CBT  87.7   89.1
## 46   CBT  84.2   83.9
## 47   CBT  86.4   82.7
## 48   CBT  76.5   75.7
## 49   CBT  80.2   82.6
## 50   CBT  87.8  100.4
## 51   CBT  83.3   85.2
## 52   CBT  79.7   83.6
## 53   CBT  84.5   84.6
## 54   CBT  80.8   96.2
## 55   CBT  87.4   86.7
## 56    FT  83.8   95.2
## 57    FT  83.3   94.3
## 58    FT  86.0   91.5
## 59    FT  82.5   91.9
## 60    FT  86.7  100.3
## 61    FT  79.6   76.7
## 62    FT  76.9   76.8
## 63    FT  94.2  101.6
## 64    FT  73.4   94.9
## 65    FT  80.5   75.2
## 66    FT  81.6   77.8
## 67    FT  82.1   95.5
## 68    FT  77.6   90.7
## 69    FT  83.5   92.5
## 70    FT  89.9   93.8
## 71    FT  86.0   91.7
## 72    FT  87.3   98.0
data("anorexia")
anorexia[3,1:3]
##   Treat Prewt Postwt
## 3  Cont  91.8   86.4
pos_casos_exito <- which(anorexia$Postwt>anorexia$Prewt) #Arroja las posiciones en los casos donde se subió de peso
casos_exito = anorexia[c(pos_casos_exito),]
mejor_tratamiento = which.max(table(casos_exito$Treat))
mejor_tratamiento = c(paste(names(mejor_tratamiento),": ",max(table(casos_exito$Treat))," casos de éxito"))
table(casos_exito$Treat)
## 
##  CBT Cont   FT 
##   18   11   13
mejor_tratamiento
## [1] "CBT :  18  casos de éxito"
pos_casos_fracaso <- which(anorexia$Postwt<anorexia$Prewt) 
casos_fracaso = anorexia[c(pos_casos_fracaso),]

ganaron_peso = length(pos_casos_exito);ganaron_peso 
## [1] 42
perdieron_peso = nrow(casos_fracaso) 

barplot(c(ganaron_peso,perdieron_peso), main = "Pacientes que ganaron vs los que perdieron peso",
        ylab = "Cantidad de pacientes", col = c("orange","purple"))
legend("topright", legend = c(paste("Ganaron peso: ",ganaron_peso),paste("Perdieron peso: ",perdieron_peso)),
       fill = c("orange","purple"))

# Punto 2.7
MASS::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
## 7    210      1   1  77 1972      5.16     1
## 8    232      3   0  60 1974      3.22     1
## 9    232      1   1  49 1968     12.88     1
## 10   279      1   0  68 1971      7.41     1
## 11   295      1   0  53 1969      4.19     1
## 12   355      3   0  64 1972      0.16     1
## 13   386      1   0  68 1965      3.87     1
## 14   426      1   1  63 1970      4.84     1
## 15   469      1   0  14 1969      2.42     1
## 16   493      3   1  72 1971     12.56     1
## 17   529      1   1  46 1971      5.80     1
## 18   621      1   1  72 1972      7.06     1
## 19   629      1   1  95 1968      5.48     1
## 20   659      1   1  54 1972      7.73     1
## 21   667      1   0  89 1968     13.85     1
## 22   718      1   1  25 1967      2.34     1
## 23   752      1   1  37 1973      4.19     1
## 24   779      1   1  43 1967      4.04     1
## 25   793      1   1  68 1970      4.84     1
## 26   817      1   0  67 1966      0.32     0
## 27   826      3   0  86 1965      8.54     1
## 28   833      1   0  56 1971      2.58     1
## 29   858      1   0  16 1967      3.56     0
## 30   869      1   0  42 1965      3.54     0
## 31   872      1   0  65 1968      0.97     0
## 32   967      1   1  52 1970      4.83     1
## 33   977      1   1  58 1967      1.62     1
## 34   982      1   0  60 1970      6.44     1
## 35  1041      1   1  68 1967     14.66     0
## 36  1055      1   0  75 1967      2.58     1
## 37  1062      1   1  19 1966      3.87     1
## 38  1075      1   1  66 1971      3.54     1
## 39  1156      1   0  56 1970      1.34     1
## 40  1228      1   1  46 1973      2.24     1
## 41  1252      1   0  58 1971      3.87     1
## 42  1271      1   0  74 1971      3.54     1
## 43  1312      1   0  65 1970     17.42     1
## 44  1427      3   1  64 1972      1.29     0
## 45  1435      1   1  27 1969      3.22     0
## 46  1499      2   1  73 1973      1.29     0
## 47  1506      1   1  56 1970      4.51     1
## 48  1508      2   1  63 1973      8.38     1
## 49  1510      2   0  69 1973      1.94     0
## 50  1512      2   0  77 1973      0.16     0
## 51  1516      1   1  80 1968      2.58     1
## 52  1525      3   0  76 1970      1.29     1
## 53  1542      2   0  65 1973      0.16     0
## 54  1548      1   0  61 1972      1.62     0
## 55  1557      2   0  26 1973      1.29     0
## 56  1560      1   0  57 1973      2.10     0
## 57  1563      2   0  45 1973      0.32     0
## 58  1584      1   1  31 1970      0.81     0
## 59  1605      2   0  36 1973      1.13     0
## 60  1621      1   0  46 1972      5.16     1
## 61  1627      2   0  43 1973      1.62     0
## 62  1634      2   0  68 1973      1.37     0
## 63  1641      2   1  57 1973      0.24     0
## 64  1641      2   0  57 1973      0.81     0
## 65  1648      2   0  55 1973      1.29     0
## 66  1652      2   0  58 1973      1.29     0
## 67  1654      2   1  20 1973      0.97     0
## 68  1654      2   0  67 1973      1.13     0
## 69  1667      1   0  44 1971      5.80     1
## 70  1678      2   0  59 1973      1.29     0
## 71  1685      2   0  32 1973      0.48     0
## 72  1690      1   1  83 1971      1.62     0
## 73  1710      2   0  55 1973      2.26     0
## 74  1710      2   1  15 1973      0.58     0
## 75  1726      1   0  58 1970      0.97     1
## 76  1745      2   0  47 1973      2.58     1
## 77  1762      2   0  54 1973      0.81     0
## 78  1779      2   1  55 1973      3.54     1
## 79  1787      2   1  38 1973      0.97     0
## 80  1787      2   0  41 1973      1.78     1
## 81  1793      2   0  56 1973      1.94     0
## 82  1804      2   0  48 1973      1.29     0
## 83  1812      2   1  44 1973      3.22     1
## 84  1836      2   0  70 1972      1.53     0
## 85  1839      2   0  40 1972      1.29     0
## 86  1839      2   1  53 1972      1.62     1
## 87  1854      2   0  65 1972      1.62     1
## 88  1856      2   1  54 1972      0.32     0
## 89  1860      3   1  71 1969      4.84     1
## 90  1864      2   0  49 1972      1.29     0
## 91  1899      2   0  55 1972      0.97     0
## 92  1914      2   0  69 1972      3.06     0
## 93  1919      2   1  83 1972      3.54     0
## 94  1920      2   1  60 1972      1.62     1
## 95  1927      2   1  40 1972      2.58     1
## 96  1933      1   0  77 1972      1.94     0
## 97  1942      2   0  35 1972      0.81     0
## 98  1955      2   0  46 1972      7.73     1
## 99  1956      2   0  34 1972      0.97     0
## 100 1958      2   0  69 1972     12.88     0
## 101 1963      2   0  60 1972      2.58     0
## 102 1970      2   1  84 1972      4.09     1
## 103 2005      2   0  66 1972      0.64     0
## 104 2007      2   1  56 1972      0.97     0
## 105 2011      2   0  75 1972      3.22     1
## 106 2024      2   0  36 1972      1.62     0
## 107 2028      2   1  52 1972      3.87     1
## 108 2038      2   0  58 1972      0.32     1
## 109 2056      2   0  39 1972      0.32     0
## 110 2059      2   1  68 1972      3.22     1
## 111 2061      1   1  71 1968      2.26     0
## 112 2062      1   0  52 1965      3.06     0
## 113 2075      2   1  55 1972      2.58     1
## 114 2085      3   0  66 1970      0.65     0
## 115 2102      2   1  35 1972      1.13     0
## 116 2103      1   1  44 1966      0.81     0
## 117 2104      2   0  72 1972      0.97     0
## 118 2108      1   0  58 1969      1.76     1
## 119 2112      2   0  54 1972      1.94     1
## 120 2150      2   0  33 1972      0.65     0
## 121 2156      2   0  45 1972      0.97     0
## 122 2165      2   1  62 1972      5.64     0
## 123 2209      2   0  72 1971      9.66     0
## 124 2227      2   0  51 1971      0.10     0
## 125 2227      2   1  77 1971      5.48     1
## 126 2256      1   0  43 1971      2.26     1
## 127 2264      2   0  65 1971      4.83     1
## 128 2339      2   0  63 1971      0.97     0
## 129 2361      2   1  60 1971      0.97     0
## 130 2387      2   0  50 1971      5.16     1
## 131 2388      1   1  40 1966      0.81     0
## 132 2403      2   0  67 1971      2.90     1
## 133 2426      2   0  69 1971      3.87     0
## 134 2426      2   0  74 1971      1.94     1
## 135 2431      2   0  49 1971      0.16     0
## 136 2460      2   0  47 1971      0.64     0
## 137 2467      1   0  42 1965      2.26     1
## 138 2492      2   0  54 1971      1.45     0
## 139 2493      2   1  72 1971      4.82     1
## 140 2521      2   0  45 1971      1.29     1
## 141 2542      2   1  67 1971      7.89     1
## 142 2559      2   0  48 1970      0.81     1
## 143 2565      1   1  34 1970      3.54     1
## 144 2570      2   0  44 1970      1.29     0
## 145 2660      2   0  31 1970      0.64     0
## 146 2666      2   0  42 1970      3.22     1
## 147 2676      2   0  24 1970      1.45     1
## 148 2738      2   0  58 1970      0.48     0
## 149 2782      1   1  78 1969      1.94     0
## 150 2787      2   1  62 1970      0.16     0
## 151 2984      2   1  70 1969      0.16     0
## 152 3032      2   0  35 1969      1.29     0
## 153 3040      2   0  61 1969      1.94     0
## 154 3042      1   0  54 1967      3.54     1
## 155 3067      2   0  29 1969      0.81     0
## 156 3079      2   1  64 1969      0.65     0
## 157 3101      2   1  47 1969      7.09     0
## 158 3144      2   1  62 1969      0.16     0
## 159 3152      2   0  32 1969      1.62     0
## 160 3154      3   1  49 1969      1.62     0
## 161 3180      2   0  25 1969      1.29     0
## 162 3182      3   1  49 1966      6.12     0
## 163 3185      2   0  64 1969      0.48     0
## 164 3199      2   0  36 1969      0.64     0
## 165 3228      2   0  58 1969      3.22     1
## 166 3229      2   0  37 1969      1.94     0
## 167 3278      2   1  54 1969      2.58     0
## 168 3297      2   0  61 1968      2.58     1
## 169 3328      2   1  31 1968      0.81     0
## 170 3330      2   1  61 1968      0.81     1
## 171 3338      1   0  60 1967      3.22     1
## 172 3383      2   0  43 1968      0.32     0
## 173 3384      2   0  68 1968      3.22     1
## 174 3385      2   0   4 1968      2.74     0
## 175 3388      2   1  60 1968      4.84     1
## 176 3402      2   1  50 1968      1.62     0
## 177 3441      2   0  20 1968      0.65     0
## 178 3458      3   0  54 1967      1.45     0
## 179 3459      2   0  29 1968      0.65     0
## 180 3459      2   1  56 1968      1.29     1
## 181 3476      2   0  60 1968      1.62     0
## 182 3523      2   0  46 1968      3.54     0
## 183 3667      2   0  42 1967      3.22     0
## 184 3695      2   0  34 1967      0.65     0
## 185 3695      2   0  56 1967      1.03     0
## 186 3776      2   1  12 1967      7.09     1
## 187 3776      2   0  21 1967      1.29     1
## 188 3830      2   1  46 1967      0.65     0
## 189 3856      2   0  49 1967      1.78     0
## 190 3872      2   0  35 1967     12.24     1
## 191 3909      2   1  42 1967      8.06     1
## 192 3968      2   0  47 1967      0.81     0
## 193 4001      2   0  69 1967      2.10     0
## 194 4103      2   0  52 1966      3.87     0
## 195 4119      2   1  52 1966      0.65     0
## 196 4124      2   0  30 1966      1.94     1
## 197 4207      2   1  22 1966      0.65     0
## 198 4310      2   1  55 1966      2.10     0
## 199 4390      2   0  26 1965      1.94     1
## 200 4479      2   0  19 1965      1.13     1
## 201 4492      2   1  29 1965      7.06     1
## 202 4668      2   0  40 1965      6.12     0
## 203 4688      2   0  42 1965      0.48     0
## 204 4926      2   0  50 1964      2.26     0
## 205 5565      2   0  41 1962      2.90     0
attach(Melanoma)

#Punto A
N_fallecidos = nrow(Melanoma[Melanoma$status==1,]) + nrow(Melanoma[Melanoma$status==3,])
N_fallecidos
## [1] 71
Melanoma$ulcer
##   [1] 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 1 0 1 1
##  [38] 1 1 1 1 1 1 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
##  [75] 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 1 0 0 1 0 1 1 0 1 0
## [112] 0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 0 1 0 1 0 1 0 0 1 0 1 1 1 1 1 0 0 1 1 0
## [149] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 1 0 0 0 0 0
## [186] 1 1 0 0 1 1 0 0 0 0 1 0 0 1 1 1 0 0 0 0
#Punto B
sum(Melanoma$ulcer=="0") # no  tienen ulcera
## [1] 115
sum(Melanoma$ulcer=="1") # si tienen ulcera
## [1] 90
#Punto C
table(thickness, status)
##          status
## thickness  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
#Punto D
barplot(table(Melanoma$ulcer), main="Presencia y ausencia de Melanoma", xlab= "ulcer", ylab = "Cantidad", beside = TRUE, legend.text = c("Presencia","Ausencia"), col=c("slateblue3","azure3"))

#Punto 2.8
attach(babyboom)

#Punto A
boy= nrow(babyboom[gender == "boy",])
boy
## [1] 26
girl= nrow(babyboom[gender == "girl",])
girl
## [1] 18
#Punto B
c_nacidos= nrow(babyboom[clock.time<=720,])
p12horas= nrow(babyboom[clock.time< 360,])
paste("Niños nacidos en las primeras 12 horas: ", p12horas)
## [1] "Niños nacidos en las primeras 12 horas:  5"
#Punto C
ninosmenos3000gr= nrow(babyboom[wt<3000,])
paste("Niños con peso menor a los 3000gr: ", ninosmenos3000gr)
## [1] "Niños con peso menor a los 3000gr:  9"
#Punto D
generoninos = babyboom[babyboom$wt < 3000,]
table(generoninos$gender,generoninos$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
#Punto E
promedioninos= aggregate(babyboom$wt, by = list(babyboom[,"gender"]), FUN = mean)
promedioninos
##   Group.1        x
## 1    girl 3132.444
## 2     boy 3375.308
#Grafica promedio peso
barplot(c(mean(babyboom$wt[babyboom$gender=="girl"]), mean(babyboom$wt[babyboom$gender=="boy"])),names.arg=c("Niña", "Niño"), col = c("seagreen2", "blue"), main = "Promedio peso", ylab="peso", xlab="", las=1, ylim=c(0,3500))

#Punto 2.9

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
#Punto a: 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
# Punto b: Numero de muertos
muertos_sida = Aids2[Aids2$status == "D",]
length(muertos_sida)
## [1] 7
#Punto c: relacion estre sexo y tipo de transmision
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
#Punto d: Grafica tipos de transmision
types = table(Aids2$T.categ)
barplot(types, main = "Tipos de contagio", col = c("red","springgreen3","slateblue3","royalblue4","red4","olivedrab1","magenta1","darkorange1"), ylab = "No. Personas", ylim = c(0,2500))

#Punto 2.10
UsingR::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
## Connecticut     375.0  495.3
## Delaware        453.1  621.2
## DC             1985.4 2832.8
## Florida         826.7 1207.2
## Georgia         456.7  733.2
## Hawaii          252.1  258.4
## Idaho           238.7  281.4
## Illinois        553.0  977.3
## Indiana         283.8  508.3
## Iowa            181.1  278.0
## Kansas          326.6  510.8
## Kentucky        322.2  535.5
## Louisiana       640.9  984.6
## Maine           159.6  130.9
## Maryland        807.1 1000.1
## Massachusetts   576.8  779.0
## Michigan        716.7  770.1
## Minnesota       190.9  338.0
## Mississippi     280.4  411.7
## Missour         477.2  740.4
## Montana         212.6  169.9
## Nebraska        217.7  348.6
## Nevada          655.2  696.8
## New Hampshire   125.1  125.7
## New Jersey      553.1  625.8
## New Mexico      686.8  934.9
## New York        914.1 1122.1
## North Carolina  409.6  681.0
## North Dakota     53.7   83.3
## Ohio            397.9  525.9
## Oklahoma        423.4  622.8
## Oregon          487.8  510.2
## Pennsylvania    342.8  427.0
## Rhode Island    355.2  394.5
## South Carolina  616.8  944.5
## South Dakota    120.0  194.5
## Tennessee       402.0  746.2
## Texas           512.2  806.3
## Utah            256.0  290.5
## Vermont         132.6  109.5
## Virginia        292.5  374.9
## Washington      371.8  534.5
## West Virginia   171.8  211.5
## Wisconsin       190.9  275.7
## Wyoming         237.2  319.5
attach(crime)

#Punto A
total83 = round(sum(crime$y1983))
total93 = round(sum(crime$y1993))
#Punto B
crimen83 =round(max(crime$y1983))  
crimen93 = round(max(crime$y1993))
#Punto C
crime["acumulado"] <- crime$y1983 + crime$y1993
crime[crime$acumulado == max(crime$acumulado),]
##     y1983  y1993 acumulado
## DC 1985.4 2832.8    4818.2
#Punto D

anos = c("1983","1993")
datos = c(total83,total93)

pie(datos, anos,
    main = "Mayor tasa de crímenes por año",
    sub = "Año 1983: 1985 - Año 1993: 2833")