-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path01.firststage.R
63 lines (51 loc) · 2.02 KB
/
01.firststage.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
################################################################################
# Updated version of the code for the analysis in:
#
# "Mortality risk attributable to high and low ambient temperature:
# a multi-country study"
# Antonio Gasparrini and collaborators
# The Lancet - 2015
# http://www.ag-myresearch.com/2015_gasparrini_lancet.html
#
# Update: 15 January 2017
# * an updated version of this code, compatible with future versions of the
# software, is available at:
# https://github.com/gasparrini/2015_gasparrini_Lancet_Rcodedata
################################################################################
################################################################################
# FIRST-STAGE ANALYSIS: RUN THE MODEL IN EACH CITY, REDUCE AND SAVE
################################################################################
################################################################################
# CREATE THE OBJECTS TO STORE THE RESULTS
# COEFFICIENTS AND VCOV FOR OVERALL CUMULATIVE SUMMARY
coef <- matrix(NA,nrow(cities),length(varper)+vardegree,
dimnames=list(cities$city))
vcov <- vector("list",nrow(cities))
names(vcov) <- cities$city
################################################################################
# RUN THE LOOP
# LOOP
time <- proc.time()[3]
for(i in seq(length(dlist))) {
# PRINT
cat(i,"")
# EXTRACT THE DATA
data <- dlist[[i]]
# DEFINE THE CROSSBASIS
argvar <- list(fun=varfun,knots=quantile(data$tmean,varper/100,na.rm=T),
degree=vardegree)
cb <- crossbasis(data$tmean,lag=lag,argvar=argvar,
arglag=list(knots=logknots(lag,lagnk)))
#summary(cb)
# RUN THE MODEL AND OBTAIN PREDICTIONS
# NB: NO CENTERING NEEDED HERE, AS THIS DOES NOT AFFECT COEF-VCOV
model <- glm(formula,data,family=quasipoisson,na.action="na.exclude")
cen <- mean(data$tmean,na.rm=T)
pred <- crosspred(cb,model,cen=cen)
# REDUCTION TO OVERALL CUMULATIVE
red <- crossreduce(cb,model,cen=cen)
coef[i,] <- coef(red)
vcov[[i]] <- vcov(red)
}
proc.time()[3]-time
#