blob: 7310d8796d8a9aa0787725593a91e8caddb30fb3 [file] [log] [blame]
avm99963a3a14192021-02-11 10:53:22 +01001# == GENERACIÓ VARIABLES ALEATÒRIES ==
2n = 100
3
4# Nid
5nid = sample(100:999, n, replace=F)
6
7# Gènere
8genere = sample(c("H", "D"), n, replace=T, prob=c(38, 62))
9tg = table(genere)
10tg
11barplot(tg)
12
13# Edat
14edat_weights = c(5, 4, 2, 2, 1, 1)
15edat = sample(19:24, n, replace=T, prob=edat_weights)
16te = table(edat)
17te_teoric = edat_weights*100/sum(edat_weights)
18
19rbind(te, te_teoric)
20
21plot(table(edat), ty="h")
22points(19:24 + 0.1, te_teoric, ty="h", col="red")
23
24# Pes
25gamma_shape = 300
26gamma_scale = 0.2
27pes = rgamma(n, shape=gamma_shape, scale=gamma_scale)
28
29mean(pes)
30mithteorica = gamma_shape*gamma_scale
31mithteorica
32
33hist(pes, freq=F)
34curve(dgamma(x, shape=gamma_shape, scale=gamma_scale), add=T)
35curve(dgamma(x, shape=310, scale=0.22), col="red", add=T)
36
37plot(sort(pes), (1:n)/n, ty="s")
38curve(pgamma(x, shape=gamma_shape, scale=gamma_scale), add=T)
39curve(pgamma(x, shape=301, scale=gamma_scale), add=T)
40
41# Alçada
42alçada = rnorm(n, 170, 8)
43
44# Coneixement anglès
45con_angles = sample(c("Cap", "Baix", "Mitjà", "Alt", "Molt Alt"), n, replace=T, prob=c(2, 7, 40, 30, 21))
avm999638d88ad42021-02-11 11:02:13 +010046# - Sense especificar l'ordre de les barres:
avm99963a3a14192021-02-11 10:53:22 +010047barplot(table(con_angles))
avm999638d88ad42021-02-11 11:02:13 +010048# - Especificant un ordre específic de les barres:
avm99963a3a14192021-02-11 10:53:22 +010049barplot(table(con_angles)[c("Cap", "Baix", "Mitjà", "Alt", "Molt Alt")])
50
51# Nombre de germans
52binom_prob = .3
53binom_size = .5
54ngermans = rnbinom(n, binom_size, binom_prob)
55ngermans
56
57table(ngermans)
58plot(table(ngermans))
59points(0:9 + 0.1, 100*dnbinom(0:9, binom_prob, binom_size), ty="h", col="red")
60
61# == DATA FRAME ==
62dd = data.frame(nid, genere, edat, pes, alçada, con_angles, ngermans)
63head(dd)
64write.csv2(dd, 'practica1.csv')