| # == GENERACIÓ VARIABLES ALEATÒRIES == |
| n = 100 |
| |
| # Nid |
| nid = sample(100:999, n, replace=F) |
| |
| # Gènere |
| genere = sample(c("H", "D"), n, replace=T, prob=c(38, 62)) |
| tg = table(genere) |
| tg |
| barplot(tg) |
| |
| # Edat |
| edat_weights = c(5, 4, 2, 2, 1, 1) |
| edat = sample(19:24, n, replace=T, prob=edat_weights) |
| te = table(edat) |
| te_teoric = edat_weights*100/sum(edat_weights) |
| |
| rbind(te, te_teoric) |
| |
| plot(table(edat), ty="h") |
| points(19:24 + 0.1, te_teoric, ty="h", col="red") |
| |
| # Pes |
| gamma_shape = 300 |
| gamma_scale = 0.2 |
| pes = rgamma(n, shape=gamma_shape, scale=gamma_scale) |
| |
| mean(pes) |
| mithteorica = gamma_shape*gamma_scale |
| mithteorica |
| |
| hist(pes, freq=F) |
| curve(dgamma(x, shape=gamma_shape, scale=gamma_scale), add=T) |
| curve(dgamma(x, shape=310, scale=0.22), col="red", add=T) |
| |
| plot(sort(pes), (1:n)/n, ty="s") |
| curve(pgamma(x, shape=gamma_shape, scale=gamma_scale), add=T) |
| curve(pgamma(x, shape=301, scale=gamma_scale), add=T) |
| |
| # Alçada |
| alçada = rnorm(n, 170, 8) |
| |
| # Coneixement anglès |
| con_angles = sample(c("Cap", "Baix", "Mitjà", "Alt", "Molt Alt"), n, replace=T, prob=c(2, 7, 40, 30, 21)) |
| # - Sense especificar l'ordre de les barres: |
| barplot(table(con_angles)) |
| # - Especificant un ordre específic de les barres: |
| barplot(table(con_angles)[c("Cap", "Baix", "Mitjà", "Alt", "Molt Alt")]) |
| |
| # Nombre de germans |
| binom_prob = .3 |
| binom_size = 5 |
| ngermans = rnbinom(n, binom_size, binom_prob) |
| ngermans |
| |
| table(ngermans) |
| plot(table(ngermans)) |
| points(0:9 + 0.1, 100*dnbinom(0:9, binom_prob, binom_size), ty="h", col="red") |
| |
| # == DATA FRAME == |
| dd = data.frame(nid, genere, edat, pes, alçada, con_angles, ngermans) |
| head(dd) |
| write.csv2(dd, 'practica1.csv') |