Commits

jlaake  committed f9042df

Changes to fix problems identified by check; also fixes accumulation issue with js

  • Participants
  • Parent commits b08e1bb

Comments (0)

Files changed (25)

File marked/DESCRIPTION

         the idea is to create an open package where researchers can add 
         models into a common framework.
 Depends: R (>= 2.12.0), Matrix, optimx
+Suggests: RMark
 LazyLoad: yes
 License: GPL version 2 or later
 Maintainer: Jeff Laake <Jeff.Laake@noaa.gov>

File marked/R/cjs.R

    for(i in which(scale.p<.99 | scale.p>1.01)) p.dm[,i]=p.dm[,i]/scale.p[i]
 #  call optim to find mles with cjs.lnl which gives -2 * log-likelihood
    cat("\n Starting optimization for ",ncol(Phi.dm)+ncol(p.dm)," parameters\n")
-   func_eval<<-0
+   assign(".markedfunc_eval", 0, envir = .GlobalEnv)
    flush.console()
    if(is.null(initial))
 	  par=c(log(Phi/(1-Phi)),rep(0,ncol(Phi.dm)-1),log(p/(1-p)),rep(0,ncol(p.dm)-1))
    while (convergence!=0 & i <= refit)
    {
       mod=optimx(par,cjs.lnl,imat=imat,Phi.dm=Phi.dm,p.dm=p.dm,Phi.fixed=Phi.fixed,p.fixed=p.fixed,
-		   Phi.links=Phi.links,p.links=p.links,method=method,hessian=F,debug=debug,time.intervals=time.intervals,control=control,
+		   Phi.links=Phi.links,p.links=p.links,method=method,hessian=FALSE,debug=debug,time.intervals=time.intervals,control=control,
 		   itnmax=itnmax,...)
 	  par=mod$par
 	  convergence=mod$conv
    res=list(beta=cjs.beta,lnl=lnl,AIC=lnl+2*length(cjs.beta),convergence=mod$conv,count=mod$itns,reals=reals,mod=mod,scale=c(scale.phi,scale.p))
    if(hessian) 
    {
-	   func_eval<<-0
+	   assign(".markedfunc_eval", 0, envir = .GlobalEnv)
 	   cat("\n Computing hessian\n")
 	   res$hessian=hessian(cjs.lnl,cjs.beta,imat=imat,Phi.dm=Phi.dm,p.dm=p.dm,Phi.fixed=Phi.fixed,p.fixed=p.fixed,Phi.links=Phi.links,
 			   p.links=p.links,time.intervals=time.intervals,all=FALSE)
 }
 
 
-

File marked/R/cjs.lnl.r

 # Value: -2*log likelihood
 ##################################################################################
 {
-	func_eval<<-func_eval+1
+	f_eval=get(".markedfunc_eval",envir=.GlobalEnv)+1
+	assign(".markedfunc_eval", f_eval, envir = .GlobalEnv)
 	if(debug)cat("\npar = ",par)
 	nocc=imat$nocc
 	nphi=ncol(Phi.dm)
 	{
 		cat("\nlnl = ",lnl$lnl)
 	} else
-	if((func_eval-100*floor(func_eval/100))==0)
+	if((f_eval-100*floor(f_eval/100))==0)
 	{
-		string=paste(" Number of evaluations: ",func_eval," -lnl:",formatC(lnl$lnl,digits=10))
+		string=paste(" Number of evaluations: ",f_eval," -lnl:",formatC(lnl$lnl,digits=10))
 		cat(paste(paste(rep("\8",nchar(string)),collapse=""),string,sep=""))
 		flush.console()
 	}	

File marked/R/create.dm.R

    }
    if(upper<nrow(x))
 	   dm[(upper+1):nrow(x)]=as(model.matrix(formula,x[(upper+1):nrow(x),,drop=FALSE]),"sparseMatrix")    
-#   dm=as(model.matrix(formula,x),"sparseMatrix")
 #  Remove any unused columns; this is slower but uses less memory
    select=vector("logical",length=npar)
 	for (i in 1:npar)

File marked/R/create.dmdf.R

    if(is.null(fields))
       res=cbind(res,x$data[rep(1:nrow(x$data),each=ntimes),!names(x$data)%in%c("ch"),drop=FALSE])                             
    else
-      res=cbind(res,x$data[rep(1:nrow(x$data),each=ntimes),names(x$data)%in%fields,drop=FALSE])                                
+      res=cbind(res,x$data[rep(1:nrow(x$data),each=ntimes),names(x$data)%in%fields,drop=FALSE])  
+   if("group"%in%names(res))
+	   levels(res$group)=apply(x$group.covariates,1,paste,collapse="")
    if(!is.null(tcv))  
       res=cbind(res,tcv)
    return(res)
 }
-

File marked/R/crm.R

 "crm" <- function(data,ddl=NULL,begin.time=1,model="cjs",title="",model.parameters=list(),design.parameters=list(),initial=NULL,
- groups = NULL, time.intervals = NULL,debug=FALSE, method="nlminb", hessian=FALSE, accumulate=TRUE,right=TRUE,chunk_size=1e7, 
+ groups = NULL, time.intervals = NULL,debug=FALSE, method="nlminb", hessian=FALSE, accumulate=TRUE,chunk_size=1e7, 
  control=list(eval.max=5000),refit=1,itnmax=5000,scale=NULL,...)
 {
 # -----------------------------------------------------------------------------------------------------------------------
 {
 	cat("\n Creating design data. This can take awhile.\n")
 	flush.console()
-	ddl=make.design.data(data.proc,design.parameters,right=right)
+	ddl=make.design.data(data.proc,design.parameters)
 }
 #
 # Setup parameter list

File marked/R/js.lnl.r

 # Value: -2*log likelihood
 ###################################################################################
 {
+	get.pent=function(beta,dm,nocc)
+	{
+		pents=cbind(rep(1,nrow(dm)/(nocc-1)),exp(matrix(as.vector(dm%*%beta),ncol=nocc-1,nrow=nrow(dm)/(nocc-1),byrow=TRUE)))
+		pents=pents/apply(pents,1,sum)
+		return(pents)
+	}
+	get.p=function(beta,dm,nocc)
+	{
+		ps=cbind(rep(1,nrow(dm)/(nocc-1)),plogis(matrix(as.vector(dm%*%beta),ncol=nocc-1,nrow=nrow(dm)/(nocc-1),byrow=TRUE)))
+		return(ps)
+	}
+# compute Phi matrix from parameters (beta) and list of design matrices (dm)
+# created by function create.dm
+	get.Phi=function(beta,dm,nocc)
+	{
+		Phis=cbind(rep(1,nrow(dm)/(nocc-1)),plogis(matrix(as.vector(dm%*%beta),ncol=nocc-1,nrow=nrow(dm)/(nocc-1),byrow=TRUE)))
+		return(Phis)
+	}
+	
 if(debug)cat("\npar = ",par)
+f_eval=get(".markedfunc_eval",envir=.GlobalEnv)+1
+assign(".markedfunc_eval", f_eval, envir = .GlobalEnv)
 # initialize constants and parameter vectors
 nocc=imat$nocc
 nphi=ncol(Phi.dm)
            as.double(time.intervals),as.integer(nrow(imat$chmat)),           
            as.integer(ncol(imat$chmat)),as.integer(nrow(Phi.fixed)),
            as.integer(nrow(p.fixed[p.fixed[,2]!=1,,drop=FALSE])),lnl=double(1),
-           p0=double(nrow(imat$chmat)),PACKAGE="RMark")
+           p0=double(nrow(imat$chmat)),PACKAGE="marked")
 # next add on likelihood component for first capture
 pents=get.pent(beta.pent,pent.dm,nocc)
 pents.dummy=pents[imat$freq==0,]
 {
 	cat("\nlnl = ",lnl)
 } else
-if((func_eval-100*floor(func_eval/100))==0)
-{
-	string=paste(" Number of evaluations: ",func_eval," -lnl:",formatC(lnl,digits=10))
-	cat(paste(paste(rep("\8",nchar(string)),collapse=""),string,sep=""))
-	flush.console()
-}	
-
+    if((f_eval-100*floor(f_eval/100))==0)
+    {
+	    string=paste(" Number of evaluations: ",f_eval," -lnl:",formatC(lnl,digits=10))
+#		cat(paste(paste(rep("\8",nchar(string)),collapse=""),string,sep=""))
+		cat(paste("\n"),string,sep="")
+	    flush.console()
+    }	
 return(lnl)
 }
-get.pent=function(beta,dm,nocc)
-{
-  pents=cbind(rep(1,nrow(dm)/(nocc-1)),exp(matrix(as.vector(dm%*%beta),ncol=nocc-1,nrow=nrow(dm)/(nocc-1),byrow=TRUE)))
-  pents=pents/apply(pents,1,sum)
-  return(pents)
-}
-get.p=function(beta,dm,nocc)
-{
-  ps=cbind(rep(1,nrow(dm)/(nocc-1)),plogis(matrix(as.vector(dm%*%beta),ncol=nocc-1,nrow=nrow(dm)/(nocc-1),byrow=TRUE)))
-  return(ps)
-}
-# compute Phi matrix from parameters (beta) and list of design matrices (dm)
-# created by function create.dm
-get.Phi=function(beta,dm,nocc)
-{
-  Phis=cbind(rep(1,nrow(dm)/(nocc-1)),plogis(matrix(as.vector(dm%*%beta),ncol=nocc-1,nrow=nrow(dm)/(nocc-1),byrow=TRUE)))
-  return(Phis)
-}
-

File marked/R/js.r

 #    ddl             - list of dataframes for design data
 #    dml             - list of design matrices
 #    parameters      - list of parameter model specifications
-#    accummulate     - if TRUE will accummulate capture histories with common value
+#    accumulate     - if TRUE will accumulate capture histories with common value
 #                      and with a common design matrix and fixed values for Phi and p
 #    Phi             - initial value for intercept 
 #    p               - initial value for intercept
    if(is.null(pent.fixed))
       pent.fixed=matrix(c(-1,-1,0),nrow=1,ncol=3)  
    freq=x$freq
-#  get first and last vectors, loc and chmat
-   ch=x$ch
-   imat=process.ch(ch,freq,all=TRUE)
-   imat.save=NULL
-   Phi.dm.save=NULL
-   p.dm.save=NULL  
-   pent.dm.save=NULL  
-   N.dm.save=NULL  
-   Phi.fixed.save=NULL
-   p.fixed.save=NULL  
-   pent.fixed.save=NULL  
-#  if data are to be accummulated based on ch and design matrices do so here;
+#  if data are to be accumulated based on ch and design matrices do so here;
 #  otherwise simply set ch
-   if(accummulate)
+   if(accumulate)
    {
 	  cat("\n Accumulating capture frequencies based on design. This can take awhile.\n")
 	  flush.console()
-      imat.save=imat   
+#     Save dms, fixed values and imat before accumulating
       Phi.dm.save=Phi.dm
       p.dm.save=p.dm
       pent.dm.save=pent.dm
       N.dm.save=N.dm
+	  Phi.fixed.save=Phi.fixed
+	  p.fixed.save=p.fixed
+	  pent.fixed.save=pent.fixed
+#     Create chmat and ch
+	  ch=x$ch
+	  imat=process.ch(ch,freq,all=TRUE)
+	  imat.save=imat
       chmat=imat$chmat
       if(sum(imat$loc)>0)
       {
          chmat[cbind(indices,imat$last[indices])]=2
          ch=apply(chmat,1,paste,collapse="")
       }
-      else
-         ch=x$ch
-      Phifixmat=NULL
-      pfixmat=NULL
-      pentfixmat=NULL
-      Phi.fixed.save=Phi.fixed
-      p.fixed.save=p.fixed
-      if(Phi.fixed[1,1]>0)
+#      else
+#         ch=x$ch
+#     Create fixed parameter matrices
+	  if(Phi.fixed[1,1]>0)
       {
          Phifixmat=rep(NA,nrow(x)*(nocc-1))
          Phifixmat[(nocc-1)*(Phi.fixed[,1]-1)+Phi.fixed[,2]]=Phi.fixed[,3]      
       }
-      if(p.fixed[1,1]>0)
+      else
+		  Phifixmat=NULL
+	  if(p.fixed[1,1]>0)
       {         
          pfixmat=rep(NA,nrow(x)*nocc)
          pfixmat[nocc*(p.fixed[,1]-1)+p.fixed[,2]-1]=p.fixed[,3]      
-      }
+      }else
+		 pfixmat=NULL
       if(pent.fixed[1,1]>0)
       {         
          pentfixmat=rep(NA,nrow(x)*(nocc-1))
          pentfixmat[(nocc-1)*(pent.fixed[,1]-1)+pent.fixed[,2]-1]=pent.fixed[,3]      
-      }
-
-#      chdesign=cbind(rep(ch,each=nocc-1),rep(freq,each=nocc-1),Phi.dm,pent.dm,Phifixmat,pentfixmat)
-#      chdesign=sapply(split(chdesign,rep(1:nrow(x),each=nocc-1)),paste,collapse="")
-	  
+      }else
+		  pentfixmat=NULL
+#     Start accumulating 
+#     Construct common splits based on Phi and pent dms and fixed parameter values
 	  nrows=nrow(Phi.dm)
 	  pieces=floor(ncol(Phi.dm)*nrows/chunk_size)+1
 	  chdesign=NULL
 		  chdesign=c(chdesign,xdesign)
 		  prev_i=i+1
 	  }
-	 	  
-
-#	  chdesignp=cbind(rep(ch,each=nocc),rep(freq,each=nocc),p.dm,pfixmat)
-#      chdesignp=sapply(split(chdesignp,rep(1:nrow(x),each=nocc)),paste,collapse="")
-	  
+#     Now use p which has different parameter structure	 	  
       nrows=nrow(p.dm)
       pieces=floor(ncol(p.dm)*nrows/chunk_size)+1
       chdesignp=NULL
 	     chdesignp=c(chdesignp,xdesign)
 	     prev_i=i+1
       }
-
-	  chsplit=split(1:nrow(x),paste(chdesign,chdesignp,apply(as.matrix(N.dm),1,paste,collapse=""),sep=""))
-
+#     Split data based on paste of all dms, fixed values
+	  chsplit=split(1:nrow(x),paste(chdesign,chdesignp,as.numeric(freq==0),sep=""))
+#     Get new set of indices
 	  indices=as.vector(sapply(chsplit,min))
+#     Accumulate frequencies
 	  counts=as.vector(sapply(chsplit,function(x)sum(freq[x])))
       freq=counts[order(indices)]
-
+#     Get reduced set of dms and fixed parameter matrices
 	  dm.index=rep(1:nrow(x),each=nocc-1)%in%indices
       Phi.dm=Phi.dm[dm.index,,drop=FALSE]
       pent.dm=pent.dm[dm.index,,drop=FALSE]
         val=pfixmat[dm.index][!is.na(pfixmat)[dm.index]]
         p.fixed=cbind( match(chi,indices),occj,val)
        }  
-      dm.index=1:nrow(x)%in%indices
       ch=x$ch[sort(indices)]
-      imat=process.ch(ch,freq,all=TRUE) 
-	  if(sum(freq)!=nrow(x))stop(paste("Error in accumulation. Number of accumulated",sum(freq),"not equal to original number",nrow(x)))
+#     Compute new imat
+	  imat=process.ch(ch,freq,all=TRUE)
+	  nx=sum(x$freq)
+	  if(sum(freq)!=nx)
+		   stop(paste("Error in accumulation. Number of accumulated",sum(freq),"not equal to original number",nx))
 	  cat(" ",nrow(x)," capture histories collapsed into ",length(ch),"\n")
+	  
+   }else
+   {
+#  get first and last vectors, loc and chmat
+	   ch=x$ch
+	   imat=process.ch(ch,freq,all=TRUE)
+	   imat.save=NULL
+	   Phi.dm.save=NULL
+	   p.dm.save=NULL  
+	   pent.dm.save=NULL  
+	   N.dm.save=NULL  
+	   Phi.fixed.save=NULL
+	   p.fixed.save=NULL  
+	   pent.fixed.save=NULL  
    }
 #  if no initial values set, set some default ones
    if(is.null(initial))
       if(is.null(p))p=0.5   
    }           
 #  call optim to find mles with js.lnl which gives -2 * log-likelihood
+   assign(".markedfunc_eval", 0, envir = .GlobalEnv)
    cat("\n Starting optimization",ncol(Phi.dm)+ncol(p.dm)+ncol(pent.dm)+ncol(N.dm)," parameters\n")
    if(is.null(initial))
       par=c(log(Phi/(1-Phi)),rep(0,ncol(Phi.dm)-1),log(p/(1-p)),rep(0,ncol(p.dm)-1),
    {
 	   mod=optimx(par,js.lnl,imat=imat,Phi.dm=Phi.dm,p.dm=p.dm,pent.dm=pent.dm,N.dm=N.dm,Phi.fixed=Phi.fixed,
 			   p.fixed=p.fixed,pent.fixed=pent.fixed,method=method,hessian=hessian,
-			   debug=debug,time.intervals=time.intervals,control=control,itnmax=itnmax,...)
-	   par=mod$par
+			   debug=debug,time.intervals=time.intervals,control=control,itnmax=itnmax,nobstot=nobstot,...)
+	   par=mod$par$par
 	   convergence=mod$conv
 	   i=i+1
    }
-   js.beta=mod$par
+   js.beta=mod$par$par
    names(js.beta)=c(paste("Phi:",colnames(Phi.dm),sep="") ,paste("p:",colnames(p.dm),sep=""),paste("pent:",colnames(pent.dm),sep=""),paste("N:",colnames(N.dm),sep=""))
    if(!is.null(imat.save)) imat=imat.save
    if(!is.null(Phi.dm.save)) Phi.dm=Phi.dm.save
    beta.pent=js.beta[(nphi+np+1):(nphi+np+npent)]
    beta.N=js.beta[(nphi+np+npent+1):(nphi+np+npent+nN)]
 # create Phi and p beta matrices excluding first occasion on p
-   Phis=plogis(Phi.dm%*%beta.phi)
-   ps=plogis(p.dm%*%beta.p)
-   pents=cbind(rep(1,nrow(pent.dm)),exp(pent.dm%*%beta.pent))
+   Phis=plogis(as.vector(Phi.dm%*%beta.phi))
+   ps=plogis(as.vector(p.dm%*%beta.p))
+   pents=cbind(rep(1,nrow(pent.dm)),exp(as.vector(pent.dm%*%beta.pent)))
    pents=pents/apply(pents,1,sum)
-   Ns=exp(N.dm%*%beta.N)
+   Ns=exp(as.vector(N.dm%*%beta.N))
    reals=p.dmdf
    names(reals)[names(reals)=="time"]="p.time"
    names(reals)[names(reals)=="age"]="p.age"
    Phirecs=Phi.dmdf[,!colnames(Phi.dmdf)%in%colnames(reals),drop=FALSE]
    Phirecs[(rep(1:nrow(x),each=nocc)-1)*(nocc-1)+c(1:(nocc-1),nocc-1),]
    Phirecs[seq(nocc,nrow(x)*nocc,by=nocc),]=NA
-   split(Phi.dmdf[,!colnames(Phi.dmdf)%in%colnames(reals),drop=FALSE],rep(1:nrow(x),each=nocc-1))
-
+#   split(Phi.dmdf[,!colnames(Phi.dmdf)%in%colnames(reals),drop=FALSE],rep(1:nrow(x),each=nocc-1))
 #   reals=cbind(reals,
    names(reals)[names(reals)=="time"]="Phi.time"
    names(reals)[names(reals)=="age"]="Phi.age"
 #   reals$p=as.vector(t(matrix(allval[[3]],nrow=nrow(x),ncol=nocc-1)))
    ui=tapply(imat$freq,list(imat$first,x$group),sum)
 #   ui=tapply(imat$freq,factor(imat$first),sum)
-   lnl=mod$value+ 2*sum(lfactorial(ui))
-   res=list(beta=js.beta,lnl=lnl,AIC=lnl+2*length(mod$par),convergence=mod$conv,count=mod$itns,reals=reals)
+   lnl=mod$fvalues$fvalues+ 2*sum(lfactorial(ui))
+   res=list(beta=js.beta,lnl=lnl,AIC=lnl+2*length(js.beta),convergence=mod$conv,count=mod$itns,reals=reals)
    if(!is.null(mod$hessian)) 
    {
       res$vcv=solve(mod$hessian)
    return(res)
 }
 
+

File marked/R/make.design.data.R

 "make.design.data" <-                                                      
-function(data,parameters=list(),remove.unused=FALSE,right=TRUE,common.zero=FALSE)
+function(data,parameters=list())
 {
 #------------------------------------------------------------------------------------------------------
 # make.design.data -  creates a design dataframe that is used to construct the design matrix for mark
 #                          age.bins         - bins for grouping ages
 #                          time.bins        - bins for grouping times
 #                          cohort.bins      - bins for grouping cohorts
-#                          pim.type         - type of pim structure "all","time","constant"
-#                          subtract.stratum - for each stratum, the one to compute by subtraction (for Psi only)
 #                          time.varying     - vector of field names that are time varying for this parameter
 #                          fields           - vector of field names to be included in design data that are not time varying
-#    remove.unused    - if TRUE, unused design data are removed; for triangular
-#                       pims, unused design data are determined based on lack of
-#                       ch for a particular row (cohort) of a group;  for square
-#                       pims. if there is a cohort field in the design data, then
-#                       it excludes any design data in which cohort < time.
-#
-#    common.zero      - if TRUE, uses a common begin.time to set origin (0) for Time variable
-#                      defaults to FALSE for legacy reasons but should be set to TRUE
-#                      for models that share formula like p and c with the Time model
 #
 # Value:
 #    full.design.data - list of design data frames for each type of parameter in the model
 # 10 Jan 06 ; added pim.type argument in call to compute.design.data
 #
 full.design.data=vector("list",length=length(parameters))
-if(is.null(model.list$stype) | model.list$stype=="mark")
-{
-   pimtypes=vector("list",length=length(parameters))
-   anyTriang=FALSE
-   anySquare=FALSE
-   for(i in 1:length(parameters))
-   {
-#
-# For mixtures, multistrata and robust designs set up values for input to
-# compute.design.data
-#
-     if(data$mixtures==1)
-     {
-        parameters[[i]]$mix=FALSE
-        parameters[[i]]$rows=1
-     }
-     if(!is.null(parameters[[i]]$bystratum) && parameters[[i]]$bystratum)
-     {
-        strata.labels=data$strata.labels
-        nstrata=data$nstrata
-        if(!is.null(parameters[[i]]$tostrata) && parameters[[i]]$tostrata)
-        {
-           if(!is.null(parameters[[i]]$subtract.stratum))
-              subtract.stratum=parameters[[i]]$subtract.stratum
-           else
-              subtract.stratum=strata.labels
-           tostrata=TRUE
-        }
-        else
-        {
-           subtract.stratum=NULL
-           tostrata=FALSE
-        }
-     } 
-     else
-     {
-        subtract.stratum=NULL
-        strata.labels=NULL
-        nstrata=1
-        tostrata=FALSE
-     }
-     if(!model.list$robust) parameters[[i]]$secondary=FALSE
-#
-#    Compute design data for this parameter
-#
-     design.data=compute.design.data(data,parameters[[i]]$begin,parameters[[i]]$num,
-                   parameters[[i]]$type,parameters[[i]]$mix,parameters[[i]]$rows,
-                   parameters[[i]]$pim.type,parameters[[i]]$secondary, nstrata,
-                   tostrata,strata.labels,subtract.stratum,common.zero=common.zero)
-     if(!is.null(parameters[[i]]$mix) && parameters[[i]]$mix)design.data$mixture=as.factor(design.data$mixture)
-     if(parameters[[i]]$secondary)design.data$session=as.factor(design.data$session+data$begin.time-1)
-     design.data$group=as.factor(design.data$group)
-     if(!is.null(data$group.covariates))
-        levels(design.data$group)=apply(data$group.covariates,1,paste,collapse="")
-     if(!is.null(design.data$cohort))
-        if(is.null(parameters[[i]]$cohort.bins))
-           design.data$cohort=factor(design.data$cohort,levels=unique(levels(factor(design.data$cohort))))
-        else
-           design.data$cohort=cut(design.data$cohort,parameters[[i]]$cohort.bins,include.lowest=TRUE,right=right)
-     if(!is.null(design.data$age))
-        if(is.null(parameters[[i]]$age.bins))
-           design.data$age=factor(design.data$age,levels=unique(levels(factor(design.data$age))))
-        else
-           design.data$age=cut(design.data$age,parameters[[i]]$age.bins,include.lowest=TRUE,right=right)
-     if(!is.null(design.data$time))
-# mod 30 Sept 09 to remove unused time factor levels
-       if(is.null(parameters[[i]]$time.bins))
-          design.data$time=factor(design.data$time,levels=unique(levels(factor(design.data$time))))
-       else
-          design.data$time=cut(design.data$time,parameters[[i]]$time.bins,include.lowest=TRUE,right=right)
-     if(model.list$closed | model.list$robust )
-     {
-        if(names(parameters)[i]=="p")
-        {
-           design.data$c=0
-           design.data$age=NULL
-           design.data$Age=NULL
-        }
-        if(names(parameters)[i]=="c")
-        {
-           design.data$c=1
-           design.data$age=NULL
-           design.data$Age=NULL
-        }
-        if(names(parameters)[i]=="N" | names(parameters)[i]=="pi")
-        {
-           design.data$age=NULL
-           design.data$Age=NULL
-           design.data$time=NULL
-           design.data$Time=NULL
-        }
-     }
-     full.design.data[[i]]=design.data
-     pimtypes[[i]]=list(pim.type=parameters[[i]]$pim.type)
-	 if(!is.null(subtract.stratum))pimtypes[[i]]$subtract.stratum=subtract.stratum
-     if(parameters[[i]]$type =="Triang"&&parameters[[i]]$pim.type=="all")anyTriang=TRUE
-     if(parameters[[i]]$type =="Square")anySquare=TRUE
-   }
-   names(full.design.data)=names(parameters)
-#
-#  Remove unused design data
-#
-   if(remove.unused)
-   {
-      ch=data$data$ch
-      if(data$model=="Multistrata")
-        ch=gsub("[A-Z]","1",ch)
-      if(anyTriang)
-      {
-#
-#    Loop over groups
-#
-         number.of.groups=dim(data$group.covariates)[1]
-         if(is.null(number.of.groups))number.of.groups=1
-         for(j in 1:number.of.groups)
-         {
-           remove.cohort=NULL
-           for(k in 1:data$nocc)
-           {
-              if(k>1)
-                 first.0=paste(rep("0",k-1),collapse="")
-              else
-                 first.0=""
-              if(number.of.groups==1)
-              {
-                 if(!any(substr(ch,1,k)==paste(first.0,"1",sep="")))
-                    remove.cohort=c(remove.cohort,k)
-              }
-              else
-                 if(!any(substr(ch[data$data$group==j],1,k)==paste(first.0,"1",sep="")))
-                    remove.cohort=c(remove.cohort,k)
-           }
-           for(i in 1:length(parameters))
-           {
-              if(parameters[[i]]$type =="Triang"&&parameters[[i]]$pim.type=="all")
-              {
-                 if(number.of.groups==1)
-                    full.design.data[[i]]=full.design.data[[i]][!(as.numeric(full.design.data[[i]]$cohort)%in%remove.cohort),]
-                 else
-                 {
-#          modified 7 Apr 08 to handle different begin.times between groups
-                    full.design.data[[i]]=full.design.data[[i]][!(as.numeric(full.design.data[[i]]$group)==j &
-                             as.numeric(factor(full.design.data[[i]]$cohort,levels=unique(full.design.data[[i]]$cohort[as.numeric(full.design.data[[i]]$group)==j ])))%in%remove.cohort),]
-#          modified 10 Aug to remove unused levels created in removing cohorts                    
-                    full.design.data[[i]]$cohort=factor(full.design.data[[i]]$cohort)
-                    full.design.data[[i]]$age=factor(full.design.data[[i]]$age)
-                    full.design.data[[i]]$time=factor(full.design.data[[i]]$time)
-                 }
-              }
-           }                             
-        }
-     }
-     if(anySquare)
-     {
-        for(i in 1:length(parameters))
-        {
-           if(parameters[[i]]$type =="Square"&is.null(parameters[[i]]$leave.unused))
-           {
-              time=full.design.data[[i]]$time
-              cohort=full.design.data[[i]]$cohort
-              full.design.data[[i]]=full.design.data[[i]][as.numeric(levels(time)[as.numeric(time)])
-                      >= as.numeric(levels(cohort)[as.numeric(cohort)]),]
-           }
-        }
-     }
-     
-   }
-   names(pimtypes)=names(parameters)
-   full.design.data$pimtypes=pimtypes
-   return(full.design.data)
-} else
-{
 #
 # Create design data for crm models
 #
    names(full.design.data)=names(parameters)
    return(full.design.data)
 }
-}
 
         
          

File marked/R/merge.design.covariates.r

-"merge.design.covariates"<-function(ddl,df,bygroup=FALSE,bytime=TRUE)
+"merge_design.covariates"<-function(ddl,df,bygroup=FALSE,bytime=TRUE)
 #                   - enables design data fields to be added to the design.data(ddl)
 #                    that match time fields in design data (eg effort), by group (eg site covariates)
 #                    or by time and group - site and time dependent covariates.

File marked/R/print.crm.r

-print.crm=function(x)
+print.crm=function(x,...)
 {
    cat("\ncrm Model Summary\n")
    cat("\nNpar : ",length(x$beta))
    print(coef(x))
    return(NULL)
 }
-coef.crm=function(x)
+coef.crm=function(object,...)
 {
-   beta=data.frame(Estimate=x$beta)
-   if(!is.null(x$vcv))
+   beta=data.frame(Estimate=object$beta)
+   if(!is.null(object$vcv))
    {
-      beta$se=sqrt(diag(x$vcv))
+      beta$se=sqrt(diag(object$vcv))
       beta$lcl=beta$Estimate - 1.96*beta$se
       beta$ucl=beta$Estimate + 1.96*beta$se
    }

File marked/data/dipper.rda

Binary file added.

File marked/man/cjs.Rd

 }
 \usage{
 cjs(x, ddl, dml, parameters, accumulate=TRUE, Phi=NULL, p=NULL, initial = NULL, 
-        method = "BFGS", hessian = FALSE, debug = FALSE, chunk_size=1e7, ...)
+        method , hessian = FALSE, debug = FALSE, chunk_size=1e7,refit,
+        itnmax = NULL, control = NULL, scale, ...)
 }
 \arguments{
   \item{x}{processed dataframe created by process.data}
   \item{method}{method to use for optimization; see \code{optim}}
   \item{hessian}{if TRUE will compute and return the hessian}
   \item{debug}{if TRUE will print out information for each iteration}
-  \item{chunk_size}{specifies amount of memory to use in accumulating capture histories; amount used is 8*chunk_size/1e6 MB (default 80MB)}
+  \item{chunk_size}{specifies amount of memory to use in accumulating capture histories; amount used is 8*chunk_size/1e6 MB (default 80MB)} 
+  \item{refit}{non-zero entry to refit}
+  \item{itnmax}{maximum number of iterations}
+  \item{control}{control string for optimization functions}
+  \item{scale}{vector of scale values for parameters}  
   \item{...}{any remaining arguments are passed to additional parameters passed to \code{optim} or  \code{\link{cjs.lnl}}}
 }
 \details{
 \author{ Jeff Laake <jeff.laake@noaa.gov> }
 \examples{
 # fit 3 cjs models with crm
+data(dipper)
 dipper.proc=process.data(dipper,model="cjs",begin.time=1)
 dipper.ddl=make.design.data(dipper.proc)
 mod.Phit.pt=crm(dipper.proc,dipper.ddl,model.parameters=list(Phi=list(formula=~time),p=list(formula=~time)))
 mod.Phidot.pdot
 mod.Phisex.pdot=crm(dipper.proc,dipper.ddl,groups="sex",model.parameters=list(Phi=list(formula=~sex),p=list(formula=~1)))
 mod.Phisex.pdot
-# fit same 3 models with calls to mark
-mod0=mark(dipper,model.parameters=list(Phi=list(formula=~time),p=list(formula=~time)),output=FALSE)
-summary(mod0,brief=TRUE)
-mod1=mark(dipper,model.parameters=list(Phi=list(formula=~1),p=list(formula=~1)),output=FALSE)
-summary(mod1,brief=TRUE)
-mod2<-mark(dipper,groups="sex",model.parameters=list(Phi=list(formula=~sex),p=list(formula=~1)),output=FALSE)
-summary(mod2,brief=TRUE)
+# fit same 3 models with calls to mark; requires RMark
+# require(RMark)
+#mod0=mark(dipper,model.parameters=list(Phi=list(formula=~time),p=list(formula=~time)),output=FALSE)
+#summary(mod0,brief=TRUE)
+#mod1=mark(dipper,model.parameters=list(Phi=list(formula=~1),p=list(formula=~1)),output=FALSE)
+#summary(mod1,brief=TRUE)
+#mod2<-mark(dipper,groups="sex",model.parameters=list(Phi=list(formula=~sex),p=list(formula=~1)),output=FALSE)
+#summary(mod2,brief=TRUE)
 }
 

File marked/man/cjs.lnl.Rd

 For a given set of parameters and data, it computes -2*log Likelihood value.
 }
 \usage{
-cjs.lnl(par, imat, Phi.dm, p.dm, Phi.fixed=NULL, p.fixed=NULL, debug = FALSE, 
+cjs.lnl(par, imat, Phi.dm, p.dm, Phi.fixed=NULL, p.fixed=NULL,
+         Phi.links = NULL, p.links = NULL, debug = FALSE, 
                time.intervals = NULL, all=FALSE)
                
 }
                        to fix phi(i,j)=f  }
   \item{p.fixed}{matrix with 3 columns: ch number(i), occasion number(j), fixed value(f)
                        to fix p(i,j)=f  }
+  \item{Phi.links}{vector of links for each parameter}
+  \item{p.links}{vector of links for each parameter}                     
   \item{debug}{if TRUE will printout values of \code{par} and function value}
   \item{time.intervals}{intervals of time between occasions if not all 1}
   \item{all}{if TRUE, returns entire list rather than just lnl; can be used to extract reals}

File marked/man/create.dm.Rd

 Creates a design matrix using the design dataframe, a formula and any intervals
 defined for time, cohort and age. }
 \usage{
-create.dm(x, formula, time.bins = NULL, cohort.bins = NULL, age.bins=NULL, chunk_size=1e7)
+create.dm(x, formula, time.bins = NULL, cohort.bins = NULL, age.bins=NULL, 
+               chunk_size=1e7, remove.intercept=NULL)
 }
 \arguments{
   \item{x}{design dataframe created by \code{\link{create.dmdf}}}
   \item{cohort.bins}{any bins of cohort to collapse values}
   \item{age.bins}{any bins of cohort to collapse values}
   \item{chunk_size}{specifies amount of memory to use in creating design matrices; amount used is 8*chunk_size/1e6 MB (default 80MB)}
+  \item{remove.intercept}{if TRUE, forces removal of intercept in design matrix}
 }
 \value{
   A design matrix constructed with the design dataframe and the formula.  It 

File marked/man/create.dmdf.Rd

 creates a dataframe with all of the data needed to fit capture-recapture models (crm) which currently includes
 "cjs" (Cormack-Jolly-Seber) or "js" (POPAN formulation of the Jolly-Seber model).
 Before jumping into the details it is useful to have an understanding of the differences
-between MARK (via the \code{\link{mark}} function) and the package \code{mra} written by Trent McDonald and how they relate
+between MARK (via the \code{mark in RMark} function) and the package \code{mra} written by Trent McDonald and how they relate
 to the implementation in \code{\link{cjs}}.  With MARK, animals can be placed in groups
 and the parameters for the model specified via PIMs (parameter information matrices) link
 the parameters to the specific animals.  For example, if for a particular group the \code{Phi} PIM is
 specific.  Had the covariate been used for \code{p} then they would be named 
 \code{cov2,cov3,cov4} because the covariate is for those occasions for \code{p} whereas
 for \code{Phi} the covariate is labelled with the occasion that begins the interval.  
-Note that unlike usage with \code{\link{mark}} the covariate labelling does not change based on the value
+Note that unlike usage with \code{mark in RMark} the covariate labelling does not change based on the value
 of \code{time} which changes with the value of the argument \code{begin.time}.
 Any number of fields can be specified in \code{fields} and \code{time.varying}
 that are specified in \code{x}. 

File marked/man/create.links.Rd

+\name{create.links}
+\alias{create.links}
+\title{Creates a 0/1 vector for real parameters with sin link}
+\description{
+For each row in a given design matrix it assigns a value 1, if the columns used in the design matrix
+are only used as an identity matrix (only one 1 and remaining columns all 0. }
+\usage{
+create.links(dm)
+}
+\arguments{
+  \item{dm}{design matrix}
+}
+\value{
+  A vector of length=nrow(dm) with value of 0 except for rows that can accommodate a sin
+  link (identity design matrix).  This function is not currently used because it has not 
+  been thoroughly tested.
+}
+\author{Jeff Laake}

File marked/man/crm.Rd

   A single function that processes data, creates the design data, makes the 
   crm model and runs it}
 \usage{
-crm(data,ddl=NULL,begin.time=1,model="cjs",title="",model.parameters=list(),design.parameters=list(),initial=NULL,
- groups = NULL, time.intervals = NULL,debug=FALSE, method="BFGS", hessian=FALSE, accumulate=TRUE, right=TRUE, chunk_size=1e7,...) 
+crm(data,ddl=NULL,begin.time=1,model="cjs",title="",model.parameters=list(),
+    design.parameters=list(),initial=NULL,groups = NULL, time.intervals = NULL,
+    debug=FALSE, method, hessian=FALSE, accumulate=TRUE,  
+    chunk_size=1e7, control = list(eval.max=5000), refit=1,itnmax =5000,scale=NULL, ...) 
 }
 \arguments{
   \item{data}{ Either the raw data which is a dataframe with at least one column named ch (a character field containing the capture history) or a processed dataframe }
   \item{debug}{if TRUE, shows optimization output}
   \item{hessian}{if TRUE, computes v-c matrix using hessian}
   \item{accumulate}{if TRUE, like capture-histories are accumulated to reduce computation}
-  \item{right}{If TRUE, bin intervals are closed on the right}
   \item{chunk_size}{specifies amount of memory to use in accumulating capture histories and design matrices; amount used is 8*chunk_size/1e6 MB (default 80MB)}
-  \item{...}{optional arguments passed to js or cjs and optim}
+  \item{control}{control string for optimization functions}
+  \item{refit}{non-zero entry to refit}
+  \item{itnmax}{maximum number of iterations}
+  \item{scale}{vector of scale values for parameters}  
+  \item{...}{optional arguments passed to js or cjs and optimx}
 }
 \details{
-This function is operationally similar to the function \code{\link{mark}} in that is is a shell that calls several
+This function is operationally similar to the function \code{mark in RMark} in that is is a shell that calls several
 other functions to perform the following steps: 1) \code{\link{process.data}} to setup data and parameters and package 
 them into a list (processed data),2) \code{\link{make.design.data}} to create the design data for each parameter in the specified model, 
 3) \code{\link{create.dm}} to create the design matrices for each parameter based on the formula provided for each parameter, 
 run for accumulation.
 
 Most of the arguments controlling the fitted model are contained in lists in the arguments \code{model.parameters}
-and \code{design.parameters} which are similar to their counterparts in \code{\link{mark}}.
+and \code{design.parameters} which are similar to their counterparts in \code{mark inb RMark}.
 Each is a named list with the names being the parameters in the model (e.g., Phi and p in "cjs"
 and "Phi","p","pent","N" in "js"). Each named element is also a list containing 
 various values defining the design data and model for the parameter. The elements
 \examples{
 # cormack-jolly-seber model
 # fit 3 cjs models with crm
+data(dipper)
 dipper.proc=process.data(dipper,model="cjs",begin.time=1)
 dipper.ddl=make.design.data(dipper.proc)
 mod.Phit.pt=crm(dipper.proc,dipper.ddl,model.parameters=list(Phi=list(formula=~time),p=list(formula=~time)))
 mod.Phisex.pdot=crm(dipper.proc,dipper.ddl,groups="sex",model.parameters=list(Phi=list(formula=~sex),p=list(formula=~1)))
 mod.Phisex.pdot
 # fit same 3 models with calls to mark
-mod0=mark(dipper,model.parameters=list(Phi=list(formula=~time),p=list(formula=~time)),output=FALSE)
-summary(mod0,brief=TRUE)
-mod1=mark(dipper,model.parameters=list(Phi=list(formula=~1),p=list(formula=~1)),output=FALSE)
-summary(mod1,brief=TRUE)
-mod2<-mark(dipper,groups="sex",model.parameters=list(Phi=list(formula=~sex),p=list(formula=~1)),output=FALSE)
-summary(mod2,brief=TRUE)
+#require(RMark)
+#mod0=mark(dipper,model.parameters=list(Phi=list(formula=~time),p=list(formula=~time)),output=FALSE)
+#summary(mod0,brief=TRUE)
+#mod1=mark(dipper,model.parameters=list(Phi=list(formula=~1),p=list(formula=~1)),output=FALSE)
+#summary(mod1,brief=TRUE)
+#mod2<-mark(dipper,groups="sex",model.parameters=list(Phi=list(formula=~sex),p=list(formula=~1)),output=FALSE)
+#summary(mod2,brief=TRUE)
 # jolly seber model
-crm(dipper,model="js",groups="sex")
-mark(dipper,model="POPAN",groups="sex")
+crm(dipper,model="js",groups="sex",accumulate=FALSE)
+#require(RMark)
+#mark(dipper,model="POPAN",groups="sex")
 }
 
 \keyword{ models}

File marked/man/dipper.Rd

+\name{dipper}
+\alias{dipper}
+\docType{data}
+\title{ Dipper capture-recapture data}
+\description{
+A capture-recapture data set on European dippers from France that accompanies MARK as an example analysis using the CJS and POPAN 
+models.  The dipper data set was orginally described as an example by Lebreton et al (1992).
+}
+\usage{data(dipper)}
+\format{
+  A data frame with 294 observations on the following 2 variables.
+  \describe{
+    \item{ch}{a character vector containing the encounter history of each bird}
+    \item{sex}{the sex of the bird: a factor with levels \code{Female} \code{Male}}
+  }
+}
+\details{
+This is a data set that accompanies program MARK as an example for CJS and POPAN analyses.  The data can be 
+stratified using sex as a grouping variable.  The functions \code{run.dipper}, \code{run.dipper.alternate},
+\code{run.dipper.popan} defined below in the examples mimic the models used in the dbf file that accompanies MARK.
+Note that the models used in the MARK example use PIM coding with the sin link function which is often better at
+identifying the number of estimable parameters.  The approach used in the R code uses design matrices and cannot 
+use the  sin link and is less capable at counting parameters.  These differences are illustrated by comparing the
+results of \code{run.dipper} and \code{run.dipper.alternate} which fit the same set of "CJS" models.  The latter
+fits the models with constraints on some parameters to achieve identifiability and the former does not. Although
+it does not influence the selection of the best model it does infleunce parameter counts and AIC ordering of some
+of the less competitive models. In using design matrices it is best to constrain parameters that are confounded 
+(e.g., last occasion parameters in Phi(t)p(t) CJS model) when possible to achieve more reliable counts of the
+number of estimable parameters.  
+
+Note that the covariate "sex" defined in dipper has values "Male" and "Female".  It cannot be used directly in 
+a formula for MARK without using it do define groups because MARK.EXE will be unable to read in a covariate with
+non-numeric values.  By using \code{groups="sex"} in the call the \code{\link{process.data}} a factor "sex" field is
+created that can be used in the formula.  Alternatively, a new covariate could be defined in the data with say
+values 0 for Female and 1 for Male and this could be used without defining groups because it is numeric.  This
+can be done easily by translating the values of the coded variables to a numeric variable.  Factor variables are
+numbered 1..k for k levels in alphabetic order.  Since Female < Male in alphabetic order then it is level 1 and
+Male is level 2.  So the following will create a numeric sex covariate.
+
+\preformatted{
+dipper$numeric.sex=as.numeric(dipper$sex)-1
+}
+
+
+}
+\source{
+Lebreton, J.-D., K. P. Burnham, J. Clobert, and D. R. Anderson. 1992. Modeling survival and testing biological 
+hypotheses using marked animals: case studies and recent advances. Ecol. Monogr. 62:67-118.
+}
+\keyword{datasets}

File marked/man/js.Rd

 }
 \usage{
 js(x, ddl, dml, parameters, accumulate=TRUE, Phi=NULL, p=NULL, initial = NULL, 
-        method = "BFGS", hessian = FALSE, debug = FALSE, chunk_size=1e7, ...)
+        method, hessian = FALSE, debug = FALSE, chunk_size=1e7,refit,
+        itnmax = NULL, control = NULL, scale, ...)
 }
 \arguments{
   \item{x}{processed dataframe created by process.data}
   \item{ddl}{list of dataframes for design data; created by call to \code{\link{make.design.data}} }
   \item{dml}{list of design matrices created by \code{\link{create.dm}} from formula and design data}
   \item{parameters}{equivalent to \code{model.parameters} in \code{\link{crm}} }
-  \item{accummulate}{if TRUE will accummulate capture histories with common value
+  \item{accumulate}{if TRUE will accumulate capture histories with common value
                      and with a common design matrix for Phi and p to speed up execution}
   \item{Phi}{initial value of Phi; used to set intercept parameter}
   \item{p}{initial value of p; used to set intercept parameter}
   \item{hessian}{if TRUE will compute and return the hessian}
   \item{debug}{if TRUE will print out information for each iteration}
   \item{chunk_size}{specifies amount of memory to use in accumulating capture histories; amount used is 8*chunk_size/1e6 MB (default 80MB)}
+  \item{refit}{non-zero entry to refit}
+  \item{itnmax}{maximum number of iterations}
+  \item{control}{control string for optimization functions}
+  \item{scale}{vector of scale values for parameters}  
   \item{...}{any remaining arguments are passed to additional parameters passed to \code{optim} or  \code{\link{js.lnl}}}
 }
 
 }
 \author{ Jeff Laake <jeff.laake@noaa.gov> }
 \examples{
-crm(dipper,model="js",groups="sex")
-mark(dipper,model="POPAN",groups="sex")
+data(dipper)
+crm(dipper,model="js",groups="sex",accumulate=FALSE)
+#mark(dipper,model="POPAN",groups="sex")
 }
 

File marked/man/make.design.data.Rd

 \alias{make.design.data}
 \title{Create design dataframes for MARK model specification}
 \description{
-For each type of parameter in the analysis model (e.g, p, Phi, r), this function creates design data based on
+For each type of parameter in the analysis model (e.g, p, Phi), this function creates design data based on
 the parameter index matrix (PIM) in MARK terminology. Design data relate parameters to the sampling
 and data structures; whereas \code{data} relate to the object(animal) being sampled. Design data relate parameters
 to time, age, cohort and group structure.  Any variables in the design data can be used in formulas to specify
-the model in \code{\link{make.mark.model}}. 
+the model. 
 }
 \usage{
-ddl=make.design.data(data, parameters = list(),remove.unused=FALSE, right=TRUE, common.zero=FALSE)
+make.design.data(data, parameters = list())
 }
 \arguments{
   \item{data}{Processed data list; resulting value from process.data}
   \item{parameters}{Optional list containing a list for each type of parameter (list of lists); 
                     each parameter list is named with the parameter name (eg Phi); each parameter list 
-                    can contain vectors named age.bins,time.bins and cohort.bins
-\tabular{ll}{
-\code{subtract.stratum} \tab a vector of strata letters (one for each strata) that specifies the tostratum that is computed by subtraction for mlogit parameters like Psi\cr
-\code{age.bins} \tab bins for binning ages\cr
-\code{time.bins} \tab bins for binning times\cr
-\code{cohort.bins} \tab bins for binning cohorts\cr
-\code{pim.type} \tab either "all" for all different, "time" for column time structure, or "constant" for all values the same within the PIM\cr}
-}
-  \item{remove.unused}{If TRUE, unused design data are deleted; see details below}
-  \item{right}{If TRUE, bin intervals are closed on the right}
-  \item{common.zero}{if TRUE, uses a common begin.time to set origin (0) for Time variable
-                      defaults to FALSE for legacy reasons but should be set to TRUE
-                      for models that share formula like p and c with the Time model}
+                    can contain vectors named age.bins,time.bins and cohort.bins, time.varying, fields
+          \tabular{ll}{
+              \code{age.bins} \tab bins for binning ages\cr
+              \code{time.bins} \tab bins for binning times\cr
+              \code{cohort.bins} \tab bins for binning cohorts\cr
+              \code{time.varying} \tab vector of field names that are time varying for this parameter\cr
+              \code{fields} \tab vector of field names that are to be included that are not time varying for this parameter\cr
+           }}
 }
 \details{
 After processing the data, the next step is to create the design data for building the models which is done with this
 function.  The design data are different than the capture
 history data that relates to animals.  The types of parameters and design data are specific to the type of 
-analysis.  For example, consider a CJS analysis that has parameters Phi and p.  If there are 4 occasions, 
+analysis.  For example, consider a CJS analysis that has parameters Phi and p.  If there are 4 occasions, 
 there are 3 cohorts and potentially 6 different Phi and 6 different p parameters for a single group.  The format
 for each parameter information matrix (PIM) in MARK is triangular.  RMark uses the all-different formulation for PIMS by default,
 so the PIMs would be
 
 the function creates default design data for each type of parameter in the model as defined by \code{proc.example.data$model}.  
 If \code{proc.example.data} was created with the call in the first example of \code{\link{process.data}}, the model is "CJS" 
-(the default model) so the return value is a list with 2 data frames one for Phi and one for p. They can be accessed as 
+(the default model) so the return value is a list with 2 data frames: one for Phi and one for p. They can be accessed as 
 \code{ddl$Phi} (or \code{ddl[["Phi"]]}) and \code{ddl$p} (or \code{ddl[["p"]]}) or as \code{ddl[[1]]} and \code{ddl[[2]]} respectively.  
 Using the former notation is easier and safer because it is independent of the ordering of the parameters in the list.
 For this example, there are 16 groups and each group has 21 Phi parameters and 21 p parameters. Thus, there are 
 
 In the above example, \code{age} is binned for Phi but not for p; likewise \code{time} is binned for p but not for Phi.
 The bins for age were defined as 0,0.5,1,2,8 because the intervals are closed ("]" - inclusive) on the right by default and
-open (�(�non-inclusive) on the left, except for the first interval which is closed on the left.  Had we used 0,1,2,8, 
+open (non-inclusive) on the left, except for the first interval which is closed on the left.  Had we used 0,1,2,8, 
 0 and 1 would have been in a single interval.  Any value less than 1 and greater than 0 could be used in place of 0.5.
 Alternatively, the same bins could be specified as:
 
 \code{ddl=make.design.data(proc.example.data,
       parameters=list(Phi=list(age.bins=c(0,1,2,3,8)),p=list(time.bins=c(1980,1984,1986))),right=FALSE)}
 
-To create the design data and maintain flexibility, I recommend creating the default design data and then adding
-other binned variables with the function \code{\link{add.design.data}}.  The 2 binned variables defined 
-above can be added to the default design data as follows:
-
-\preformatted{
-ddl=make.design.data(proc.example.data)
-ddl=add.design.data(proc.example.data,ddl,parameter="Phi",type="age",bins=c(0,.5,1,2,8),name="agebin")
-ddl=add.design.data(proc.example.data,ddl,parameter="p",type="time",bins=c(1980,1983,1986),name="timebin")
-}
-
-Adding the other binned variables to the default design data, allows models based on either time, timebin, or Time 
-for p and age, agebin or Age for Phi.  Any number of additional binned variables can be defined as long as they are 
-given unique names. Note that R is case-specific so \code{~Time} specifies a model which is a linear
-trend over time ((e.g. Phi(T) or p(T) in MARK) whereas \code{~time} creates a model with a different factor level for each
-\code{time} in the data (e.g. Phi(t) or p(t) in MARK) and \code{~timebin} creates a model with 2 factor levels 1980-1983
-and 1984-1986. 
 
 Some circumstances may require direct manipulation of the design data to 
 create the needed variable when simple binning isn't sufficient or when the design data is a variable other than one
 Any of the fields in the design data can be used in formulae for the parameters.  However, it is important to 
 recognize that additional variables you define and add to the design data are specific to a particular type of 
 parameter (e.g., Phi, p, etc). Thus, in the above example, you could not use Agesq in a model for Phi without also 
-adding it to the Phi design data.  As described in \code{\link{make.mark.model}}, there is actually 
+adding it to the Phi design data.  As described in \code{make.mark.model in RMark}, there is actually 
 a simpler way to add simple functions of variables to a formula without defining them in the design data.
 
 The above manipulations are sufficient if there is only one or two variables that need to be added to the
 design data.  If there are many covariates that are time(occasion)-specific then it may be easier to setup
-a dataframe with the covariate data and use \code{\link{merge.occasion.data}}.
+a dataframe with the covariate data and use \code{\link{merge_design.covariates}}.
 
 The fields that are automatically created in the design data depends on the model.  For example,
 with models such as "POPAN" or any of the "Pradel" models, the PIM structure is called
 are not relevant and should be treated as "structural zeros" and not as a zero based on
 stochastic events.
 
-If the design data are removed, when the model is constructed with \code{\link{make.mark.model}}, the argument
+If the design data are removed, when the model is constructed with \code{make.mark.model in RMark}, the argument
 \code{default.fixed} controls what happens with the real parameters defined by the missing design data.  If
 \code{default.fixed=TRUE}, then the real parameters are fixed at values that explain with certainty the observed data
 (e.g., p=0).  That is necessary for models with "Square" PIMS (eg, POPAN and Pradel models) that include each
 assign the missing design data to a row in the design matrix which has all 0's.  That row will show as a real
 parameter of (0.5 for a logit link) but it is not included in any parameter count and does not affect any calculation.
 The advantage in using this approach is that the R code recognizes these and displays blanks for these missing
-parameters, so it makes for more readable output when say every other cohort is missing. See \code{\link{make.mark.model}}
+parameters, so it makes for more readable output when say every other cohort is missing. See \code{make.mark.model in RMark}
 for more information about deleted design data and what this means to development of model formula.
 
 For design data of "Multistrata" models, additional fields are added to represent strata.  A separate PIM is
 by multimonial logit links and the probability of remaining in the stratum is determined by substraction.  To represent
 these differences, a factor variable \code{tostratum} is created in addition to \code{stratum}. Likewise, dummy variables
 are created for each stratum with names created by pasting "to" and the strata label (e.g., toA, toB etc). Some examples
-of using these variables to create models for "Psi" are given in \code{\link{make.mark.model}}.
+of using these variables to create models for "Psi" are given in \code{make.mark.model in RMark}.
 
 }
-
 \value{
 The function value is a list of data frames. The list contains a data frame for each type of parameter in the model 
 (e.g., Phi and p for CJS).  The names of the list elements are the parameter names (e.g., Phi).  The structure of the
 dataframe depends on the calling arguments and the model & data structure as described in the details above. 
    
 }
-
 \author{ Jeff Laake }
-
-\seealso{\code{\link{process.data}},\code{\link{add.design.data}},\code{\link{make.mark.model}},
-\code{\link{run.mark.model}},\code{\link{merge.occasion.data}}}
-
+\seealso{\code{\link{process.data}},\code{\link{merge_design.covariates}}}
 \examples{
-
-data(example.data)
-proc.example.data=process.data(example.data)
-ddl=make.design.data(proc.example.data)
-ddl=add.design.data(proc.example.data,ddl,parameter="Phi",type="age",bins=c(0,.5,1,2,8),name="agebin")
-ddl=add.design.data(proc.example.data,ddl,parameter="p",type="time",bins=c(1980,1983,1986),name="timebin")
-
-
+data(dipper)
+dipper.proc=process.data(dipper)
+ddl=make.design.data(dipper.proc)
 }
 \keyword{utility}
 

File marked/man/merge.design.covariates.Rd

-\name{merge.design.covariates}
+\name{merge_design.covariates}
+\alias{merge_design.covariates}
 \alias{merge.design.covariates}
 \title{Merge time (occasion) and/or group specific covariates into design data}
 \description{
 
 
 \usage{
-ddl[[parameter]]=merge.design.covariates(ddl[[parameter]], df, bygroup=FALSE, bytime=TRUE)
+merge_design.covariates(ddl, df, bygroup=FALSE, bytime=TRUE)
 }
 \arguments{
   \item{ddl}{current design dataframe for a specific parameter and not the entire design data list (ddl)}
 Design data can be added to the parameter specific design dataframes with R commands.  This function
 simplifies the process by enabling the merging of a dataframe with a time and/or group field and one or more
 time and/or group specific covariates into the design data list for a specific model parameter.
-This is a replacement for the older function \code{\link{merge.occasion.data}}. Unlike the older function,
+This is a replacement for the older function \code{merge.occasion.data}. Unlike the older function,
 it uses the R function \code{\link{merge}} but before merging it makes sure all of the fields
 exist and that you are not merging data that already exists in the design data.  It also maintains the
 row names in the case where design data have been deleted prior to merging the design covariate data.
 \value{
 Design dataframe (for a particular parameter) with new fields added.
 See \code{\link{make.design.data}} for a description of the design data list structure. The return value is
-only one element in the list rather than the entire list as with the older function \code{\link{merge.occasion.data}}.
+only one element in the list rather than the entire list as with the older function \code{merge.occasion.data}.
 }
 \author{Jeff Laake}
-\seealso{\code{\link{make.design.data}}, \code{\link{process.data}}, \code{\link{add.design.data}}}
+\seealso{\code{\link{make.design.data}}, \code{\link{process.data}}}
 \examples{
 data(dipper)
 dipper.proc=process.data(dipper)
 # note that the value for time 1 is superfluous for CJS but not for POPAN
 # the value 10 will not appear in the summary because there is no p for time 1
 summary(ddl$p)
-ddl$p=merge.design.covariates(ddl$p,df)
+ddl$p=merge_design.covariates(ddl$p,df)
 summary(ddl$p)
 #Statement below will create an error because a value for time 7 not given
 #ddl=merge.occasion.data(dipper.proc,ddl,"p",data.frame(time=c(1:6),effort=c(10,5,2,8,1,2)))
 #
 # Assign group-specific values
 #
-df=data.frame(group=c(rep("Female",7),rep("Male",7)),time=rep(c(1:7),2),effort=c(10,5,2,8,1,2,3,20,10,4,16,2,4,6))
-merge.design.covariates(dipper.ddl$p,df,bygroup=TRUE)
+
+data(dipper)
+dipper.proc=process.data(dipper)
+ddl=make.design.data(dipper.proc)
+df=data.frame(time=c(1:7),effort=c(10,5,2,8,1,2,3))
+# note that the value for time 1 is superfluous for CJS but not for POPAN
+# the value 10 will not appear in the summary because there is no p for time 1
+summary(ddl$p)
+ddl$p=merge_design.covariates(ddl$p,df)
+summary(ddl$p)
+#Statement below will create an error because a value for time 7 not given
+#ddl=merge.occasion.data(dipper.proc,ddl,"p",data.frame(time=c(1:6),effort=c(10,5,2,8,1,2)))
+#
+# Assign group-specific values
+#
+dipper.proc=process.data(dipper,groups="sex")
+ddl=make.design.data(dipper.proc)
+df=data.frame(group=c(rep("Female",6),rep("Male",6)),time=rep(c(2:7),2),
+		effort=c(10,5,2,8,1,2,3,20,10,4,16,2))
+merge_design.covariates(ddl$p,df,bygroup=TRUE)
 }
 \keyword{utility}

File marked/man/print.crm.Rd

+\name{print.crm}
+\alias{print.crm}
+\alias{coef.crm}
+\title{Print model results or extract coefficients}
+\description{
+Provides a printed simple summary of the model results or extracts the beta
+coefficients from the model results.
+}
+\usage{
+\method{print}{crm}(x,...)
+\method{coef}{crm}(object,...)
+}
+\arguments{
+  \item{x}{crm model result}
+  \item{object}{crm model result}
+  \item{...}{generic arguments not used here}
+}
+\value{
+\code{print} prints a simple summary of the model to the screen and returns NULL.
+\code{coef} returns a dataframe with estimates and standard errors and confidence intervals
+if hessian=TRUE on model run.}
+\author{Jeff Laake }
+\seealso{ \code{\link{crm}} }
+\keyword{utility}
+
+

File marked/man/process.data.Rd

               history. The default structure is freq=1 and it need not be included in the dataframe. \code{data} can also contain an 
               arbitrary number of covariates specific to animals with that capture history. }
   \item{begin.time}{Time of first capture occasion or vector of times if different for each group}
-  \item{model}{Type of analysis model. See \code{\link{mark}} for a list of possible values for \code{model}}
+  \item{model}{Type of analysis model. See \code{mark in RMark} for a list of possible values for \code{model}}
   \item{mixtures}{Number of mixtures in closed capture models with heterogeneity}
   \item{groups}{Vector of factor variable names (in double quotes) in \code{data} that will be used to create 
                 groups in the data. A group is created for each unique combination of the levels of the factor
            one more value beyond what is in ch for an unobservable state}
 }
 \details{
-For examples of \code{data}, see \code{\link{dipper}},\code{\link{edwards.eberhardt}},\code{\link{example.data}}.
+For examples of \code{data}, see \code{\link{dipper}}.
 The structure of the encounter history and the analysis depends on the analysis model to some extent. Thus, it
 is necessary to process a dataframe with the encounter history (\code{ch}) and a chosen \code{model} to define the 
 relevant values.  For example, number of capture occasions (\code{nocc}) is automatically computed based on the 
 as "CJS", "Pradel" and others, the number of \code{time.intervals} is \code{nocc-1}; whereas, for capture&recovery(resight) models
 the number of \code{time.intervals} is \code{nocc}. The default time interval is unit time (1) and if this is adequate, 
 the function will assign the appropriate length.  A processed data frame can only be analyzed using the model that was specified.  The 
-\code{model} value is used by the functions \code{\link{make.design.data}}, \code{\link{add.design.data}},
-and \code{\link{make.mark.model}} to define the model structure as it relates to the data. Thus, if the data are going to
+\code{model} value is used by the functions \code{\link{make.design.data}}
+and \code{\link{crm}} to define the model structure as it relates to the data. Thus, if the data are going to
 be analysed with different underlying models, create different processed data sets with the model name as an 
-extension.  For example, \code{dipper.cjs=process.data(dipper)} and \code{dipper.popan=process.data(dipper,model="POPAN")}.
+extension.  For example, \code{dipper.cjs=process.data(dipper)}.
 
 This function will report inconsistencies in the lengths of the capture history values and when invalid entries are
 given in the capture history.  For example, with the "CJS" model, the capture history should only contain 0 and 1 whereas
 \item{time.intervals}{length of time intervals between capture occasions}
 \item{begin.time}{time of first capture occasion}
 \item{age.unit}{increment of age for each increment of time}
-\item{initial.ages}{an initial age for each group in the data; Note that this isn�t the original argument 
+\item{initial.ages}{an initial age for each group in the data; Note that this is not the original argument 
                     but is a vector with the initial age for each group. In the first example below
                     \code{proc.example.data$initial.ages} is a vector with 16 elements as follows 0 1 1 2 0 1 1 2 0 1 1 2 0 1 1 2}
 \item{nstrata}{number of strata in Multistrata models}
 \item{strata.labels}{vector of alphabetic characters used to identify strata in Multistrata models} 
 \item{group.covariates}{factor covariates used to define groups}
 }
-\seealso{\code{\link{import.chdata}},\code{\link{dipper}},\code{\link{edwards.eberhardt}},\code{\link{example.data}}}
+\seealso{\code{\link{dipper}},\code{\link{crm}}}
 \author{Jeff Laake}
 \examples{
-data(example.data)
-proc.example.data=process.data(data=example.data,begin.time=1980,groups=c("sex","age","region"),
-age.var=2,initial.age=c(0,1,2))
 
 data(dipper)
 dipper.process=process.data(dipper)

File marked/man/setup.parameters.Rd

 \title{Setup parameter structure specific to model (internal use)}
 \description{
 Defines list of parameters used in the specified type of model (\code{model}) and adds default values for
-each parameter to the list of user specified values (eg formula, link etc) defined in the call to
-\code{\link{make.mark.model}}
+each parameter to the list of user specified values (eg formula, link etc)
 }
 \usage{
 setup.parameters(model, parameters = list(), nocc = NULL, check = FALSE, number.of.groups=1)
 user.  That is, it takes the input argument \code{parameters} and adds list elements for parameters not
 specified by the user and adds default values for each type of parameter and then returns the modified list.
 The structure of the argument \code{parameters} and the return value of this function are the same as the
-structure of the argument \code{parameters} in \code{\link{make.mark.model}} and argument \code{model.parameters} 
-in \code{\link{mark}}.  They are lists with an element for each type of parameter in the model and the
+structure of the argument \code{parameters} in \code{make.mark.model in RMark} and argument \code{model.parameters} 
+in \code{mark in RMark}.  They are lists with an element for each type of parameter in the model and the
 name of each list element is the parameter name (e.g., "p", "Phi","S", etc).  For each parameter there are
 a list of values (e.g., formula, link, num etc as defined below).  Thus \code{parameters} is a list of lists. 
 }