Home > Back-end >  Need to generate 5000 datasets from N(0,1) with sample size 20 and run t-tests with different alphas
Need to generate 5000 datasets from N(0,1) with sample size 20 and run t-tests with different alphas

Time:03-29

lapply(1:5000, function(x) rnorm(n=20, mean=0, sd=1))

is the function I used to generate the datasets.

t.test(x, mu=mu0, alt="two.sided, lev=0.95)

is the t.test equation I made but I do not know how to run the t.test with the data from the lapply function. I need to conduct six tests with mu0=0,2 and the three alternatives.

CodePudding user response:

You can simplify your code generating the 5,000 samples with:

X <- replicate(5000, rnorm(n=20))

The replicate() function is documented at ?replicate and the default arguments for rnorm are mean=0 and sd=1 so you do not need to include them. X is a list with 5,000 samples of 20 so the code to run a t-test on the first sample is

t.test(X[[1]], mu=0, alternative="two.sided", conf.level=0.95)

You can use a for-loop or lapply to run the variations on this test on all 5000 samples, but you may want to extract the results in some other form if you want to plot them or use summary statistics.

CodePudding user response:

if I understood correctly

set.seed(5)
library(tidyverse)
df <- lapply(1:2, function(x) rnorm(n=20))

mu0 <- c(0.2, 0.3)

alt <- c("two.sided", "less", "greater")

# solution 1
res <- sapply(df, function(x) {
  lapply(mu0, function(mu0) {
    lapply(alt, function(alt) {
      t.test(x = x,
             mu = mu0,
             alternative = alt)
    })
  })
})

map_df(flatten(res), broom::tidy)
#> # A tibble: 12 x 8
#>    estimate statistic p.value parameter  conf.low conf.high method   alternative
#>       <dbl>     <dbl>   <dbl>     <dbl>     <dbl>     <dbl> <chr>    <chr>      
#>  1   -0.281    -2.31  0.0321         19   -0.716     0.154  One Sam~ two.sided  
#>  2   -0.281    -2.31  0.0161         19 -Inf         0.0788 One Sam~ less       
#>  3   -0.281    -2.31  0.984          19   -0.640   Inf      One Sam~ greater    
#>  4   -0.281    -2.79  0.0116         19   -0.716     0.154  One Sam~ two.sided  
#>  5   -0.281    -2.79  0.00579        19 -Inf         0.0788 One Sam~ less       
#>  6   -0.281    -2.79  0.994          19   -0.640   Inf      One Sam~ greater    
#>  7    0.416     0.826 0.419          19   -0.132     0.964  One Sam~ two.sided  
#>  8    0.416     0.826 0.791          19 -Inf         0.869  One Sam~ less       
#>  9    0.416     0.826 0.209          19   -0.0363  Inf      One Sam~ greater    
#> 10    0.416     0.444 0.662          19   -0.132     0.964  One Sam~ two.sided  
#> 11    0.416     0.444 0.669          19 -Inf         0.869  One Sam~ less       
#> 12    0.416     0.444 0.331          19   -0.0363  Inf      One Sam~ greater

# solution 2
df_expand <- expand_grid(df, mu0, alt)

pmap(df_expand, ~t.test(x = unlist(..1), mu = ..2, alternative = ..3)) %>% 
  map_df(broom::tidy)
#> # A tibble: 12 x 8
#>    estimate statistic p.value parameter  conf.low conf.high method   alternative
#>       <dbl>     <dbl>   <dbl>     <dbl>     <dbl>     <dbl> <chr>    <chr>      
#>  1   -0.281    -2.31  0.0321         19   -0.716     0.154  One Sam~ two.sided  
#>  2   -0.281    -2.31  0.0161         19 -Inf         0.0788 One Sam~ less       
#>  3   -0.281    -2.31  0.984          19   -0.640   Inf      One Sam~ greater    
#>  4   -0.281    -2.79  0.0116         19   -0.716     0.154  One Sam~ two.sided  
#>  5   -0.281    -2.79  0.00579        19 -Inf         0.0788 One Sam~ less       
#>  6   -0.281    -2.79  0.994          19   -0.640   Inf      One Sam~ greater    
#>  7    0.416     0.826 0.419          19   -0.132     0.964  One Sam~ two.sided  
#>  8    0.416     0.826 0.791          19 -Inf         0.869  One Sam~ less       
#>  9    0.416     0.826 0.209          19   -0.0363  Inf      One Sam~ greater    
#> 10    0.416     0.444 0.662          19   -0.132     0.964  One Sam~ two.sided  
#> 11    0.416     0.444 0.669          19 -Inf         0.869  One Sam~ less       
#> 12    0.416     0.444 0.331          19   -0.0363  Inf      One Sam~ greater

Created on 2022-03-29 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related