-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapc.functions.R
More file actions
459 lines (408 loc) · 13.7 KB
/
Copy pathapc.functions.R
File metadata and controls
459 lines (408 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# apc_functions.R
# functions for age, period, cohort analysis
# using the intristic estimator
#
require(MASS)
#
#average.apc.R
average.apc <- function(df1,df2) {
b <- (df1$estimate + df2$estimate)/2
bL <- (df1$lower + df2$lower)/2
bU <- (df1$upper + df2$upper)/2
x <- df1$x
apcfact <- df1$apcfact
return(data.frame(y=b,x=x, apcfact=apcfact, lower=bL, upper=bU ))
}
checknames.apc <- function(df) {
check_names <- c("N", "Y", "period", "age", "cohort")
errcount <- 0
for (name in check_names) {
if (!name %in% colnames(df)) {
errcount <- errcount + 1
cat("variable ", name, " not found in data", "\n")
}
}
if (errcount > 0) {
print("required variable names are: Y, N, age, period, cohort")
stop("check variable names")
}
}
# design.apc.R
design.apc <- function(df, ref="last") {
# produce design matrix for age, period, cohort analysis
# assume df with colnames: Y, N, age, period, cohort
# process to construct effect coded design matrix
# options: ref = "last" (default)
# = "first"
# handles 1st and last factor levels as reference
n <- length(df$Y)
o <- rep(1,n)
age <- df$age
period <- df$period
cohort <- df$cohort
#
# construct centered design matrix
#
# convert apc factors to (0/1) dummy-variable coding
#
plab <- unique(period)
P <- array(NA, c(n,length(plab)))
for (i in 1:length(plab)) {
P[,i] <- as.numeric(period==plab[i])
}
alab <- unique(age)
A <- array(NA, c(n,length(alab)))
for (i in 1:length(alab)) {
A[,i] <- as.numeric(age==alab[i])
}
clab <- sort(unique(cohort))
C <- array(NA,c(n, length(clab)))
for (i in 1:length(clab)) {
C[,i] <- as.numeric(cohort==clab[i])
}
# A,P,C,alab,plab,clab used below
# normalization switch default to LAST if empty
if (ref=="last") {
#
# convert dummies to centered effects (ANOVA) [LAST category as reference] Default
#
dimA <- length(alab)
dimP <- length(plab)
dimC <- length(clab)
rA <- A[,dimA]
rP <- P[,dimP]
rC <- C[,dimC]
cA.L <- A[,1:(dimA-1)] - rA
cP.L <- P[,1:(dimP-1)] - rP
cC.L <- C[,1:(dimC-1)] - rC
fname.last <- function(xlab,name) {
vname <- rep(NA, length(xlab) - 1)
for(i in 1:length(vname)) {
#function name for each column
vname[i] <- paste(name,"_",unique(xlab[i]),sep='')
}
return(vname)
}
# apply names function
fname.last(alab, "age") -> agename
fname.last(plab, "period") -> periodname
fname.last(clab, "cohort") -> cohortname
# X.str <- as.matrix(data.frame(o, A.str, P.str, C.str))
X.L <- as.matrix(data.frame(o,cA.L,cP.L,cC.L))
colnames(X.L) <- c("Intercept",agename, periodname, cohortname)
#
return(list(X=X.L, alab=alab, plab=plab, clab=clab, ref="last"))
}
#
else if (ref=="first") {
#
# centered effects (ANOVA) [using FIRST category as reference]
#
dimA <- length(alab)
dimP <- length(plab)
dimC <- length(clab)
rA <- A[,1]
rP <- P[,1]
rC <- C[,1]
cA.F <- A[,2:dimA] - rA
cP.F <- P[,2:dimP] - rP
cC.F <- C[,2:dimC] - rC
# apc coef names function
fname.first <- function(xlab,name) {
vname <- rep(NA, length(xlab)-1)
for(i in 1:length(vname)) {
#function name for each column
vname[i] <- paste(name,"_",xlab[i+1],sep='')
}
return(vname)
}
fname.first(alab,"age") -> agename
fname.first(plab,"period") -> periodname
fname.first(clab,"cohort") -> cohortname
#
X.F <- as.matrix(data.frame(o,cA.F,cP.F,cC.F))
colnames(X.F) <- c("Intercept",agename, periodname, cohortname)
return(list(X=X.F, alab=alab, plab=plab, clab=clab, ref="first"))
}
}
# IE_apc.R
IE_linear <- function(df, ref="last", out="raw") {
# ref is one of "first" or "last" (default)
# out NULL=normalized output raw=unormalized output
# df must contain variables named: age, period, cohort, Y, N
checknames.apc(df)
dX <- design.apc(df, ref)
X <- dX$X
if (!is.matrix(X)) X <- as.matrix(X)
ref <- dX$ref
alab <- dX$alab
plab <- dX$plab
clab <- dX$clab
Y <- df$Y
N <- df$N
XpX <- t(X)%*%X
# empirical log rates
y <- log(Y/N)
n <- length(y)
b <- ginv(XpX)%*%t(X)%*%y
s2.e <- sum( (y - X%*%b)^2)/(n - ncol(X) + 1)
v.b <- s2.e * ginv(XpX)
s.b <- sqrt(diag(v.b))
rownames(b) <- colnames(X)
rownames(v.b) <- colnames(X)
colnames(v.b) <- colnames(X)
results <- data.frame(b = b,
se = s.b,
Z = b/s.b,
p.val = 2*pnorm(-abs(b/s.b)))
rlist <- list(estimate = b, std.error = s.b,
vcov = v.b,
deviance = s2.e, results = results, alab = alab,
plab = plab, clab = clab, ref = ref)
olist <- list(estimate = b, std.error = s.b,
vcov = v.b,
deviance = s2.e, results=results)
if (is.null(out)) {
outp <- normparam.apc(rlist)
return(outp) }
else {
return(olist)
stop()
}
}
###################################################
# glm APC uses iterative algorithm (Newton Raphson)
###################################################
# Poisson Regression: mu = Nexp(XB)
newt.raphP <- function(b,D,R,X) { #ML
mu <- R*exp(X%*%b)
g <- t(X)%*%(D - mu)
H <- t(X)%*%(c(mu)*X) # this is negative of hessian (which is neg def) so this is pos def
b.new <- b + ginv(H)%*%g
v.b <- ginv(H)
s.b <- sqrt(diag(v.b))
out <- list(b=b.new, se=s.b, var.b=v.b)
return(out)
}
# Logit Regression: mu = Nexp(XB)/(1 + exp(XB))
newt.raphL <- function(b,D,R,X) { #ML
mu <- R*exp(X%*%b)/(1 + exp(X%*%b))
v <- mu/(1 + exp(X%*%b))
g <- t(X)%*%(D - mu)
H <- t(X)%*%(c(v)*X) # this is negative of hessian (which is neg def) so this is pos def
b.new <- b + ginv(H)%*%g
v.b <- ginv(H)
s.b <- sqrt(diag(v.b))
out <- list(b=b.new, se=s.b, var.b=v.b)
return(out)
}
# fit rate model
# df must contain variables named: age, period, cohort, Y, N
# ref is one of "first" or "last" (default)
# out NULL=normalized output raw=unormalized output
# bstart is optional arg for start values
IE_rate <- function(df,
bstart=NULL,
family="Poisson",
ref="last",
out=NULL) {
checknames.apc(df)
dX <- design.apc(df, ref)
X <- dX$X
ref <- dX$ref
alab <- dX$alab
plab <- dX$plab
clab <- dX$clab
Y <- df$Y
N <- df$N
# other useful returns
b.old <- bstart
if (is.null(bstart)) b.old <- rep(0,dim(X)[2])
if (is.null(family)) family <- "Poisson" # default
iter <- 1
db <- 1
tol <- 1.e-8
if (family == "Poisson") {
while (db > tol) {
cat("Iteration = ",iter,"\n")
out.A <- newt.raphP(b.old,Y,N,X)
b.new <- out.A$b
s.b <- out.A$se
v.b <- out.A$var.b
db <- mean(abs((b.old - b.new)/b.old))
mu.hat <- N*exp(X%*%b.new)
dev <- 2 * sum(Y * log(Y/mu.hat) - Y + mu.hat)
cat("Deviance = ",dev, "\n")
b.old <- b.new
iter <- iter + 1
}
b.new <- as.matrix(b.new)
rownames(b.new) <- colnames(X)
rownames(v.b) <- colnames(X)
colnames(v.b) <- colnames(X)
results <- data.frame(b = b.new,
se = s.b,
Z = b.new/s.b,
p.val = 2*pnorm(-abs(b.new/s.b)))
rlist <- list(estimate = b.new, std.error = s.b,
vcov = v.b,
deviance = dev, results = results, alab = alab,
plab = plab, clab = clab, ref = ref)
olist <- list(estimate = b.new, std.error = s.b,
vcov = v.b,
deviance = dev, results = results)
if (is.null(out)) {
outp <- normparam.apc(rlist)
return(outp) }
else {
return(olist)
stop()
}
}
else if (family=="Binomial") {
while (db > tol) {
cat("Iteration=",iter,"\n")
out.A <- newt.raphL(b.old,Y,N,X)
b.new <- out.A$b
s.b <- out.A$se
v.b <- out.A$var.b
db <- mean(abs((b.old - b.new)/b.old))
mu.hat <- N*exp(X%*%b.new)
dev <- 2 * sum(Y * log(Y/mu.hat) - Y + mu.hat)
cat("Deviance = ", dev, "\n")
b.old <- b.new
iter <- iter + 1
}
b.new <- as.matrix(b.new)
rownames(b.new) <- colnames(X)
rownames(v.b) <- colnames(X)
colnames(v.b) <- colnames(X)
results <- data.frame(b = b.new,
se = s.b,
Z = b.new/s.b,
p.val = 2*pnorm(-abs(b.new/s.b)))
rlist <- list(estimate = b.new, std.error = s.b,
vcov = v.b,
deviance = dev, results = results, alab = alab,
plab = plab, clab = clab, ref = ref)
olist <- list(estimate = b.new, std.error = s.b,
vcov = v.b,
deviance = dev, results=results)
if (is.null(out)) {
outp <- normparam.apc(rlist)
return(outp) }
else {
return(olist)
stop()
}
}
}
# # done with ML
#normparam.apc.R
# normalize coefficiens for all
# factor levels
normparam.apc <- function(mod) {
alab <- mod$alab
plab <- mod$plab
clab <- mod$clab
ref <- mod$ref
b <- mod$estimate
v <- mod$vcov
# get ref cat estimates and variances
if (is.na(ref) | ref=="last") {
b.refA <- -sum(b[2:length(alab)])
v.refA <- sum(as.vector(v[2:length(alab),2:length(alab)]))
b.A <- c(b[2:length(alab)],b.refA)
se.bA <- c(sqrt(diag(v[2:length(alab),2:length(alab)])), sqrt(v.refA))
p.idx <- length(alab) + 1 # starting index (location) for period effects
p.maxi <- p.idx + length(plab) - 2 # ending index (location) for period effects
b.refP <- -sum(b[p.idx: p.maxi])
v.refP <- sum(as.vector(v[p.idx:p.maxi,p.idx:p.maxi]))
b.P <- c(b[p.idx:p.maxi],b.refP)
se.bP <- c(sqrt(diag(v[p.idx:p.maxi,p.idx:p.maxi])), sqrt(v.refP))
c.idx <- length(alab) + length(plab) # starting index (location) for cohort effects
c.maxi <- length(b) # ending index (location) for cohort effects
b.refC <- -sum(b[c.idx: c.maxi])
v.refC <- sum(as.vector(v[c.idx:c.maxi,c.idx:c.maxi]))
b.C <- c(b[c.idx:c.maxi],b.refC)
se.bC <- c(sqrt(diag(v[c.idx:c.maxi,c.idx:c.maxi])), sqrt(v.refC))
# assemble new coef and se vector
b.norm <- as.vector(c(b[1], b.A, b.P, b.C))
seb.norm <- c(sqrt(v[1,1]), se.bA, se.bP, se.bC)
# revised names:
# function to generate coef names
fname.L <- function(xlab,name) {
vname <- NULL
for(i in 1:length(xlab)) {
#function name for each coef
vname[i] <- paste(name,"_",xlab[i],sep='')
}
return(vname)
}
fname.L(alab,"age") -> agename
fname.L(plab,"period") -> periodname
fname.L(clab,"cohort") -> cohortname
names(b.norm) <- names(seb.norm) <- c("Intercept",agename, periodname, cohortname)
Z <- b.norm/seb.norm
p.Z <- 2 * (1-pnorm(abs(Z)))
b.025 <- b.norm - 1.96 * (seb.norm)
b.975 <- b.norm + 1.96 * (seb.norm)
#print(data.frame(b.norm,seb.norm,Z,p.Z, b.025, b.975), digits=3)
results <- data.frame(estimate=b.norm,
std.error=seb.norm,
Z=Z,
p.val=p.Z,
lower = b.025,
upper = b.975)
return(results)
}
else if (ref=="first") {
dimA <- length(alab)
a.idx <- 2
a.maxi <- dimA
b.refA <- -sum(b[a.idx:a.maxi]) #
v.refA <- sum(as.vector(v[a.idx:a.maxi,a.idx:a.maxi]))
b.A <- c(b.refA, b[a.idx:a.maxi])
se.bA <- c(sqrt(v.refA), sqrt(diag(v[a.idx:a.maxi,a.idx:a.maxi])))
p.idx <- length(alab) + 1 # starting index (location) for period effects
p.maxi <- p.idx + length(plab) - 2 # ending index (location) for period effects
b.refP <- -sum(b[p.idx: p.maxi])
v.refP <- sum(as.vector(v[p.idx:p.maxi,p.idx:p.maxi]))
b.P <- c(b.refP, b[p.idx:p.maxi])
se.bP <- c(sqrt(v.refP), sqrt(diag(v[p.idx:p.maxi, p.idx:p.maxi])))
c.idx <- length(alab) + length(plab) # starting index (location) for cohort effects
c.maxi <- length(b) # ending index (location) for cohort effects
b.refC <- -sum(b[c.idx: c.maxi])
v.refC <- sum(as.vector(v[c.idx:c.maxi,c.idx:c.maxi]))
b.C <- c(b.refC, b[c.idx:c.maxi])
se.bC <- c(sqrt(v.refC), sqrt(diag(v[c.idx:c.maxi,c.idx:c.maxi])))
# assemble new coef and se vector
b.norm <- as.vector(c(b[1], b.A, b.P, b.C))
seb.norm <- c(sqrt(v[1,1]), se.bA, se.bP, se.bC)
# revised names:
# function to generate coef names (not used)
fname.L <- function(xlab,name) {
vname <- NULL
for(i in 1:length(xlab)) {
#function name for each coef
vname[i] <- paste(name,"_", xlab[i],sep='')
}
return(vname)
}
fname.L(alab,"age") -> agename
fname.L(plab,"period") -> periodname
fname.L(clab,"cohort") -> cohortname
names(b.norm) <- names(seb.norm) <- c("Intercept",agename, periodname, cohortname)
Z <- b.norm/seb.norm
p.Z <- 2 * (1-pnorm(abs(Z)))
b.025 <- b.norm - 1.96 * (seb.norm)
b.975 <- b.norm + 1.96 * (seb.norm)
results <- data.frame(estimate=b.norm,
std.error=seb.norm,
Z=Z,
p.val=p.Z,
lower = b.025,
upper = b.975)
return(results)
}
}