## selected data
ages <- 50:100
years <- 1950:2006
death <- selectHMDdata("Sweden", "Deaths", "Females",
ages = ages, years = years)
exposure <- selectHMDdata("Sweden", "Exposures", "Females",
ages = ages, years = years)
## fit with BIC
fitBIC <- Mort2Dsmooth(x=ages, y=years, Z=death,
offset=log(exposure))
fitBIC
summary(fitBIC)
## plot age 50 log death rates (1st row)
plot(years, log(death[1,]/exposure[1,]),
main="Mortality rates, log-scale.
Swedish females, age 50, 1950:2006")
lines(years, fitBIC$logmortality[1,], col=2, lwd=2)
## plot over age and years
## fitted log death rates from fitBIC
grid. <- expand.grid(list(ages=ages, years=years))
grid.$lmx <- c(fitBIC$logmortality)
levelplot(lmx ~ years * ages , grid.,
at=quantile(grid.$lmx, seq(0,1,length=10)),
col.regions=rainbow(9))
## about Extra-Poisson variation (overdispersion)
## checking the presence of overdispersion
fitBIC$psi2
## fitting accounting for overdispersion
fitBICover <- Mort2Dsmooth(x=ages, y=years, Z=death,
offset=log(exposure),
overdispersion=TRUE)
## difference in the selected smoothing parameters
fitBIC$lambdas;fitBICover$lambdas
## interpolation when exposure equal to zero
new.exposure <- exposure
new.exposure[20:30, ] <- 0
## Wrong example (don't run)
## fit2Dint <- Mort2Dsmooth(x=ages, y=years, Z=death,
## offset=log(new.exposure))
## using the argument W, with warning
W <- matrix(1, length(ages), length(years))
W[20:30, ] <- 0
fit2Dint <- Mort2Dsmooth(x=ages, y=years, Z=death,
offset=log(new.exposure), W=W,
method=3, lambdas=c(100, 100))
plot(fit2Dint)
## using only 90% of the data
W <- matrix(1, length(ages), length(years))
set.seed(3)
zeros <- sample(x=1:prod(dim(W)),
size=round(prod(dim(W))/100*90),
replace=FALSE)
W[zeros] <- 0
fit2Dint <- Mort2Dsmooth(x=ages, y=years, Z=death,
offset=log(exposure), W=W,
method=3, lambdas=c(100, 100))
plot(fit2Dint)
## extrapolation over years, better predict.Mort2Dsmooth
years.new <- 1950:2025
M <- matrix(0, nrow=length(ages),
ncol=length(years.new)-length(years))
death.new <- cbind(death, M)
exposure.new <- cbind(exposure, M)
W <- matrix(1, length(ages), length(years))
W.new <- cbind(W, M)
fit2Dext <- Mort2Dsmooth(x=ages, y=years.new, Z=death.new,
offset=log(exposure.new), W=W.new,
method=3, lambdas=c(1000, 316))
plot(fit2Dext)Run the code above in your browser using DataLab