library(UsingR)
## Warning: package 'UsingR' was built under R version 4.3.3
## Loading required package: MASS
## Loading required package: HistData
## Warning: package 'HistData' was built under R version 4.3.3
## Loading required package: Hmisc
## Warning: package 'Hmisc' was built under R version 4.3.3
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
library(MASS)
data(package = "UsingR")
length(ls("package:UsingR"))
## [1] 150
par(mfrow=c(1,2))
hist(bumpers, main = "Histograma de Bumpers", xlab = "Valores", ylab = "Frecuencia")
boxplot(bumpers, main = "Boxplot de Bumpers", ylab = "Valores")
par(mfrow=c(1,2))
hist(firstchi, main = "Histograma de Firstchi", xlab = "Valores", ylab = "Frecuencia")
boxplot(firstchi, main = "Boxplot de Firstchi", ylab = "Valores")
par(mfrow=c(1,2))
hist(math, main = "Histograma de Math", xlab = "Valores", ylab = "Frecuencia")
boxplot(math, main = "Boxplot de Math", ylab = "Valores")
#Bumpers
mean(bumpers)
## [1] 2122.478
median(bumpers)
## [1] 2129
sd(bumpers)
## [1] 798.4574
hist(bumpers, main = "Histograma de Bumpers", xlab = "Valores", ylab = "Frecuencia")
#Firstchi
hist(firstchi, main = "Histograma de Firstchi", xlab = "Valores", ylab = "Frecuencia")
mean(firstchi)
## [1] 23.97701
median(firstchi)
## [1] 23
sd(firstchi)
## [1] 6.254258
#Math
hist(math, main = "Histograma de Math", xlab = "Valores", ylab = "Frecuencia")
mean(math)
## [1] 54.9
median(math)
## [1] 54
sd(math)
## [1] 9.746264
data(brightness)
hist(brightness, prob = TRUE)
lines(density(brightness),lwd=2, col="blue")
outliers = boxplot(brightness)$out
outliers
## [1] 12.31 11.71 5.53 11.28 4.78 5.13 4.37 5.04 12.43 12.04 4.55 11.55
## [13] 12.14 11.63 4.99 11.67 4.61 11.99 12.04 5.55 12.17 11.55 11.79 12.19
## [25] 2.07 11.65 11.73 2.28 5.42 3.88 5.54 5.29 5.01 11.55 4.89 11.80
## [37] 5.41 5.24
Sí presentan outliers. El segundo menor outliers es:
#Extraer el segundo menor outliner
min(brightness[brightness > min(brightness)])
## [1] 2.28
quantile(brightness)
## 0% 25% 50% 75% 100%
## 2.0700 7.7025 8.5000 9.1300 12.4300
brightness.sin= brightness[brightness > 7.7025 & brightness < 9.130]
boxplot(brightness.sin)
data("UScereal")
str(UScereal)
## 'data.frame': 65 obs. of 11 variables:
## $ mfr : Factor w/ 6 levels "G","K","N","P",..: 3 2 2 1 2 1 6 4 5 1 ...
## $ calories : num 212 212 100 147 110 ...
## $ protein : num 12.12 12.12 8 2.67 2 ...
## $ fat : num 3.03 3.03 0 2.67 0 ...
## $ sodium : num 394 788 280 240 125 ...
## $ fibre : num 30.3 27.3 28 2 1 ...
## $ carbo : num 15.2 21.2 16 14 11 ...
## $ sugars : num 18.2 15.2 0 13.3 14 ...
## $ shelf : int 3 3 3 1 2 3 1 3 2 1 ...
## $ potassium: num 848.5 969.7 660 93.3 30 ...
## $ vitamins : Factor w/ 3 levels "100%","enriched",..: 2 2 2 2 2 2 2 2 2 2 ...
head(UScereal)
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$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
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
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$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
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)
cor(mammals$body, mammals$brain)
## [1] 0.9341638
plot(mammals)
plot(log(mammals))
data(emissions)
str(emissions)
## 'data.frame': 26 obs. of 3 variables:
## $ GDP : num 8083000 3080000 1740000 1320000 1242000 ...
## $ perCapita: num 29647 24409 21197 22381 21010 ...
## $ CO2 : num 6750 1320 1740 550 675 540 2000 700 370 480 ...
head(emissions)
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")])
#PerCapita
capita_r=lm(perCapita ~ CO2,data = emissions)
summary(capita_r)
##
## Call:
## lm(formula = perCapita ~ CO2, data = emissions)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15006 -3491 2955 4648 10350
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 16714.300 1586.333 10.536 1.75e-10 ***
## CO2 1.509 1.074 1.406 0.173
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7211 on 24 degrees of freedom
## Multiple R-squared: 0.07606, Adjusted R-squared: 0.03757
## F-statistic: 1.976 on 1 and 24 DF, p-value: 0.1726
#GDP
gdp_r= lm(GDP ~ CO2, data = emissions)
summary(gdp_r)
##
## Call:
## lm(formula = GDP ~ CO2, data = emissions)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1675695 -125418 -11255 47815 1497913
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 57082.67 114313.83 0.499 0.622
## CO2 1155.31 77.37 14.933 1.2e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 519600 on 24 degrees of freedom
## Multiple R-squared: 0.9028, Adjusted R-squared: 0.8988
## F-statistic: 223 on 1 and 24 DF, p-value: 1.197e-13
boxplot(emissions)
emissions_sin = which(emissions$CO2 %in% c(boxplot.stats(emissions$CO2)$out))
CO2_sin = emissions$CO2[-c(emissions_sin)]
GDP_sin = emissions$GDP[-c(emissions_sin)]
perCapita_sin = emissions$perCapita[-c(emissions_sin)]
regresin_sin = lm(CO2_sin ~ GDP_sin + perCapita_sin, data = emissions)
summary(regresin_sin)
##
## Call:
## lm(formula = CO2_sin ~ GDP_sin + perCapita_sin, data = emissions)
##
## Residuals:
## Min 1Q Median 3Q Max
## -130.88 -63.84 -32.27 16.79 334.38
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.233e+02 7.202e+01 3.101 0.00588 **
## GDP_sin 4.912e-04 6.989e-05 7.028 1.09e-06 ***
## perCapita_sin -8.525e-03 4.114e-03 -2.072 0.05209 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 121.4 on 19 degrees of freedom
## Multiple R-squared: 0.7225, Adjusted R-squared: 0.6933
## F-statistic: 24.73 on 2 and 19 DF, p-value: 5.142e-06
plot(GDP_sin + perCapita_sin,CO2_sin)
boxplot(emissions$CO2)
boxplot(CO2_sin)
data(anorexia)
str(anorexia)
## 'data.frame': 72 obs. of 3 variables:
## $ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
## $ Prewt : num 80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
## $ Postwt: num 80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
head(anorexia)
tratamiento = which(anorexia$Postwt>anorexia$Prewt)
caso_exito = anorexia[c(tratamiento),]
mejor_tra = which.max(table(caso_exito$Treat))
mejor_tra = c(paste(names(mejor_tra),":
",max(table(caso_exito$Treat))," casos exitosos"))
table(caso_exito$Treat)
##
## CBT Cont FT
## 18 11 13
mejor_tra
## [1] "CBT : \n 18 casos exitosos"
#Casos donde ganan peso
perdida_peso = which(anorexia$Postwt<anorexia$Prewt)
casos_fracaso = anorexia[c(perdida_peso),]
ganar_peso = length(tratamiento)
ganar_peso
## [1] 42
#Casos donde pierden peso
pierden_peso = nrow(casos_fracaso)
pierden_peso
## [1] 29
barplot(c(ganar_peso,pierden_peso), main = "Pacientes ganaron y perdieron peso", ylab = "Numero de pacientes", col = c("blue","red"))
legend("topright", legend = c(paste("Ganan peso:
",ganar_peso),paste("Perdida peso: ",pierden_peso)),
fill = c("blue","red"))
data(Melanoma)
str(Melanoma)
## 'data.frame': 205 obs. of 7 variables:
## $ time : int 10 30 35 99 185 204 210 232 232 279 ...
## $ status : int 3 3 2 3 1 1 1 3 1 1 ...
## $ sex : int 1 1 1 0 1 1 1 0 1 0 ...
## $ age : int 76 56 41 71 52 28 77 60 49 68 ...
## $ year : int 1972 1968 1977 1968 1965 1971 1972 1974 1968 1971 ...
## $ thickness: num 6.76 0.65 1.34 2.9 12.08 ...
## $ ulcer : int 1 0 0 0 1 1 1 1 1 1 ...
head(Melanoma)
fallecidos = nrow(Melanoma[Melanoma$status==1,]) + nrow(Melanoma[Melanoma$status==3,])
fallecidos
## [1] 71
# No tienen ulcera
sum(Melanoma$ulcer=="0")
## [1] 115
# Si tienen ulcera
sum(Melanoma$ulcer=="1")
## [1] 90
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
barplot(table(Melanoma$ulcer), main="Presencia y ausencia de Melanoma", xlab= "ulcer", ylab = "Cantidad", beside = TRUE, legend.text = c("Ausencia","Presencia"), col=c("orange","blue"))
data(babyboom)
head(babyboom)
str(babyboom)
## 'data.frame': 44 obs. of 4 variables:
## $ clock.time : num 5 104 118 155 257 405 407 422 431 708 ...
## $ gender : Factor w/ 2 levels "girl","boy": 1 1 2 2 2 1 1 2 2 2 ...
## $ wt : num 3837 3334 3554 3838 3625 ...
## $ running.time: num 5 64 78 115 177 245 247 262 271 428 ...
table(babyboom$gender)
##
## girl boy
## 18 26
cantidad_nacidos= (babyboom[babyboom$clock.time<=708,])
cantidad_nacidos
primeras_horas= nrow(babyboom[babyboom$clock.time< 708,])
cat("Niños que nacieron en las primeras 12 horas: ", primeras_horas)
## Niños que nacieron en las primeras 12 horas: 9
nacidos_m= nrow(babyboom[babyboom$gender == "boy" & babyboom$wt<3000,])
cat("Niños con un peso menor a los 3000gr: ", nacidos_m)
## Niños con un peso menor a los 3000gr: 4
relacion = babyboom[babyboom$wt < 3000,]
table(relacion$gender,relacion$wt)
##
## 1745 2121 2184 2208 2383 2576 2635 2846 2902
## girl 1 0 1 1 1 1 0 0 0
## boy 0 1 0 0 0 0 1 1 1
barplot(table(relacion$gender,relacion$wt<3000), beside = T, main = "Relación entre peso y sexo", col = c("blue", "red"), xlab = "Genero", ylab = "Cantidad")
p_ninos = median(babyboom$wt[babyboom$gender=='boy'])
p_ninos
## [1] 3404
p_ninas = median(babyboom$wt[babyboom$gender=='girl'])
p_ninas
## [1] 3381
barplot(c(p_ninos,p_ninas),ylab = "Peso (gr)",main = "Promedio de pesos total (niños y niñas)",names.arg=c("Niña", "Niño"), col = c("blue", "violet"), ylim = c(0, 3500))
data(Aids2)
head(Aids2)
str(Aids2)
## 'data.frame': 2843 obs. of 7 variables:
## $ state : Factor w/ 4 levels "NSW","Other",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ sex : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
## $ diag : int 10905 11029 9551 9577 10015 9971 10746 10042 10464 10439 ...
## $ death : int 11081 11096 9983 9654 10290 10344 11135 11069 10956 10873 ...
## $ status : Factor w/ 2 levels "A","D": 2 2 2 2 2 2 2 2 2 2 ...
## $ T.categ: Factor w/ 8 levels "hs","hsid","id",..: 1 1 1 5 1 1 8 1 1 2 ...
## $ age : int 35 53 42 44 39 36 36 31 26 27 ...
table(Aids2$state,Aids2$T.categ)
##
## hs hsid id het haem blood mother other
## NSW 1539 50 28 18 30 70 3 42
## Other 204 4 12 8 6 5 2 8
## QLD 186 7 4 5 4 15 1 4
## VIC 536 11 4 10 6 4 1 16
table(Aids2$status)
##
## A D
## 1082 1761
fallecidos = sum(Aids2$status == "D")
cat("Número de fallecidos: ",fallecidos)
## Número de fallecidos: 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
barplot(table(Aids2$sex,Aids2$T.categ), xlab= "Tipo de Transmisión",ylab="Cantidad", beside=TRUE, col=c("red", "green"))
legend("topright",levels(Aids2$sex),fill =c("red","green"))
mujer_trans = Aids2[Aids2$sex=='F',]
barplot(table(mujer_trans$sex=='F',mujer_trans $T.categ), main = "Relación Mujeres y tipo de transmisión",ylab = "Cantidad", col="red")
hombre_trans = Aids2[Aids2$sex=='M',]
barplot(table(hombre_trans$sex=='M',hombre_trans$T.categ), main = "Relación Hombres y tipo de transmisión",ylab = "Cantidad", col="blue")
colores = c("Red","blue","yellow","red","green","pink","orange","brown")
pie(table(Aids2$T.categ),col = colores,labels = table(Aids2$T.categ), main = "Cantidad y tipos de transmisión")
legend("topright",legend = levels(Aids2$T.categ),fill = colores)
data(crime)
head(crime)
str(crime)
## 'data.frame': 51 obs. of 2 variables:
## $ y1983: num 416 614 494 298 773 ...
## $ y1993: num 872 660 671 576 1120 ...
mean(crime$y1993)
## [1] 606.8294
mean(crime$y1983)
## [1] 437.5196
total_83 = round(sum(crime$y1983))
total_83
## [1] 22313
total_93 = round(sum(crime$y1993))
total_93
## [1] 30948
max_estado_1993 = crime[crime$y1993 == max(crime$y1993),]
max_estado_1993
max_estado_1983 = crime[crime$y1983 == max(crime$y1983),]
max_estado_1983
crime["acumulado"] = crime$y1983 + crime$y1993
crime[crime$acumulado == max(crime$acumulado),]
orden_estados = rownames(crime)[order(crime$y1983)]
crime_sorted = crime[orden_estados, ]
numeros = 1:nrow(crime_sorted)
barplot(t(as.matrix(crime_sorted[, c("y1983", "y1993")])), beside = TRUE, main = "Tasa de Crimenes por Estado en 1983 y 1993", xlab = " Estados", ylab = "Tasa de Crimen", col = c("red", "blue"), legend.text = c("1983", "1993"), args.legend = list(x = "topleft", bty = "n"))