Closed andytimm closed 1 year ago
@martijnweterings, is it alright if I work from your provided code to implement this? Happy to add you as a contributor in that case if you want.
Sure, you can use that code.
Also check out the following code where I made the previous code more streamlined to help generating the following image:
The code is mostly doing thew same as the previous code but this time enclosed in a separate function. An important difference is that the simulations to compute the type-m error are only done once for multiple different levels of power. This saves some computation time.
library(retrodesign) ### for shifted t-distribution significance tests
### compute power and error rates for two sample t-test
retropower = function(d, n, alpha = 0.05, n.sim = 10^4) {
nu = 2*n-2
### boundary for alpha level t-test
tc = qt(1-alpha/2, df = nu)
### power
power = pt(-tc, df = nu, ncp=d/sqrt(2/n)) +
1-pt( tc, df = nu, ncp=d/sqrt(2/n))
### s-error rate
type_s = pt(-tc, df = nu, ncp=d/sqrt(2/n))/power
### simulate experiments
x0 = rnorm(n.sim,0)
s = sqrt(rchisq(n.sim,nu)/nu)
### m-error
type_m = sapply(d, FUN = function(di) {
x = abs(x0+di*sqrt(n/2))
significant = x/s>tc
return(mean(x[significant == 1]/sqrt(n/2))/di)
})
return(list(power = power, type_s = type_s, type_m = type_m))
}
### some settings
set.seed(1)
d = seq(0,3,0.05)
####
### creating plots for image 4
####
layout(matrix(1:2,1, byrow = TRUE))
par(mgp =c(2,1,0), mar = c(4,4,3,1))
plot(2,2, log = "xy", type = "l", xlim = c(0.05,1), ylim = c(0.0001,1),
xlab = "power", ylab = "type-s error")
m = c(5,20,100)
i = 0
for (mi in m) {
i = i+1
r = retropower(d/sqrt(mi/5),mi)
q = retrodesign(A = as.list(d/sqrt(mi/5)), s = sqrt(2/mi), df = 2*mi - 2)
lines(r$power,r$type_s, col = i)
lines(q$power,q$type_s, col = i, lty = 2)
}
plot(2,2, log = "xy", type = "l", xlim = c(0.05,1), ylim = c(1,20),
xlab = "power", ylab = "type-m error")
m = c(5,20,100)
i = 0
for (mi in m) {
i = i+1
r = retropower(d/sqrt(mi/5),mi)
q = retrodesign(A = as.list(d/sqrt(mi/5)), s = sqrt(2/mi), df = 2*mi - 2)
lines(r$power,r$type_m, col = i)
lines(q$power,q$type_m, col = i, lty = 2)
}
title(main="type S/M errors versus power for different type of tests", outer=TRUE, line=-1, cex = 1)
legend(0.08,18, c("n=5","n=20","n=100"), col = c(1,2,3), lty = 1, cex = 0.7, title = "non-central\n t-distribution", box.lwd = 0)
legend(0.3,18, c("n=5","n=20","n=100"), col = c(1,2,3), lty = 2, cex = 0.7, title = "shifted\n t-distribution", box.lwd = 0)
####
## creating plots for image 3
###
d = seq(0,5,0.025)
r = retropower(d,5)
layout(matrix(1:3,3))
par(mgp =c(2,1,0), mar = c(4,4,2,1))
plot(d,r$power, type ="l", xlab = "effect size in terms of sigma", ylab = "power", main = "power for two sample test with n = 5" , ylim = c(0,1))
plot(d,r$type_s, type ="l", xlab = "effect size in terms of sigma", ylab = "error rate", main = "S-type error for two sample test with n = 5" , ylim = c(0,0.5))
plot(d,r$type_m, type ="l", xlab = "effect size in terms of sigma", ylab = "magnification", main = "M-type error for two sample test with n = 5" , ylim = c(0,30))
These are suggested improvements from @martijnweterings over email, making issues to track everything I want in a 0.2.0 version of the package.
Martijn provided the attached reproducible example of both features which gives slightly improved performance in the simulation case (i.e, in
retrodesign
). It's a pretty small difference, but worth adding in since it's not hard to implement.