extract PIMS

posts related to the RMark library, which may not be of general interest to users of 'classic' MARK

extract PIMS

Postby tommyg » Tue Jul 09, 2013 2:10 pm

Hi,

Is there a way to extract the PIM matrix from a model in RMark ? The PIMS function only prints the PIM matrix and does not return an object. I also tried modifying the PIMS function, but the load.model function, used by the PIMS function, is not accessible.

Any help is appreciated.

Thanks.

Tommy
tommyg
 
Posts: 21
Joined: Tue Mar 01, 2011 1:55 pm

Re: extract PIMS

Postby jlaake » Tue Jul 09, 2013 2:27 pm

You can extract the non-simplified PIMS as shown below:

Code: Select all
data(dipper)
mod=mark(dipper)
mod$pims


If you want the simplified PIMS, you can modify the PIMS function as I've shown below renamed to myPIMS. When you want to modify and use a function in a package and it uses other functions in the package that are not exported, then all you need to do is add packagename::: in front of the function name as shown below for the calls to RMark:::load.model and RMark:::valid.parameters. That qualifier lets you use any function in a package even if it is not exported.

regards--jeff

Code: Select all
myPIMS=function(model,parameter,simplified=TRUE,use.labels=TRUE)
{
#
# Extract PIMS for a particular parameter and display either the full PIM structure
# or the simplified PIM structure
#
# Arguments:
#
#  model     - mark model object
#  parameter - character string of a particular type of parameter in the model (eg "p","Phi","pent","S")
#  simplified- if TRUE show simplified PIM structure; otherwise show full structure
#
  model=RMark:::load.model(model)
  if(!RMark:::valid.parameters(model$model,parameter)) stop()
   number.of.pims = length(model$pims[[parameter]])
   nn = names(model$pims[[parameter]][[1]])
   nn = nn[nn != "pim"]
   for (i in 1:number.of.pims) {
       struct = model$pims[[parameter]][[i]][2:(length(nn) +
           1)]
       struct[nn == "group"] = model$group.labels[unlist(struct[nn ==
           "group"])]
       pim = model$pims[[parameter]][[i]]$pim
       fullpim = pim
       if (simplified)
           pim[pim > 0] = model$simplify$pim.translation[pim[pim >
               0]]
       pim[pim == 0] = NA
       if (!use.labels) {
           row.names(pim) = 1:dim(pim)[1]
           colnames(pim) = 1:dim(pim)[2]
       }
       else {
           colnames(pim) = model$design.data[[parameter]]$time[1:dim(pim)[2]]
           if (!is.null(model$design.data[[parameter]]$cohort))
               row.names(pim) = model$design.data[[parameter]]$cohort[(diag(fullpim) -
                 fullpim[1, 1] + 1)]
           else row.names(pim) = 1:dim(pim)[1]
       }
       return(pim)
   }
}


Code: Select all
> phipim=myPIMS(mod,"Phi")
> phipim
   1  2  3  4  5 6
1  1  1  1  1  1 1
2 NA  1  1  1  1 1
3 NA NA  1  1  1 1
4 NA NA NA  1  1 1
5 NA NA NA NA  1 1
6 NA NA NA NA NA 1
>
jlaake
 
Posts: 1479
Joined: Fri May 12, 2006 12:50 pm
Location: Escondido, CA

Re: extract PIMS

Postby tommyg » Tue Jul 09, 2013 3:05 pm

Thanks, Jeff. It was the simplified PIMS that I was after, so your function helped. I wouldn't have figured out the ":::".

In case anyone else uses the function Jeff posted, but has group covariates in the model, I made the following modifications :
Code: Select all
myPIMS=function(model,parameter,simplified=TRUE,use.labels=TRUE)
{
 
    # Code . . . .
   
   out <- vector("list", number.of.pims)
   for (i in 1:number.of.pims) {
 
    # Code . . . .

           else row.names(pim) = 1:dim(pim)[1]
       }
    out[[i]] <- pim
   }
return(out)
}
tommyg
 
Posts: 21
Joined: Tue Mar 01, 2011 1:55 pm


Return to RMark

Who is online

Users browsing this forum: No registered users and 1 guest

cron