Skip to content

Commit

Permalink
setting up full runs
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Jul 1, 2011
1 parent 67a70ea commit 6a14041
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 69 deletions.
9 changes: 8 additions & 1 deletion R/roc.R
@@ -1,12 +1,19 @@

reformat_tau_dists <- function(taus){
# rename tau_dists so that they follow the same naming convention as likelihood
# rename tau_dists so that they follow the same naming conv as likelihood
# this lets them work with the same commands as the montecarlotest output
# but does not break any functionality of functions using the old form.
lapply(taus,
function(pow){
pow$null_dist <- pow$null_tau_dist[1,] # just tau, not p-values
pow$test_dist <- pow$test_tau_dist[1,] # just tau, not p-values

# mc plot fns look for this, need to remove. meanwhile:
dummy <- list(loglik=0, k=0)
class(dummy) <- "gauss"
pow$test <- dummy
pow$null <- dummy

pow})
}

Expand Down
53 changes: 22 additions & 31 deletions demo/analysis.R
@@ -1,51 +1,42 @@
# analysis.R
# custom functions for calling all the analysis and plot functions on a dataset


analysis <- function(data, cpu, nboot, freq){
m <- fit_models(data, "LSN")
sampling <- sampling_freq(m$const, m$timedep, cpu=cpu, nboot=nboot,
sample_effort=freq, length.original=length(m$X))
taus <- bootstrap_tau(m$X, m$const, m$timedep, cpu=cpu, nboot=nboot)
mc <- montecarlotest(m$const, m$timedep, cpu=cpu, nboot=nboot)
list(m=m, taus=taus, mc=mc, sampling=sampling, data=data, freq=freq)
}

compare_roc_curves <- function(taus, mc, legend=TRUE, ...){
mc <- remove_unconverged(mc)
taus <- reformat_tau_dists(taus)
auc1 <- roc_curve(taus[[1]], ...)
auc2 <- roc_curve(taus[[2]], add=TRUE, col=2, ...)
auc3 <- roc_curve(taus[[3]], add=TRUE, col=3, ...)
auc4 <- roc_curve(mc, add=TRUE, col=4, ...)
if(legend)
legend("bottomright", c(paste("Var: ", round(auc1,3)),
paste("Acor:", round(auc2, 3)),
paste("Skew:", round(auc3,3)),
paste("Lik: ", round(auc4,3))),
col=c(1,2,3,4), lty=1, cex=1, lwd=2)
}



plot_roc_curves <- function(objects, legend=TRUE, ...){
plot_roc_curves <- function(objects, legend=TRUE, cex.legend=1, ...){
# objects a list in which entry contains a fit object with null and
# test dists for all the indicators.
stats <- which_statistic(objects)

auc <- numeric(length(objects)) # store area under curve
legend_txt <- character(length(objects)) # AUC will go in legend

auc[1] <- roc_curve(objects[[1]], lwd=2, col=1, ...)
legend_txt[1] <- paste("AUC =",round(auc[1],2))
legend_txt[1] <- paste(stats[1], ", ", round(auc[1],2), sep="")
for(i in 2:length(objects)){
auc[i] <- roc_curve(objects[[i]], lwd=2, col=i, add=TRUE, ...)
legend_txt[i] <- paste("AUC =",round(auc[i],2))
legend_txt[i] <- paste(stats[i], ", ", round(auc[i],2),sep="")
}
legend("bottomright",legend_txt, col=c(1:length(objects)), lty=1, lwd=3)
legend("bottomright",legend_txt, col=c(1:length(objects)),
lty=1, lwd=3, cex=cex.legend)
}


plot_dists <- function(objects, ...){
n <- length(objects)
stats <- which_statistic(objects)
par(mfrow=c(1,n), mar=c(6,6,4,2))
for(i in 1:n){
plot(objects[[i]], xlab=stats[i], ...)
}
}


which_statistic <- function(objects){
sapply(objects, function(x){
if(is(x, "pow"))
"log(Lik Ratio)"
else if(is(x, "tau_dist_montecarlo"))
gsub("^(.{7}).*", "\\1",x$signal) ## TRUNCATE name to 7 chars
})
}


62 changes: 40 additions & 22 deletions demo/deut_analysis.R
Expand Up @@ -9,42 +9,60 @@ gitaddr <- gitcommit(script)
tags="warningsignals, stochpop"
tweet_errors(script, tags=tags)
###############
source("analysis.R")

data(deuterium)
i <- 3 ## Which deut?
m <- fit_models(deuterium[[i]], "LSN")


cpu <- 16
nboot <- 500
freq <- c(.3, .5, .7, .9)
freq_indicator <- c(1, 2, 5, 10)
freq <- c(25, 50, 100, 200, 500)

source("analysis.R")
data(deuterium)

i <- 3
## Run the Analyses
sampling <-
sampling_freq(m$const, m$timedep, cpu=cpu,
nboot=nboot, sample_effort=freq)

m <- fit_models(deuterium[[i]], "LSN")
#sampling <- sampling_freq(m$const, m$timedep, cpu=cpu, nboot=nboot,
# sample_effort=freq, length.original=length(m$X))
#taus <- bootstrap_tau(m$X, m$const, m$timedep, cpu=cpu, nboot=nboot)
#mc <- montecarlotest(m$const, m$timedep, cpu=cpu, nboot=nboot)
indicator_sampling <- indicator_sampling_freq(m, cpu, nboot,
sample_effort=freq_indicator,
length.original=length(m$X))
save(list=ls(), file="deut_analysis.rda")
taus <-
reformat_tau_dists(
bootstrap_tau(m$X, m$const, m$timedep,
cpu=cpu, nboot=nboot))

mc <-
remove_unconverged(
montecarlotest(m$const, m$timedep,
cpu=cpu, nboot=nboot))

indicator_sampling <-
indicator_sampling_freq(m, cpu, nboot,
sample_effort=freq)



#png(paste("deut_", i, "_roc.png", sep="")); compare_roc_curves(taus, mc); dev.off()
#upload(paste("deut_", i, "_roc.png", sep=""), script=script, gitaddr=gitaddr, tags=tags)
### Plot methods
## Original plot
png("deut_roc.png"); plot_roc_curves(c(list(mc), taus)); dev.off()
upload("deut_roc.png", script=script, gitaddr=gitaddr, tags=tags)


#png(paste("deut_", i, "_sampling.png", sep="")); plot_sampling_freq(sampling, freq); dev.off()
#upload(paste("deut_", i, "_sampling.png", sep=""), script=script, gitaddr=gitaddr, tags=tags)
for(i in 1:length(freq)){
input <- c(sampling[i], indicator_sampling[[i]])
file <- paste("deut_", freq[i], ".png", sep="")
png(file);
plot_roc_curves(input, cex.axis=2, cex.lab=2);
dev.off()
upload(file, script=script, gitaddr=gitaddr, tags=tags)

file <- paste("dist_deut_", freq[i], ".png", sep="")
png(file, width=480*length(input))
plot_dists(input, cex.axis=3, cex.lab=3.5);
dev.off()
upload(file, script=script, gitaddr=gitaddr, tags=tags)
}

png("tau_sampling.png"); # variance (stat=1) at different sampling efforts
plot_tau_sampling_freq(indicator_sampling, freq, pts=100, stat=1);
dev.off()
upload("tau_sampling.png", script=script, gitaddr=gitaddr, tags=tags)



28 changes: 13 additions & 15 deletions demo/ibm_analysis.R
Expand Up @@ -9,9 +9,8 @@ tweet_errors(script, tags=tags)
on.exit(system("git push"))

cpu <- 16
nboot <- 32
#freq <- c(25, 50, 100, 200, 500)
freq <- c(50)
nboot <- 500
freq <- c(25, 50, 100, 200, 500)

source("analysis.R")

Expand All @@ -27,31 +26,30 @@ mc <- remove_unconverged(montecarlotest(m$const, m$timedep,
indicator_sampling <- indicator_sampling_freq(m, cpu, nboot,
sample_effort=freq)

save(list=ls(), file="ibm_analysis.Rdat")

### Plot methods

## Original plot
png("ibm_crit_roc.png"); plot_roc_curves(c(list(mc), taus)); dev.off()
upload("ibm_crit_roc.png", script=script, gitaddr=gitaddr, tags=tags)

## plots at increasing effort

for(i in 1:length(freq)){
file <- paste("ibm_crit_", freq[i], ".png")
input <- c(sampling[i], indicator_sampling[[i]])
file <- paste("ibm_crit_", freq[i], ".png", sep="")
png(file);
plot_roc_curves(c(sampling[i], indicator_sampling[[i]]));
plot_roc_curves(input, cex.axis=2, cex.lab=2);
dev.off()
upload(file, script=script, gitaddr=gitaddr, tags=tags)

file <- paste("dist_ibm_crit_", freq[i], ".png", sep="")
png(file, width=480*length(input))
plot_dists(input, cex.axis=3, cex.lab=3.5);
dev.off()
upload(file, script=script, gitaddr=gitaddr, tags=tags)
}



## IBM STABLE MODEL
#ibm_stab <- analysis(ibm_stable, cpu=cpu, nboot=nboot, freq=freq)
#png("ibm_stab_roc.png"); compare_roc_curves(ibm_stab$taus, ibm_stab$mc); dev.off()
#upload("ibm_stab_roc.png", script=script, gitaddr=gitaddr, tags=tags)
#png("ibm_stab_sampling.png"); plot_sampling_freq(ibm_stab$sampling, ibm_stab$freq); dev.off()
#upload("ibm_stab_sampling.png", script=script, gitaddr=gitaddr, tags=tags)

## IBM STABLE MODEL


52 changes: 52 additions & 0 deletions demo/ibm_stable.R
@@ -0,0 +1,52 @@
# ibm_stable_analysis.R
rm(list=ls())
require(warningsignals)
require(socialR)
script <- "ibm_analysis.R"
gitaddr <- gitcommit(script)
tags="warningsignals, stochpop"
tweet_errors(script, tags=tags)
on.exit(system("git push"))

cpu <- 4
nboot <- 500
freq <- c(25, 50, 100, 200, 500)

source("analysis.R")

## The analyses -- slow!
data(ibms)
m <- fit_models(ibm_stable, "LSN")
sampling <- sampling_freq(m$const, m$timedep, cpu=cpu, nboot=nboot,
sample_effort=freq)
taus <- reformat_tau_dists(bootstrap_tau(m$X, m$const, m$timedep,
cpu=cpu, nboot=nboot))
mc <- remove_unconverged(montecarlotest(m$const, m$timedep,
cpu=cpu, nboot=nboot))
indicator_sampling <- indicator_sampling_freq(m, cpu, nboot,
sample_effort=freq)

### Plot methods
## Original plot
png("ibm_stable_roc.png"); plot_roc_curves(c(list(mc), taus)); dev.off()
upload("ibm_stable_roc.png", script=script, gitaddr=gitaddr, tags=tags)


for(i in 1:length(freq)){
input <- c(sampling[i], indicator_sampling[[i]])
file <- paste("ibm_stable_", freq[i], ".png", sep="")
png(file);
plot_roc_curves(input, cex.axis=2, cex.lab=2);
dev.off()
upload(file, script=script, gitaddr=gitaddr, tags=tags)

file <- paste("dist_ibm_stable_", freq[i], ".png", sep="")
png(file, width=480*length(input))
plot_dists(input, cex.axis=3, cex.lab=3.5);
dev.off()
upload(file, script=script, gitaddr=gitaddr, tags=tags)
}




0 comments on commit 6a14041

Please sign in to comment.