blob: 5daa5f6685426896aa111be8e3d8cfdcfece611b [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))
46barplot(table(con_angles))
47barplot(table(con_angles)[c("Cap", "Baix", "Mitjà", "Alt", "Molt Alt")])
48
49# Nombre de germans
50binom_prob = .3
51binom_size = .5
52ngermans = rnbinom(n, binom_size, binom_prob)
53ngermans
54
55table(ngermans)
56plot(table(ngermans))
57points(0:9 + 0.1, 100*dnbinom(0:9, binom_prob, binom_size), ty="h", col="red")
58
59# == DATA FRAME ==
60dd = data.frame(nid, genere, edat, pes, alçada, con_angles, ngermans)
61head(dd)
62write.csv2(dd, 'practica1.csv')