[R] `acting' on variables
Tobias Verbeke
tobias_verbeke at skynet.be
Wed Jul 9 21:54:01 CEST 2003
Thanks to all of you for your answers,
The situation I was dangerously trying
to cope with was an incomplete life table,
with incomplete ages and incomplete survivors.
This is the table I want to complete using
a Gompertz function to estimate mortality
at high ages:
age lx
1 75 53803
2 80 37441
3 85 21134
This is the function I wrote
(I warn you, this is not meant
for programmer's eyes):
gompertz.estimates <- function(age, surv){
n <- age[length(age)] - age[length(age) - 1]
ly2n <- surv[length(surv)]
lyn <- surv[length(surv) - 1]
ly <- surv[length(surv) - 2]
y <- age[length(age) - 2]
lastage <- age[length(age)]
# list(n =n, ly2n = ly2n, lyn = lyn,
# ly = ly, y = y, lastage = lastage) OK
b <- (log(ly2n / lyn) / log(lyn / ly)) ** (1 / n)
a <- exp(log(lyn / ly) / ((b ** y) * (b ** n - 1)))
C <- ly * exp(-(b ** y) * log(a))
equation <- function(d) C * (a ** (b ** d))
# return(equation(c(90,95,100,105,110))) OK
ageseq <- seq(lastage + n, lastage + (20 * n), by=n)
l.est <- equation(ageseq[1])
newl.est <- 1
i <- 1
while(newl.est >= 0.5){
newl.est <- equation(ageseq[i])
l.est <- c(l.est, newl.est)
i <- i + 1
}
app.age <- ageseq[1:length(l.est)]
list(app.age = app.age, l.est = round(l.est, digits=0))
}
The components in the list are the things I
would like to add to the columns in my incomplete
life table.
If this is too dangerous (quaint etc.),
I will not do it that way.
If you can recommend me more elegant ways
to do what I wanted, I will be grateful and
promise never ever to post this kind of
functions on this list ;-)
Regards,
Tobias
More information about the R-help
mailing list