library(ggplot2)
library(vtree)
library(modeest)
library(fdth)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
iris$Species=as.factor(iris$Species)
TR=table(iris$Species)
TR
##
## setosa versicolor virginica
## 50 50 50
cbind(TR)
## TR
## setosa 50
## versicolor 50
## virginica 50
barplot(TR, main="Gráfico de barras", xlab="Species", names.arg = c("setosa", "versicolor", "virginica"),
ylim=c(0,55), ylab = "Frecuencia absoluta")
TR1=prop.table(TR)
TR1
##
## setosa versicolor virginica
## 0.3333333 0.3333333 0.3333333
TR2=table(iris$Species,iris$Sepal.Width)
addmargins(prop.table(TR2))
##
## 2 2.2 2.3 2.4 2.5
## setosa 0.000000000 0.000000000 0.006666667 0.000000000 0.000000000
## versicolor 0.006666667 0.013333333 0.020000000 0.020000000 0.026666667
## virginica 0.000000000 0.006666667 0.000000000 0.000000000 0.026666667
## Sum 0.006666667 0.020000000 0.026666667 0.020000000 0.053333333
##
## 2.6 2.7 2.8 2.9 3
## setosa 0.000000000 0.000000000 0.000000000 0.006666667 0.040000000
## versicolor 0.020000000 0.033333333 0.040000000 0.046666667 0.053333333
## virginica 0.013333333 0.026666667 0.053333333 0.013333333 0.080000000
## Sum 0.033333333 0.060000000 0.093333333 0.066666667 0.173333333
##
## 3.1 3.2 3.3 3.4 3.5
## setosa 0.026666667 0.033333333 0.013333333 0.060000000 0.040000000
## versicolor 0.020000000 0.020000000 0.006666667 0.006666667 0.000000000
## virginica 0.026666667 0.033333333 0.020000000 0.013333333 0.000000000
## Sum 0.073333333 0.086666667 0.040000000 0.080000000 0.040000000
##
## 3.6 3.7 3.8 3.9 4
## setosa 0.020000000 0.020000000 0.026666667 0.013333333 0.006666667
## versicolor 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
## virginica 0.006666667 0.000000000 0.013333333 0.000000000 0.000000000
## Sum 0.026666667 0.020000000 0.040000000 0.013333333 0.006666667
##
## 4.1 4.2 4.4 Sum
## setosa 0.006666667 0.006666667 0.006666667 0.333333333
## versicolor 0.000000000 0.000000000 0.000000000 0.333333333
## virginica 0.000000000 0.000000000 0.000000000 0.333333333
## Sum 0.006666667 0.006666667 0.006666667 1.000000000
TR2
##
## 2 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7
## setosa 0 0 1 0 0 0 0 0 1 6 4 5 2 9 6 3 3
## versicolor 1 2 3 3 4 3 5 6 7 8 3 3 1 1 0 0 0
## virginica 0 1 0 0 4 2 4 8 2 12 4 5 3 2 0 1 0
##
## 3.8 3.9 4 4.1 4.2 4.4
## setosa 4 2 1 1 1 1
## versicolor 0 0 0 0 0 0
## virginica 2 0 0 0 0 0
addmargins(prop.table(TR2,1))
##
## 2 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3
## setosa 0.00 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.02 0.12 0.08 0.10 0.04
## versicolor 0.02 0.04 0.06 0.06 0.08 0.06 0.10 0.12 0.14 0.16 0.06 0.06 0.02
## virginica 0.00 0.02 0.00 0.00 0.08 0.04 0.08 0.16 0.04 0.24 0.08 0.10 0.06
## Sum 0.02 0.06 0.08 0.06 0.16 0.10 0.18 0.28 0.20 0.52 0.22 0.26 0.12
##
## 3.4 3.5 3.6 3.7 3.8 3.9 4 4.1 4.2 4.4 Sum
## setosa 0.18 0.12 0.06 0.06 0.08 0.04 0.02 0.02 0.02 0.02 1.00
## versicolor 0.02 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00
## virginica 0.04 0.00 0.02 0.00 0.04 0.00 0.00 0.00 0.00 0.00 1.00
## Sum 0.24 0.12 0.08 0.06 0.12 0.04 0.02 0.02 0.02 0.02 3.00
addmargins(prop.table(TR2,2))
##
## 2 2.2 2.3 2.4 2.5
## setosa 0.00000000 0.00000000 0.25000000 0.00000000 0.00000000
## versicolor 1.00000000 0.66666667 0.75000000 1.00000000 0.50000000
## virginica 0.00000000 0.33333333 0.00000000 0.00000000 0.50000000
## Sum 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##
## 2.6 2.7 2.8 2.9 3
## setosa 0.00000000 0.00000000 0.00000000 0.10000000 0.23076923
## versicolor 0.60000000 0.55555556 0.42857143 0.70000000 0.30769231
## virginica 0.40000000 0.44444444 0.57142857 0.20000000 0.46153846
## Sum 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##
## 3.1 3.2 3.3 3.4 3.5
## setosa 0.36363636 0.38461538 0.33333333 0.75000000 1.00000000
## versicolor 0.27272727 0.23076923 0.16666667 0.08333333 0.00000000
## virginica 0.36363636 0.38461538 0.50000000 0.16666667 0.00000000
## Sum 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##
## 3.6 3.7 3.8 3.9 4
## setosa 0.75000000 1.00000000 0.66666667 1.00000000 1.00000000
## versicolor 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## virginica 0.25000000 0.00000000 0.33333333 0.00000000 0.00000000
## Sum 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
##
## 4.1 4.2 4.4 Sum
## setosa 1.00000000 1.00000000 1.00000000 10.82902098
## versicolor 0.00000000 0.00000000 0.00000000 7.26198246
## virginica 0.00000000 0.00000000 0.00000000 4.90899656
## Sum 1.00000000 1.00000000 1.00000000 23.00000000
round(addmargins(prop.table(TR2,)),3)
##
## 2 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1
## setosa 0.000 0.000 0.007 0.000 0.000 0.000 0.000 0.000 0.007 0.040 0.027
## versicolor 0.007 0.013 0.020 0.020 0.027 0.020 0.033 0.040 0.047 0.053 0.020
## virginica 0.000 0.007 0.000 0.000 0.027 0.013 0.027 0.053 0.013 0.080 0.027
## Sum 0.007 0.020 0.027 0.020 0.053 0.033 0.060 0.093 0.067 0.173 0.073
##
## 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4 4.1 4.2
## setosa 0.033 0.013 0.060 0.040 0.020 0.020 0.027 0.013 0.007 0.007 0.007
## versicolor 0.020 0.007 0.007 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
## virginica 0.033 0.020 0.013 0.000 0.007 0.000 0.013 0.000 0.000 0.000 0.000
## Sum 0.087 0.040 0.080 0.040 0.027 0.020 0.040 0.013 0.007 0.007 0.007
##
## 4.4 Sum
## setosa 0.007 0.333
## versicolor 0.000 0.333
## virginica 0.000 0.333
## Sum 0.007 1.000
barplot(prop.table(TR2), beside = TRUE, main="Species vs Sepal.Width",
ylab="Probabilidad relativa", col= 1:2, ylim=c(0.00,0.080))
vtree(iris$Species)
vtree(iris, c("Species", "Sepal.Width"))
vtree(iris, c("Sepal.Width","Species"))
max(iris$Sepal.Length)
## [1] 7.9
min(iris$Sepal.Length)
## [1] 4.3
mean(iris$Sepal.Length)
## [1] 5.843333
median(iris$Sepal.Length)
## [1] 5.8
cbind(table(iris$Sepal.Length))
## [,1]
## 4.3 1
## 4.4 3
## 4.5 1
## 4.6 4
## 4.7 2
## 4.8 5
## 4.9 6
## 5 10
## 5.1 9
## 5.2 4
## 5.3 1
## 5.4 6
## 5.5 7
## 5.6 6
## 5.7 8
## 5.8 7
## 5.9 3
## 6 6
## 6.1 6
## 6.2 4
## 6.3 9
## 6.4 7
## 6.5 5
## 6.6 2
## 6.7 8
## 6.8 3
## 6.9 4
## 7 1
## 7.1 1
## 7.2 3
## 7.3 1
## 7.4 1
## 7.6 1
## 7.7 4
## 7.9 1
frec=data.frame(table(iris$Sepal.Length))
moda=frec[which.max(frec$Freq),1]
moda
## [1] 5
## 35 Levels: 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 ... 7.9
var(iris$Sepal.Length)
## [1] 0.6856935
sd(iris$Sepal.Length)
## [1] 0.8280661
CV=sd(iris$Sepal.Length)/mean(iris$Sepal.Length)*100
CV
## [1] 14.17113
quantile(iris$Sepal.Length, c(0.75))
## 75%
## 6.4
quantile(iris$Sepal.Length, c(0.1, 0.25, 0.80))
## 10% 25% 80%
## 4.80 5.10 6.52
boxplot(iris$Sepal.Length, horizontal = TRUE, col="red", main="Diagrama de caja y bigotes",
xlab="Sepal.Length")
box=boxplot(iris$Sepal.Length)
box$stats
## [,1]
## [1,] 4.3
## [2,] 5.1
## [3,] 5.8
## [4,] 6.4
## [5,] 7.9
box$conf
## [,1]
## [1,] 5.632292
## [2,] 5.967708
box$out
## numeric(0)
out=subset(iris, Sepal.Length >=4.0)
View(out)
hist(iris$Sepal.Length)
hist(iris$Sepal.Length, main="Histograma", xlab = "Sepal.Length", ylab = "Frecuencia absoluta",
col="red", ylim=c(0,40))
abline(v=mean(iris$Sepal.Length), col="blue")
abline(v=median(iris$Sepal.Length), col="green")
abline(v=mfv(iris$Sepal.Length), col="yellow")
mfv(iris$Sepal.Length)
## [1] 5
Cut=cut(iris$Sepal.Length,
breaks = c(-0.01, 20, 40, 60, 80, 100),
right = TRUE)
Cut
## [1] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [7] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [13] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [19] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [25] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [31] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [37] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [43] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [49] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [55] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [61] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [67] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [73] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [79] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [85] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [91] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [97] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [103] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [109] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [115] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [121] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [127] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [133] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [139] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## [145] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20] (-0.01,20]
## Levels: (-0.01,20] (20,40] (40,60] (60,80] (80,100]
cbind(table(Cut))
## [,1]
## (-0.01,20] 150
## (20,40] 0
## (40,60] 0
## (60,80] 0
## (80,100] 0
barplot(table(Cut))
cut1=cut(iris$Sepal.Length, breaks = 8, right = TRUE)
cbind(table(cut1))
## [,1]
## (4.3,4.75] 11
## (4.75,5.2] 34
## (5.2,5.65] 20
## (5.65,6.1] 30
## (6.1,6.55] 25
## (6.55,7] 18
## (7,7.45] 6
## (7.45,7.9] 6
barplot(table(cut1))
histo=fdt(iris$Sepal.Length)
histo
## Class limits f rf rf(%) cf cf(%)
## [4.257,4.671) 9 0.06 6.00 9 6.00
## [4.671,5.084) 23 0.15 15.33 32 21.33
## [5.084,5.498) 20 0.13 13.33 52 34.67
## [5.498,5.911) 31 0.21 20.67 83 55.33
## [5.911,6.325) 25 0.17 16.67 108 72.00
## [6.325,6.738) 22 0.15 14.67 130 86.67
## [6.738,7.152) 9 0.06 6.00 139 92.67
## [7.152,7.565) 5 0.03 3.33 144 96.00
## [7.565,7.979) 6 0.04 4.00 150 100.00
plot(histo, type="fh")
plot(histo, type="fp", col = "red")
plot(histo, type="cfh")
plot(histo, type="cfp")
plot(histo, type="cdh")
ggplot(iris, aes(x=Species, y=Sepal.Length, fill=Species))+ geom_boxplot()
ggplot(iris, aes(y=Sepal.Length))+ geom_histogram()
##
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(iris, aes(y=Sepal.Length, fill=Species))+ geom_histogram()
##
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.