x = c(1.52, 5.24, 0.23, 2.47, 2.63)
library(furrr)
## Loading required package: future
library(distr6)
##
## Attaching package: 'distr6'
## The following object is masked from 'package:stats':
##
## qqplot
## The following object is masked from 'package:base':
##
## truncate
plan(multiprocess)
## Warning: Strategy 'multiprocess' is deprecated in future (>= 1.20.0). Instead,
## explicitly specify either 'multisession' or 'multicore'. In the current R
## session, 'multiprocess' equals 'multisession'.
set.seed(2020)
###Estimando los parámetros de la población
s = sd(x)
mu = 0 ###hipótesis nula
###Contruir la distribución muestral
pop = Normal$new(mean = mu, sd = s)
cal_stat = function(x, mu0 = 0) {
(mean(x) - mu0)/(sd(x)/sqrt(length(x)))
}
N = 5e4
samp_dist = future_map_dbl(1:N, ~ cal_stat(pop$rand(5)))
## Deprecated. Use $properties$kurtosis instead.
## Deprecated. Use $properties$skewness instead.
## Deprecated. Use $properties$support instead.
## Deprecated. Use $properties$symmetry instead.
## Deprecated. Use $traits$type instead.
## Deprecated. Use $traits$valueSupport instead.
## Deprecated. Use $traits$variateForm instead.
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
plot(density(samp_dist,), xlab = "t statistic", main = "", xlim = c(-10,10)) # Histograma de samp_dist
curve(dt(x,4), -10, 10, add = T, col = 2, n = 1e3) # distribución teórica t(n-1)
abline(v = cal_stat(x), col = 3) # estadístico observado

###Calcular el p valor
sum(samp_dist >= cal_stat(x))/N
## [1] 0.0218
## [1] 0.02212
1 - pt(cal_stat(x), 4)
## [1] 0.02137046
## [1] 0.02137046
t.test(x, alternative = "greater")$p.val
## [1] 0.02137046
## [1] 0.0213706
###¿Por qué no utilizar la media muestral como estadístico?
N = 5e4
sampling_distribution = future_map_dbl(1:N, ~ mean(pop$rand(5)))
## Deprecated. Use $properties$kurtosis instead.
## Deprecated. Use $properties$skewness instead.
## Deprecated. Use $properties$support instead.
## Deprecated. Use $properties$symmetry instead.
## Deprecated. Use $traits$type instead.
## Deprecated. Use $traits$valueSupport instead.
## Deprecated. Use $traits$variateForm instead.
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
## Warning: UNRELIABLE VALUE: Future ('<none>') unexpectedly generated random
## numbers without specifying argument 'seed'. There is a risk that those random
## numbers are not statistically sound and the overall results might be invalid.
## To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random
## numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use
## 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".
plot(density(sampling_distribution, bw = "SJ"), main = "", xlab = "x mean")
abline(v = mean(x), col = 2) # estadístico

sum(sampling_distribution >= mean(x))/N
## [1] 0.0016
## [1] 0.00166
##El valor del p valor es diferente que el obtenido anteriormente. Esto no indica que esté mal sino que el p valor depende del estadístico que se está calculando.