[ADMB Users] running admb from R using system()

Ben Bolker bolker at ufl.edu
Mon May 31 17:09:36 PDT 2010


  I don't know if it helps or not, but I'll put in this plug -- I'd be
happy for people using R to try out the R2admb package on R-forge and
let me know what can be improved ...


Mark Maunder wrote:
> Here is the whole code if you want the see the details
> 
> 
> 
> lwmreg.admb<-function(x,y,rtype="regression",ams=1000000,gbs=100000000,cbs=100000000,maxfn=500,thetaphz=1,thetainit=1,exedr)
> 
> 
> {
> 
> #Modified from glmm.admb orignially written by Hans Julius Skaug
> 
> #written by Mark maunder
> 
> #need to put executable file in the working directory
> 
> #This needs to be a directory with no spaces in the name
> 
> #for example c:\test\
> 
> #use this command to set working directory setwd("C:/test")
> 
> 
> 
> ams<-as.integer(ams)
> 
> gbs<-as.integer(gbs)
> 
> cbs<-as.integer(cbs)
> 
> nn <- nrow(x)
> 
> p <- ncol(x)
> 
> 
> 
> if (!(nn == length(y)))
> 
> stop("x and y not same length")
> 
> if (missing(exedr) )
> 
> stop("Need to give directory for execuitable")
> 
> if (!file.exists(paste(exedr,"lwmreg.exe",sep="")))
> 
> stop("Cant find execuitable")
> 
> 
> 
> cmdoptions = paste("-maxfn",maxfn,"-ams",ams,"-gbs",gbs,"-cbs",cbs)
> 
> file_name = "lwmreg"
> 
> 
> 
> if(p==1) phz<--1
> 
> else phz<-c(-1,rep(1,p-1))
> 
> 
> 
> if(rtype=="regression") rt<-1
> 
> else
> 
> {
> 
> if (rtype=="average") rt<-0
> 
> else stop("rtype= regression or average")
> 
> }
> 
> 
> 
> 
> 
> dat_list = list(rtype = rt, p = p,phz=phz, thetaphz=thetaphz, n = 
> nn,x = x, y = y)
> 
> pin_list = list(theta=thetainit,lnlambda=rep(0,p))
> 
> 
> 
> dat_write(file_name, dat_list)
> 
> pin_write(file_name, pin_list)
> 
> std_file = paste(file_name, ".std", sep = "")
> 
> file.remove(std_file)
> 
> if (.Platform$OS.type == "windows") {
> 
> shell(paste(exedr, file_name,
> 
> ".exe", " ", cmdoptions, sep = ""), invisible = TRUE)
> 
> }
> 
> else {
> 
> system(paste("cp ", exedr,
> 
> file_name, " .", sep = ""))
> 
> system(paste("./", file_name, " ", cmdoptions, sep = ""))
> 
> unlink(file_name)
> 
> }
> 
> if (!file.exists(std_file))
> 
> stop("The function maximizer failed")
> 
> 
> 
> out<-read.fit(file_name)
> 
> out<-c(out,reptoRlist(paste(file_name, ".rep",sep="")))
> 
> 
> 
> if (abs(out$maxgrad) >= 0.001)
> 
> warning("Proper convergence could not be reached gradiant >= 0.001")
> 
> return(out)
> 
> }
> 
> 
> 
> 
> 
> 
> 
> Mark Maunder
> 
> Head of the Stock Assessment Program
> 
> Inter-American  Tropical Tuna Commission 8604 La Jolla Shores Drive 
> La Jolla, CA, 92037-1508, USA
> 
> Tel: (858) 546-7027 Fax: (858) 546-7133 mmaunder at iattc.org
> <mailto:mmaunder at iattc.org> 
> http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mark+Maunder
> 
> 
> 
> 
> Visit the AD Model Builder project at http://admb-project.org/
> 
> 
> 
> See the following website for information on fisheries stock
> assessment
> 
> http://www.fisheriesstockassessment.com/
> 
> 
> 
> *From:* Nathan Taylor [mailto:n.taylor at fisheries.ubc.ca] *Sent:*
> Monday, May 31, 2010 10:00 AM *To:* Mark Maunder; Benedikt Gehr;
> users at admb-project.org *Subject:* RE: [ADMB Users] running admb from
> R using system()
> 
> 
> 
> 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).
> 
> setwd("dirname1") system("model
> -commandoptions",wait=F,show.output.on.console=F,invisible=F)
> 
> setwd("dirname2") system("model
> -commandoptions",wait=F,show.output.on.console=F,invisible=F)
> 
> 
> 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.
> 
> good luck.
> 
> NG
> 
> -----Original Message----- From: users-bounces at admb-project.org on
> behalf of Mark Maunder Sent: Mon 31/05/2010 9:08 AM To: Benedikt
> Gehr; users at admb-project.org Subject: Re: [ADMB Users] running admb
> from R using system()
> 
> Try this
> 
> if (.Platform$OS.type == "windows") { shell(paste(exedr, file_name, 
> ".exe", " ", cmdoptions, sep = ""), invisible = TRUE) } else { 
> system(paste("cp ", exedr, file_name, " .", sep = "")) 
> system(paste("./", file_name, " ", cmdoptions, sep = "")) 
> unlink(file_name) }
> 
> 
> 
> Mark Maunder Head of the Stock Assessment Program Inter-American
> Tropical Tuna Commission 8604 La Jolla Shores Drive La Jolla, CA,
> 92037-1508, USA
> 
> Tel: (858) 546-7027 Fax: (858) 546-7133 mmaunder at iattc.org 
> http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mar
>  k+Maunder
> 
> Visit the AD Model Builder project at http://admb-project.org/
> 
> See the following website for information on fisheries stock
> assessment http://www.fisheriesstockassessment.com/
> 
> -----Original Message----- From: users-bounces at admb-project.org 
> [mailto:users-bounces at admb-project.org] On Behalf Of Benedikt Gehr 
> Sent: Monday, May 31, 2010 1:44 AM To: users at admb-project.org 
> Subject: [ADMB Users] running admb from R using system()
> 
> Hi
> 
> I'm trying to run an admb model from R by using the system ()
> command. The admb model runs fine when running it from the admb
> command line or when using emacs. However when I try it with system()
> then R crashes every time. And I tried using the R command line and
> RGui and in both it crashes. I also tried it from different
> computers.
> 
> What I do is I first set the directory where I keep the admb template
>  file with the corresponding admb data file and then invoke the
> system command either step by step or directly. I can build the model
> but when executing the model.exe file R crashes.
> 
> ########################## #setting the working directory 
> setwd("Documents and Settings\\Beni User\\Desktop\\Transfer\\Model 
> Fournier\\admb models\\simulated data\\stochastic\\pooled age
> classes")
> 
> #building the model -> this works fine system('makeadm stocpool')
> 
> #running the model -> makes R crash system('stoc.exe') 
> ########################### #or directly like this: setwd("Documents
> and Settings\\Beni User\\Desktop\\Transfer\\Model Fournier\\admb
> models\\simulated data\\stochastic\\pooled age classes") 
> system("./stocpool") -> makes R crash right away
> 
> 
> Does anyone know why this happens and how I can make this work?
> 
> Thanks a lot for the help!!
> 
> cheers
> 
> Beni
> 
> 
> 
> 
> 
> _______________________________________________ Users mailing list 
> Users at admb-project.org 
> http://lists.admb-project.org/mailman/listinfo/users 
> _______________________________________________ Users mailing list 
> Users at admb-project.org 
> http://lists.admb-project.org/mailman/listinfo/users
> 


-- 
Ben Bolker
Associate professor, Biology Dep't, Univ. of Florida
*** NEW E-MAIL ADDRESSES:
***   bbolker at gmail.com , bolker at math.mcmaster.ca
bolker at ufl.edu / people.biology.ufl.edu/bolker
GPG key: people.biology.ufl.edu/bolker/benbolker-publickey.asc

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 261 bytes
Desc: OpenPGP digital signature
URL: <http://lists.admb-project.org/pipermail/users/attachments/20100531/78969e18/attachment.pgp>


More information about the Users mailing list