Add more R scripts

This commit is contained in:
mattbk
2015-08-16 14:58:49 -05:00
parent e69edb519a
commit 52198ac043
4 changed files with 120 additions and 0 deletions

31
confplotd.R Normal file
View File

@ -0,0 +1,31 @@
# Requires plotrix library and split.file() output files.
# See Burton-Kelly (2008) for complete documentation.
confplotd <- function(path, pattern, vardis="Disparity", smoo=c(1:20), reps=1000, ext="-confplotd.txt", probs=c(0.025,0.975))
{
files<-list.files(path=path,pattern=pattern,full.names=TRUE);
for(i in files)
#Loop through files and assign variable names
{
x <- read.table(i,header=TRUE);
assign(i, x);
}
for(j in 1:length(files))
{
SOV <- get(files[j]);
bootSOV <- numeric(reps); #creates place for bootstrap values
for (k in 1:reps) {bootSOV[k] <- sum(diag(var(sample(SOV,replace=TRUE))))};
conf<-quantile(bootSOV,probs=probs);
toplot<-paste(smoo[j],mean(bootSOV),conf[2],conf[1]);
write.table(toplot,file=paste(substring(files[1],1, nchar(files[1])-4), ext, sep=""),quote=FALSE,append=TRUE, row.names=FALSE, col.names=FALSE)
}
tableback<-read.table(file=paste(substring(files[1],1,nchar(files[1])-4), ext, sep=""));
x<-unlist(tableback[1]);
y<-unlist(tableback[2]);
cui<-unlist(tableback[3]);
cli<-unlist(tableback[4]);
plotCI(x,y,ui=cui,li=cli,xlab=c("Smoothing"),ylab=vardis, ylim=c(0,0.025));
pdf(paste(substring(files[1],1,nchar(files[1])-4), ext, c(".pdf"), sep=""));
plotCI(x,y,ui=cui,li=cli,xlab=c("Smoothing"),ylab=vardis, ylim=c(0,0.025));
dev.off();
}

31
confplotv.R Normal file
View File

@ -0,0 +1,31 @@
# Requires plotrix library and split.file() output files.
# See Burton-Kelly (2008) for complete documentation.
confplotv <- function(path, pattern, vardis="Variation", smoo=c(1:20), reps=1000, ext="-confplotv.txt", probs=c(0.025,0.975))
{
files<-list.files(path=path,pattern=pattern,full.names=TRUE);
for(i in files)
#Loop through files and assign variable names
{
x <- read.table(i,header=TRUE,row.names=1);
assign(i, x);
}
for(j in 1:length(files))
{
SOV <- get(files[j]);
bootSOV <- numeric(reps); #creates place for bootstrap values
for (k in 1:reps) {bootSOV[k] <- sum(diag(var(sample(SOV,replace=TRUE))))};
conf<-quantile(bootSOV,probs=probs);
toplot<-paste(smoo[j],mean(bootSOV),conf[2],conf[1]);
write.table(toplot,file=paste(substring(files[1],1, nchar(files[1])-4), ext, sep=""),quote=FALSE,append=TRUE, row.names=FALSE, col.names=FALSE)
}
tableback<-read.table(file=paste(substring(files[1],1,nchar(files[1])-4), ext, sep=""));
x<-unlist(tableback[1]);
y<-unlist(tableback[2]);
cui<-unlist(tableback[3]);
cli<-unlist(tableback[4]);
plotCI(x,y,ui=cui,li=cli,xlab=c("Smoothing"),ylab=vardis, ylim=c(0,0.025));
pdf(paste(substring(files[1],1,nchar(files[1])-4), ext, c(".pdf"), sep=""));
plotCI(x,y,ui=cui,li=cli,xlab=c("Smoothing"),ylab=vardis, ylim=c(0,0.025));
dev.off();
}

36
confplotvbar.R Normal file
View File

@ -0,0 +1,36 @@
# Requires gplots, gregmisc package. split.file() output files.
# See Burton-Kelly (2008) for complete documentation.
confplotvbar<-function(path, pattern, vardis="Variation", barnames=c(1:20),xlabel="Genus", reps=1000, ext="-confplotvbar.txt", probs=c(0.025,0.975))
{
require(gplots);
files<-list.files(path=path,pattern=pattern,full.names=TRUE);
for(i in files)
#Loop through files and assign variable names [This is not necessary]
{
x <- read.table(i,header=TRUE);
assign(i, x);
}
for(j in 1:length(files))
{
SOV <- get(files[j]);
bootSOV <- numeric(reps);
#creates place for bootstrap values
for (k in 1:reps) {bootSOV[k] <- sum(diag(var(sample(SOV, replace=TRUE))))};
conf<-quantile(bootSOV,probs=probs);
toplot<-paste(barnames[j],mean(bootSOV), conf[2],conf[1],files[j]);
write.table(toplot,file=paste(substring(files[1],1, nchar(files[1])-4), ext, sep=""),quote=FALSE,append=TRUE, row.names=FALSE,col.names=FALSE)
}
tableback<-read.table(file=paste(substring(files[1],1, nchar(files[1])-4), ext, sep=""));
x<-unlist(tableback[1]);
y<-unlist(tableback[2]);
cui<-unlist(tableback[3]);
cli<-unlist(tableback[4]);
barplot2(height=y,names.arg=x,xlab=xlabel, ylab=vardis,ci.l=cli,ci.u=cui,plot.ci=TRUE);
pdf(paste(substring(files[1],1,nchar(files[1])-4), ext, c(".pdf"), sep=""));
barplot2(height=y,names.arg=x,xlab=xlabel,ylab=vardis,ci.l=cli,ci.u=cui,plot.ci=TRUE);
dev.off();
file.rename(paste(substring(files[1],1,nchar(files[1])-4), ext, sep=""),paste(path,"/confplotvbar-",pattern,".txt",sep=""));
file.rename(paste(substring(files[1],1,nchar(files[1])-4), ext, c(".pdf"), sep=""),paste(path,"/confplotvbar-", pattern,".pdf",sep=""));
# If using OS X, can announce when done
# system("say All done!");
}

22
euc.group.R Normal file
View File

@ -0,0 +1,22 @@
# Requires function cbind.all(). Single-column input files without column or row names.
# See Burton-Kelly (2008) for complete documentation.
euc.group <- function(path,pattern,ext="-eucgroup.euc",len=11)
{
file.list <- list.files(path=path,pattern=pattern,full.names=TRUE);
#List files that match pattern.
for(i in file.list)
#Loop through files and assign variable names
{
x <- read.table(i);
assign(i, x);
}
output <- cbind.all(get(file.list[1])); #Initiate output
for (j in 2:length(file.list))
#Dump everything into that array
{
output <- cbind.all(output, get(file.list[j]));
}
write(t(as.matrix(output)),file=paste(substring(file.list[1],1, nchar(file.list[1])-4), ext, sep=""), ncolumns=dim(output)[2],sep="\t");
return(output);
}