[R] function odiag(): assigning values to off-diagonal
Benedikt Gehr
benedikt.gehr at ieu.uzh.ch
Mon Mar 1 16:53:42 CET 2010
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
More information about the R-help
mailing list