Skip to content

Instantly share code, notes, and snippets.

@Lakens
Last active May 13, 2019 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lakens/745273f58be0a59a050483a52b32c322 to your computer and use it in GitHub Desktop.
Save Lakens/745273f58be0a59a050483a52b32c322 to your computer and use it in GitHub Desktop.
justify alpha part 2
optimal_alpha <- function(power_function, costT1T2 = 1, prior_H1H0 = 1, error = "minimal") {
#Define the function to be minimized
f = function(x, power_function, costT1T2 = 1, prior_H1H0 = 1, error = "minimal") {
y <- 1 - eval(parse(text=paste(power_function)))
print(c(x, y, x+y)) #optional: print alpha, beta, and objective
if(error == "balance"){
max((costT1T2*x - prior_H1H0*y)/(prior_H1H0+1), (prior_H1H0*y - costT1T2*x)/(prior_H1H0+1))
} else if (error == "minimal"){
2*(costT1T2*x + prior_H1H0*y)/(prior_H1H0+1)
}
}
#Run optimize to find the minimum
res <- optimize(f,
c(0, 1),
tol = 0.00001,
power_function = power_function,
costT1T2 = costT1T2,
prior_H1H0 = prior_H1H0,
error = error)
if(error == "balance"){
beta <- res$minimum - res$objective
} else if (error == "minimal"){
beta <- res$objective - res$minimum
}
x <- res$minimum
#Store results
invisible(list(alpha = res$minimum,
beta = 1 - eval(parse(text=paste(power_function))),
tot = res$objective
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment