/
plot_apc.R
153 lines (142 loc) · 4.98 KB
/
plot_apc.R
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
#' Plot apc object
#'
#' @param x apc object
#' @param quantiles quantiles to plot. Default: \code{c(0.05,0.5,0.95)} is median and 90\% credible interval.
#' @param ... Additional arguments will be ignored
#'
#' @details Plot of age, period and cohort effects from apc objects. If covariates have been used for period/cohort, a second plot with covariate, absolute effect and relative effect is created. Absolute effect is relative effect times covariate.
#' @import stats graphics
#' @return plot
#' @export
#' @examples
#' \dontrun{
#' data(apc)
#' model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5)
#' plot(model)
#' }
plot.apc<-function(x, quantiles=c(0.05,0.5,0.95), ...)
{
age<-as.array(x$samples$age)
age<-apply(age,2,quantile,quantiles)
period<-as.array(x$samples$period)
period<-apply(period,2,quantile,quantiles)
cohort<-as.array(x$samples$cohort)
cohort<-apply(cohort,2,quantile,quantiles)
q<-length(quantiles)
lty=1
if (length(quantiles)==3)lty=c(2,1,2)
if (length(quantiles)==5)lty=c(3,2,1,2,3)
#par(mfrow=c(3,1))
if (x$model$age!=" ")
{
plot(age[1,],type="l",lty=lty[1],ylim=range(age),
axes=FALSE,main="age",xlab="",ylab="")
if (is.null(x$data$agegroups))axis(1,lwd=0)
if (!is.null(x$data$agegroups))axis(1,lwd=0,at=1:length(x$data$agegroups),labels=x$data$agegroups)
axis(2,lwd=0)
for (i in 1:q)
lines(age[i,],lty=lty[i])}
if (x$model$period!=" ")
{
plot(period[1,],type="l",lty=lty[1],ylim=range(period),
axes=FALSE,ylab="", xlab="", main="period")
if (is.null(x$data$periods))axis(1,lwd=0)
if (!is.null(x$data$periods))
{
axis(1,lwd=0,at=1:length(x$data$periods),labels=x$data$periods)
}
axis(2,lwd=0)
for (i in 1:q)
lines(period[i,],lty=lty[i])
}
if (x$model$cohort!=" ")
{
plot(cohort[1,],type="l",lty=lty[1],ylim=range(cohort),
axes=FALSE,ylab="", xlab="", main="cohort")
if (is.null(x$data$cohorts))axis(1,lwd=0)
if (!is.null(x$data$cohorts))
{
axis(1,lwd=0,at=1:length(x$data$cohorts),labels=x$data$cohorts)
}
axis(2,lwd=0)
for (i in 1:q)
lines(cohort[i,],lty=lty[i])
}
if (!is.null(x$covariate))
{
if (!is.null(x$covariate$period))
{
c<-dim(period)[2]
plot(x$covariate$period,type="l", main="period covariate", ylab="",
xlim=c(1,c), axes=FALSE)
if (is.null(x$data$periods))axis(1,lwd=0)
if (!is.null(x$data$periods))
{
axis(1,lwd=0,at=1:length(x$data$periods),labels=x$data$periods)
}
axis(2,lwd=0)
plot(period[1,],type="l",lty=lty[1],ylim=range(period),
axes=FALSE,ylab="", xlab="", main="period effect")
if (is.null(x$data$periods))axis(1,lwd=0)
if (!is.null(x$data$periods))
{
axis(1,lwd=0,at=1:length(x$data$periods),labels=x$data$periods)
}
axis(2,lwd=0)
for (i in 1:q)
lines(period[i,],lty=lty[i])
periodcov<-as.array(x$samples$period)
for (i in 1:dim(periodcov)[1])
for (j in 1:dim(periodcov)[3])
periodcov[i,,j]<-periodcov[i,,j]/x$covariate$period[1:c]
periodcov<-apply(periodcov,2,quantile,quantiles)
plot(periodcov[1,],type="l",lty=lty[1],ylim=range(periodcov),
axes=FALSE,ylab="", xlab="", main="raw period covariate effect")
if (is.null(x$data$periods))axis(1,lwd=0)
if (!is.null(x$data$periods))
{
axis(1,lwd=0,at=1:length(x$data$periods),labels=x$data$periods)
}
axis(2,lwd=0)
for (i in 1:q)
lines(periodcov[i,],lty=lty[i])
}
if (!is.null(x$covariate$cohort))
{
c<-dim(cohort)[2]
plot(x$covariate$cohort,type="l", main="cohort covariate", ylab="",
xlim=c(1,c), axes=FALSE)
if (is.null(x$data$cohorts))axis(1,lwd=0)
if (!is.null(x$data$cohorts))
{
axis(1,lwd=0,at=1:length(x$data$cohorts),labels=x$data$cohorts)
}
axis(2,lwd=0)
plot(cohort[1,],type="l",lty=lty[1],ylim=range(cohort),
axes=FALSE,ylab="", xlab="", main="cohort effect")
if (is.null(x$data$cohorts))axis(1,lwd=0)
if (!is.null(x$data$cohorts))
{
axis(1,lwd=0,at=1:length(x$data$cohorts),labels=x$data$cohorts)
}
axis(2,lwd=0)
for (i in 1:q)
lines(cohort[i,],lty=lty[i])
cohortcov<-as.array(x$samples$cohort)
for (i in 1:dim(cohortcov)[1])
for (j in 1:dim(cohortcov)[3])
cohortcov[i,,j]<-cohortcov[i,,j]/x$covariate$cohort[1:c]
cohortcov<-apply(cohortcov,2,quantile,quantiles)
plot(cohortcov[1,],type="l",lty=lty[1],ylim=range(cohortcov),
axes=FALSE,ylab="", xlab="", main="raw effect of cohort covariate")
if (is.null(x$data$cohorts))axis(1,lwd=0)
if (!is.null(x$data$cohorts))
{
axis(1,lwd=0,at=1:length(x$data$cohorts),labels=x$data$cohorts)
}
axis(2,lwd=0)
for (i in 1:q)
lines(cohortcov[i,],lty=lty[i])
}
}
}