Skip to content

Instantly share code, notes, and snippets.

#Now imagine we're looking at the effect of calorie restriction, and this does actually have a true effect
#Of 5kg weight loss
#And we've managed to get a bigger sample size so our power is better!
#First we need to calculate the post-treatment sd as this is an input in the power calculation
#The post-treatment variable has sd comprised of following variance sources: pretest variance and
#Random weight loss/gain during intervention (we'll assume this random noise again has SD = 3)
post_sd = sd(population_pre + rnorm(10000, 0, 3)) #SD of about 16.6
#Now calculate power
set.seed(123)
#Generate a population of pretest body weights for 10,000 men (1) and women (0), with some random noise
#Men, are, 13kg heavier than women on average in our population (I.e., gender is a relevant covariate)
gender = rbinom(10000, 1, 0.5)
population_pre = 65+13*gender + rnorm(10000, 0, 15)
#In each experiment, we will sample 30 people and randomly assign them to a paleo diet (1) or no treatment (0)
#Sadly, in our example, the paleo diet has zero effect on weight.
#But there is some random weight gain/loss during the intervention for each person
#Power calculator function
power_fun = function(effect_d = 0.5, rel = 0.8){
SEM = 1*sqrt(1-rel)
SEdiff = sqrt(2*SEM^2)
CR = 1.96*SEdiff
pnorm(q = CR, mean = effect_d, sd = SEdiff, lower.tail = FALSE)
}
#Plot power by true effect size and reliability
@mattnw
mattnw / Journal of Dental Research edited
Created January 23, 2012 02:32
Journal of Dental Research style, with corrections
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="sort-only" default-locale="en-US">
<info>
<title>Journal of Dental Research</title>
<id>http://www.zotero.org/styles/journal-of-dental-research</id>
<link href="http://www.zotero.org/styles/journal-of-dental-research" rel="self"/>
<link href="http://www.zotero.org/styles/harvard1" rel="template"/>
<author>
<name>Emmanuel Charpentier</name>
<email>emm.charpentier@free.fr</email>