Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
} | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
|
||
|
||
|