Skip to content

Instantly share code, notes, and snippets.

@Lakens
Last active January 9, 2017 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lakens/7ae8dc0929fd61cd98e6fe015ba58fa7 to your computer and use it in GitHub Desktop.
Save Lakens/7ae8dc0929fd61cd98e6fe015ba58fa7 to your computer and use it in GitHub Desktop.
likelihood of true positive and false positive as a function of power and alpha
#Create single plot for graph----
power <- seq(0.01,1,0.01)
alpha <- seq(0.01,1,0.01)
pH0 <- 0.5
tp<-((1-pH0)*power)
fp<-((pH0)*alpha)
likelihood_ratio<-outer(tp, fp, "/")
#Create color shading
nrz<-nrow (likelihood_ratio)
ncz<-ncol(likelihood_ratio)
jet.colors<-colorRampPalette(c("yellow","red"))
nbcol<-100
color<-jet.colors(nbcol)
zfacet<-likelihood_ratio[-1, -1]+likelihood_ratio[-1,-ncz]+likelihood_ratio[-nrz,-1]+likelihood_ratio[-nrz,-ncz]
facetcol<-cut(zfacet,nbcol)
#Save plot----
png(file = "LRplot.png", width = 4000, height = 4000, res = 500)
persp(x=power, y=alpha, z=likelihood_ratio, theta = 130, phi = 0, col = color[facetcol], ticktype = "detailed", lwd=0.2)
dev.off()
#Create 360 plots for animated .gif (to combine in gif application)----
library(stringr)
#loop through plots
for(i in 0:360){
currFile <- paste(getwd(), "/plot_", str_pad(i, 3, pad = "0"), ".png", sep = "")
png(file = currFile, width = 1200, height = 1000, res = 200)
x <- seq(0,1,0.01)
y <- x
power <- seq(0.01,1,0.01)
alpha <- seq(0.01,1,0.01)
pH0 <- 0.5
tp<-((1-pH0)*power)
fp<-((pH0)*alpha)
likelihood_ratio<-outer(tp, fp, "/")
persp(x=power, y=alpha, z=likelihood_ratio, theta = i, phi = 10, col = color[facetcol], ticktype = "detailed", lwd=0.2)
dev.off()
}
#likelihood ratio for some example, e.g., a range of power, alpha = 0.05
power<-seq(1, 0.1, -0.1)
alpha<-0.05
pH0 <- 0.5
tp<-((1-pH0)*power)
fp<-((pH0)*alpha)
tp/fp
#likelihood ratio for some example, e.g., a range of alpha levels, power = 0.8
power<-0.8
alpha<-seq(0.05, 0.5, 0.05)
pH0 <- 0.5
tp<-((1-pH0)*power)
fp<-((pH0)*alpha)
tp/fp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment