# Testing sample sizes test.samp = function(maxN=10000,infn,outfn,test="exact",ssize=24,startsize=24) { # maxN: number of simulated experiments # infn: input data filename # outfn: output filename # test: "exact" for exact tests, anything else for mixed-effects logistic regression # ssize: size of actual experiment (number of speakers) # startsize: first size to test (subsequent sizes are smaller) # Load data claim = read.table(infn,T) attach(claim) # Prepare output file write(c("data:",infn),outfn,ncolumns=2,sep="\t") write(c("test:",test),outfn,ncolumns=2,append=T,sep="\t") write(c("maxN =",maxN),outfn,ncolumns=2,append=T,sep="\t") # Get factor name(s) cn = colnames(claim) Fact1 = NULL Fact2 = NULL Fact1 = get(cn[3]) if(length(cn) == 5) { Fact2 = get(cn[4]) # Second factor, if any } # Choose sample ans = matrix(nrow = ssize, ncol = 2) i = startsize keeptry = TRUE while (i > 0 & keeptry) { i = i - 1 ans[i,1] = i count.sig = 0 nsamp = choose(ssize,i) combos = t(combn((1:ssize),i)) if (nsamp >= maxN) { N = maxN } else { N = nsamp } for (j in 1:N) { if (N == maxN) { s = sample((1:ssize),i) } else { s = combos[j,] } samp = is.element(Speaker,s) Sp = Speaker[samp] J = Judgment[samp] F1 = Fact1[samp] if (!is.null(Fact2)) { F2 = Fact2[samp] claim.res = data.frame(Sp,J,F1,F2) } else { claim.res = data.frame(Sp,J,F1) } if (test == "exact") { if (!is.null(Fact2)) { you.p = small.exp(J,F1,F2,group=Sp)[3] } else { you.p = small.exp(J,F1,group=Sp) } } else { if (!is.null(Fact2)) { you.p = summary(lmer(J ~ F1*F2 + (1|Sp), family="binomial", data=claim.res))@coefs[4,4] } else { you.p = summary(lmer(J ~ F1 + (1|Sp), family="binomial", data=claim.res))@coefs[2,4] } } if (you.p < 0.05) { count.sig = count.sig + 1 } } samp.p = count.sig/N ans[i,2] = samp.p write(c(i,ans[i,2]),outfn,ncolumns=2,append=T,sep="\t") if (samp.p < 0.5) { keeptry = FALSE } } detach(claim) return(ans) }