Introduction

The proposed study adapts the methods used on Drosophila by Chattopadhyay (2012). Thus, we estimate the sample size required to answer similar questions using black soldier fly larvae at three larval instar. Because the study is primarily descriptive, we estimate the required sample size by constraining the margin of error, rather than the minimum effect size of interest. See Valliant et al. (2015) for explanations of the formulas used in the functions below.

### load packages and set directory
library(PracTools)
library(tidyverse)
library(pwr)

Proportion of Responders

Following Chattopadhyay (2012), we categorize individuals by the response latency of nocifensive behavior:

The goal is to see what the minimum sample size required is so that our estimates of each of these categories are not wider than a specified margin of error. We estimate the required sample sizes required for margins of error between .01 (e.g., a point estimate of .50 with a 95% confidence interval of [.49, .51]), and .20 (e.g., point estimate of .50 and 95% confidence interval of [.30,.70]).

Note that we are investigating the sample sizes required for each instar, as different individuals will represent each instar. Also, if different individuals are needed for each temperature tested, then the sample size requirements printed below must be multiplied by the number of temperatures tested. Similarly, if different individuals are needed for the local vs. global assays, then the results should be multiplied by two.

Also, the same results address the sample size requirements for answering, “If there is more than one nocifensive behavior found, what % of larvae respond using each type of behavior at each temperature?” and “What % of larvae exhibit each behavior in the progression?”

marginvary<-nPropMoe(moe.sw=1, e= seq(0.01,0.20,0.01), alpha=0.05, pU=0.50)

#moe.sw=1: setting desired margin of error to confidence interval half-width on the proportion
#e = desired margin of error. estimate for intervals of 0.01 between 0.01 and 0.20 
#alpha = 1 - (confidence level). We are computing 95% confidence intervals
# pU = population proprotion to be estimated. 0.50 is the noisiest proportion to estimate, so we use it to obtain the upper bound of the required sample size.

MOE <- seq(0.01,0.20,0.01)
required<-cbind(MOE,marginvary)
colnames(required) <- c("Margin of Error","N Per Instar")
required
##       Margin of Error N Per Instar
##  [1,]            0.01   9603.64705
##  [2,]            0.02   2400.91176
##  [3,]            0.03   1067.07189
##  [4,]            0.04    600.22794
##  [5,]            0.05    384.14588
##  [6,]            0.06    266.76797
##  [7,]            0.07    195.99280
##  [8,]            0.08    150.05699
##  [9,]            0.09    118.56354
## [10,]            0.10     96.03647
## [11,]            0.11     79.36898
## [12,]            0.12     66.69199
## [13,]            0.13     56.82631
## [14,]            0.14     48.99820
## [15,]            0.15     42.68288
## [16,]            0.16     37.51425
## [17,]            0.17     33.23061
## [18,]            0.18     29.64089
## [19,]            0.19     26.60290
## [20,]            0.20     24.00912
Proportion Difference Between Instars

What if we wanted to know whether the proportions differ by instar? By convention, by desire 80% power to detect a significant difference between groups using a two-sided alpha of .05. We vary the absolute difference between the proportions from a minimum of .05 (e.g., .10 vs .15) and a maximum of 1 (viz., 0 and 1).

p1 <- seq(0.05,1,0.05)
instarcomparison<-sapply(p1,pwr.2p.test,sig.level=0.05, power = .80, alternative="two.sided")
betweeninstar<-cbind(instarcomparison[1,], instarcomparison[2,])
colnames(betweeninstar)<-c("Proportion Difference", "N per instar")
betweeninstar
##       Proportion Difference N per instar
##  [1,] 0.05                  6279.088    
##  [2,] 0.1                   1569.772    
##  [3,] 0.15                  697.6765    
##  [4,] 0.2                   392.443     
##  [5,] 0.25                  251.1635    
##  [6,] 0.3                   174.4191    
##  [7,] 0.35                  128.1447    
##  [8,] 0.4                   98.11076    
##  [9,] 0.45                  77.51961    
## [10,] 0.5                   62.79088    
## [11,] 0.55                  51.89329    
## [12,] 0.6                   43.60478    
## [13,] 0.65                  37.15437    
## [14,] 0.7                   32.03617    
## [15,] 0.75                  27.90707    
## [16,] 0.8                   24.52769    
## [17,] 0.85                  21.72695    
## [18,] 0.9                   19.37988    
## [19,] 0.95                  17.3936     
## [20,] 1                     15.69774

Average Response Time

Next, we are interested in whether the average response time to exhibit nocifensive behavior. To estimate the minimal sample size required to bound the estimates with a margin of error, we need to make an assumption about the population variance of response time. Examining Figure 6 of Chattopadhyay (2012), we assume the error bars represent standard deviations, although the authors do not specify. Rolls and whips appear to have the largest standard deviations, but black soldier fly larvae do not roll, so we will make a guess about the standard deviations of the whips.

Note that the margin of error is now on the metric of seconds, not proportions. The same caveats from above about needing different individuals for different temperatures and global vs. local assays apply here too.

# Based on visual examination of the genotype with the greatest variability, we conservatively estimate that the error bars for whips are, at most, +/- 2 seconds. So the variance is 4 seconds.

#Note that if those error bars are actually standard errors, we would need to first convert the largest one to a standard deviation, then to a variance. The sample size the authors used per genotype was 30, and the formula would be:
#(standard error*sqrt(30))^2
#The implication would be is that a much larger sample is necessary.

marginvarysecond<-nContMoe(moe.sw=1, e= seq(0.5,5.0,0.5), alpha=0.05, S2= 4)

#moe.sw=1: setting desired margin of error to confidence interval half-width on the proportion
#e = desired margin of error. 
#alpha = 1 - (confidence level). We are computing 95% confidence intervals
# S2 = population variance

MOE <- seq(0.5,5.0,0.5)
requiredsecond<-cbind(MOE,marginvarysecond)
colnames(requiredsecond) <- c("Margin of Error","N Per Instar")
requiredsecond
##       Margin of Error N Per Instar
##  [1,]             0.5   61.4633411
##  [2,]             1.0   15.3658353
##  [3,]             1.5    6.8292601
##  [4,]             2.0    3.8414588
##  [5,]             2.5    2.4585336
##  [6,]             3.0    1.7073150
##  [7,]             3.5    1.2543539
##  [8,]             4.0    0.9603647
##  [9,]             4.5    0.7588067
## [10,]             5.0    0.6146334
Average Response Time Between Instars

What if we wanted to know whether the average response time differs by instar? By convention, we desire 80% power to detect a significant difference between groups as small as a given effect size using a two-sided alpha of .05. The effect size metric is cohen’s d, which is the difference between groups on the standard deviation metric. The result is the required sample size for each instar. The results indicate that the a “small” effect size (d <= .20) requires a prohibitively large sample size, but that detecting a “medium” effect size (d = .5) could be feasible. See this visual for interpreting cohen’s d.

dseq<- seq(.10, 1, .05)


instarmean <- function(cohend) {
  samplerequire <- pwr.t.test(d= cohend,power=0.80,sig.level=0.05,type="two.sample",alternative="two.sided")
  return(samplerequire)
}

ssrequire<-sapply(dseq,instarmean)
instarmeandiff<-cbind(ssrequire[2,], ssrequire[1,])
colnames(instarmeandiff)<-c("Cohen's D", "N per instar")
instarmeandiff
##       Cohen's D N per instar
##  [1,] 0.1       1570.733    
##  [2,] 0.15      698.6382    
##  [3,] 0.2       393.4057    
##  [4,] 0.25      252.1275    
##  [5,] 0.3       175.3847    
##  [6,] 0.35      129.1121    
##  [7,] 0.4       99.08032    
##  [8,] 0.45      78.49162    
##  [9,] 0.5       63.76561    
## [10,] 0.55      52.87103    
## [11,] 0.6       44.58577    
## [12,] 0.65      38.13894    
## [13,] 0.7       33.02457    
## [14,] 0.75      28.89959    
## [15,] 0.8       25.52458    
## [16,] 0.85      22.72847    
## [17,] 0.9       20.38631    
## [18,] 0.95      18.40518    
## [19,] 1         16.71472