This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.R
114 lines (97 loc) · 2.92 KB
/
globals.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
read.admb <-
function(ifile)
{
ret=read.fit(ifile)
fn=paste(ifile,'.rep', sep='')
A=read.rep(fn)
A$fit=ret
pfn=paste(ifile,'.psv',sep='')
if(file.exists(pfn))
A$post.samp=read.psv(pfn)
return(A)
}
read.fit <-
function(ifile)
{
# __Example:
# file <-("~/admb/simple")
# A <- reptoRlist(file)
# Note there is no extension on the file name.
## The following is a contribution from:
## Anders Nielsen that reads the par & cor files.
ret<-list()
parfile<-as.numeric(scan(paste(ifile,'.par', sep=''),
what='', n=16, quiet=TRUE)[c(6,11,16)])
ret$nopar<-as.integer(parfile[1])
ret$nlogl<-parfile[2]
ret$maxgrad<-parfile[3]
file<-paste(ifile,'.cor', sep='')
lin<-readLines(file)
ret$npar<-length(lin)-2
ret$logDetHess<-as.numeric(strsplit(lin[1], '=')[[1]][2])
sublin<-lapply(strsplit(lin[1:ret$npar+2], ' '),function(x)x[x!=''])
ret$names<-unlist(lapply(sublin,function(x)x[2]))
ret$est<-as.numeric(unlist(lapply(sublin,function(x)x[3])))
ret$std<-as.numeric(unlist(lapply(sublin,function(x)x[4])))
ret$cor<-matrix(NA, ret$npar, ret$npar)
corvec<-unlist(sapply(1:length(sublin), function(i)sublin[[i]][5:(4+i)]))
ret$cor[upper.tri(ret$cor, diag=TRUE)]<-as.numeric(corvec)
ret$cor[lower.tri(ret$cor)] <- t(ret$cor)[lower.tri(ret$cor)]
ret$cov<-ret$cor*(ret$std%o%ret$std)
return(ret)
}
read.rep <-
function(fn)
{
# The following reads a report file
# Then the 'A' object contains a list structure
# with all the elemements in the report file.
# In the REPORT_SECTION of the AMDB template use
# the following format to output objects:
# report<<"object \n"<<object<<endl;
#
# The part in quotations becomes the list name.
# Created By Steven Martell
options(warn=-1) #Suppress the NA message in the coercion to double
ifile=scan(fn,what="character",flush=TRUE,blank.lines.skip=FALSE,quiet=TRUE)
idx=sapply(as.double(ifile),is.na)
vnam=ifile[idx] #list names
nv=length(vnam) #number of objects
A=list()
ir=0
for(i in 1:nv)
{
ir=match(vnam[i],ifile)
if(i!=nv) irr=match(vnam[i+1],ifile) else irr=length(ifile)+1 #next row
dum=NA
if(irr-ir==2) dum=as.double(scan(fn,skip=ir,nlines=1,quiet=TRUE,what=""))
if(irr-ir>2) dum=as.matrix(read.table(fn,skip=ir,nrow=irr-ir-1,fill=TRUE))
if(is.numeric(dum))#Logical test to ensure dealing with numbers
{
A[[vnam[i]]]=dum
}
}
options(warn=0)
return(A)
}
read.psv <-
function(fn, nsamples=10000)
{
#This function reads the binary output from ADMB
#-mcsave command line option.
#fn = paste(ifile,'.psv',sep='')
filen <- file(fn, "rb")
nopar <- readBin(filen, what = integer(), n = 1)
mcmc <- readBin(filen, what = numeric(), n = nopar * nsamples)
mcmc <- matrix(mcmc, byrow = TRUE, ncol = nopar)
close(filen)
return(mcmc)
}
# A simple function for creating transparent colors
# Author: Nathan Stephens (hacks package)
colr <-
function(col.pal=1,a=1)
{
col.rgb<-col2rgb(col.pal)/255
rgb(t(col.rgb),alpha=a)
}