Please pardon the simplicity of this question of biological nature.
I'm trying to calculate a statistic, px, the proportion of a cohort
that survives through the interval x:x+1. I have the vector from
which the calc is to be made but I can't figure out how to tell R to
take the current value and divide it by the next value.
The formula is P0=L1/LO
The following is an example of the lifetable I'm constructing:
example=as.vector(c(8,2,1,5,6,7,7,0,8,10,13,8,11,11,11,2,7,1,5,6,8,6))
#prime
k=1
#Deaths#
deaths=numeric(k)
for(k in 0:max(example))
{
deaths[k]=sum(example==k)}
#adjust for no zero!!#
deaths=c(0,deaths)
#Alive, Kx#
alive=sum(deaths)-cumsum(deaths)
#Day, or age class,x#
day=seq(from=0,to=length(deaths)-1)
#mortality, lx#
lx=alive/sum(deaths)
#proportion surviving to next interval, px#
#####Here's where I'm having trouble!!###
p=1
px=numeric(p)
for(p in 0:length(lx))
{
px[p]=lx/lx}
#create data frame#
iC=data.frame("x"=day,"Kx"=alive,"Dx"=deaths,"Lx"=lx,"Px"=px)
print(iC)
Thanks for any suggestions!
On May 26, 2010, at 6:24 PM, Frostygoat wrote:> Please pardon the simplicity of this question of biological nature. > I'm trying to calculate a statistic, px, the proportion of a cohort > that survives through the interval x:x+1. I have the vector from > which the calc is to be made but I can't figure out how to tell R to > take the current value and divide it by the next value. > > The formula is P0=L1/LO > > The following is an example of the lifetable I'm constructing: > > example=as.vector(c(8,2,1,5,6,7,7,0,8,10,13,8,11,11,11,2,7,1,5,6,8,6)) > #prime > k=1 > #Deaths# > deaths=numeric(k) > for(k in 0:max(example)) > { > deaths[k]=sum(example==k)} > #adjust for no zero!!# > deaths=c(0,deaths) > #Alive, Kx# > alive=sum(deaths)-cumsum(deaths) > #Day, or age class,x# > day=seq(from=0,to=length(deaths)-1) > #mortality, lx# > lx=alive/sum(deaths) > #proportion surviving to next interval, px# > #####Here's where I'm having trouble!!### > p=1 > px=numeric(p) > for(p in 0:length(lx)) > { > px[p]=lx/lx} > > #create data frame# > iC=data.frame("x"=day,"Kx"=alive,"Dx"=deaths,"Lx"=lx,"Px"=px) > print(iC)iC$Px <- with(iC, c(1, Lx[2:nrow(iC)]/Lx[1:(nrow(iC)-1)] ) ) -- David.
I don't know if my understanding of P is right.
P ?= (the number of lives at the end of the interval)/(the number of lives
at the beginning of the interval)
### Compute proportion of a cohort that survives through the interval
### The formula is P0=L1/LO
## Original data is a vector of death days
example=as.vector(c(8,2,1,5,6,7,7,0,8,10,13,8,11,11,11,2,7,1,5,6,8,6))
## Count the frenquency of each death day
## Add more levels to fulfill days range
tb <- table(factor(example,levels=seq(0, max(example))))
## Output
iC <- data.frame("x"=names(tb),
"Kx"=sum(tb)-cumsum(tb),
"Dx"=tb[],
"Lx"=iC$Kx/sum(tb),
"Px"=iC$Kx/(iC$Kx+tb[]))
-----
A R learner.
--
View this message in context:
http://r.789695.n4.nabble.com/sequential-treatment-of-a-vector-for-formula-tp2232517p2232598.html
Sent from the R help mailing list archive at Nabble.com.