Etienne Stockhausen
2011-May-24 13:08 UTC
[R] Problem with visualization of power of t test
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
___________________________________________________________
Schon geh?rt? WEB.DE hat einen genialen Phishing-Filter in die
Toolbar eingebaut! http://produkte.web.de/go/toolbar
Richard M. Heiberger
2011-May-24 13:35 UTC
[R] Problem with visualization of power of t test
Etienne,
For this type of graph, I would use the functions in the HH package
## install.packages("HH") ## if necessary
library(HH)
normal.and.t.dist(std.dev=2, Use.alpha.left=FALSE, deg.free=6,
mu.H1=2, Use.mu.H1=TRUE,
obs.mean=2.5, Use.obs.mean=TRUE, xmin=-7)
Rich
On Tue, May 24, 2011 at 9:08 AM, Etienne Stockhausen
<einohr2002@web.de>wrote:
> 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.
>
[[alternative HTML version deleted]]
Etienne Stockhausen
2011-May-24 13:57 UTC
[R] Problem with visualization of power of t test
Dear Richard M. Heiberger, I've some problems understanding your function. Where is the power visualized? May you give me an example including the following parameter given by? power.t.test(n = 2, sd = 1, power = 0.5,? sig.level = 0.05, type = "one.sample", alternative = "one.sided") ? Thanks in advance ?tienne ___________________________________________________________ Schon geh?rt? WEB.DE hat einen genialen Phishing-Filter in die