Benedikt Gehr
2010-Mar-01 15:53 UTC
[R] function odiag(): assigning values to off-diagonal
hi I'm trying to use the function odiag(x) for matrix calculations. I would like to assign new values to an off-diagonal in a matrix. When I use the diag (x) function I could write something like p<-matrix(seq(1:12),ncol=4) p.new<-matrix(rep(0,12),ncol=4) diag(p.new)<-diag(p) p.new But this won't work with odiag. How can I turn odiag (x) into something like diag (x) in order to assign off-diagonals new values? In the end I would like to do something like this: h<-matrix(rep(c(0.2,0.3,0.3,1),4),ncol=4,byrow=T) #harvest rates for the 3 diferent years S<-matrix(rep(c(0.9,0.8,0.7),4),ncol=3,byrow=T) #survival rates for (i in 0:(L-2)){ odiag(p,i) <- (odiag(h,i)*c(1,cumprod((1-odiag(h[1:R,1:(L-1)],i))*odiag(S[1:R,1:(L-1)],i)))) p[Rp-i,Lp] <- 1-sum(odiag(p[1:Rp,-Lp],i)) ## add one more value p[1,L]<-h[1,L] p[2,Lp]<-1-p[1,L] } Does anybody know how I can do this? Thank you very much for the help Best wishes Ben
David Winsemius
2010-Mar-01 16:24 UTC
[R] function odiag(): assigning values to off-diagonal
On Mar 1, 2010, at 10:53 AM, Benedikt Gehr wrote:> hi > I'm trying to use the function odiag(x) for matrix calculations.You assume that everyone is using the package that contains odiag?> I would like to assign new values to an off-diagonal in a matrix. > When I use the diag (x) function I could write something like > > p<-matrix(seq(1:12),ncol=4) > p.new<-matrix(rep(0,12),ncol=4) > > diag(p.new)<-diag(p) > p.new > > But this won't work with odiag. How can I turn odiag (x) into > something like diag (x) in order to assign off-diagonals new values?Perhaps something like: p.new[row(p.new) == col(p.new) - 1] <- diag(p) > p.new [,1] [,2] [,3] [,4] [1,] 1 1 0 0 [2,] 0 5 5 0 [3,] 0 0 9 9> In the end I would like to do something like this: > > h<-matrix(rep(c(0.2,0.3,0.3,1),4),ncol=4,byrow=T) #harvest rates > for the 3 diferent years > S<-matrix(rep(c(0.9,0.8,0.7),4),ncol=3,byrow=T) #survival > rates > > for (i in 0:(L-2)){It's not clear if this is correct code on you machine, but on our machines we don't have "L".> > odiag(p,i) <- (odiag(h,i)*c(1,cumprod((1-odiag(h[1:R,1: > (L-1)],i))*odiag(S[1:R,1:(L-1)],i)))) > p[Rp-i,Lp] <- 1-sum(odiag(p[1:Rp,-Lp],i)) ## add one more value > p[1,L]<-h[1,L] > p[2,Lp]<-1-p[1,L] > } > > Does anybody know how I can do this?Do what? You have given non-executable code but no verbal description of what you are attempting.> > Thank you very much for the help > > Best wishes >David Winsemius, MD Heritage Laboratories West Hartford, CT