1

I am new to quadratic programming and having trouble running the function QPmat in the package popbio, which uses a matrix of stage class counts to calculate stage class transition probabilities.

The code I am running:

####Create a matrix of time series stage class counts
Total<- 
matrix(c(17,74,86,41,17,11,75,84,46,25,7,60,90,46,24,10,61,82,44,25),nrow=5, 
ncol=4)
Total
## list nonzero elements counting by column, indices
nonzero <- c(1,2,7,8,13,14,19,20,25)
## create a constraint matrix, C
C <- rbind(diag(-1,5), c(1,1,0,0,0), c(0,0,1,0,0), c(0,0,0,0,1))
C
## calculate b vector
b <- apply(C, 1, max)
b
QPmat(Total,C,b,nonzero)

This call returns the error "Amat and dvec are incompatible!"

I think the problem is in the constraint matrix, C, but I have been unable to troubleshoot this. I have worked through a couple examples of the solve.QP function in quadprog but to no avail.

halfer
  • 19,824
  • 17
  • 99
  • 186
Peter B
  • 71
  • 4

1 Answers1

1

I had the constraint matrix completely wrong. I checked out Caswell 2001 for the actual example and saw what the constraints were meant to accomplish.

for the constraint matrix C in the above code, substitute:

C<-rbind(diag(-1,9), c(1,1,0,0,0,0,0,0,0), c(0,0,1,1,0,0,0,0,0), 
c(0,0,0,0,1,1,0,0,0),c(0,0,0,0,0,0,1,1,0),c(0,0,0,0,0,0,0,0,1))

This guarantees that all nonzero output matrix elements will be nonnegative, that sums of the consecutive pairs of the nonzero matrix elements will be less than or equal to 1 and that the last nonzero matrix element will be less than or equal to 1.

This is a very quick way to get a projection matrix with transition probabilities when stage class counts are the data and not individual fates.

halfer
  • 19,824
  • 17
  • 99
  • 186
Peter B
  • 71
  • 4