Actividades
2.1. Carga (o instala primero y luego carga) el paquete UsingR
#install.packages("UsingR")
#install.packages("MASS")
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)
length(ls("package:UsingR"))
## [1] 150
datacenterbumper=UsingR::bumpers
hist(datacenterbumper,main="Histograma del dataset bumpers")
datacenterfirstchi=UsingR::firstchi
boxplot(datacenterfirstchi,main="Boxplot del dataset firstchi")
datacentermath=UsingR::math
hist(datacentermath,main="Histograma del dataset math")
median(bumpers)
## [1] 2129
mean(bumpers)
## [1] 2122.478
sd(bumpers)
## [1] 798.4574
hist(datacenterbumper,main="Histograma del dataset bumpers")
legend("topleft", legend = c(paste("Mean =", round(mean(bumpers), 2)),
paste("Median =", round(median(bumpers), 2)),
paste("SD =", round(sd(bumpers), 2))))
boxplot(datacenterbumper,main="Boxplot del dataset bumpers")
legend("topleft", legend = c(paste("Mean =", round(mean(bumpers), 2)),
paste("Median =", round(median(bumpers), 2)),
paste("SD =", round(sd(bumpers), 2))),
pch = 1)
median(firstchi)
## [1] 23
mean(firstchi)
## [1] 23.97701
sd(firstchi)
## [1] 6.254258
hist(datacenterfirstchi,main="Histograma del dataset firstchi")
legend("topleft", legend = c(paste("Mean =", round(mean(firstchi), 2)),
paste("Median =", round(median(firstchi), 2)),
paste("SD =", round(sd(firstchi), 2))))
boxplot(datacenterfirstchi,main="Boxplot del dataset firstchi")
legend("topleft", legend = c(paste("Mean =", round(mean(firstchi), 2)),
paste("Median =", round(median(firstchi), 2)),
paste("SD =", round(sd(firstchi), 2))),
pch = 1)
median(math)
## [1] 54
mean(math)
## [1] 54.9
sd(math)
## [1] 9.746264
hist(datacentermath,main="Histograma del dataset math")
legend("topleft", legend = c(paste("Mean =", round(mean(math), 2)),
paste("Median =", round(median(math), 2)),
paste("SD =", round(sd(math), 2))))
boxplot(datacentermath,main="Boxplot del dataset math")
legend("topleft", legend = c(paste("Mean =", round(median(math), 2)),
paste("Median =", round(mean(math), 2)),
paste("SD =", round(sd(math), 2))),
pch = 1)
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.
data(brightness)
hist(brightness, freq = F)
plot(density(brightness))
data(brightness)
hist(brightness, probability = TRUE)
lines(density(brightness), col="orange",lwd=3)
boxplot(brightness, main="Brightness")
“R/ Presentan outliers, y el segundo menor es:”
min(brightness[brightness > min(brightness)])
## [1] 2.28
quantile(brightness)
## 0% 25% 50% 75% 100%
## 2.0700 7.7025 8.5000 9.1300 12.4300
filtramos los valores del dataset de brightness en el rango de valores
brightness.sin=brightness[brightness > 7.702 & brightness < 9.130]
boxplot( brightness.sin, main="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 ...
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
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
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
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
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
table(UScereal$sodium, UScereal$sugars)
##
## 0 0.8 1.769912 2 3 4 4.477612 5.681818 6 6.666667 7.462687 8.270677
## 0 3 0 0 0 0 0 0 0 0 0 0 0
## 51.13636 0 0 0 0 0 0 0 0 0 0 0 0
## 90 0 0 0 0 0 0 0 0 0 0 0 0
## 93.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 125 0 0 0 0 0 0 0 0 0 0 0 0
## 135.33835 0 0 0 0 0 0 0 0 0 0 0 1
## 140 0 0 0 0 0 0 0 0 0 0 0 0
## 159.09091 0 0 0 0 0 0 0 1 0 0 0 0
## 173.33333 0 0 0 1 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 0 0 0 0 0 0 0
## 186.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 190 0 0 0 0 0 0 0 0 0 0 0 0
## 200 0 0 0 0 3 0 0 0 0 0 0 0
## 212.38938 0 0 1 0 0 0 0 0 0 0 0 0
## 220 0 0 0 0 1 0 0 0 1 0 0 0
## 223.8806 0 0 0 0 0 0 0 0 0 0 0 0
## 226.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 227.27273 0 0 0 0 0 0 0 0 0 0 0 0
## 230 0 0 0 0 1 0 0 0 0 0 0 0
## 232 0 1 0 0 0 0 0 0 0 0 0 0
## 238.80597 0 0 0 0 0 0 0 0 0 0 0 0
## 240 0 0 0 0 0 0 0 0 0 0 0 0
## 253.33333 0 0 0 0 0 0 0 0 0 1 0 0
## 266.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 270 0 0 0 0 0 0 0 0 0 0 0 0
## 280 1 0 0 0 1 0 0 0 0 0 0 0
## 283.58209 0 0 0 0 0 0 0 0 0 0 0 0
## 290 0 0 0 1 1 0 0 0 0 0 0 0
## 293.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 298.50746 0 0 0 0 0 0 0 0 0 0 0 0
## 313.43284 0 0 0 0 0 0 0 0 0 0 1 0
## 320 0 0 0 0 1 0 0 0 0 0 0 0
## 328.35821 0 0 0 0 0 0 0 0 0 0 0 0
## 333.33333 0 0 0 0 0 1 0 0 0 0 0 0
## 340 0 0 0 0 0 0 0 0 0 0 0 0
## 343.28358 0 0 0 0 0 0 1 0 0 0 0 0
## 358.20896 0 0 0 0 0 0 0 0 0 0 0 0
## 373.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 393.93939 0 0 0 0 0 0 0 0 0 0 0 0
## 680 0 0 0 0 0 0 0 0 0 0 0 0
## 787.87879 0 0 0 0 0 0 0 0 0 0 0 0
##
## 8.75 8.955224 10.447761 10.666667 11 12 12.121212 13 13.333333
## 0 1 0 0 0 0 1 0 0 0
## 51.13636 0 0 0 0 0 0 0 0 0
## 90 0 0 0 0 0 1 0 0 0
## 93.33333 0 0 0 0 0 0 0 0 0
## 125 0 0 0 0 0 0 0 1 0
## 135.33835 0 0 0 0 0 0 0 0 0
## 140 0 0 0 0 0 1 0 0 0
## 159.09091 0 0 0 0 0 0 0 0 0
## 173.33333 0 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 1 0 2 0
## 186.66667 0 0 0 0 0 0 0 0 1
## 190 0 0 0 0 0 0 0 0 0
## 200 0 0 0 0 0 0 0 0 0
## 212.38938 0 0 0 0 0 0 0 0 0
## 220 0 0 0 0 1 0 0 0 0
## 223.8806 0 1 0 0 0 0 0 0 0
## 226.66667 0 0 0 0 0 1 0 0 0
## 227.27273 0 0 0 0 0 0 1 0 0
## 230 0 0 0 0 0 0 0 0 0
## 232 0 0 0 0 0 0 0 0 0
## 238.80597 0 0 0 0 0 0 0 0 0
## 240 0 0 0 0 0 0 0 0 1
## 253.33333 0 0 0 0 0 0 0 0 0
## 266.66667 0 0 0 1 0 0 0 0 0
## 270 0 0 0 0 0 1 0 0 0
## 280 0 0 0 1 0 1 0 0 0
## 283.58209 0 0 0 0 0 0 0 0 0
## 290 0 0 0 0 0 0 0 0 0
## 293.33333 0 0 0 0 0 0 0 0 0
## 298.50746 0 1 0 0 0 0 0 0 0
## 313.43284 0 0 0 0 0 0 0 0 0
## 320 0 0 0 0 0 0 0 0 0
## 328.35821 0 0 1 0 0 0 0 0 0
## 333.33333 0 0 0 0 0 0 0 0 1
## 340 0 0 0 0 0 0 0 0 0
## 343.28358 0 0 0 0 0 0 0 0 0
## 358.20896 0 0 0 0 0 0 0 0 0
## 373.33333 0 0 0 0 0 1 0 0 0
## 393.93939 0 0 0 0 0 0 0 0 0
## 680 0 0 0 0 0 1 0 0 0
## 787.87879 0 0 0 0 0 0 0 0 0
##
## 13.432836 14 14.666667 14.925373 15.151515 16 17.045455 17.910448
## 0 0 0 0 0 0 0 0 0
## 51.13636 0 0 0 0 0 0 1 0
## 90 0 0 0 0 0 0 0 0
## 93.33333 0 0 0 0 0 0 0 0
## 125 0 1 0 0 0 0 0 0
## 135.33835 0 0 0 0 0 0 0 0
## 140 0 0 0 0 0 0 0 0
## 159.09091 0 0 0 0 0 0 0 0
## 173.33333 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 1 0 0
## 186.66667 0 0 0 0 0 0 0 0
## 190 0 1 0 0 0 0 0 0
## 200 0 0 0 0 0 0 0 0
## 212.38938 0 0 0 0 0 0 0 0
## 220 0 0 0 0 0 0 0 0
## 223.8806 0 0 0 0 0 0 0 0
## 226.66667 0 0 0 0 0 0 0 0
## 227.27273 0 0 0 0 0 0 0 0
## 230 0 0 0 0 0 0 0 0
## 232 0 0 0 0 0 0 0 0
## 238.80597 0 0 0 1 0 0 0 0
## 240 0 0 0 0 0 0 0 0
## 253.33333 0 0 0 0 0 0 0 0
## 266.66667 0 0 1 0 0 0 0 0
## 270 0 0 0 0 0 0 0 0
## 280 0 2 0 0 0 2 0 0
## 283.58209 1 0 0 0 0 0 0 0
## 290 0 0 0 0 0 0 0 0
## 293.33333 0 0 0 0 0 1 0 0
## 298.50746 0 0 0 0 0 0 0 0
## 313.43284 0 0 0 0 0 0 0 0
## 320 0 0 0 0 0 0 0 0
## 328.35821 0 0 0 0 0 0 0 0
## 333.33333 0 0 0 0 0 0 0 0
## 340 0 0 0 0 0 0 0 0
## 343.28358 0 0 0 0 0 0 0 0
## 358.20896 0 0 0 0 0 0 0 1
## 373.33333 0 0 0 0 0 0 0 0
## 393.93939 0 0 0 0 0 0 0 0
## 680 0 0 0 0 0 0 0 0
## 787.87879 0 0 0 0 1 0 0 0
##
## 18.181818 19.402985 20 20.895522
## 0 0 0 0 0
## 51.13636 0 0 0 0
## 90 0 0 0 0
## 93.33333 0 0 1 0
## 125 0 0 0 0
## 135.33835 0 0 0 0
## 140 0 0 0 0
## 159.09091 0 0 0 0
## 173.33333 0 0 0 0
## 180 0 0 0 0
## 186.66667 0 0 0 0
## 190 0 0 0 0
## 200 0 0 0 0
## 212.38938 0 0 0 0
## 220 0 0 0 0
## 223.8806 0 1 0 0
## 226.66667 0 0 0 0
## 227.27273 0 0 0 0
## 230 0 0 0 0
## 232 0 0 0 0
## 238.80597 0 0 0 0
## 240 0 0 0 0
## 253.33333 0 0 0 0
## 266.66667 0 0 0 0
## 270 0 0 0 0
## 280 0 0 0 0
## 283.58209 0 0 0 0
## 290 0 0 0 0
## 293.33333 0 0 0 0
## 298.50746 0 0 0 1
## 313.43284 0 0 0 0
## 320 0 0 0 0
## 328.35821 0 0 0 0
## 333.33333 0 0 0 0
## 340 0 0 1 0
## 343.28358 0 0 0 0
## 358.20896 0 0 0 0
## 373.33333 0 0 0 0
## 393.93939 1 0 0 0
## 680 0 0 0 0
## 787.87879 0 0 0 0
2.4. El conjunto de datos mammals contiene datos sobre la relación entre peso corporal y peso del cerebro. a) ¿Cuál es la correlación lineal entre estas variables?
data(mammals)
cor(mammals$body, mammals$brain)
## [1] 0.9341638
plot(mammals, main="Mammals", col="purple")
plot(log(mammals), main="Datos transformados (LOG)", col="purple")
La grafica cambia de tal manera que cuando crece el valor en eje x, el eje y tambien aumenta de valor, formando una espece de regresion lineal, de tal manera que el eje x baja a valores desde -5 hasta 8 y el eje y baja a valores de -2 a 9
2.5. Enlaza la base de datos emissions del paquete UsingR.
datacenteremisions=UsingR::emissions
print(datacenteremisions)
## 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
## Russia 692000 4727 2000
## Canada 658000 21221 700
## Spain 642400 16401 370
## Australia 394000 20976 480
## Netherlands 343900 21755 240
## Poland 280700 7270 400
## Belgium 236300 23208 145
## Sweden 176200 19773 75
## Austria 174100 21390 80
## Switzerland 172400 23696 54
## Portugal 149500 15074 75
## Greece 137400 12833 125
## Ukraine 124900 2507 420
## Denmark 122500 22868 75
## Norway 120500 27149 56
## Romania 114200 5136 160
## CzechRepublic 111900 10885 150
## Finland 102100 19793 76
## Hungary 73200 7186 85
## Ireland 59900 16488 63
cor(emissions)
## GDP perCapita CO2
## GDP 1.0000000 0.4325303 0.9501753
## perCapita 0.4325303 1.0000000 0.2757962
## CO2 0.9501753 0.2757962 1.0000000
pairs(emissions, c("GDP", "perCapita", "CO2"))
modelo1 <- lm(CO2 ~ GDP, data = emissions)
summary(modelo1)
##
## 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
modelo2 <- lm(CO2 ~ GDP + perCapita, data = emissions)
summary(modelo2)
##
## 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
#install.packages("car")
library(car)
## Loading required package: carData
outlierTest(modelo2)
## rstudent unadjusted p-value Bonferroni p
## Russia 3.858676 0.00085079 0.022120
## Japan -3.537122 0.00185150 0.048139
modelo3 <- lm(CO2 ~ GDP + perCapita, data = emissions[-c(14, 52), ])
summary(modelo3)
##
## Call:
## lm(formula = CO2 ~ GDP + perCapita, data = emissions[-c(14, 52),
## ])
##
## Residuals:
## Min 1Q Median 3Q Max
## -1036.83 -180.84 4.64 158.26 1051.99
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.100e+02 2.090e+02 2.441 0.0232 *
## GDP 8.409e-04 5.352e-05 15.712 1.92e-13 ***
## perCapita -3.045e-02 1.187e-02 -2.565 0.0177 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 391.3 on 22 degrees of freedom
## Multiple R-squared: 0.9247, Adjusted R-squared: 0.9179
## F-statistic: 135.1 on 2 and 22 DF, p-value: 4.408e-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.
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
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
anorexia["diferencia"] <- anorexia$Postwt - anorexia$Prewt
ganaron <- length(anorexia[anorexia$diferencia > 0,"diferencia"])
ganaron
## [1] 42
perdieron <- length(anorexia[anorexia$diferencia < 0,"diferencia"])
perdieron
## [1] 29
pie(c(ganaron, perdieron), labels = c("Ganaron","Perdieron"), col = c("yellow","gray"),main = "Peso de los pacientes")
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.
num_fallecidos = nrow(Melanoma[Melanoma$status==1,]) + nrow(Melanoma[Melanoma$status==3,])
num_fallecidos
## [1] 71
table(Melanoma[,"ulcer"])
##
## 0 1
## 115 90
cor(Melanoma$status==1, Melanoma$status==3)
## [1] -0.1680173
presencia=length(Melanoma[Melanoma$ulcer == 1,"ulcer"])
ausencia=length(Melanoma[Melanoma$ulcer == 0,"ulcer"])
barplot(c(presencia, ausencia), col = c("red","blue"), legend.text = c("presencia","ausencia"), main = "Presencia y ausencia de melanoma")
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
table(babyboom$gender)
##
## girl boy
## 18 26
sum(with(babyboom, clock.time <= 1200))
## [1] 18
pesobajo=sum(with(babyboom,gender == "boy" & wt<3000))
pesobajo
## [1] 4
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
table(babyboom$gender[babyboom$wt<3000])
##
## girl boy
## 5 4
peso.girl.mean = round((mean(babyboom$gender == "girl") * 100), 2)
peso.boy.mean = round((mean(babyboom$gender == "boy") * 100), 2)
pesos = c(peso.girl.mean, peso.boy.mean)
etiquetas = c("Niñas", "Niños")
barplot(pesos, names.arg = etiquetas, col = c("pink", "green"),
main = "Promedio de peso de Niñas y Niños Babyboom",
ylab = "Peso", ylim = c(0, 80))
# Agregar valores encima de las barras
text(1:2, pesos, labels = paste(pesos, "kg"), pos = 3, cex = 0.8, col = "black")
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. a) Determine en número de contagios por estado
Aids2 = MASS::Aids2
Other = sum(Aids2$state=="Other")
NSW = sum(Aids2$state=="NSW")
QLD = sum(Aids2$state=="QLD")
VIC = sum(Aids2$state=="VIC")
cat("Numero de contagios en estado Other :",Other)
## Numero de contagios en estado Other : 249
cat("Numero de contagios en estado NSW :",NSW)
## Numero de contagios en estado NSW : 1780
cat("Numero de contagios en estado QLD :",QLD)
## Numero de contagios en estado QLD : 226
cat("Numero de contagios en estado VIC :",VIC)
## Numero de contagios en estado VIC : 588
NumeroFallecidos=Aids2[Aids2$status == "D",]
length(NumeroFallecidos$state)
## [1] 1761
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
plot(main = "Tipos de transmición", Aids2$T.categ, col = rainbow(8), ylim = c(0, 2500))
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. a) La tasa total en 1993 fue mayor o menor que en 1983.
crime = UsingR::crime
promedio.1983 = mean(crime$y1983)
promedio.1993 = mean(crime$y1993)
if (promedio.1993 > promedio.1983) {
print("La tasa total en 1993 fue mayor")
} else {
print("La tasa total en 1993 fue menor")
}
## [1] "La tasa total en 1993 fue mayor"
crime[crime$y1983 == max(crime$y1983),]
## y1983 y1993
## DC 1985.4 2832.8
“R/ El estado DC presenta la mayor tasa de crímenes en cada año” 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
data("crime")
hist(crime)