install.packages("MASS")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
install.packages("UsingR")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (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
# a. ¿Cuantos conjuntos de datos de trabajo contiene el paquete?
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.4.0; ; 2024-04-25 03:26:24 UTC; unix
##
## -- File: /cloud/lib/x86_64-pc-linux-gnu-library/4.4/UsingR/Meta/package.rds
data(package = .packages(all.available = TRUE))
# b. Representacion grafica bumpers, firstchi, math con un histograma y/o un boxplot.
data("bumpers")
data("firstchi")
data("math")
# Histograma del conjunto de datos bumpers
hist(bumpers, col = "#7FFFD4")

# Boxplot del conjunto de datos firstchi
boxplot(firstchi, col = "#76EEC6")

# Histograma del conjunto de datos math
hist(math, col = "#458B74")

# c. medias, mediana y desviacion estandar y graficas respectivas
# para bumpers
mean(bumpers)
## [1] 2122.478
median(bumpers)
## [1] 2129
sd(bumpers)
## [1] 798.4574
hist(bumpers, col = "#7FFF00")

# para firstchi
mean(firstchi)
## [1] 23.97701
median(firstchi)
## [1] 23
sd(firstchi)
## [1] 6.254258
hist(firstchi, col = "#76EE00")

# para math
mean(math)
## [1] 54.9
median(math)
## [1] 54
sd(math)
## [1] 9.746264
hist(math, col = "#66CD00")

# Brillos de estrellas
# a. Representacion garfica - Histograma
hist(brightness, probability = TRUE, col = "#FFF8DC")
lines(density(brightness), col="#FF1493", lwd=3)

#b. Representacion garfica - 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
#c. Datos no atipicos - encima primer bisagra y debajo cuarta bisagra
quantile(brightness)
## 0% 25% 50% 75% 100%
## 2.0700 7.7025 8.5000 9.1300 12.4300
#primer_bisagra = (Q1) / cuarta_bisagra = (Q3)
brightness_sin = brightness[brightness > 7.703 & brightness < 9.130]
boxplot(brightness_sin, col = "#79CDCD")

#Paquete MASS - base de datos UScereal
# a. Tipo de dato de las variables
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. Asociones entre variables
# 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
# Conjunto de datos mammals - peso corporal y peso del cerebro
data("mammals")
# a. correlacion de los datos
cor(mammals$body, mammals$brain)
## [1] 0.9341638
# b. representacion con plot
plot(mammals)

# c. repeticion con la funcion log
cor(log(mammals$body), log(mammals$brain))
## [1] 0.9595748
plot(log(mammals))

# Enlazar base de datos "emissions" al 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. 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
# 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 = "#FF3030")
## Warning in abline(lineal_regre, col = "#FF3030"): 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
# 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 = "#8B1A1A")

# Base de datos MASS - Anorexia
data("anorexia")
head(anorexia)
## Treat Prewt Postwt
## 1 Cont 80.7 80.2
## 2 Cont 89.4 80.1
## 3 Cont 91.8 86.4
## 4 Cont 74.0 86.3
## 5 Cont 78.1 76.1
## 6 Cont 88.3 78.1
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. ganancia y perdida de peso
anorexia["diferencia"] = anorexia$Postwt - anorexia$Prewt
#Total que Ganaron peso:
ganancia = length(anorexia[anorexia$diferencia > 0,"diferencia"])
ganancia
## [1] 42
#Total que perdieron peso:
perdida = length(anorexia[anorexia$diferencia < 0,"diferencia"])
perdida
## [1] 29
# c. grafica
barplot(c(ganancia,perdida), main = "Ganancia y Perdida de peso", ylab = "No. Personas", col = c("#54FF9F","#2E8B57"))
legend("topright", legend = c(paste("Ganaron peso"),paste("Perdieron peso")),fill = c("#54FF9F","#2E8B57"))

# Base de datos MASS - Melanoma
data("Melanoma")
head(Melanoma)
## time status sex age year thickness ulcer
## 1 10 3 1 76 1972 6.76 1
## 2 30 3 1 56 1968 0.65 0
## 3 35 2 1 41 1977 1.34 0
## 4 99 3 0 71 1968 2.90 0
## 5 185 1 1 52 1965 12.08 1
## 6 204 1 1 28 1971 4.84 1
#status == 1 muertas por melanoma // status == 2 muertas por otras causas
# a, Fallecidos por Melanoma y otras
length(Melanoma[Melanoma$status == 1 | Melanoma$status == 3,"status"])
## [1] 71
# b. Presencia y ausencia de Melanoma
table(Melanoma[,"ulcer"])
##
## 0 1
## 115 90
# c. 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. Grafica muertes por Melanoma
presencia = length(Melanoma[Melanoma$ulcer == 1,"ulcer"])
ausencia = length(Melanoma[Melanoma$ulcer == 0,"ulcer"])
barplot(main = "Presencia de Melanoma", ylab = "No. Personas", c(presencia, ausencia), col = c("#CD661D","#FF7F24"), legend.text = c("presencia","ausencia"))

# BASE DE DATOS "babyboom"
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. Numero de niños y niñas
child_boy = nrow(babyboom[babyboom$gender == "boy",])
cat("Cantidad de niños",child_boy,"\n")
## Cantidad de niños 26
child_girl = nrow(babyboom[babyboom$gender == "girl",])
cat("Cantidad de niñas", child_girl, "\n")
## Cantidad de niñas 18
# b. nacidos en las primeras 12 horas
first_born = nrow(babyboom[babyboom$clock.time < 708,])
cat("Bebes nacidos en las primeras doce horas", first_born, "\n")
## Bebes nacidos en las primeras doce horas 9
# c. Bebes con peso menor a 3000gr
ninos_bajo_peso = nrow(babyboom[babyboom$gender=='boy' & babyboom$wt<3000,])
ninos_bajo_peso
## [1] 4
bebes_low_wt = nrow(babyboom[babyboom$wt<3000,])
bebes_low_wt
## [1] 9
# d. Peso por debajo de 3000gr y sexo
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
# e. Grafica promedio total de pesos
prome_boy = mean(babyboom$wt[babyboom$gender == "boy"])
prome_boy
## [1] 3375.308
prome_girl = mean(babyboom$wt[babyboom$gender == "girl"])
prome_girl
## [1] 3132.444
barplot(c(prome_boy, prome_girl), main = "Promedio de peso de los bebes", names.arg=c("Niño", "Niña"), col=c("#009ACD","#FF34B3"), ylab="Peso promedio", xlab="Sexo", las=1, ylim=c(0,3500))

# BASE DE DATOS "Aids2" - Sida
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. 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. Numero de muertos
muertos_sida = Aids2[Aids2$status == "D",]
length(muertos_sida)
## [1] 7
# 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
# d. Grafica tipos de transmision
types = table(Aids2$T.categ)
barplot(types, main = "Tipos de contagio", col = c("#607B8B","#8DB6CD","#A4D3EE","#B0E2FF","#B0E2FF","#68838B","#9AC0CD","#B2DFEE"), ylab = "No. Personas", ylim = c(0,2500))

# BASE DE DATOS "crime" - crimenes en 50 estados
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. En que año la tasa de cimenes fue mayor
mean(crime$y1983)
## [1] 437.5196
mean(crime$y1993) # La tasa de crimenes fue mayor en el año 1993
## [1] 606.8294
# b. Tasa de crimenes mayor por estado y año
crime_max1983 = crime[c(which.max(crime$y1983)),]
crime_max1983
## y1983 y1993
## DC 1985.4 2832.8
crime_max1993 = crime[c(which.max(crime$y1993)),]
crime_max1993
## y1983 y1993
## DC 1985.4 2832.8
# c. Acumulado
crime["acumulado"] <- crime$y1983 + crime$y1993
crime[crime$acumulado == max(crime$acumulado),]
## y1983 y1993 acumulado
## DC 1985.4 2832.8 4818.2
# d. Graficos
total83 = round(sum(crime$y1983))
total93 = round(sum(crime$y1993))
anos = c("1983","1993")
datos = c(total83,total93)
pie(datos, anos, main = "Mayor tasa de crímenes por año")
