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