source('../../Data/get_cancer_data.R') DF = get_cancer_data() ## Here we follow the code in the book for this exercise: ## age = rep(1:18, 5) pop = c(DF$pop47, DF$pop51, DF$pop56, DF$pop61, DF$pop66) death = c(DF$d47, DF$d51, DF$d56, DF$d61, DF$d66) period = rep(1:5, each=18) ## period categories cohort = age - period cohort = cohort - min(cohort) + 1 ## categories start at one cancer = cbind(death, pop, age, period, cohort) cancer = data.frame(cancer) print(head(cancer)) ## A model with age and year periods: ## m1 = glm(death ~ as.factor(age) + as.factor(period), offset=log(pop), family=poisson, data=cancer) print(summary(m1)) residuals1 = residuals(m1) plot(residuals1) ## Plot the coefficients of the factor age: ## coefs = coefficients(m1) age_coefs = coefs[2:18] period_coefs = coefs[19:22] plot(age_coefs, pch=19, main='model 1: coefficients of the age factor') grid() plot(period_coefs, pch=19, main='model 1: coefficients of the period factor') grid() ## A model with age, period, and cohort ## m2 = glm(death ~ as.factor(age) + as.factor(period) + as.factor(cohort), offset=log(pop), family=poisson, data=cancer) print(summary(m2)) ## Lets see if the same pattern of the coefficients (as with model m1) holds in this model: ## coefs = coefficients(m2) age_coefs = coefs[2:18] period_coefs = coefs[19:22] cohort_coefs = coefs[23:42] plot(age_coefs, pch=19, main='model 2: coefficients of the age factor') grid() plot(period_coefs, pch=19, main='model 2: coefficients of the period factor') grid() plot(cohort_coefs, pch=19, main='model 2: coefficients of the cohort factor') grid()