Aplicaciones de R para el estudio de la Estadística Inferencial

Muestras aleatorio simple con R

#poblacion
P=c(1,2,3,4,5,6,7,8,9,10)
P
##  [1]  1  2  3  4  5  6  7  8  9 10
#muestra de tamaño 5 de la poblaciòn P
m1=sample(P,5,rep=T)
m1
## [1] 2 7 2 4 2
#muestra de tamaño 5 de la poblacion P sin reposicion

m2=sample(P,5,rep=F)
m2
## [1]  6  7  9  5 10
help(rnorm)
#rnorm(n, mean = 0, sd = 1)

P=rnorm(1000, mean = 25, sd = 4)
hist(P)

# Probabilidad de bolillas rojas en un recipiente con 2400 bolillas

# random binomial
help(rbinom)
q=seq(0,20,1)
q
##  [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
p=pbinom(q,20,0.3)
p
##  [1] 0.0007979227 0.0076372598 0.0354831323 0.1070868045 0.2375077789
##  [6] 0.4163708294 0.6080098122 0.7722717974 0.8866685371 0.9520381027
## [11] 0.9828551836 0.9948618385 0.9987211204 0.9997389530 0.9999570600
## [16] 0.9999944497 0.9999994573 0.9999999623 0.9999999983 1.0000000000
## [21] 1.0000000000

#pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE)

pacum=pbinom(q,20,0.3,lower.tail=T)
pacum
##  [1] 0.0007979227 0.0076372598 0.0354831323 0.1070868045 0.2375077789
##  [6] 0.4163708294 0.6080098122 0.7722717974 0.8866685371 0.9520381027
## [11] 0.9828551836 0.9948618385 0.9987211204 0.9997389530 0.9999570600
## [16] 0.9999944497 0.9999994573 0.9999999623 0.9999999983 1.0000000000
## [21] 1.0000000000

#Simular una poblacion de 2400 canicas con 30% rojas y obtener 15 muestras de tamaño 20 con y sin reemplazamiento #calcular la distribucion muestral de las proporciones #rbinom(n, size, prob)

m3=rbinom(2400,1,0.3)
m3
##    [1] 0 0 0 0 1 0 0 1 1 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 1 1 0 1
##   [38] 0 1 0 1 0 1 0 0 0 0 0 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 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 1
p4=prop.table(table(m4))
p4
## m4
##   0   1 
## 0.7 0.3
help(sapply)
muestras20=sapply(1:15, function(x){sum(sample(m3,20,rep=T))/20})
muestras20
##  [1] 0.30 0.45 0.40 0.25 0.15 0.25 0.10 0.30 0.25 0.40 0.30 0.20 0.40 0.20 0.40
hist(muestras20)

muestras300=sapply(1:100, function(x){sum(sample(m3,300,rep=T))/300})
hist(muestras300)