Skip to content

Commit 2c3e61d

Browse files
committed
Tema 6 completado
1 parent 8e27f24 commit 2c3e61d

13 files changed

Lines changed: 2319 additions & 97 deletions

data/EnergyDrink

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"estudio" "bebe" "sexo"
2+
"1" "Informatica" "No" "Mujer"
3+
"2" "Mates" "No" "Hombre"
4+
"3" "Industriales" "Si" "Mujer"
5+
"4" "Informatica" "Si" "Hombre"
6+
"5" "Industriales" "No" "Mujer"
7+
"6" "Mates" "No" "Mujer"
8+
"7" "Informatica" "Si" "Hombre"
9+
"8" "Industriales" "No" "Hombre"
10+
"9" "Informatica" "No" "Hombre"
11+
"10" "Informatica" "No" "Hombre"
12+
"11" "Informatica" "No" "Hombre"
13+
"12" "Industriales" "No" "Hombre"
14+
"13" "Informatica" "Si" "Hombre"
15+
"14" "Informatica" "No" "Hombre"
16+
"15" "Telematica" "No" "Hombre"
17+
"16" "Industriales" "No" "Mujer"
18+
"17" "Informatica" "No" "Hombre"
19+
"18" "Industriales" "No" "Hombre"
20+
"19" "Informatica" "No" "Hombre"
21+
"20" "Informatica" "No" "Hombre"
22+
"21" "Industriales" "No" "Hombre"
23+
"22" "Industriales" "No" "Mujer"
24+
"23" "Mates" "No" "Hombre"
25+
"24" "Industriales" "No" "Hombre"
26+
"25" "Informatica" "No" "Mujer"
27+
"26" "Industriales" "No" "Hombre"
28+
"27" "Telematica" "No" "Hombre"
29+
"28" "Telematica" "Si" "Hombre"
30+
"29" "Informatica" "No" "Mujer"
31+
"30" "Industriales" "Si" "Hombre"
32+
"31" "Industriales" "Si" "Hombre"
33+
"32" "Industriales" "No" "Hombre"
34+
"33" "Mates" "No" "Mujer"
35+
"34" "Mates" "No" "Hombre"
36+
"35" "Informatica" "No" "Hombre"
37+
"36" "Industriales" "No" "Hombre"
38+
"37" "Industriales" "No" "Mujer"
39+
"38" "Telematica" "No" "Hombre"
40+
"39" "Informatica" "No" "Mujer"
41+
"40" "Mates" "No" "Mujer"
42+
"41" "Industriales" "Si" "Mujer"
43+
"42" "Mates" "No" "Hombre"
44+
"43" "Industriales" "No" "Hombre"
45+
"44" "Industriales" "No" "Hombre"
46+
"45" "Informatica" "No" "Mujer"
47+
"46" "Informatica" "Si" "Mujer"
48+
"47" "Informatica" "No" "Hombre"
49+
"48" "Informatica" "Si" "Hombre"
50+
"49" "Mates" "Si" "Hombre"
51+
"50" "Industriales" "No" "Hombre"
52+
"51" "Industriales" "No" "Hombre"
53+
"52" "Informatica" "Si" "Hombre"
54+
"53" "Industriales" "Si" "Hombre"
55+
"54" "Industriales" "No" "Hombre"
56+
"55" "Informatica" "No" "Mujer"
57+
"56" "Informatica" "Si" "Mujer"
58+
"57" "Industriales" "No" "Mujer"
59+
"58" "Industriales" "No" "Hombre"
60+
"59" "Telematica" "No" "Hombre"
61+
"60" "Industriales" "Si" "Hombre"
62+
"61" "Informatica" "No" "Hombre"
63+
"62" "Industriales" "No" "Hombre"
64+
"63" "Informatica" "No" "Hombre"
65+
"64" "Informatica" "No" "Hombre"
66+
"65" "Telematica" "No" "Hombre"
67+
"66" "Informatica" "Si" "Mujer"
68+
"67" "Informatica" "No" "Hombre"
69+
"68" "Informatica" "No" "Hombre"
70+
"69" "Telematica" "No" "Hombre"
71+
"70" "Industriales" "No" "Mujer"
72+
"71" "Informatica" "No" "Hombre"
73+
"72" "Informatica" "No" "Mujer"
74+
"73" "Informatica" "No" "Hombre"
75+
"74" "Industriales" "No" "Hombre"
76+
"75" "Mates" "No" "Hombre"
77+
"76" "Informatica" "No" "Hombre"
78+
"77" "Telematica" "No" "Mujer"
79+
"78" "Informatica" "No" "Hombre"
80+
"79" "Mates" "No" "Mujer"
81+
"80" "Telematica" "No" "Hombre"
82+
"81" "Informatica" "No" "Hombre"
83+
"82" "Industriales" "Si" "Hombre"
84+
"83" "Informatica" "No" "Hombre"
85+
"84" "Informatica" "No" "Hombre"
86+
"85" "Informatica" "No" "Hombre"
87+
"86" "Industriales" "No" "Mujer"
88+
"87" "Telematica" "No" "Mujer"
89+
"88" "Informatica" "No" "Mujer"
90+
"89" "Informatica" "No" "Mujer"
91+
"90" "Industriales" "No" "Mujer"
92+
"91" "Industriales" "No" "Hombre"
93+
"92" "Mates" "No" "Hombre"
94+
"93" "Informatica" "No" "Hombre"
95+
"94" "Telematica" "No" "Hombre"
96+
"95" "Industriales" "No" "Hombre"
97+
"96" "Informatica" "Si" "Mujer"
98+
"97" "Mates" "No" "Hombre"
99+
"98" "Industriales" "No" "Mujer"
100+
"99" "Telematica" "Si" "Hombre"
101+
"100" "Informatica" "Si" "Mujer"
102+
"101" "Mates" "No" "Mujer"
103+
"102" "Telematica" "No" "Hombre"
104+
"103" "Informatica" "No" "Hombre"
105+
"104" "Industriales" "No" "Mujer"
106+
"105" "Informatica" "No" "Hombre"
107+
"106" "Informatica" "Si" "Hombre"
108+
"107" "Industriales" "No" "Hombre"
109+
"108" "Informatica" "No" "Hombre"
110+
"109" "Informatica" "No" "Hombre"
111+
"110" "Informatica" "No" "Hombre"
112+
"111" "Mates" "Si" "Mujer"
113+
"112" "Mates" "No" "Mujer"
114+
"113" "Industriales" "Si" "Hombre"
115+
"114" "Telematica" "Si" "Mujer"
116+
"115" "Informatica" "No" "Mujer"
117+
"116" "Telematica" "No" "Hombre"
118+
"117" "Informatica" "No" "Mujer"
119+
"118" "Informatica" "No" "Hombre"
120+
"119" "Informatica" "No" "Hombre"
121+
"120" "Telematica" "No" "Mujer"
122+
"121" "Informatica" "Si" "Hombre"
123+
"122" "Mates" "No" "Hombre"

scripts/tema6/01-table.Rmd

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: "Tablas de Contingencia"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "24/12/2018"
5+
output: html_document
6+
---
7+
8+
# Tablas de contingencia
9+
10+
```{r}
11+
datos = factor(c("H", "M", "M", "M", "H", "H", "M", "M"))
12+
table(datos)
13+
table(datos)["M"]
14+
sum(table(datos))
15+
```
16+
17+
# Frecuencias relativas
18+
19+
$$f_i = \frac{n_i}{n}$$
20+
21+
```{r}
22+
prop.table(table(datos))
23+
100*prop.table(table(datos))
24+
table(datos)/length(datos)
25+
names(which(table(datos)==3))
26+
27+
moda <- function(d){
28+
names(which(table(d)==max(table(d))))
29+
}
30+
31+
m_t = moda(datos)
32+
```
33+
34+
La moda del data frame es: `r m_t`.
35+
36+
# Paquete `gmodels`
37+
```{r}
38+
library(gmodels)
39+
sex = factor(c("H", "M", "M", "M", "H", "H", "M", "M"))
40+
ans = factor(c("S", "N", "S", "S", "S", "N", "N", "S"))
41+
CrossTable(sex, ans, prop.chisq = FALSE)
42+
```
43+
44+
# Sumas por filas y columnas
45+
```{r}
46+
tt <- table(sex, ans)
47+
tt# Frec. absolutas
48+
prop.table(tt)#Frec. Rel. Global
49+
prop.table(tt, margin = 1)#Frec. Rel. Por sexo
50+
prop.table(tt, margin = 2)#Frec. Rel. Por respuesta
51+
52+
colSums(tt)
53+
rowSums(tt)
54+
colSums(prop.table(tt))
55+
rowSums(prop.table(tt))
56+
57+
apply(tt, FUN = sum, MARGIN = 1)
58+
apply(tt, FUN = sqrt, MARGIN = c(1,2))
59+
```
60+

scripts/tema6/01-table.html

Lines changed: 344 additions & 0 deletions
Large diffs are not rendered by default.

scripts/tema6/02-multivariant.Rmd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: "Multivariante"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "24/12/2018"
5+
output: html_document
6+
---
7+
8+
# Datos multidimensionales
9+
10+
## Ejemplo con tres dimensiones
11+
12+
```{r}
13+
ans = sample(c("Si", "No"), size = 100, replace = TRUE)
14+
sex = sample(c("H", "M"), size = 100, replace = TRUE)
15+
place = sample(c("San Francisco", "Barcelona", "Valencia", "Cobija", "Asturias"), size = 100, replace = TRUE)
16+
table(sex, ans, place)
17+
ftable(sex, ans, place)
18+
ftable(sex, ans, place, col.vars = c("sex", "ans"))
19+
```
20+
21+
### Filtrar las tablas
22+
23+
```{r}
24+
table(sex, ans, place)["M", "Si", "San Francisco"]
25+
table(sex, ans, place)[ , "Si", "Valencia"]
26+
table(sex, ans, place)[ , "No", ]
27+
table(sex, ans, place)["M", , "Cobija"]
28+
```
29+
30+
### Frecuencias relativas
31+
```{r}
32+
prop.table(table(sex, ans, place))#Frec. Rel. Globales
33+
prop.table(table(sex, ans, place), margin = 3) # Frec. Rel. Marginal por Lugar
34+
prop.table(table(sex, ans, place), margin = c(1, 3)) # Frec. Rel. Marg. por Sexo y País
35+
36+
ftable(prop.table(table(sex, ans, place)))
37+
```
38+

scripts/tema6/02-multivariant.html

Lines changed: 415 additions & 0 deletions
Large diffs are not rendered by default.

scripts/tema6/03-people.Rmd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "03-people"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "24/12/2018"
5+
output:
6+
html_document: default
7+
pdf_document: default
8+
---
9+
10+
# Ejemplo de color de ojos y de pelo
11+
12+
```{r}
13+
HairEyeColor
14+
15+
sum(HairEyeColor) -> total
16+
```
17+
18+
El total de individuos de la tabla de datos es `r total`.
19+
20+
```{r}
21+
prop.table(HairEyeColor, margin = 3)
22+
prop.table(HairEyeColor, margin = c(1,2))
23+
```
24+
25+
```{r}
26+
aperm(HairEyeColor, perm = c("Sex", "Hair", "Eye"))
27+
```
28+
29+
```{r}
30+
library(kableExtra)
31+
kable(HairEyeColor)
32+
```
33+
34+
```{r, results='asis'}
35+
library(xtable)
36+
sex = factor(c("H", "M", "M", "M", "H", "H", "M", "M"))
37+
ans = factor(c("S", "N", "S", "S", "S", "N", "N", "S"))
38+
39+
xtable(table(sex, ans))
40+
```
41+

scripts/tema6/03-people.html

Lines changed: 788 additions & 0 deletions
Large diffs are not rendered by default.

scripts/tema6/03-people.pdf

145 KB
Binary file not shown.

scripts/tema6/04-final-example.Rmd

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: "Ejemplo Final"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "24/12/2018"
5+
output: html_document
6+
---
7+
8+
# Ejemplo final
9+
10+
## Juntar color de ojos y pelo sin distinguir por sexo
11+
12+
```{r}
13+
ftable(HairEyeColor)
14+
male <- HairEyeColor[, ,"Male"]
15+
female <- HairEyeColor[, ,"Female"]
16+
data <- as.table(male+female)
17+
data
18+
```
19+
20+
## Manipulación de datos
21+
22+
```{r}
23+
dimnames(data) = list(
24+
Pelo = c("Negro", "Marron", "Pelirrojo", "Rubio"),
25+
Ojos = c("Marrones", "Azules", "Pardos", "Verdes")
26+
)
27+
data
28+
```
29+
30+
## Diagrama de Mosaico
31+
32+
```{r}
33+
plot(data, col = c("lightblue"), main = "Diagrama de Mosaico")
34+
```
35+
36+
## Datos numéricos
37+
```{r}
38+
sum(data)
39+
colSums(data)
40+
rowSums(data)
41+
round(prop.table(colSums(data)), 3)
42+
round(prop.table(rowSums(data)), 3)
43+
```
44+
45+
## Diagramas de barras
46+
47+
```{r}
48+
barplot(prop.table(colSums(data)), ylim = c(0, 0.4),
49+
main = "Frecuencias relativas del color de ojos",
50+
col = c("burlywood4", "lightblue", "orange3", "lightgreen")
51+
)
52+
```
53+
54+
## Frecuencias relativas globales y marginales
55+
56+
```{r}
57+
round(prop.table(data), 3)
58+
round(prop.table(data, margin = 1), 3)
59+
round(prop.table(data, margin = 2), 3)
60+
61+
barplot(prop.table(data, margin = 1), beside = TRUE,
62+
legend.text = TRUE, ylim = c(0, 0.8),
63+
col = c("black", "burlywood4", "red", "gold"),
64+
main = "Frecuencias relativas del color de pelo\n para cada color de ojo.")
65+
66+
67+
68+
barplot(t(prop.table(data, margin = 2)), beside = TRUE,
69+
legend.text = TRUE, ylim = c(0, 0.6),
70+
col = c("burlywood4", "lightblue", "orange3", "lightgreen"),
71+
main = "Frecuencias relativas del color de ojos\n para cada color de pelo")
72+
```
73+

scripts/tema6/04-final-example.html

Lines changed: 337 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)