<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<title>RE: [ADMB Users] running admb from R using system()</title>
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Here is the whole code if you want the see the details<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>lwmreg.admb<-function(x,y,rtype="regression",ams=1000000,gbs=100000000,cbs=100000000,maxfn=500,thetaphz=1,thetainit=1,exedr)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>{<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#Modified from glmm.admb orignially written by Hans Julius Skaug<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#written by Mark maunder<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#need to put executable file in the working directory<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#This needs to be a directory with no spaces in the name<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#for example c:\test\<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#use this command to set working directory
setwd("C:/test")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> ams<-as.integer(ams)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> gbs<-as.integer(gbs)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> cbs<-as.integer(cbs)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> nn <- nrow(x)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> p <- ncol(x)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (!(nn == length(y))) <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> stop("x and y not same length")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (missing(exedr) ) <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> stop("Need to give directory for execuitable")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (!file.exists(paste(exedr,"lwmreg.exe",sep="")))
<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> stop("Cant find execuitable")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> cmdoptions =
paste("-maxfn",maxfn,"-ams",ams,"-gbs",gbs,"-cbs",cbs)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> file_name = "lwmreg"<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if(p==1) phz<--1<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> else phz<-c(-1,rep(1,p-1))<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if(rtype=="regression") rt<-1<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> else<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> {<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (rtype=="average") rt<-0<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> else stop("rtype= regression or average") <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> }<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> dat_list = list(rtype = rt, p = p,phz=phz,
thetaphz=thetaphz, n = nn,x = x, y = y)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> pin_list = list(theta=thetainit,lnlambda=rep(0,p))<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> dat_write(file_name, dat_list)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> pin_write(file_name, pin_list)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> std_file = paste(file_name, ".std", sep =
"")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> file.remove(std_file)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (.Platform$OS.type == "windows") {<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> shell(paste(exedr, file_name, <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> ".exe", " ", cmdoptions, sep =
""), invisible = TRUE)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> }<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> else {<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> system(paste("cp ", exedr, <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> file_name, " .", sep = ""))<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> system(paste("./", file_name, " ",
cmdoptions, sep = ""))<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> unlink(file_name)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> }<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (!file.exists(std_file)) <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> stop("The function maximizer failed")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> out<-read.fit(file_name)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> out<-c(out,reptoRlist(paste(file_name,
".rep",sep="")))<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> if (abs(out$maxgrad) >= 0.001) <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> warning("Proper convergence could not be reached
gradiant >= 0.001")<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'> return(out)<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>}<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<div>
<p class=MsoNormal><span style='font-size:11.0pt;color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;color:#1F497D'>Mark Maunder <o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'>Head of the Stock Assessment
Program<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'>Inter-American Tropical
Tuna Commission<br>
8604 La Jolla Shores Drive<br>
La Jolla, CA, 92037-1508, USA<br>
<br>
Tel: (858) 546-7027<br>
Fax: (858) 546-7133<br>
<a href="mailto:mmaunder@iattc.org">mmaunder@iattc.org</a><br>
</span><span style='font-size:10.0pt;font-family:"Arial","sans-serif";
color:#1F497D'><a
href="http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mark+Maunder">http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mark+Maunder</a></span><span
style='font-size:11.0pt;color:#1F497D'><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;color:#1F497D'>Visit the AD
Model Builder project at<br>
<a href="http://admb-project.org/"><span style='color:#810081'>http://admb-project.org/</span></a></span><span
style='color:#1F497D'><o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> <o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;color:#1F497D'>See the
following website for information on fisheries stock assessment<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;color:#1F497D'><a
href="http://www.fisheriesstockassessment.com/">http://www.fisheriesstockassessment.com/</a></span><span
style='font-size:11.0pt;color:#1F497D'><o:p></o:p></span></p>
</div>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif";
color:#1F497D'> </span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p></o:p></span></p>
<div>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>
<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Nathan Taylor
[mailto:n.taylor@fisheries.ubc.ca] <br>
<b>Sent:</b> Monday, May 31, 2010 10:00 AM<br>
<b>To:</b> Mark Maunder; Benedikt Gehr; users@admb-project.org<br>
<b>Subject:</b> RE: [ADMB Users] running admb from R using system()<o:p></o:p></span></p>
</div>
</div>
<p class=MsoNormal><o:p> </o:p></p>
<p style='margin-bottom:12.0pt'><span style='font-size:10.0pt'>It's much less
elegant than Mark's code because directories and model names are hard wired but
for multiple instances such as multiple mcmc chains, i've been successful using
system() as shown below (64 bit windows, R version 2.11.0).<br>
<br>
setwd("dirname1")<br>
system("model
-commandoptions",wait=F,show.output.on.console=F,invisible=F)<br>
<br>
setwd("dirname2")<br>
system("model
-commandoptions",wait=F,show.output.on.console=F,invisible=F)<br>
<br>
<br>
It's been a while since i set this up but remember having some issues with the
show.output.on.console option using system(). The default for this option
is show.output.on.console = TRUE.<br>
<br>
good luck.<br>
<br>
NG<br>
<br>
-----Original Message-----<br>
From: users-bounces@admb-project.org on behalf of Mark Maunder<br>
Sent: Mon 31/05/2010 9:08 AM<br>
To: Benedikt Gehr; users@admb-project.org<br>
Subject: Re: [ADMB Users] running admb from R using system()<br>
<br>
Try this<br>
<br>
if (.Platform$OS.type == "windows") {<br>
shell(paste(exedr, file_name,<br>
".exe", " ", cmdoptions, sep = ""), invisible =
TRUE)<br>
}<br>
else {<br>
system(paste("cp ", exedr,<br>
file_name,
" .", sep = ""))<br>
system(paste("./",
file_name, " ", cmdoptions, sep = ""))<br>
unlink(file_name)<br>
}<br>
<br>
<br>
<br>
Mark Maunder<br>
Head of the Stock Assessment Program<br>
Inter-American Tropical Tuna Commission<br>
8604 La Jolla Shores Drive<br>
La Jolla, CA, 92037-1508, USA<br>
<br>
Tel: (858) 546-7027<br>
Fax: (858) 546-7133<br>
mmaunder@iattc.org<br>
<a
href="http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mar">http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mar</a><br>
k+Maunder<br>
<br>
Visit the AD Model Builder project at<br>
<a href="http://admb-project.org/">http://admb-project.org/</a><br>
<br>
See the following website for information on fisheries stock assessment<br>
<a href="http://www.fisheriesstockassessment.com/">http://www.fisheriesstockassessment.com/</a><br>
<br>
-----Original Message-----<br>
From: users-bounces@admb-project.org<br>
[<a href="mailto:users-bounces@admb-project.org">mailto:users-bounces@admb-project.org</a>]
On Behalf Of Benedikt Gehr<br>
Sent: Monday, May 31, 2010 1:44 AM<br>
To: users@admb-project.org<br>
Subject: [ADMB Users] running admb from R using system()<br>
<br>
Hi<br>
<br>
I'm trying to run an admb model from R by using the system () command.<br>
The admb model runs fine when running it from the admb command line or<br>
when using emacs. However when I try it with system() then R crashes<br>
every time.<br>
And I tried using the R command line and RGui and in both it crashes. I<br>
also tried it from different computers.<br>
<br>
What I do is I first set the directory where I keep the admb template<br>
file with the corresponding admb data file and then invoke the system<br>
command either step by step or directly. I can build the model but when<br>
executing the model.exe file R crashes.<br>
<br>
##########################<br>
#setting the working directory<br>
setwd("Documents and Settings\\Beni User\\Desktop\\Transfer\\Model<br>
Fournier\\admb models\\simulated data\\stochastic\\pooled age classes")<br>
<br>
#building the model -> this works fine<br>
system('makeadm stocpool')<br>
<br>
#running the model -> makes R crash<br>
system('stoc.exe')<br>
###########################<br>
#or directly like this:<br>
setwd("Documents and Settings\\Beni User\\Desktop\\Transfer\\Model<br>
Fournier\\admb models\\simulated data\\stochastic\\pooled age classes")<br>
system("./stocpool") -> makes R crash right away<br>
<br>
<br>
Does anyone know why this happens and how I can make this work?<br>
<br>
Thanks a lot for the help!!<br>
<br>
cheers<br>
<br>
Beni<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
Users mailing list<br>
Users@admb-project.org<br>
<a href="http://lists.admb-project.org/mailman/listinfo/users">http://lists.admb-project.org/mailman/listinfo/users</a><br>
_______________________________________________<br>
Users mailing list<br>
Users@admb-project.org<br>
<a href="http://lists.admb-project.org/mailman/listinfo/users">http://lists.admb-project.org/mailman/listinfo/users</a></span><o:p></o:p></p>
</div>
</body>
</html>