[R] power.t.test visualization problem

stocdarf at mail.tu-berlin.de stocdarf at mail.tu-berlin.de
Tue May 24 14:44:20 CEST 2011


Dear R-User,

I'm trying to visualize the results of the power calculation with the  
function power.t.test(). Therefore I want to plot the related  
t-distributions and shade the surfaces indicatingt the type I error,  
the type II error and the power. For sample sizes greater 30 I got  
results which are very satisfying. For small sample sizes I got stuck  
and did'nt find a mistake.
To show you the problem I wrote some lines in R:

par(mfrow = c(4,2))
for(n in c(2,6,10,14,18,22,26,30))
{
  temp = power.t.test(n = n, sd = 1, power = 0.5,  sig.level = 0.05,    
                                           # power calculation -->  
power is specified with 50%
                     type = "one.sample", alternative = "one.sided")
  s = temp$sd                                                           
                                                                       
# get standard deviation out of test distribution
  n = temp$n                                                            
                                                                        
# get sample size out of test distribution
  delta = temp$delta                                                    
                                                                   #  
get delta (distance between centrality points) out of test ditribution

  plot(1:10, xlim = c(-5,10), ylim = c(0, 0.5), type = "n")             
                                                     # create plot  
window

  x = seq(-5, 10, length = 400)                                         
                                                               #  
x-values
  y1 = dt(x, df = n-1)                                                  
                                                                       
# y-values calculated with related t-distribution (df=n-1)

  lines(x, y1, col = 2)                                                 
                                                                        
# plot related t-distribution
  lines(x + delta/(s/sqrt(n)), y1)                                      
                                                                 #  
plot related t-distribution shifted with normalized delta

  abline(v = qt(0.95, df = n-1))                                        
                                                                 #  
draws a vertical line at the
  abline(v=delta/(s/sqrt(n)),lty=2)

  legend("topright",legend=c(paste("n=",n),
              paste("bias=",round(qt(0.95, df =  
n-1)-delta/(s/sqrt(n)),2))))                                         #  
creates legend
}

This code creates some plots with different sample size n.  I would  
expect the solid vertical line and the dotted vertical line one above  
the other. But indeed the space between both of them is increasing  
with a decreasing sample size.
Whe re is my mistake? Is it  a error in reasoning or is it "just" not  
possible to visualize this problem for small sample sizes?

I look foward to any suggestions and hints. So much thanks in advance.

Étienne



More information about the R-help mailing list