Skip to content

Commit c9bac13

Browse files
committed
add gold_section example
1 parent ef35837 commit c9bac13

File tree

2 files changed

+176
-2
lines changed

2 files changed

+176
-2
lines changed

R/golden_section.R

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,90 @@
5454
#' res <- gold_section(f, interval = c(-10,20), integer=TRUE, y=y, x=x)
5555
#' res
5656
#'
57+
#'
58+
#' #############################################
59+
#' # ANOVA example with Cohen's f= .2, incorporating cost function using SimSolve()
60+
#' #
61+
#' # Goal: solve N (within) and N.groups (between) given the cost function
62+
#' # cost = 5 * N.g + 50 * N.groups
63+
#' #
64+
#' # Setup allows N to be solved with SimSolve() given N.groups and target,
65+
#' # golden_section() used to solve outer N.groups component given cost function
66+
#'
67+
#' # Design row conditions must have N and N.groups
68+
#' costfun <- function(condition)
69+
#' with(condition, 5 * N * N.groups + 50 * N.groups)
70+
#'
71+
#' Generate <- function(condition, fixed_objects = NULL){
72+
#' Attach(condition)
73+
#' group <- rep(letters[1:N.groups], each=N)
74+
#' means <- rnorm(N.groups, sd=.2)
75+
#' DV <- as.vector(sapply(1:N.groups, \(i) rnorm(N, mean=means[i])))
76+
#' dat <- data.frame(group, DV)
77+
#' dat
78+
#' }
79+
#'
80+
#' Analyse <- function(condition, dat, fixed_objects = NULL) {
81+
#' mod <- aov(DV ~ group, dat)
82+
#' p <- summary(mod)[[1]]$`Pr(>F)`[1L]
83+
#' p
84+
#' }
85+
#'
86+
#' Summarise <- function(condition, results, fixed_objects = NULL) {
87+
#' ret <- EDR(results)
88+
#' ret
89+
#' }
90+
#'
91+
#' \dontrun{
92+
#'
93+
#' # Solutions given fixed N.groups input
94+
#' Design <- createDesign(N = NA, N.groups=5:25)
95+
#'
96+
#' # test cost function
97+
#' condition <- Design[1,]
98+
#' condition$N <- 200
99+
#' costfun(condition)
100+
#'
101+
#' # solve for 1-beta = .95 power
102+
#' results <- SimSolve(design=Design, b=.95, interval=c(10, 500),
103+
#' generate=Generate, analyse=Analyse, summarise=Summarise)
104+
#' results
105+
#'
106+
#' # (optional) confirm results accuracy
107+
#' if(FALSE){
108+
#' cres <- results
109+
#' cres$N <- ceiling(cres$N)
110+
#' confirm <- runSimulation(cres, replications = 10000,
111+
#' generate=Generate, parallel=TRUE,
112+
#' analyse=Analyse, summarise=Summarise)
113+
#' confirm
114+
#' }
115+
#'
116+
#' costs <- numeric(nrow(results))
117+
#' for(i in 1:length(costs))
118+
#' costs[i] <- costfun(ceiling(results[i,]))
119+
#' results_costs <- cbind(results, costs)
120+
#' results_costs
121+
#'
122+
#' library(ggplot2)
123+
#' ggplot(results_costs, aes(N, costs, size = N.groups)) +
124+
#' geom_point()
125+
#'
126+
#'
127+
#' # solved via gold_section() with integer option for N.groups
128+
#' Design <- createDesign(N = NA, N.groups = NA)
129+
#'
130+
#' fn <- function(par, condition){
131+
#' condition$N.groups <- par
132+
#' res <- SimSolve(design=condition, b=.95, interval=c(10, 500),
133+
#' generate=Generate, analyse=Analyse, summarise=Summarise)
134+
#' costfun(res)
135+
#' }
136+
#'
137+
#' res <- gold_section(fn, interval=c(5,25), integer=TRUE, condition=Design)
138+
#' res
139+
#'
140+
#' }
57141
gold_section <- function(f, interval, integer = FALSE,
58142
tol = .001, maxiter = 100L, ...){
59143
if(integer) interval <- round(interval)
@@ -82,13 +166,19 @@ gold_section <- function(f, interval, integer = FALSE,
82166
xl <- x2; fl <- f2
83167
x2 <- x1; f2 <- f1
84168
x1 <- xl + d.c * (xu - xl)
85-
if(integer) x1 <- floor(x1)
169+
if(integer){
170+
x1 <- floor(x1)
171+
if(x1 == x2) break
172+
}
86173
f1 <- f(x1, ...)
87174
} else {
88175
xu <- x1; fu <- f1
89176
x1 <- x2; f1 <- f2
90177
x2 <- xu - d.c * (xu - xl)
91-
if(integer) x2 <- ceiling(x2)
178+
if(integer){
179+
x2 <- ceiling(x2)
180+
if(x1 == x2) break
181+
}
92182
f2 <- f(x2, ...)
93183
}
94184
if(integer) if(abs(xu - xl) <= 1L) break

man/gold_section.Rd

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)