"Error in t.test.default: not enough observations"

Hi all,

I was just trying to demonstrate the mathematical properties of p values under the alternative hypothesis over several sample sizes, but I keep getting this error and don't understand where it's coming from. Here's the code:

#p value decreases as sample size increases when null is false. demonstration

sample_size=c(50,100,200,500,1000,2000,10000)
s=1000
p_values=matrix(NA, nrow = s, ncol = length(sample_size))

for (i in 1:s){
  for (j in 1:length(sample_size)){
  muestra_a=rnorm(n=j, mean = 0, sd=1)
  muestra_b=rnorm(n=j, mean = 20, sd=1)
  p_values[i,j]=t.test(muestra_a, muestra_b, var.equal = TRUE, conf.level = 0.95)$p.value
  
}}
#> Error in t.test.default(muestra_a, muestra_b, var.equal = TRUE, conf.level = 0.95): not enough observations

Created on 2020-01-22 by the reprex package (v0.3.0)

Any help would be appreciated.

Thanks!

I think you mean to use sample_size[j] inside of the for loop.

muestra_a=rnorm(n=sample_size[j], mean = 0, sd=1)
  muestra_b=rnorm(n=sample_size[j], mean = 20, sd=1)

Otherwise, your first sample has n=1, which will not work with a t test.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.