Last active
September 30, 2021 18:32
-
-
Save MJacobs1985/5cffb999a1182a57f94ec6e1f9640568 to your computer and use it in GitHub Desktop.
How many samples do you need
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### POST-HOC POWER ANALYSIS #### | |
## To obtain a power analysis, I need to look at the probability of obtaining a significant p-value (a<=0.05) | |
## What I could do is use bootstrapping to estimate the population mean and standard deviation (sd) | |
## Once I have the population mean and sd, I can then simulate 1000 trials, and look at the achieved 'power' | |
k=(length(unique(ARCdata$Tank))) | |
n=10000 | |
popmean<-matrix(data=NA, nrow=n, ncol=k) | |
popsd<-matrix(data=NA, nrow=n, ncol=k) | |
for (i in 1:n){ | |
ARCdatanew<-lapply(split(ARCdata, ARCdata$Tank), | |
function(subdf) | |
subdf[sample(1:nrow(subdf), 35, replace=TRUE),]) # with replacement to follow bootstrapping rules | |
new<-do.call(rbind, lapply(ARCdatanew, data.frame, stringsAsFactors=FALSE)) | |
popmean[i,]<-tapply(new$gain, new$Tank, mean, na.rm=TRUE) | |
popsd[i,]<-tapply(new$gain, new$Tank, sd, na.rm=TRUE) | |
} | |
# Obtain population mean and SD by tank | |
popmean<-as.data.frame(popmean) | |
popmean<-colMeans(popmean, na.rm=TRUE) | |
popsd<-as.data.frame(popsd) | |
popsd<-colMeans(popsd,na.rm=TRUE) | |
samplemean<-tapply(ARCdata$gain, ARCdata$Tank, mean, na.rm=TRUE) | |
samplesd<-tapply(ARCdata$gain, ARCdata$Tank, sd, na.rm=TRUE) | |
popmean<-as.array(popmean) | |
popsd<-as.array(popsd) | |
for (i in 1:12){ | |
rownames(popmean)[i]<-rownames(samplemean)[i] | |
rownames(popsd)[i]<-rownames(samplesd)[i] | |
} | |
# Compare population mean and sd to sample mean and sd | |
popmean-(tapply(ARCdata$gain, ARCdata$Tank, mean, na.rm=TRUE)) # this is the bias for the mean by looking at the sample we had | |
popsd-(tapply(ARCdata$gain, ARCdata$Tank, sd, na.rm=TRUE)) # this is the bias for the sd by looking at the sample we had |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment